1
1
#!/usr/bin/env node
2
2
3
3
// @ts -check
4
- const fs = require ( 'fs' )
5
- const path = require ( 'path' )
6
- // Avoids autoconversion to number of the project name by defining that the args
7
- // non associated with an option ( _ ) needs to be parsed as a string. See #4606
8
- const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) , { string : [ '_' ] } )
9
- // eslint-disable-next-line node/no-restricted-require
10
- const prompts = require ( 'prompts' )
11
- const {
12
- yellow,
13
- green,
14
- cyan,
4
+ import fs from 'fs'
5
+ import path from 'path'
6
+ import { fileURLToPath } from 'url'
7
+ import minimist from 'minimist'
8
+ import prompts from 'prompts'
9
+ import {
15
10
blue ,
16
- magenta,
11
+ cyan ,
12
+ green ,
17
13
lightRed ,
14
+ magenta ,
18
15
red ,
19
- reset
20
- } = require ( 'kolorist' )
16
+ reset ,
17
+ yellow
18
+ } from 'kolorist'
21
19
20
+ // Avoids autoconversion to number of the project name by defining that the args
21
+ // non associated with an option ( _ ) needs to be parsed as a string. See #4606
22
+ const argv = minimist ( process . argv . slice ( 2 ) , { string : [ '_' ] } )
22
23
const cwd = process . cwd ( )
23
24
24
25
const FRAMEWORKS = [
@@ -238,7 +239,11 @@ async function init() {
238
239
239
240
console . log ( `\nScaffolding project in ${ root } ...` )
240
241
241
- const templateDir = path . join ( __dirname , `template-${ template } ` )
242
+ const templateDir = path . resolve (
243
+ fileURLToPath ( import . meta. url ) ,
244
+ '..' ,
245
+ `template-${ template } `
246
+ )
242
247
243
248
const write = ( file , content ) => {
244
249
const targetPath = renameFiles [ file ]
@@ -256,7 +261,9 @@ async function init() {
256
261
write ( file )
257
262
}
258
263
259
- const pkg = require ( path . join ( templateDir , `package.json` ) )
264
+ const pkg = JSON . parse (
265
+ fs . readFileSync ( path . join ( templateDir , `package.json` ) , 'utf-8' )
266
+ )
260
267
261
268
pkg . name = packageName || targetDir
262
269
@@ -291,12 +298,18 @@ function copy(src, dest) {
291
298
}
292
299
}
293
300
301
+ /**
302
+ * @param {string } projectName
303
+ */
294
304
function isValidPackageName ( projectName ) {
295
305
return / ^ (?: @ [ a - z 0 - 9 - * ~ ] [ a -z 0 -9 -* ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / . test (
296
306
projectName
297
307
)
298
308
}
299
309
310
+ /**
311
+ * @param {string } projectName
312
+ */
300
313
function toValidPackageName ( projectName ) {
301
314
return projectName
302
315
. trim ( )
@@ -306,6 +319,10 @@ function toValidPackageName(projectName) {
306
319
. replace ( / [ ^ a - z 0 - 9 - ~ ] + / g, '-' )
307
320
}
308
321
322
+ /**
323
+ * @param {string } srcDir
324
+ * @param {string } destDir
325
+ */
309
326
function copyDir ( srcDir , destDir ) {
310
327
fs . mkdirSync ( destDir , { recursive : true } )
311
328
for ( const file of fs . readdirSync ( srcDir ) ) {
@@ -315,11 +332,17 @@ function copyDir(srcDir, destDir) {
315
332
}
316
333
}
317
334
335
+ /**
336
+ * @param {string } path
337
+ */
318
338
function isEmpty ( path ) {
319
339
const files = fs . readdirSync ( path )
320
340
return files . length === 0 || ( files . length === 1 && files [ 0 ] === '.git' )
321
341
}
322
342
343
+ /**
344
+ * @param {string } dir
345
+ */
323
346
function emptyDir ( dir ) {
324
347
if ( ! fs . existsSync ( dir ) ) {
325
348
return
0 commit comments