From 37bc209eb62d9544f85947e67d949b8be62079e8 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: Thu, 4 Jul 2024 07:32:00 +0800 Subject: [PATCH 1/6] feat: support select template from templates dir --- packages/create-mako/mako-project/index.ts | 1 + packages/create-mako/src/cli.ts | 52 +++++++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 packages/create-mako/mako-project/index.ts diff --git a/packages/create-mako/mako-project/index.ts b/packages/create-mako/mako-project/index.ts new file mode 100644 index 000000000..537d8ade9 --- /dev/null +++ b/packages/create-mako/mako-project/index.ts @@ -0,0 +1 @@ +console.log('Creating angular project...'); diff --git a/packages/create-mako/src/cli.ts b/packages/create-mako/src/cli.ts index 1dd3d497c..7015a029f 100644 --- a/packages/create-mako/src/cli.ts +++ b/packages/create-mako/src/cli.ts @@ -1,12 +1,19 @@ import fs from 'fs'; import path from 'path'; import { globSync } from 'glob'; +import { QuestionCollection } from 'inquirer'; import yargs from 'yargs-parser'; const args = yargs(process.argv.slice(2)); +const baseTemplatesPath = path.join(__dirname, '../templates'); -async function init(projectName: string) { - let templatePath = path.join(__dirname, '../templates/react'); +type InitOptions = { + projectName: string; + template: string; +}; + +async function init({ projectName, template }: InitOptions) { + let templatePath = path.join(baseTemplatesPath, template); let files = globSync('**/*', { cwd: templatePath, nodir: true }); let cwd = path.join(process.cwd(), projectName); @@ -41,21 +48,42 @@ async function init(projectName: string) { console.log('Happy coding!'); } +type InitQuestion = { + name: string; + template: string; +}; async function main() { let name = args._[0]; + let template = args.template; + let questions: QuestionCollection[] = []; if (!name) { + questions.push({ + type: 'input', + name: 'name', + message: 'Project name:', + default: 'mako-project', + }); + } + if (!template) { + const templates = globSync('**/', { + cwd: baseTemplatesPath, + maxDepth: 1, + }).filter((dir) => dir !== '.'); + questions.push({ + type: 'list', + name: 'template', + message: 'Select a template:', + choices: templates, + default: 'react', + }); + } + if (questions.length > 0) { const inquirer = (await import('inquirer')).default; - let answers = await inquirer.prompt([ - { - type: 'input', - name: 'name', - message: 'Project name:', - default: 'mako-project', - }, - ]); - name = answers.name; + let answers = await inquirer.prompt(questions); + name = name || answers.name; + template = template || answers.template; } - return init(String(name)); + return init({ projectName: String(name), template }); } main().catch((err) => { From 04b3b4faf64308982271c9e18b47d505b96b373b 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: Thu, 4 Jul 2024 07:40:25 +0800 Subject: [PATCH 2/6] feat: support select template from templates dir --- packages/create-mako/src/cli.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/create-mako/src/cli.ts b/packages/create-mako/src/cli.ts index 7015a029f..ab6fe4e3c 100644 --- a/packages/create-mako/src/cli.ts +++ b/packages/create-mako/src/cli.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import path from 'path'; import { globSync } from 'glob'; -import { QuestionCollection } from 'inquirer'; +import type { QuestionCollection } from 'inquirer'; import yargs from 'yargs-parser'; const args = yargs(process.argv.slice(2)); @@ -54,7 +54,7 @@ type InitQuestion = { }; async function main() { let name = args._[0]; - let template = args.template; + let { template } = args; let questions: QuestionCollection[] = []; if (!name) { questions.push({ From 14aae667f3a710b739c07a8f896cf96c974ca5e2 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: Fri, 5 Jul 2024 08:22:29 +0800 Subject: [PATCH 3/6] feat: optimize code From 81e07df4a7a5bc3868573c41322d25b8c7a84433 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: Fri, 5 Jul 2024 08:23:51 +0800 Subject: [PATCH 4/6] feat: optimize code --- packages/create-mako/src/cli.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/create-mako/src/cli.ts b/packages/create-mako/src/cli.ts index ab6fe4e3c..e436782a9 100644 --- a/packages/create-mako/src/cli.ts +++ b/packages/create-mako/src/cli.ts @@ -14,6 +14,10 @@ type InitOptions = { async function init({ projectName, template }: InitOptions) { let templatePath = path.join(baseTemplatesPath, template); + if (!fs.existsSync(templatePath)) { + console.error(`Template "${template}" does not exist.`); + process.exit(1); + } let files = globSync('**/*', { cwd: templatePath, nodir: true }); let cwd = path.join(process.cwd(), projectName); From 7362e1f56a58f98e51bf49d45cd31091de69c830 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 22:44:40 +0800 Subject: [PATCH 5/6] feat: optimize code --- packages/create-mako/mako-project/index.ts | 1 - 1 file changed, 1 deletion(-) delete mode 100644 packages/create-mako/mako-project/index.ts diff --git a/packages/create-mako/mako-project/index.ts b/packages/create-mako/mako-project/index.ts deleted file mode 100644 index 537d8ade9..000000000 --- a/packages/create-mako/mako-project/index.ts +++ /dev/null @@ -1 +0,0 @@ -console.log('Creating angular project...'); 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 6/6] 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 });