Skip to content

Commit 21d4116

Browse files
committed
feat(core): add spport for setting the project name
1 parent a9c8d8c commit 21d4116

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,6 @@ dist
138138
.yarn/install-state.gz
139139
.pnp.*
140140

141-
npm
141+
npm
142+
143+
test-copies

src/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import {
55
existsSync,
66
lstatSync,
77
mkdirSync,
8+
readFileSync,
89
readdirSync,
910
rmSync,
11+
writeFileSync,
1012
} from 'node:fs';
1113
import { join, resolve } from 'node:path';
1214
import { 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

6886
async 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+
150185
async 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

Comments
 (0)