Skip to content

Commit 0e5b70e

Browse files
committed
refactor: extract argv parsing logic into separate function
1 parent 095a17e commit 0e5b70e

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

src/index.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ const readJSON = async (path: string) =>
149149
const readPackageJson = async (filePath: string) =>
150150
readJSON(path.join(filePath, 'package.json'));
151151

152+
const parseArgv = () => {
153+
const argv = minimist<Argv>(process.argv.slice(2), {
154+
alias: { h: 'help', d: 'dir', t: 'template' },
155+
});
156+
157+
// Set dir to first argument if not specified via `--dir`
158+
if (!argv.dir && argv._[0]) {
159+
argv.dir = argv._[0];
160+
}
161+
162+
if (argv['package-name']) {
163+
argv.packageName = argv['package-name'];
164+
}
165+
166+
return argv;
167+
};
168+
152169
export async function create({
153170
name,
154171
root,
@@ -171,18 +188,11 @@ export async function create({
171188
version?: Record<string, string> | string;
172189
noteInformation?: string[];
173190
}) {
174-
const argv = minimist<Argv>(process.argv.slice(2), {
175-
alias: { h: 'help', d: 'dir', t: 'template' },
176-
});
177-
178-
// Set dir to first argument if not specified via `--dir`
179-
if (!argv.dir && argv._[0]) {
180-
argv.dir = argv._[0];
181-
}
182-
183191
console.log('');
184192
logger.greet(`◆ Create ${upperFirst(name)} Project`);
185193

194+
const argv = parseArgv();
195+
186196
if (argv.help) {
187197
logHelpMessage(name, templates);
188198
return;
@@ -214,8 +224,7 @@ export async function create({
214224

215225
const formatted = formatProjectName(projectName);
216226
const { targetDir } = formatted;
217-
const packageName =
218-
argv.packageName || argv['package-name'] || formatted.packageName;
227+
const packageName = argv.packageName || formatted.packageName;
219228
const distFolder = path.isAbsolute(targetDir)
220229
? targetDir
221230
: path.join(cwd, targetDir);

0 commit comments

Comments
 (0)