11#!/usr/bin/env node
2- import fs from 'node:fs' ;
2+ import fs , { rmSync } from 'node:fs' ;
33import os from 'node:os' ;
44import path from 'node:path' ;
55import { fileURLToPath } from 'node:url' ;
@@ -8,8 +8,10 @@ import { camelCase } from 'lodash-es';
88import minimist from 'minimist' ;
99import prompts from 'prompts' ;
1010import shell from 'shelljs' ;
11+ import { Framework , PromptResult } from './types' ;
1112import {
1213 Args ,
14+ checkPromptResultFlag ,
1315 copy ,
1416 emptyDir ,
1517 formatArgs ,
@@ -24,34 +26,6 @@ import {
2426const argv = formatArgs ( minimist < Args > ( process . argv . slice ( 2 ) , { string : [ '_' ] } ) ) ;
2527const cwd = process . cwd ( ) ;
2628
27- type ColorFunc = ( str : string | number ) => string ;
28-
29- type Framework = {
30- name : string ;
31- display: string ;
32- color: ColorFunc ;
33- publish ? : boolean ;
34- variants ?: FrameworkVariant [ ] ;
35- } ;
36-
37- type FrameworkVariant = {
38- name : string ;
39- display : string ;
40- color : ColorFunc ;
41- publish ?: boolean ;
42- customCommand ?: string ;
43- } ;
44-
45- interface PromptResult {
46- projectName ?: string ;
47- overwrite ?: boolean ;
48- overwriteChecker ?: any ;
49- packageName ?: string ;
50- framework ?: Framework ;
51- variant ?: string ;
52- publish ?: boolean ;
53- }
54-
5529const FRAMEWORKS : Framework [ ] = [
5630 {
5731 name : 'vue' ,
@@ -92,6 +66,7 @@ const FRAMEWORKS: Framework[] = [
9266 display : 'Node' ,
9367 color : blue ,
9468 publish : true ,
69+ test : true ,
9570 variants : [
9671 {
9772 name : 'node' ,
@@ -203,20 +178,24 @@ async function run() {
203178 } ,
204179 {
205180 type : ( pre , values : PromptResult ) => {
206- const { variant, framework } = values ;
207- const { publish, variants } = framework || { } ;
208- if ( ! publish && ! variants ?. find ( s => s . name === variant && s . publish ) ) {
209- return ;
210- }
211-
212- return 'toggle' ;
181+ return checkPromptResultFlag ( values , 'publish' ) ? 'toggle' : null ;
213182 } ,
214- name : 'publish ' ,
183+ name : 'needPublish ' ,
215184 message : reset ( 'Whether to publish to the npm repository?' ) ,
216185 initial : true ,
217186 active : 'yes' ,
218187 inactive : 'no' ,
219188 } ,
189+ {
190+ type : ( pre , values : PromptResult ) => {
191+ return checkPromptResultFlag ( values , 'test' ) ? 'toggle' : null ;
192+ } ,
193+ name : 'needTest' ,
194+ message : reset ( 'Whether to add Test?' ) ,
195+ initial : true ,
196+ active : 'yes' ,
197+ inactive : 'no' ,
198+ } ,
220199 ] ,
221200 {
222201 onCancel : ( ) => {
@@ -227,7 +206,7 @@ async function run() {
227206 ) ;
228207
229208 // user choice associated with prompts
230- const { framework, overwrite, packageName, variant, publish } = result ;
209+ const { framework, overwrite, packageName, variant, needPublish , needTest } = result ;
231210
232211 const root = path . join ( cwd , targetDir . substring ( targetDir . indexOf ( '/' ) + 1 ) ) ;
233212
@@ -273,33 +252,44 @@ async function run() {
273252 name : 'UserName' ,
274253 email : 'name@github.com' ,
275254 } ;
276- if ( publish ) {
277- if ( shell . which ( 'git' ) ) {
278- gitUser . name = getGitInfo ( 'user.name' ) || os . userInfo ( ) . username ;
279- gitUser . email = getGitInfo ( 'user.email' ) || '' ;
280- pkg . author = Object . assign ( pkg . author , gitUser ) ;
281- }
282- const regName = pkgName . startsWith ( '@' )
283- ? pkgName . split ( '/' ) [ 0 ] . substring ( 1 )
284- : camelCase ( gitUser . name ) ;
285- pkg . repository . url = `git+https://github.com/${ regName } /${ pkgName . substring (
286- pkgName . indexOf ( '/' ) + 1 ,
287- ) } .git`;
288- } else {
289- delete pkg . author ;
290- delete pkg . publishConfig ;
291- delete pkg . repository ;
292- delete pkg . scripts . prepublishOnly ;
293- delete pkg . devDependencies . np ;
255+ handlePkgJson ( ) ;
256+
257+ // conditionally change files
258+ if ( isNode ) {
259+ replaceFileContent ( ) ;
294260 }
295261
296- fs . writeFileSync ( path . join ( root , 'package.json' ) , JSON . stringify ( pkg , null , 2 ) + '\n' ) ;
262+ const pkgInfo = pkgFromUserAgent ( process . env . npm_config_user_agent ) ;
263+ const pkgManager = pkgInfo ? pkgInfo . name : 'npm' ;
297264
298- // package name in README eg.
299- if ( isNode ) {
265+ // git init
266+ if ( shell . which ( 'git' ) ) {
267+ shell . exec ( `cd ${ root } && git init` ) ;
268+ }
269+
270+ const cdProjectName = path . relative ( cwd , root ) ;
271+ console . log ( `\nDone. Now run:\n` ) ;
272+ if ( root !== cwd ) {
273+ console . log ( ` cd ${ cdProjectName . includes ( ' ' ) ? `"${ cdProjectName } "` : cdProjectName } ` ) ;
274+ }
275+ switch ( pkgManager ) {
276+ case 'yarn' :
277+ console . log ( ' yarn' ) ;
278+ console . log ( ' yarn dev' ) ;
279+ break ;
280+ default :
281+ console . log ( ` ${ pkgManager } install` ) ;
282+ console . log ( ` ${ pkgManager } run dev` ) ;
283+ break ;
284+ }
285+
286+ /**
287+ * replace template name in files
288+ */
289+ function replaceFileContent ( ) {
300290 [ 'LICENSE' , 'README.md' , 'README.zh_CN.md' ] . forEach ( name => {
301291 const file = path . join ( root , name ) ;
302- if ( ! publish ) {
292+ if ( ! needPublish ) {
303293 if ( fs . existsSync ( file ) ) {
304294 fs . rmSync ( file ) ;
305295
@@ -336,28 +326,51 @@ async function run() {
336326 } ) ;
337327 }
338328
339- const pkgInfo = pkgFromUserAgent ( process . env . npm_config_user_agent ) ;
340- const pkgManager = pkgInfo ? pkgInfo . name : 'npm' ;
329+ /**
330+ * handle package.json
331+ */
332+ function handlePkgJson ( ) {
333+ if ( needPublish ) {
334+ if ( shell . which ( 'git' ) ) {
335+ gitUser . name = getGitInfo ( 'user.name' ) || os . userInfo ( ) . username ;
336+ gitUser . email = getGitInfo ( 'user.email' ) || '' ;
337+ pkg . author = Object . assign ( pkg . author , gitUser ) ;
338+ }
339+ const regName = pkgName . startsWith ( '@' )
340+ ? pkgName . split ( '/' ) [ 0 ] . substring ( 1 )
341+ : camelCase ( gitUser . name ) ;
342+ pkg . repository . url = `git+https://github.com/${ regName } /${ pkgName . substring (
343+ pkgName . indexOf ( '/' ) + 1 ,
344+ ) } .git`;
345+ } else {
346+ delete pkg . author ;
347+ delete pkg . publishConfig ;
348+ delete pkg . repository ;
349+ delete pkg . scripts . prepublishOnly ;
350+ delete pkg . devDependencies . np ;
351+ }
341352
342- // git init
343- if ( shell . which ( 'git' ) ) {
344- shell . exec ( `cd ${ root } && git init` ) ;
345- }
353+ if ( ! needTest ) {
354+ delete pkg . scripts . test ;
355+ [ 'jest.config.js' , 'test' ] . forEach ( name => {
356+ const file = path . join ( root , name ) ;
357+ if ( fs . existsSync ( file ) ) {
358+ rmSync ( file , { force : true , recursive : true } ) ;
359+ }
360+ } ) ;
361+
362+ // remove jest deps
363+ const deps = Object . keys ( pkg . devDependencies || { } ) ;
364+ if ( Array . isArray ( deps ) ) {
365+ deps . forEach ( dep => {
366+ if ( dep . includes ( 'jest' ) ) {
367+ delete pkg . devDependencies [ dep ] ;
368+ }
369+ } ) ;
370+ }
371+ }
346372
347- const cdProjectName = path . relative ( cwd , root ) ;
348- console . log ( `\nDone. Now run:\n` ) ;
349- if ( root !== cwd ) {
350- console . log ( ` cd ${ cdProjectName . includes ( ' ' ) ? `"${ cdProjectName } "` : cdProjectName } ` ) ;
351- }
352- switch ( pkgManager ) {
353- case 'yarn' :
354- console . log ( ' yarn' ) ;
355- console . log ( ' yarn dev' ) ;
356- break ;
357- default :
358- console . log ( ` ${ pkgManager } install` ) ;
359- console . log ( ` ${ pkgManager } run dev` ) ;
360- break ;
373+ fs . writeFileSync ( path . join ( root , 'package.json' ) , JSON . stringify ( pkg , null , 2 ) + '\n' ) ;
361374 }
362375}
363376
0 commit comments