From 2aa64bb0cdc7e691dc39c59a2e8594d29536d9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kiner-tang=28=E6=96=87=E8=BE=89=29?= <1127031143@qq.com> Date: Mon, 8 Jul 2024 23:02:27 +0800 Subject: [PATCH] feat: optimize code --- packages/create-mako/src/cli.ts | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/create-mako/src/cli.ts b/packages/create-mako/src/cli.ts index b45f90eac..7e337b108 100644 --- a/packages/create-mako/src/cli.ts +++ b/packages/create-mako/src/cli.ts @@ -1,5 +1,5 @@ import fs from 'fs'; -import path from 'path'; +import path, { resolve } from 'path'; import { globSync } from 'glob'; import type { QuestionCollection } from 'inquirer'; import yargs from 'yargs-parser'; @@ -64,15 +64,12 @@ type InitQuestion = { name: string; template: string; }; -async function main() { - const inquirer = (await import('inquirer')).default; - // Check if the current directory is empty. +async function checkEmptyDir(name: string) { + const inquirer = (await import('inquirer')).default; const cwd = process.cwd(); - const isDirEmpty = fs.readdirSync(cwd).length === 0; - - // If the current directory is not empty, prompt the user to confirm whether they want to continue creating the project. - if (!isDirEmpty) { + const exist = fs.existsSync(resolve(cwd, name)); + if (exist && fs.readdirSync(resolve(cwd, name)).length > 0) { const answersContinue = await inquirer.prompt([ { type: 'confirm', @@ -84,21 +81,29 @@ async function main() { ]); if (!answersContinue.continue) { - return; + process.exit(1); } } +} - let name = args._[0]; +async function main() { + const inquirer = (await import('inquirer')).default; + + let name: string = args._[0] as string; let { template } = args; let questions: QuestionCollection[] = []; if (!name) { - questions.push({ - type: 'input', - name: 'name', - message: 'Project name:', - default: 'mako-project', - }); + let answers = await inquirer.prompt([ + { + type: 'input', + name: 'name', + message: 'Project name:', + default: 'mako-project', + }, + ]); + name = answers.name; } + await checkEmptyDir(name); if (!template) { const templates = globSync('**/', { cwd: baseTemplatesPath, @@ -113,9 +118,7 @@ async function main() { }); } if (questions.length > 0) { - const inquirer = (await import('inquirer')).default; let answers = await inquirer.prompt(questions); - name = name || answers.name; template = template || answers.template; } return init({ projectName: String(name), template });