Skip to content

Commit

Permalink
feat(cmd): 新增clean指令
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 24, 2021
1 parent 0a62abe commit adc6788
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/preset-built-in/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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 cmdClean from './plugin/command/clean'

export default createPreset({
plugins: [commandHelp, commitlint, buildLib, bundlerRollup]
plugins: [commandHelp, commitlint, buildLib, bundlerRollup, cmdClean]
})
63 changes: 63 additions & 0 deletions packages/preset-built-in/src/plugin/command/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { createPlugin, HookTypes, rimraf } from '@xus/cli'
import { existsSync } from 'fs'
import { join, relative } from 'path'

export default createPlugin({
name: 'cmd:clean',
apply(api) {
api.registerCommand(
'clean',
{
desc: 'a command for clean dir',
options: {
'--dirOrFile': 'point need to clean dir or file name',
'--skip':
'use RegExp to skip some packages/** dir name in lerna project',
'--point': 'use RegExp to point packages/** dir name in lerna project'
}
},
async (args) => {
if (args.dirOrFile) {
const skipRE = args?.skip ? new RegExp(args.skip) : false
const pointRE = args?.point ? new RegExp(args.point) : false

;(args.dirOrFile as string)
.split(',')
.map((dir) => [
api.getPathBasedOnCtx(dir),
...api
.getLernaPkgs()
.filter((pkg) => {
const relativePkg = relative(api.cwd, pkg)
if (skipRE && skipRE.test(relativePkg)) return false
if (pointRE && !pointRE.test(relativePkg)) return false
return true
})
.map((pkg) => join(pkg, dir))
])
.flat()
.filter((dirPath) => existsSync(dirPath))
.forEach((dir) => {
api.registerHook({
name: `remove.dir`,
pluginName: 'cmd:clean',
fn: () => {
rimraf.sync(dir)
}
})
})

try {
await api.applyHook({
name: 'remove.dir',
type: HookTypes.parallel
})
api.logger.success(`clean dir or file succeed`)
} catch (e) {
api.logger.error(`clean dir or file failed, ${e.massage}`)
}
}
}
)
}
})

0 comments on commit adc6788

Please sign in to comment.