@@ -7,7 +7,7 @@ import glob from 'tiny-glob/sync.js';
7
7
import { mkdirp , rimraf } from '../utils.js' ;
8
8
9
9
/** @param {string } content */
10
- function convert_typescript ( content ) {
10
+ async function convert_typescript ( content ) {
11
11
let { code } = transform ( content , {
12
12
transforms : [ 'typescript' ] ,
13
13
disableESTransforms : true
@@ -19,7 +19,7 @@ function convert_typescript(content) {
19
19
// Prettier strips 'unnecessary' parens from .ts files, we need to hack them back in
20
20
code = code . replace ( / ( \/ \* \* @ t y p e .+ ? \* \/ ) ( .+ ?) \/ \* \* \* \/ / g, '$1($2)' ) ;
21
21
22
- return prettier . format ( code , {
22
+ return await prettier . format ( code , {
23
23
parser : 'babel' ,
24
24
useTabs : true ,
25
25
singleQuote : true ,
@@ -78,7 +78,8 @@ async function generate_templates(shared) {
78
78
null : [ ]
79
79
} ;
80
80
81
- glob ( '**/*' , { cwd, filesOnly : true , dot : true } ) . forEach ( ( name ) => {
81
+ const files = glob ( '**/*' , { cwd, filesOnly : true , dot : true } ) ;
82
+ for ( const name of files ) {
82
83
// the package.template.json thing is a bit annoying — basically we want
83
84
// to be able to develop and deploy the app from here, but have a different
84
85
// package.json in newly created projects (based on package.template.json)
@@ -87,14 +88,14 @@ async function generate_templates(shared) {
87
88
// TODO package-specific versions
88
89
contents = contents . replace ( / w o r k s p a c e : \* / g, 'next' ) ;
89
90
fs . writeFileSync ( `${ dir } /package.json` , contents ) ;
90
- return ;
91
+ continue ;
91
92
}
92
93
93
94
// ignore files that are written conditionally
94
- if ( shared . has ( name ) ) return ;
95
+ if ( shared . has ( name ) ) continue ;
95
96
96
97
// ignore contents of .gitignore or .ignore
97
- if ( ! gitignore . accepts ( name ) || ! ignore . accepts ( name ) || name === '.ignore' ) return ;
98
+ if ( ! gitignore . accepts ( name ) || ! ignore . accepts ( name ) || name === '.ignore' ) continue ;
98
99
99
100
if ( / \. ( t s | s v e l t e ) $ / . test ( name ) ) {
100
101
const contents = fs . readFileSync ( path . join ( cwd , name ) , 'utf8' ) ;
@@ -103,7 +104,7 @@ async function generate_templates(shared) {
103
104
if ( name . endsWith ( 'app.d.ts' ) ) types . checkjs . push ( { name, contents } ) ;
104
105
types . typescript . push ( { name, contents } ) ;
105
106
} else if ( name . endsWith ( '.ts' ) ) {
106
- const js = convert_typescript ( contents ) ;
107
+ const js = await convert_typescript ( contents ) ;
107
108
108
109
types . typescript . push ( {
109
110
name,
@@ -125,9 +126,14 @@ async function generate_templates(shared) {
125
126
// possible (e.g. preserving double line breaks). Sucrase is the best
126
127
// tool for the job because it just removes the types; Prettier then
127
128
// tidies up the end result
128
- const js_contents = contents . replace (
129
+ const js_contents = await replace_async (
130
+ contents ,
129
131
/ < s c r i p t ( [ ^ > ] + ) > ( [ \s \S ] + ?) < \/ s c r i p t > / g,
130
- ( m , attrs , typescript ) => {
132
+ async (
133
+ /** @type {any } */ m ,
134
+ /** @type {string } */ attrs ,
135
+ /** @type {string } */ typescript
136
+ ) => {
131
137
// Sucrase assumes 'unused' imports (which _are_ used, but only
132
138
// in the markup) are type imports, and strips them. This step
133
139
// prevents it from drawing that conclusion
@@ -149,14 +155,15 @@ async function generate_templates(shared) {
149
155
disableESTransforms : true
150
156
} ) . code . slice ( 0 , - suffix . length ) ;
151
157
152
- const contents = prettier
153
- . format ( transformed , {
158
+ const contents = (
159
+ await prettier . format ( transformed , {
154
160
parser : 'babel' ,
155
161
useTabs : true ,
156
162
singleQuote : true ,
157
163
trailingComma : 'none' ,
158
164
printWidth : 100
159
165
} )
166
+ )
160
167
. trim ( )
161
168
. replace ( / ^ ( .) / gm, '\t$1' ) ;
162
169
@@ -184,7 +191,7 @@ async function generate_templates(shared) {
184
191
mkdirp ( path . dirname ( dest ) ) ;
185
192
fs . copyFileSync ( path . join ( cwd , name ) , dest ) ;
186
193
}
187
- } ) ;
194
+ }
188
195
189
196
fs . copyFileSync ( meta_file , `${ dir } /meta.json` ) ;
190
197
fs . writeFileSync (
@@ -196,6 +203,20 @@ async function generate_templates(shared) {
196
203
}
197
204
}
198
205
206
+ /**
207
+ * @param {string } string
208
+ * @param {RegExp } regexp
209
+ * @param {{ (m: any, attrs: string, typescript: string): Promise<string>; (arg0: any): any; } } replacer
210
+ */
211
+ async function replace_async ( string , regexp , replacer ) {
212
+ const replacements = await Promise . all (
213
+ // @ts -ignore
214
+ Array . from ( string . matchAll ( regexp ) , ( match ) => replacer ( ...match ) )
215
+ ) ;
216
+ let i = 0 ;
217
+ return string . replace ( regexp , ( ) => replacements [ i ++ ] ) ;
218
+ }
219
+
199
220
async function generate_shared ( ) {
200
221
const cwd = path . resolve ( 'shared' ) ;
201
222
@@ -205,7 +226,8 @@ async function generate_shared() {
205
226
/** @type {Array<{ name: string, include: string[], exclude: string[], contents: string }> } */
206
227
const files = [ ] ;
207
228
208
- glob ( '**/*' , { cwd, filesOnly : true , dot : true } ) . forEach ( ( file ) => {
229
+ const globbed = glob ( '**/*' , { cwd, filesOnly : true , dot : true } ) ;
230
+ for ( const file of globbed ) {
209
231
const contents = fs . readFileSync ( path . join ( cwd , file ) , 'utf8' ) ;
210
232
211
233
/** @type {string[] } */
@@ -232,7 +254,7 @@ async function generate_shared() {
232
254
if ( name . endsWith ( '.ts' ) && ! include . includes ( 'typescript' ) ) {
233
255
// file includes types in TypeScript and JSDoc —
234
256
// create .js file, with and without JSDoc
235
- const js = convert_typescript ( contents ) ;
257
+ const js = await convert_typescript ( contents ) ;
236
258
const js_name = name . replace ( / \. t s $ / , '.js' ) ;
237
259
238
260
// typescript
@@ -265,7 +287,7 @@ async function generate_shared() {
265
287
shared . add ( name ) ;
266
288
files . push ( { name, include, exclude, contents } ) ;
267
289
}
268
- } ) ;
290
+ }
269
291
270
292
files . sort ( ( a , b ) => a . include . length + a . exclude . length - ( b . include . length + b . exclude . length ) ) ;
271
293
0 commit comments