11#!/usr/bin/env node
22
33// @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 {
1510 blue ,
16- magenta,
11+ cyan ,
12+ green ,
1713 lightRed ,
14+ magenta ,
1815 red ,
19- reset
20- } = require ( 'kolorist' )
16+ reset ,
17+ yellow
18+ } from 'kolorist'
2119
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 : [ '_' ] } )
2223const cwd = process . cwd ( )
2324
2425const FRAMEWORKS = [
@@ -238,7 +239,11 @@ async function init() {
238239
239240 console . log ( `\nScaffolding project in ${ root } ...` )
240241
241- const templateDir = path . join ( __dirname , `template-${ template } ` )
242+ const templateDir = path . resolve (
243+ fileURLToPath ( import . meta. url ) ,
244+ '..' ,
245+ `template-${ template } `
246+ )
242247
243248 const write = ( file , content ) => {
244249 const targetPath = renameFiles [ file ]
@@ -256,7 +261,9 @@ async function init() {
256261 write ( file )
257262 }
258263
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+ )
260267
261268 pkg . name = packageName || targetDir
262269
@@ -291,12 +298,18 @@ function copy(src, dest) {
291298 }
292299}
293300
301+ /**
302+ * @param {string } projectName
303+ */
294304function isValidPackageName ( projectName ) {
295305 return / ^ (?: @ [ a - z 0 - 9 - * ~ ] [ a -z 0 -9 -* ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / . test (
296306 projectName
297307 )
298308}
299309
310+ /**
311+ * @param {string } projectName
312+ */
300313function toValidPackageName ( projectName ) {
301314 return projectName
302315 . trim ( )
@@ -306,6 +319,10 @@ function toValidPackageName(projectName) {
306319 . replace ( / [ ^ a - z 0 - 9 - ~ ] + / g, '-' )
307320}
308321
322+ /**
323+ * @param {string } srcDir
324+ * @param {string } destDir
325+ */
309326function copyDir ( srcDir , destDir ) {
310327 fs . mkdirSync ( destDir , { recursive : true } )
311328 for ( const file of fs . readdirSync ( srcDir ) ) {
@@ -315,11 +332,17 @@ function copyDir(srcDir, destDir) {
315332 }
316333}
317334
335+ /**
336+ * @param {string } path
337+ */
318338function isEmpty ( path ) {
319339 const files = fs . readdirSync ( path )
320340 return files . length === 0 || ( files . length === 1 && files [ 0 ] === '.git' )
321341}
322342
343+ /**
344+ * @param {string } dir
345+ */
323346function emptyDir ( dir ) {
324347 if ( ! fs . existsSync ( dir ) ) {
325348 return
0 commit comments