Skip to content

Commit 52a2dbb

Browse files
authored
feat: Support setting the directory from CLI args (#24)
1 parent a0778bf commit 52a2dbb

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/index.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ const s = prompts.spinner();
1010
const brandColor = /** @type {const} */ ([174, 128, 255]);
1111

1212
(async function createPreact() {
13+
const args = process.argv.slice(2);
14+
1315
// Silences the 'Getting Started' info, mainly
1416
// for use in other initializers that may wrap this
1517
// one but provide their own scripts/instructions.
16-
//
17-
// Don't love the flag, need to find a better name.
18-
const skipHint = process.argv.slice(2).includes('--skip-hints');
18+
const skipHint = args.includes('--skip-hints');
19+
const argDir = args.find((arg) => !arg.startsWith('--'));
1920
const packageManager = getPkgManager();
2021

2122
prompts.intro(
@@ -27,17 +28,19 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
2728
const { dir, language, useRouter, usePrerender, useESLint } = await prompts.group(
2829
{
2930
dir: () =>
30-
prompts.text({
31-
message: 'Project directory:',
32-
placeholder: 'my-preact-app',
33-
validate(value) {
34-
if (value.length == 0) {
35-
return 'Directory name is required!';
36-
} else if (existsSync(value)) {
37-
return 'Refusing to overwrite existing directory or file! Please provide a non-clashing name.';
38-
}
39-
},
40-
}),
31+
argDir
32+
? Promise.resolve(argDir)
33+
: prompts.text({
34+
message: 'Project directory:',
35+
placeholder: 'my-preact-app',
36+
validate(value) {
37+
if (value.length == 0) {
38+
return 'Directory name is required!';
39+
} else if (existsSync(value)) {
40+
return 'Refusing to overwrite existing directory or file! Please provide a non-clashing name.';
41+
}
42+
},
43+
}),
4144
language: () =>
4245
prompts.select({
4346
message: 'Project language:',
@@ -226,7 +229,8 @@ async function installDeps(to, opts) {
226229
if (opts.useESLint) devDependencies.push('eslint', 'eslint-config-preact');
227230

228231
await installPackages(dependencies, { ...installOpts });
229-
devDependencies.length && await installPackages(devDependencies, { ...installOpts, dev: true });
232+
devDependencies.length &&
233+
(await installPackages(devDependencies, { ...installOpts, dev: true }));
230234
}
231235

232236
/**
@@ -253,8 +257,8 @@ function installPackages(pkgs, opts) {
253257
* @returns {'yarn' | 'pnpm' | 'npm'}
254258
*/
255259
function getPkgManager() {
256-
const userAgent = process.env.npm_config_user_agent || ''
257-
if (userAgent.startsWith('yarn')) return 'yarn'
258-
if (userAgent.startsWith('pnpm')) return 'pnpm'
259-
return 'npm'
260+
const userAgent = process.env.npm_config_user_agent || '';
261+
if (userAgent.startsWith('yarn')) return 'yarn';
262+
if (userAgent.startsWith('pnpm')) return 'pnpm';
263+
return 'npm';
260264
}

0 commit comments

Comments
 (0)