From 6cca930369f9392bc0c66b7ca48b2e46f6d119b8 Mon Sep 17 00:00:00 2001 From: lexmin0412 Date: Tue, 23 Apr 2024 17:13:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=E5=BC=95=E5=85=A5=20command=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=A4=9A=E8=84=9A=E6=9C=AC=E5=AE=9A=E4=B9=89?= =?UTF-8?q?,=20=E5=BC=95=E5=85=A5=20figlet=20=E5=AE=9E=E7=8E=B0=20banner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/package.json | 8 +++- packages/cli/src/commands/create.ts | 29 +++++++++++++++ packages/cli/src/commands/gen.ts | 29 +++++++++++++++ packages/cli/src/index.ts | 58 +++++++++++++++++++++-------- packages/cli/tsconfig.json | 3 +- pnpm-lock.yaml | 27 +++++++++++++- 6 files changed, 135 insertions(+), 19 deletions(-) create mode 100644 packages/cli/src/commands/create.ts create mode 100644 packages/cli/src/commands/gen.ts diff --git a/packages/cli/package.json b/packages/cli/package.json index 73c07d7..52be9ac 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,7 +1,7 @@ { "name": "@readue/cli", "version": "0.3.4", - "description": "Readue 命令行接口", + "description": "Readue 命令行接口。", "bin": { "readue": "./bin/readue.js", "ru": "./bin/readue.js" @@ -42,6 +42,7 @@ "url": "https://github.com/lexmin0412/readu/issues" }, "devDependencies": { + "@types/figlet": "^1.5.8", "@types/node": "^20.12.7", "rimraf": "^5.0.5", "ts-node": "^10.9.2", @@ -53,6 +54,9 @@ "dependencies": { "@readue/api": "workspace:*", "@readue/config": "workspace:*", - "@toolkit-fe/request": "^0.1.22" + "@toolkit-fe/request": "^0.1.22", + "commander": "^12.0.0", + "figlet": "^1.7.0", + "picocolors": "^1.0.0" } } diff --git a/packages/cli/src/commands/create.ts b/packages/cli/src/commands/create.ts new file mode 100644 index 0000000..6fcafe7 --- /dev/null +++ b/packages/cli/src/commands/create.ts @@ -0,0 +1,29 @@ +/** + * 创建一个插件 + */ +export const create = () => { + console.log(process.argv) + if (!process.argv[3]) { + console.error('请输入插件名称') + process.exit(1) + } + + const content = +`import type { ReadueBlockFunction } from '@readue/config' + +const generator: ReadueBlockFunction = (readueConfig, pkgJson) => { + + const content: string[] = [] + + return { + name: '', + content + } +} + +export default generator +` + // TODO 通过 tpc 插件注入内容 + + return content +} diff --git a/packages/cli/src/commands/gen.ts b/packages/cli/src/commands/gen.ts new file mode 100644 index 0000000..62eaa42 --- /dev/null +++ b/packages/cli/src/commands/gen.ts @@ -0,0 +1,29 @@ +import * as path from 'path' +import * as fs from 'fs' +import { generate4Monorepo, generate4SinglePkg, isMonorepo } from "@readue/api" +import { writeReadme } from "../utils" + +/** + * 生成 RAEDME 文件 + */ +export const gen = () => { + const content = fs.readFileSync(path.resolve(process.cwd(), 'package.json')).toString() + + const pkgJson = JSON.parse(content) + + const genContent = () => { + if (isMonorepo(process.cwd())) { + return generate4Monorepo(pkgJson, process.cwd()) + } else { + return generate4SinglePkg(pkgJson) + } + } + + const readmeLines = genContent() + + // 写到 当前目录的 README.md 中 + writeReadme(readmeLines) + + console.log('') +} + diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 2ccfa82..e125288 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,27 +1,55 @@ import * as path from 'path' import * as fs from 'fs' -import { generate4Monorepo, generate4SinglePkg, isMonorepo } from '@readue/api' -import { writeReadme } from './utils' +import figlet from 'figlet' +import pc from 'picocolors' +import { program } from 'commander' +import { gen } from './commands/gen' +import { create } from './commands/create' + +const content = fs.readFileSync(path.resolve(__dirname, '../package.json')).toString() + +const pkgJson = JSON.parse(content) console.log(` [info] @readue/cli 启动 `) -const content = fs.readFileSync(path.resolve(process.cwd(), 'package.json')).toString() +const artText = figlet.textSync('R e a d u e', { + font: 'Standard', + horizontalLayout: 'default', + verticalLayout: 'default', + width: 80, + whitespaceBreak: true +}) -const pkgJson = JSON.parse(content) +console.log(pc.green(artText)) -const genContent = () => { - if (isMonorepo(process.cwd())) { - return generate4Monorepo(pkgJson, process.cwd()) - } else { - return generate4SinglePkg(pkgJson) - } -} +console.log(`> readue ${process.argv[2]} +`) -const readmeLines = genContent() +program + .version(pkgJson.version) + .command('gen') + .description('生成 README.md 文件') + .action(() => { + try { + gen() + } catch (error) { + console.error(error) + process.exit(1) + } + }) -// 写到 当前目录的 README.md 中 -writeReadme(readmeLines) +program + .command('create') + .description('创建 Readue 插件') + .action(() => { + try { + create() + } catch (error) { + console.error(error) + process.exit(1) + } + }) -console.log('') +program.parse() diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 06c89bd..5b11f3f 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "moduleResolution": "node", + "esModuleInterop": true, "lib": [ "es6" ], @@ -11,4 +12,4 @@ "includes": [ "src/*" ] -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f4b3d80..9f40b88 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -251,7 +251,19 @@ importers: '@toolkit-fe/request': specifier: ^0.1.22 version: 0.1.22 + commander: + specifier: ^12.0.0 + version: 12.0.0 + figlet: + specifier: ^1.7.0 + version: 1.7.0 + picocolors: + specifier: ^1.0.0 + version: 1.0.0 devDependencies: + '@types/figlet': + specifier: ^1.5.8 + version: 1.5.8 '@types/node': specifier: ^20.12.7 version: 20.12.7 @@ -2993,6 +3005,7 @@ packages: resolution: {integrity: sha512-zsVroEhA5T6HhFxiQCrEZiqEAeHP379iT+Scupp3JD7ITfDvNqCTcXObJUUhZctQWPlxn4MqtuL3goRPgvpWWQ==} cpu: [arm64] os: [linux] + libc: [glibc] requiresBuild: true dev: false optional: true @@ -3001,6 +3014,7 @@ packages: resolution: {integrity: sha512-5x8i92eP9ubMDEO3OLT31GdjF9nEyQCPNequAspwv98spaazbiUawPcWZN5XPhsAR41f/w2CvrNUNL7EOHJMkQ==} cpu: [arm64] os: [linux] + libc: [musl] requiresBuild: true dev: false optional: true @@ -3009,6 +3023,7 @@ packages: resolution: {integrity: sha512-oOtLICpLwdgKexHBQi9DERo2Ciweadf2b942Gh9vnMrVJz/oXf9eubewmN4mni5/S+RpSGJoChVvtV1E8ugN/Q==} cpu: [x64] os: [linux] + libc: [glibc] requiresBuild: true dev: false optional: true @@ -3017,6 +3032,7 @@ packages: resolution: {integrity: sha512-yCEb90A3uWOvW4XKxTSaAma70P9UuPffgvt16W+uNb/rGSmTDcZr9/jytvmB5nbOwOq7dx2cu2NEJwWdSvMvxQ==} cpu: [x64] os: [linux] + libc: [musl] requiresBuild: true dev: false optional: true @@ -3854,6 +3870,10 @@ packages: '@types/serve-static': 1.15.7 dev: false + /@types/figlet@1.5.8: + resolution: {integrity: sha512-G22AUvy4Tl95XLE7jmUM8s8mKcoz+Hr+Xm9W90gJsppJq9f9tHvOGkrpn4gRX0q/cLtBdNkWtWCKDg2UDZoZvQ==} + dev: true + /@types/hast@2.3.10: resolution: {integrity: sha512-McWspRw8xx8J9HurkVBfYj0xKoE25tOFlHGdx4MJ5xORQrMGZNqJhVQWaIbm6Oyla5kYOXtDiopzKRJzEOkwJw==} dependencies: @@ -5148,7 +5168,6 @@ packages: /commander@12.0.0: resolution: {integrity: sha512-MwVNWlYjDTtOjX5PiD7o5pK0UrFU/OYgcJfjjK4RaHZETNtjJqrZa9Y9ds88+A+f+d5lv+561eZ+yCKoS3gbAA==} engines: {node: '>=18'} - dev: true /commander@2.20.3: resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==} @@ -6652,6 +6671,12 @@ packages: web-streams-polyfill: 3.3.3 dev: false + /figlet@1.7.0: + resolution: {integrity: sha512-gO8l3wvqo0V7wEFLXPbkX83b7MVjRrk1oRLfYlZXol8nEpb/ON9pcKLI4qpBv5YtOTfrINtqb7b40iYY2FTWFg==} + engines: {node: '>= 0.4.0'} + hasBin: true + dev: false + /figures@3.2.0: resolution: {integrity: sha512-yaduQFRKLXYOGgEn6AZau90j3ggSOyiqXU0F9JZfeXYhNa+Jk4X+s45A2zg5jns87GAFa34BBm2kXw4XpNcbdg==} engines: {node: '>=8'}