Skip to content

Commit

Permalink
feat(cli): 引入 command 实现多脚本定义, 引入 figlet 实现 banner
Browse files Browse the repository at this point in the history
  • Loading branch information
lexmin0412 committed Apr 23, 2024
1 parent b727a9f commit 6cca930
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 19 deletions.
8 changes: 6 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@readue/cli",
"version": "0.3.4",
"description": "Readue 命令行接口",
"description": "Readue 命令行接口",
"bin": {
"readue": "./bin/readue.js",
"ru": "./bin/readue.js"
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
29 changes: 29 additions & 0 deletions packages/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions packages/cli/src/commands/gen.ts
Original file line number Diff line number Diff line change
@@ -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('')
}

58 changes: 43 additions & 15 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "node",
"esModuleInterop": true,
"lib": [
"es6"
],
Expand All @@ -11,4 +12,4 @@
"includes": [
"src/*"
]
}
}
27 changes: 26 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6cca930

Please sign in to comment.