@@ -7,195 +7,195 @@ import { parseMdx } from "./mdx";
7
7
import type { HeadingNode } from "./plugins/rehype-headings" ;
8
8
9
9
export const ERROR_CODES = {
10
- REPO_NOT_FOUND : "REPO_NOT_FOUND" ,
11
- FILE_NOT_FOUND : "FILE_NOT_FOUND" ,
12
- BUNDLE_ERROR : "BUNDLE_ERROR" ,
10
+ REPO_NOT_FOUND : "REPO_NOT_FOUND" ,
11
+ FILE_NOT_FOUND : "FILE_NOT_FOUND" ,
12
+ BUNDLE_ERROR : "BUNDLE_ERROR" ,
13
13
} as const ;
14
14
15
15
type Source = {
16
- type : "PR" | "commit" | "branch" ;
17
- owner : string ;
18
- repository : string ;
19
- ref ?: string ;
16
+ type : "PR" | "commit" | "branch" ;
17
+ owner : string ;
18
+ repository : string ;
19
+ ref ?: string ;
20
20
} ;
21
21
22
22
export type BundlerOutput = {
23
- source : Source ;
24
- ref : string ;
25
- stars : number ;
26
- forks : number ;
27
- private : boolean ;
28
- baseBranch : string ;
29
- path : string ;
30
- config : Config ;
31
- markdown : string ;
32
- headings : HeadingNode [ ] ;
33
- frontmatter : Record < string , unknown > ;
34
- code : string ;
23
+ source : Source ;
24
+ ref : string ;
25
+ stars : number ;
26
+ forks : number ;
27
+ private : boolean ;
28
+ baseBranch : string ;
29
+ path : string ;
30
+ config : Config ;
31
+ markdown : string ;
32
+ headings : HeadingNode [ ] ;
33
+ frontmatter : Record < string , unknown > ;
34
+ code : string ;
35
35
} ;
36
36
37
37
type CreateBundlerParams = {
38
- // The owner of the repository to bundle.
39
- owner : string ;
40
- // The repository to bundle.
41
- repository : string ;
42
- // The path to file in the repository to bundle.
43
- path : string ;
44
- // An optional ref to use for the content.
45
- ref ?: string ;
46
- // A list of components which are supported for bundling.
47
- components ?: Array < string > ;
38
+ // The owner of the repository to bundle.
39
+ owner : string ;
40
+ // The repository to bundle.
41
+ repository : string ;
42
+ // The path to file in the repository to bundle.
43
+ path : string ;
44
+ // An optional ref to use for the content.
45
+ ref ?: string ;
46
+ // A list of components which are supported for bundling.
47
+ components ?: Array < string > ;
48
48
} ;
49
49
50
50
export class Bundler {
51
- readonly #owner: string ;
52
- readonly #repository: string ;
53
- readonly #path: string ;
54
- readonly #components: Array < string > ;
55
- #ref: string | undefined ;
56
- #source?: Source ;
57
- #config?: Config ;
58
- #markdown?: string ;
59
-
60
- constructor ( params : CreateBundlerParams ) {
61
- this . #owner = params . owner ;
62
- this . #repository = params . repository ;
63
- this . #path = params . path ;
64
- this . #ref = params . ref ;
65
- this . #components = params . components || [ ] ;
66
- }
67
-
68
- /**
69
- * Gets the source of the bundle.
70
- *
71
- * If the ref is a PR, it will fetch the PR metadata and update the source.
72
- */
73
- private async getSource ( ) : Promise < Source > {
74
- if ( this . #ref) {
75
- // If the ref is a PR
76
- if ( / ^ [ 0 - 9 ] * $ / . test ( this . #ref) ) {
77
- const pullRequest = await getPullRequestMetadata (
78
- this . #owner,
79
- this . #repository,
80
- this . #ref
81
- ) ;
82
- if ( pullRequest ) {
83
- return {
84
- type : "PR" ,
85
- ...pullRequest ,
86
- } ;
87
- }
88
- }
89
-
90
- // If the ref is a commit hash
91
- if ( / ^ [ a - f A - F 0 - 9 ] { 40 } $ / . test ( this . #ref) ) {
92
- return {
93
- type : "commit" ,
94
- owner : this . #owner,
95
- repository : this . #repository,
96
- ref : this . #ref,
97
- } ;
98
- }
99
- }
100
-
101
- return {
102
- type : "branch" ,
103
- owner : this . #owner,
104
- repository : this . #repository,
105
- ref : this . #ref,
106
- } ;
107
- }
108
-
109
- /**
110
- * Builds the payload with the MDX bundle.
111
- */
112
- async build ( ) : Promise < BundlerOutput > {
113
- // Get the real source of the request
114
- this . #source = await this . getSource ( ) ;
115
-
116
- // Update the ref to the real ref
117
- this . #ref = this . #source. ref ;
118
-
119
- const metadata = await getGitHubContents ( {
120
- owner : this . #source. owner ,
121
- repository : this . #source. repository ,
122
- path : this . #path,
123
- ref : this . #ref,
124
- } ) ;
125
-
126
- if ( ! metadata ) {
127
- throw new BundlerError ( {
128
- code : 404 ,
129
- name : ERROR_CODES . REPO_NOT_FOUND ,
130
- message : `The repository ${ this . #source. owner } /${
131
- this . #source. repository
132
- } was not found.`,
133
- } ) ;
134
- }
135
-
136
- if ( ! metadata . md ) {
137
- throw new BundlerError ( {
138
- code : 404 ,
139
- name : ERROR_CODES . FILE_NOT_FOUND ,
140
- message : `No file was found in the repository matching this path. Ensure a file exists at <code>/docs/${
141
- this . #path
142
- } .mdx<code> or <code>/docs/${ this . #path} /index.mdx<code>.`,
143
- source : `https://github.com/${ this . #source. owner } /${
144
- this . #source. repository
145
- } `,
146
- } ) ;
147
- }
148
-
149
- this . #markdown = metadata . md ;
150
-
151
- // If there is no ref (either not provided, or not a PR), use the metadata.
152
- if ( ! this . #ref) {
153
- this . #ref = metadata . baseBranch ;
154
- this . #source. ref = metadata . baseBranch ;
155
- }
156
-
157
- // Parse the users config, but fallback if it errors.
158
- try {
159
- this . #config = parseConfig ( {
160
- json : metadata . config . configJson ,
161
- yaml : metadata . config . configYaml ,
162
- } ) ;
163
- } catch {
164
- this . #config = defaultConfig ;
165
- }
166
-
167
- try {
168
- // Bundle the markdown file via MDX.
169
- const mdx = await parseMdx ( this . #markdown, {
170
- headerDepth : this . #config. content ?. headerDepth ?? 3 ,
171
- components : this . #components,
172
- } ) ;
173
-
174
- return {
175
- source : this . #source,
176
- ref : this . #ref,
177
- stars : metadata . stars ,
178
- forks : metadata . forks ,
179
- private : metadata . isPrivate ,
180
- baseBranch : metadata . baseBranch ,
181
- path : this . #path,
182
- config : this . #config,
183
- markdown : this . #markdown,
184
- headings : mdx . headings ,
185
- frontmatter : mdx . frontmatter ,
186
- code : replaceMoustacheVariables ( this . #config. variables ?? { } , mdx . code ) ,
187
- } ;
188
- } catch ( e ) {
189
- console . error ( e ) ;
190
- // @ts -ignore
191
- throw new BundlerError ( {
192
- code : 500 ,
193
- name : ERROR_CODES . BUNDLE_ERROR ,
194
- message : `Something went wrong while bundling the file /${ metadata . path } .mdx. Are you sure the MDX is valid?` ,
195
- source : `https://github.com/${ this . #source. owner } /${
196
- this . #source. repository
197
- } `,
198
- } ) ;
199
- }
200
- }
51
+ readonly #owner: string ;
52
+ readonly #repository: string ;
53
+ readonly #path: string ;
54
+ readonly #components: Array < string > ;
55
+ #ref: string | undefined ;
56
+ #source?: Source ;
57
+ #config?: Config ;
58
+ #markdown?: string ;
59
+
60
+ constructor ( params : CreateBundlerParams ) {
61
+ this . #owner = params . owner ;
62
+ this . #repository = params . repository ;
63
+ this . #path = params . path ;
64
+ this . #ref = params . ref ;
65
+ this . #components = params . components || [ ] ;
66
+ }
67
+
68
+ /**
69
+ * Gets the source of the bundle.
70
+ *
71
+ * If the ref is a PR, it will fetch the PR metadata and update the source.
72
+ */
73
+ private async getSource ( ) : Promise < Source > {
74
+ if ( this . #ref) {
75
+ // If the ref is a PR
76
+ if ( / ^ [ 0 - 9 ] * $ / . test ( this . #ref) ) {
77
+ const pullRequest = await getPullRequestMetadata (
78
+ this . #owner,
79
+ this . #repository,
80
+ this . #ref,
81
+ ) ;
82
+ if ( pullRequest ) {
83
+ return {
84
+ type : "PR" ,
85
+ ...pullRequest ,
86
+ } ;
87
+ }
88
+ }
89
+
90
+ // If the ref is a commit hash
91
+ if ( / ^ [ a - f A - F 0 - 9 ] { 40 } $ / . test ( this . #ref) ) {
92
+ return {
93
+ type : "commit" ,
94
+ owner : this . #owner,
95
+ repository : this . #repository,
96
+ ref : this . #ref,
97
+ } ;
98
+ }
99
+ }
100
+
101
+ return {
102
+ type : "branch" ,
103
+ owner : this . #owner,
104
+ repository : this . #repository,
105
+ ref : this . #ref,
106
+ } ;
107
+ }
108
+
109
+ /**
110
+ * Builds the payload with the MDX bundle.
111
+ */
112
+ async build ( ) : Promise < BundlerOutput > {
113
+ // Get the real source of the request
114
+ this . #source = await this . getSource ( ) ;
115
+
116
+ // Update the ref to the real ref
117
+ this . #ref = this . #source. ref ;
118
+
119
+ const metadata = await getGitHubContents ( {
120
+ owner : this . #source. owner ,
121
+ repository : this . #source. repository ,
122
+ path : this . #path,
123
+ ref : this . #ref,
124
+ } ) ;
125
+
126
+ if ( ! metadata ) {
127
+ throw new BundlerError ( {
128
+ code : 404 ,
129
+ name : ERROR_CODES . REPO_NOT_FOUND ,
130
+ message : `The repository ${ this . #source. owner } /${
131
+ this . #source. repository
132
+ } was not found.`,
133
+ } ) ;
134
+ }
135
+
136
+ if ( ! metadata . md ) {
137
+ throw new BundlerError ( {
138
+ code : 404 ,
139
+ name : ERROR_CODES . FILE_NOT_FOUND ,
140
+ message : `No file was found in the repository matching this path. Ensure a file exists at <code>/docs/${
141
+ this . #path
142
+ } .mdx<code> or <code>/docs/${ this . #path} /index.mdx<code>.`,
143
+ source : `https://github.com/${ this . #source. owner } /${
144
+ this . #source. repository
145
+ } `,
146
+ } ) ;
147
+ }
148
+
149
+ this . #markdown = metadata . md ;
150
+
151
+ // If there is no ref (either not provided, or not a PR), use the metadata.
152
+ if ( ! this . #ref) {
153
+ this . #ref = metadata . baseBranch ;
154
+ this . #source. ref = metadata . baseBranch ;
155
+ }
156
+
157
+ // Parse the users config, but fallback if it errors.
158
+ try {
159
+ this . #config = parseConfig ( {
160
+ json : metadata . config . configJson ,
161
+ yaml : metadata . config . configYaml ,
162
+ } ) ;
163
+ } catch {
164
+ this . #config = defaultConfig ;
165
+ }
166
+
167
+ try {
168
+ // Bundle the markdown file via MDX.
169
+ const mdx = await parseMdx ( this . #markdown, {
170
+ headerDepth : this . #config. content ?. headerDepth ?? 3 ,
171
+ components : this . #components,
172
+ } ) ;
173
+
174
+ return {
175
+ source : this . #source,
176
+ ref : this . #ref,
177
+ stars : metadata . stars ,
178
+ forks : metadata . forks ,
179
+ private : metadata . isPrivate ,
180
+ baseBranch : metadata . baseBranch ,
181
+ path : this . #path,
182
+ config : this . #config,
183
+ markdown : this . #markdown,
184
+ headings : mdx . headings ,
185
+ frontmatter : mdx . frontmatter ,
186
+ code : replaceMoustacheVariables ( this . #config. variables ?? { } , mdx . code ) ,
187
+ } ;
188
+ } catch ( e ) {
189
+ console . error ( e ) ;
190
+ // @ts -ignore
191
+ throw new BundlerError ( {
192
+ code : 500 ,
193
+ name : ERROR_CODES . BUNDLE_ERROR ,
194
+ message : `Something went wrong while bundling the file /${ metadata . path } .mdx. Are you sure the MDX is valid?` ,
195
+ source : `https://github.com/${ this . #source. owner } /${
196
+ this . #source. repository
197
+ } `,
198
+ } ) ;
199
+ }
200
+ }
201
201
}
0 commit comments