Skip to content

Commit

Permalink
refactor: 将基础指令整合到preset中
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 24, 2021
1 parent af88b67 commit 65a796b
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 130 deletions.
11 changes: 0 additions & 11 deletions packages/plugin-command-commit-lint/README.md

This file was deleted.

36 changes: 0 additions & 36 deletions packages/plugin-command-commit-lint/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/plugin-command-commit-lint/src/constants.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/plugin-command-commit-lint/tsconfig.json

This file was deleted.

11 changes: 0 additions & 11 deletions packages/plugin-command-help/README.md

This file was deleted.

31 changes: 0 additions & 31 deletions packages/plugin-command-help/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions packages/plugin-command-help/src/pad.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/plugin-command-help/tsconfig.json

This file was deleted.

4 changes: 1 addition & 3 deletions packages/preset-built-in/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
"dependencies": {
"@xus/cli": "^0.0.1",
"@xus/plugin-build-lib": "^0.0.1",
"@xus/plugin-bundler-rollup": "^0.0.1",
"@xus/plugin-command-commit-lint": "^0.0.1",
"@xus/plugin-command-help": "^0.0.1"
"@xus/plugin-bundler-rollup": "^0.0.1"
}
}
6 changes: 3 additions & 3 deletions packages/preset-built-in/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createPreset } from '@xus/cli'
// plugins
import commandHelp from '@xus/plugin-command-help'
import commitlint from '@xus/plugin-command-commit-lint'
import buildLib from '@xus/plugin-build-lib'
import bundlerRollup from '@xus/plugin-bundler-rollup'
// cmd
import commandHelp from './plugin/command/help'
import cmdCommitlint from './plugin/command/commitlint'
import cmdClean from './plugin/command/clean'

export default createPreset({
plugins: [commandHelp, commitlint, buildLib, bundlerRollup, cmdClean]
plugins: [buildLib, bundlerRollup, commandHelp, cmdCommitlint, cmdClean]
})
4 changes: 4 additions & 0 deletions packages/preset-built-in/src/plugin/command/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default createPlugin({
'clean',
{
desc: 'a command for clean dir',
usage: `xus clean --dirOrFile dist --point 'plugin|preset'`,
options: {
'--dirOrFile': 'point need to clean dir or file name',
'--skip':
Expand Down Expand Up @@ -38,6 +39,8 @@ export default createPlugin({
.flat()
.filter((dirPath) => existsSync(dirPath))
.forEach((dir) => {
api.logger.debug(`register dir to clean hook: `)
api.logger.debug(dir)
api.registerHook({
name: `remove.dir`,
pluginName: 'cmd:clean',
Expand All @@ -48,6 +51,7 @@ export default createPlugin({
})

try {
api.logger.debug(`apply clean hook`)
await api.applyHook({
name: 'remove.dir',
type: HookTypes.parallel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { createPlugin, chalk } from '@xus/cli'
import { HuskyGitParamsEnv, GitParamsEnv, commitRE, mergeRE } from './constants'
import { readFileSync } from 'fs-extra'

// const
const HuskyGitParamsEnv = 'HUSKY_GIT_PARAMS'
const GitParamsEnv = 'GIT_PARAMS'
const commitRE = /^(revert: )?(fix|feat|docs|perf|test|types|style|build|chore|refactor|ci|wip|breaking change)(\(.+\))?: .{1,50}/
const mergeRE = /Merge /

export default createPlugin({
name: 'commandCommitlint',
name: 'cmd:commitlint',
apply(api) {
api.registerCommand(
'commit-lint',
Expand All @@ -14,11 +19,21 @@ export default createPlugin({
() => {
const gitMsgPath = api.getEnv(GitParamsEnv)
const huskyMsgPath = api.getEnv(HuskyGitParamsEnv)

api.logger.debug(`git message path: `)
api.logger.debug(gitMsgPath)
api.logger.debug(`husky message path: `)
api.logger.debug(huskyMsgPath)

const msgPath = gitMsgPath || huskyMsgPath
if (msgPath) {
const commitMsg = readFileSync(msgPath, 'utf-8').trim()

api.logger.debug(`commit message content: `)
api.logger.debug(commitMsg)

if (!commitRE.test(commitMsg) && !mergeRE.test(commitMsg)) {
api.logger.info(`invalid commit message: "${chalk.red(
api.logger.error(`invalid commit message: "${chalk.red(
commitMsg.replace(/# .*/, ' ')
)}".
Expand Down Expand Up @@ -50,7 +65,7 @@ export default createPlugin({
}
return
}
api.logger.log(chalk.red(`no HUSKY_GIT_PARAMS`))
api.logger.wran(chalk.red(`no HUSKY_GIT_PARAMS`))
process.exit(1)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
chalk,
createPlugin
} from '@xus/cli'
import { getPadLength } from './pad'

export default createPlugin({
name: 'commandHelp',
Expand All @@ -26,18 +25,18 @@ export default createPlugin({

function logAll(commads: Record<string, ICommand>) {
console.info(
`\n usage: ${chalk.green(`xus-cli <command> [options]`)}` + `\n Commands:`
`\nusage: ${chalk.green(`xus-cli <command> [options]`)}` + `\nCommands:`
)
const padLen = getPadLength(commads)
for (const name in commads) {
if (name !== 'help') {
const ops = commads[name].ops
console.info(`\n ${chalk.blue(name.padEnd(padLen))}${ops?.desc || ''}`)
console.info(`\n ${chalk.blue(name.padEnd(padLen))}${ops?.desc || ''}`)
}
}
console.info(
`\n run ${chalk.green(`xus-cli help [command]`)}` +
`\n for detail information of specific command`
`\nrun ${chalk.green(`xus-cli help [command]`)}` +
`\nfor detail information of specific command\n`
)
}

Expand All @@ -62,3 +61,11 @@ function logPointCommand(name: string, commad: ICommand) {
}
console.info('')
}

function getPadLength(obj: Record<string, any>): number {
let longest = 10
for (const name in obj) {
if (name.length + 1 > longest) longest = name.length + 1
}
return longest
}

0 comments on commit 65a796b

Please sign in to comment.