Skip to content

Commit fa90670

Browse files
committed
🎉 feat(app): 更新
1 parent b32c89c commit fa90670

File tree

3 files changed

+117
-45
lines changed

3 files changed

+117
-45
lines changed

README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
1-
# codeg-clear-plugin
1+
# @codegenius/clear-plugin
2+
3+
## 安装
4+
5+
```bash
6+
npm i @codegenius/clear-plugin
7+
```
8+
9+
```javascript
10+
// 开发期间使用
11+
import { defineConfig } from "code-genius";
12+
import clearInstaller from "@codegenius/clear-plugin";
13+
14+
export default defineConfig({
15+
plugins: [
16+
clearInstaller({
17+
files: ["./src"],
18+
}),
19+
],
20+
});
21+
```
22+
23+
## 使用
24+
25+
运行 `rimraf` 删除不再需要的文件或文件夹, 支持命令模式, 询问模式和 **API** 模式;
26+
27+
使用场景: 用于删除可以通过项目运行自动生成的文件, 如: `dist` 目录, 还有顽固的 `node_modules`.
28+
29+
### 命令模式
30+
31+
```bash
32+
# 删除 dist 文件夹
33+
codeg clear -p ./dist
34+
35+
# 删除 dist 和 node_modules 两个文件夹
36+
codeg clear -p ./dist -p ./node_modules
37+
```
38+
39+
| 选项 | 描述 |
40+
| ------------------------- | ------------ |
41+
| -p, --pattern \<pattern\> | 设置匹配规则 |
42+
| -a, --ask | 启用询问模式 |
43+
44+
### 询问模式
45+
46+
```bash
47+
# 启动询问模式
48+
codeg clear --ask
49+
```
50+
51+
```
52+
# 询问过程
53+
1. 请选择需要清理的文件/夹
54+
```
55+
56+
### API 模式
57+
58+
```typescript
59+
import { clear } from "code-genius";
60+
61+
(async () => {
62+
await clear(["./dist"]);
63+
})();
64+
```
65+
66+
### 配置文件
67+
68+
```typescript
69+
# 覆盖默认的 `files` 配置
70+
import { defineConfig } from "code-genius";
71+
72+
export default defineConfig({
73+
commands: {
74+
clear: {
75+
files: ["./dist"]
76+
},
77+
},
78+
});
79+
```

src/common.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Ajv from "ajv";
2+
import type { CAC } from "cac";
3+
4+
export interface ClearOptions {
5+
cli: CAC,
6+
files: Array<string>;
7+
}
8+
9+
export const clearGlob = ["./src"];
10+
11+
export const schema = {
12+
type: "object",
13+
properties: {
14+
paths: { type: "array" },
15+
},
16+
required: ["paths"],
17+
};
18+
19+
export const validateArgs = (schema: object, data: unknown) => {
20+
const ajv = new Ajv();
21+
const validate = ajv.compile(schema);
22+
const valid = validate(data);
23+
if (!valid && validate.errors && validate.errors?.length > 0) {
24+
throw new Error(validate.errors[0].message);
25+
}
26+
};

src/index.ts

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
11
import path from "node:path";
22
import { performance } from "node:perf_hooks";
33

4-
import Ajv from "ajv";
5-
import type { CAC } from "cac";
64
import enquirer from "enquirer";
75
import fs from "fs-extra";
86

9-
import { ACTIVATION, clearGlob } from "code-genius";
10-
import { execCommand, loggerInfo, printInfo } from "code-genius";
11-
import { ClearOptions, CodeGeniusOptions } from "code-genius";
12-
13-
const schema = {
14-
type: "object",
15-
properties: {
16-
paths: { type: "array" },
17-
},
18-
required: ["paths"],
19-
};
20-
21-
const mergeConfig = async (config: CodeGeniusOptions) => {
22-
const commands = config && config?.commands;
23-
if (commands && commands.clear) {
24-
const { files } = commands.clear;
25-
return {
26-
paths: files && files.length > 0 ? files : clearGlob,
27-
};
28-
}
29-
return {
30-
paths: clearGlob,
31-
};
32-
};
7+
import { ACTIVATION, execCommand, loggerInfo, printInfo } from "code-genius";
8+
import { ClearOptions, clearGlob, schema, validateArgs } from "./common";
339

3410
const generateEnquirer = async (
35-
config: CodeGeniusOptions
36-
): Promise<ClearOptions> => {
11+
paths: Array<string>
12+
): Promise<Array<string>> => {
3713
const files = fs
3814
.readdirSync(path.join(process.cwd(), "."))
3915
.filter((v) => !v.startsWith("."))
@@ -45,7 +21,6 @@ const generateEnquirer = async (
4521
});
4622
files.sort((v1, v2) => v1.sort - v2.sort);
4723

48-
const { paths } = await mergeConfig(config);
4924
const fileMultiChoices = files.map((v) => {
5025
return {
5126
name: `./${v.file}`,
@@ -61,32 +36,24 @@ const generateEnquirer = async (
6136
choices: fileMultiChoices,
6237
},
6338
]);
64-
return {
65-
files: result.files,
66-
};
39+
return result.files;
6740
};
6841

6942
const clear = async (paths: string[]) => {
7043
if (ACTIVATION) {
7144
loggerInfo(`clear 参数信息: \n ${JSON.stringify(paths)}`);
7245
}
7346

74-
const ajv = new Ajv();
75-
const validate = ajv.compile(schema);
76-
const valid = validate({
77-
paths,
78-
});
79-
if (!valid && validate.errors && validate.errors?.length > 0) {
80-
throw new Error(validate.errors[0].message);
81-
}
47+
validateArgs(schema, paths);
8248

8349
await execCommand("npx", ["rimraf", "--glob", ...paths], {
8450
stdio: "inherit",
8551
});
8652
printInfo("清理结束");
8753
};
8854

89-
export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
55+
const clearInstaller = (config: ClearOptions) => {
56+
const { cli, files } = config;
9057
return {
9158
name: "clearInstaller",
9259
setup: () => {
@@ -98,8 +65,7 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
9865
const { pattern, ask } = options;
9966
let paths = [];
10067
if (ask) {
101-
const result = await generateEnquirer(config);
102-
paths = result.files;
68+
paths = await generateEnquirer(files || clearGlob);
10369
} else {
10470
paths = typeof pattern === "string" ? [pattern] : pattern;
10571
}
@@ -110,4 +76,6 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
11076
});
11177
},
11278
};
113-
}
79+
};
80+
81+
export { clear, clearInstaller };

0 commit comments

Comments
 (0)