Skip to content

Commit

Permalink
perf: ⚡ update commander action handler
Browse files Browse the repository at this point in the history
  • Loading branch information
i7eo committed Jan 22, 2023
1 parent 7574a17 commit 26c50cb
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 17 deletions.
7 changes: 7 additions & 0 deletions packages/commands/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @eo-cli-pro/commandsw

🚀 The package offer commandsw for @eo-cli-pro!

## Build

Use tsup to build and this package don't have `devDependencies`. See [ts-up#excluding-packages](https://tsup.egoist.dev/#excluding-packages)
22 changes: 22 additions & 0 deletions packages/commands/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from 'tsup'
import type { Options } from 'tsup'

export default defineConfig((options: Options) => ({
entry: ['./src/index.ts'],
format: ['cjs', 'esm'],
tsconfig: '../../tsconfig.node.json',
dts: {
compilerOptions: {
// use tsconfig references generate .d.ts has error. see https://github.com/egoist/tsup/issues/647
composite: false,
},
},
// platform: 'node',
// shims: true,
banner: {
// esbuild build commonjs package error. see https://github.com/evanw/esbuild/issues/1921
// node description: https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#no-require-exports-or-moduleexports
js: "import { createRequire } from 'node:module';const require = createRequire(import.meta.url);",
},
minify: !options.watch,
}))
56 changes: 56 additions & 0 deletions packages/commands/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@eo-cli-pro/commands",
"version": "0.0.0",
"description": "🚀 The package offer commands for @eo-cli-pro!",
"type": "module",
"keywords": [
"cli",
"monorepo",
"pnpm",
"turborepo",
"changesets"
],
"homepage": "https://github.com/eopol/eo-cli-pro/blob/master/packages/cli#readme",
"bugs": {
"url": "https://github.com/eopol/eo-cli-pro/issues"
},
"license": "MIT",
"author": "i7eo <admin@i7eo.com> (https://github.com/i7eo)",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
".": {
"dev": "./src/index.ts",
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"./*": "./*"
},
"types": "./dist/index.d.ts",
"sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/eopol/eo-cli-pro.git",
"directory": "packages/cli"
},
"scripts": {
"build": "rimraf dist && tsup --config ./build.config.ts",
"lint": "run-s lint:**",
"lint:eslint": "TIMING=1 eslint \"*.{js,jsx,ts,tsx,md,json}\" --fix",
"lint:prettier": "prettier --write \"*.{js,ts,jsx,tsx,md,json}\"",
"clean": "run-p clean:**",
"clean:cache": "rimraf .turbo && rimraf dist",
"clean:deps": "rimraf node_modules"
},
"dependencies": {
"commander": "10.0.0"
},
"devDependencies": {
"tsup": "^6.5.0",
"typescript": "^4.9.4"
},
"engines": {
"node": ">=18"
}
}
1 change: 1 addition & 0 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './init'
6 changes: 6 additions & 0 deletions packages/commands/src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function commandInitActionHandler(
projectName: string,
options: Record<string, any>
) {
console.log(`command init name: ${projectName}, arg: ${options.force}`)
}
2 changes: 1 addition & 1 deletion packages/core/cli/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export default defineConfig((options: Options) => ({
// node description: https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#no-require-exports-or-moduleexports
js: "import { createRequire } from 'node:module';const require = createRequire(import.meta.url);",
},
minify: !options.watch,
// minify: !options.watch,
}))
1 change: 1 addition & 0 deletions packages/core/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"clean:deps": "rimraf node_modules"
},
"dependencies": {
"@eo-cli-pro/commands": "workspace:*",
"@eo-cli-pro/utils": "workspace:*",
"@types/import-local": "^3.1.0",
"@types/minimist": "1.2.2",
Expand Down
29 changes: 13 additions & 16 deletions packages/core/cli/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
userHome,
versionGreaterThanOrEqual,
} from '@eo-cli-pro/utils'
import { commandInitActionHandler } from '@eo-cli-pro/commands'
import {
DEFAULT_ENV_CLI_HOME,
LOWEST_NODE_VERSION as lowestVersion,
Expand Down Expand Up @@ -47,8 +48,6 @@ async function core(args: string[]) {
checkEnv()
await checkCliVersion()
registerCommand()
checkDebugArg(program.opts<ProgramOptions>().debug)
logger.debug(`${pkg.name} 本地缓存地址:${process.env.CLI_HOME}`, pkg.name)
} catch (error: any) {
logger.error(error.message, pkg.name)
}
Expand All @@ -65,29 +64,27 @@ function registerCommand() {
// 修改默认值
.version(version, '-v, --version', '查看版本号')
.helpOption('-h, --help', '查看使用帮助')
.option('-d, --debug', '开启调试模式', false)

// .option('-d, --debug', '开启调试模式', false)
.addOption(new Option('-d, --debug', '开启调试模式').default(false))
.parse(process.argv)

// program.parse(process.argv)
// console.log(program.opts())
program
.command('init [projectName]')
.option('-f --force', '是否强制初始化项目') // 覆盖同名项目
.action(commandInitActionHandler)

// see https://github.com/tj/commander.js/issues/1517
// program.on('option:debug', () => {
// console.log(`program.debug: ${program.debug}`)
// checkDebugArg(program.debug)
// logger.debug(`${pkg.name} 本地缓存地址:${process.env.CLI_HOME}`, pkg.name)
// })
program.on('option:debug', () => {
checkDebugArg(program.opts<ProgramOptions>().debug)
logger.debug(`${pkg.name} 本地缓存地址:${process.env.CLI_HOME}`, pkg.name)
})

// see https://github.com/tj/commander.js/issues/1609
// program.on('command:*', (operands) => {
// console.error(`error: unknown command '${operands[0]}'`)
// })
program.showHelpAfterError('(输入 --help 获取使用说明)') // 代替上面监听全局 command 捕捉错误
console.log(program)

// program.help({
// error: true,
// })
program.parse(process.argv)
}

/**
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 26c50cb

Please sign in to comment.