@@ -5,8 +5,10 @@ import {
55 existsSync ,
66 lstatSync ,
77 mkdirSync ,
8+ readFileSync ,
89 readdirSync ,
910 rmSync ,
11+ writeFileSync ,
1012} from 'node:fs' ;
1113import { join , resolve } from 'node:path' ;
1214import { parseArgs } from 'node:util' ;
@@ -64,6 +66,22 @@ async function ensureEngine(engine?: string): Promise<Engine> {
6466
6567 return selection as Engine ;
6668}
69+ async function ensureName ( name ?: string ) : Promise < string > {
70+ if ( name && name . trim ( ) . length > 0 ) {
71+ return name ;
72+ }
73+
74+ const enteredName = await text ( {
75+ message : 'Enter a project name:' ,
76+ placeholder : 'my-project' ,
77+ } ) ;
78+
79+ if ( ! enteredName || typeof enteredName !== 'string' ) {
80+ process . exit ( 1 ) ;
81+ }
82+
83+ return enteredName ;
84+ }
6785
6886async function ensureTargetDir ( target ?: string ) : Promise < string > {
6987 if ( target ) return target ;
@@ -147,7 +165,25 @@ async function handleTargetDirectory(targetPath: string) {
147165 }
148166}
149167
168+ async function handleSuccessfulCopy ( path : string , name : string ) {
169+ if ( ! existsSync ( join ( path , 'package.json' ) ) ) {
170+ console . log ( 'nope' ) ;
171+ } else {
172+ const packageJson = JSON . parse (
173+ readFileSync ( join ( path , 'package.json' ) , 'utf-8' ) ,
174+ ) ;
175+
176+ packageJson . name = name ;
177+
178+ writeFileSync (
179+ join ( path , 'package.json' ) ,
180+ JSON . stringify ( packageJson , null , 2 ) ,
181+ ) ;
182+ }
183+ }
184+
150185async function main ( ) {
186+ const name = await ensureName ( args . values . name ) ;
151187 const engine = await ensureEngine ( args . values . engine ) ;
152188 const targetDir = await ensureTargetDir ( args . values . target ) ;
153189 const templatePath = resolve ( import . meta. dirname , '../templates' , engine ) ;
@@ -161,6 +197,7 @@ async function main() {
161197 await handleTargetDirectory ( targetPath ) ;
162198
163199 copyRecursive ( templatePath , targetPath ) ;
200+ handleSuccessfulCopy ( targetPath , name ) ;
164201 console . log ( `✅ Template "${ engine } " copied to "${ targetPath } "` ) ;
165202}
166203
0 commit comments