Skip to content

Commit

Permalink
feat(@142vip/fairy-cli): 增加clean命令,支持在当前目录下指定删除
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Jul 17, 2024
1 parent 16a5540 commit be939b2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 46 deletions.
7 changes: 6 additions & 1 deletion packages/fairy-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "module",
"description": "公众号搜:储凡、fairy-cli",
"keywords": [
"公众号搜:储凡"
"公众号搜:储凡",
"clean"
],
"sideEffects": false,
"exports": {
Expand Down Expand Up @@ -34,11 +35,15 @@
"dependencies": {
"@142vip/release-version": "workspace:*",
"commander": "^12.1.0",
"del": "^7.1.0",
"execa": "^8.0.1",
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@inquirer/confirm": "^3.1.15",
"@types/inquirer": "^9.0.7",
"@types/js-yaml": "^4.0.9",
"inquirer": "7",
"typescript": "^5.5.3",
"unbuild": "^2.0.0",
"vitest": "^2.0.2"
Expand Down
104 changes: 64 additions & 40 deletions packages/fairy-cli/src/core/clean.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,88 @@
export interface CleanUpOptions {
import * as process from 'node:process'
import confirm from '@inquirer/confirm'
import { deleteAsync } from 'del'

/**
* 删除配置
*/
export interface DelOptions {
dryRun?: boolean
force?: boolean
all?: boolean
}

export interface CleanUpOptions extends DelOptions {
dist?: boolean
nuxt?: boolean
midway?: boolean
deep?: boolean
ignoreTips?: boolean
}

/**
* 清除
* 删除文件或文件夹
* - 恢复项目初始状态
*/
export function execCleanUp(args: CleanUpOptions) {
// 删除node_modules
export async function execCleanUp(args: CleanUpOptions) {
// 默认删除node_modules
const dirPatterns = generateDirPatterns('node_modules', args.all)

// 删除各层级dist目录
// 删除各层级dist目录,dist
if (args.dist) {
cleanDist()
dirPatterns.push(...generateDirPatterns('dist', args.all))
}

// 删除nuxt构建目录
// 删除nuxt构建目录,.nuxt output
if (args.nuxt) {
cleanNuxt()
dirPatterns.push(...generateDirPatterns(['.nuxt', 'output'], args.all))
}

// 删除midway构建目录,run logs
if (args.midway) {
cleanMidway()
dirPatterns.push(...generateDirPatterns(['run', 'logs'], args.all))
}
}

/**
* 删除所有node_modules目录
* - 默认删除
*/
export function cleanNodeModules() {
// 删除所有node_modules目录
// find . -name "node_modules" -type d -exec rm -rf '{}' +
}
// 删除前,对话框确认
if (!args.ignoreTips) {
const deleted = await confirm({
message: '是否需要删除?',
default: true,
})

/**
* 删除构建的dist目录
*/
export function cleanDist() {
// 删除dist目录
}
if (!deleted) {
// 不删除,非0退出
process.exit(1)
}
}

/**
* 删除nuxt构建目录
* - .nuxt
* - output
*/
export function cleanNuxt() {
// .nuxt output
// 需要删除的目录
console.log('删除规则:', dirPatterns)

// 删除
const deletedDirs = await deleteAsync(dirPatterns, {
dryRun: args.dryRun,
force: args.force,
})
console.log(deletedDirs)
}

/**
* 根据配置删除所有
*/
export function cleanByConfig() {
function generateDirPatterns(dirName: string | string[], delAll?: boolean) {
let delDirs: string[] = []

}
if (typeof dirName === 'string') {
delDirs.push(dirName)
}
else {
delDirs.push(...dirName)
}

if (delAll) {
// 删除程序上下文中所有的该目录
delDirs = delDirs.map(dir => `**/${dir}/*`)
}
else {
// 只删除该目录
delDirs = delDirs.map(dir => `${dir}/*`)
}

// 删除midway的构建、运行、日志目录
export function cleanMidway() {
// run logs
return delDirs
}
10 changes: 5 additions & 5 deletions packages/fairy-cli/src/fairy-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ program
.option('-n,--nuxt', '删除nuxt构建目录', false)
.option('-d,--dist', '删除dist目录', false)
.option('-m,--midway', '删除midway构建目录', false)
.option('-c --clean', 'registry address', 'CHANGELOG.md')
.option('--deep', '深度删除', false)
.option('-f,--force', '强制删除,默认值:false', false)
.option('--all', '深度删除所有', false)
.option('--ignore-tips', '忽略提示,直接删除', false)
.action((args: CleanUpOptions) => {
console.log(CliCommandEnum.CLEAN, args)
execCleanUp(args)
.option('--dry-run', '试运行,不做实际删除操作', false)
.action(async (args: CleanUpOptions) => {
await execCleanUp(args)
})

program.parse(process.argv)

0 comments on commit be939b2

Please sign in to comment.