From 8db505c4d167bc2ae505cd6cc0d7948e082cad52 Mon Sep 17 00:00:00 2001 From: MuyianKing Date: Thu, 26 Dec 2024 14:44:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E5=A2=9E=E5=8A=A0lib?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/README.md | 29 ++++++++++++++- core/bin/command/inquirer.js | 14 ++++--- core/bin/command/inquirer.lib.js | 63 ++++++++++++++++++++++++++++++++ core/bin/index.js | 4 ++ core/package.json | 46 +++++++++++------------ script/utils/file.js | 2 +- 6 files changed, 127 insertions(+), 31 deletions(-) create mode 100644 core/bin/command/inquirer.lib.js diff --git a/core/README.md b/core/README.md index 1af482e..b6803f0 100644 --- a/core/README.md +++ b/core/README.md @@ -4,7 +4,11 @@ 快速创建项目脚手架

-![NPM Version](https://img.shields.io/npm/v/%40muyianking%2Fcli) ![NPM License](https://img.shields.io/npm/l/%40muyianking%2Fcli) +

+ + + +

## Install @@ -19,12 +23,33 @@ yarn global add @muyianking/cli pnpm i @muyianking/cli -g ``` -### Usage +## Usage ```bash # 创建web项目 mu web +# 创建h5项目 +mu h5 + # 创建简易html项目 mu html ``` + +## Contributors + + + + + + + + +
+ + MuyianKing +
+ MuyianKing +
+
+ diff --git a/core/bin/command/inquirer.js b/core/bin/command/inquirer.js index 1b428e0..5aeac87 100644 --- a/core/bin/command/inquirer.js +++ b/core/bin/command/inquirer.js @@ -2,7 +2,7 @@ import process from 'node:process' import ora from 'ora' export default function () { - const cmd = ['web', 'h5', 'html'] + const cmd = ['web', 'h5', 'html', 'lib'] const spinner = ora().start() @@ -11,13 +11,17 @@ export default function () { } else if (process.argv.length === 3 && !cmd.includes(process.argv[2])) { spinner.fail(`找不到命令:mu `) } else { + spinner.stop() return } spinner.succeed(`Usage: - mu web 创建一个web项目 - mu h5 创建一个h5项目 - mu html 创建普通项目(没有打包工具) - `) + mu web 创建一个web项目 + mu h5 创建一个h5项目 + mu lib 创建一个Lib项目 + mu html 创建普通项目(没有打包工具) + `) + + spinner.stop() } diff --git a/core/bin/command/inquirer.lib.js b/core/bin/command/inquirer.lib.js new file mode 100644 index 0000000..45f94df --- /dev/null +++ b/core/bin/command/inquirer.lib.js @@ -0,0 +1,63 @@ +import path from 'node:path' +import process from 'node:process' +import inquirer from 'inquirer' +import ora from 'ora' +import { checkMkdirExists } from '../utils/copy.js' +import download from '../utils/download.js' + +function inquirerPrompt(argv) { + const { name } = argv + return new Promise((resolve, reject) => { + inquirer.prompt([ + { + type: 'input', + name: 'name', + message: '项目名称', + default: name, + validate(val) { + if (!val) { + return '请输入项目名称:' + } + return true + }, + }, + { + type: 'list', + name: 'build_type', + message: '组件类型', + choices: ['Vue组件'], + filter(value) { + return { + Vue组件: 'vue-comp', + }[value] + }, + }, + ]).then((answers) => { + resolve(answers) + }).catch((error) => { + reject(error) + }) + }) +} + +export default function (argv) { + inquirerPrompt(argv).then(async (answers) => { + const { name, build_type } = answers + const isMkdirExists = checkMkdirExists( + path.resolve(process.cwd(), `./${name}`), + ) + + const spinner = ora().start() + + if (isMkdirExists) { + spinner.warning(`${name}文件夹已经存在`) + } else { + const _path = ['lib', build_type] + + // 下载文件 + await download(name, _path) + + spinner.succeed(`created successfully`) + } + }) +} diff --git a/core/bin/index.js b/core/bin/index.js index dc06973..a44f9e8 100644 --- a/core/bin/index.js +++ b/core/bin/index.js @@ -6,6 +6,7 @@ import { hideBin } from 'yargs/helpers' import inquirerH5Prompt from './command/inquirer.h5.js' import inquirerHtmlPrompt from './command/inquirer.html.js' import inquirerPrompt from './command/inquirer.js' +import inquirerLibPrompt from './command/inquirer.lib.js' import inquirerWebPrompt from './command/inquirer.web.js' // 无操作提示 @@ -18,6 +19,9 @@ yargs(hideBin(process.argv)) .command('h5', '新建一个H5项目', (argv) => { inquirerH5Prompt(argv) }) + .command(['lib'], '新建一个Lib项目', (argv) => { + inquirerLibPrompt(argv) + }) .command(['html'], '新建一个HTML项目', (argv) => { inquirerHtmlPrompt(argv) }) diff --git a/core/package.json b/core/package.json index 1e7d60d..0c4d0d2 100644 --- a/core/package.json +++ b/core/package.json @@ -1,24 +1,24 @@ { - "name": "@muyianking/cli", - "type": "module", - "version": "0.2.1", - "description": "慕易安的脚手架", - "author": "muyianking", - "license": "ISC", - "keywords": [ - "cli", - "vue", - "rsbuild" - ], - "bin": { - "mu": "./bin/index.js" - }, - "dependencies": { - "copy-dir": "^1.3.0", - "fs-extra": "^11.2.0", - "inquirer": "8.2.5", - "mustache": "^4.2.0", - "ora": "^8.1.1", - "yargs": "^17.7.2" - } -} + "name": "@muyianking/cli", + "type": "module", + "version": "0.2.1", + "description": "慕易安的脚手架", + "author": "muyianking", + "license": "ISC", + "keywords": [ + "cli", + "vue", + "rsbuild" + ], + "bin": { + "mu": "./bin/index.js" + }, + "dependencies": { + "copy-dir": "^1.3.0", + "fs-extra": "^11.2.0", + "inquirer": "8.2.5", + "mustache": "^4.2.0", + "ora": "^8.1.1", + "yargs": "^17.7.2" + } +} \ No newline at end of file diff --git a/script/utils/file.js b/script/utils/file.js index 5f8380d..9fbdc73 100644 --- a/script/utils/file.js +++ b/script/utils/file.js @@ -5,7 +5,7 @@ import { copyFile, readFileSync } from 'node:fs' * @param {string} path 文件路径 * @returns */ -export default function getObjectFromJson(path) { +export function getObjectFromJson(path) { const data = readFileSync(path) return JSON.parse(data) }