Skip to content

Commit d02045b

Browse files
committed
feat(feature): 增加run命令,解析并选择执行执行脚本
1 parent c0464c5 commit d02045b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ npm i -g code-genius
2424
| cmv | -- | 帮助验证 git commit 的内容是否符合规范 |
2525
| cup | --ignore <path> | 清理运行时生成的文件 |
2626
| ihooks | -- | 使用且有修改 simple-git-hooks 后需要重新初始化 |
27+
| run | -- | 运行列出的脚本 |
2728

2829
## API
2930

@@ -33,6 +34,7 @@ npm i -g code-genius
3334
| 2 | `gitCommitVerify()` | `--` | `Promise<void>` |
3435
| 3 | `cleanUp(paths)` | `paths: string[]` | `Promise<void>` |
3536
| 4 | `gitInitSimpleHooks(cwd)` | `cwd?: string` | `Promise<void>` |
37+
| 5 | `npmRun(cwd)` | `cwd?: string` | `Promise<void>` |
3638

3739
## API 示例
3840

@@ -84,6 +86,18 @@ gitInitSimpleHooks(cwd);
8486
npx esno index.ts
8587
```
8688

89+
npmRun()
90+
91+
```typescript
92+
// ./index.ts
93+
import { gitInnpmRunitSimpleHooks, cwd } from "code-genius";
94+
95+
npmRun(cwd);
96+
97+
// 运行
98+
npx esno index.ts
99+
```
100+
87101
## 执照
88102

89103
MIT License

src/command/npm-run.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import enquirer from "enquirer";
2+
import path from "node:path";
3+
import fs from "fs/promises";
4+
import { execCommand } from "../shared/index";
5+
6+
export const npmRun = async (cwd = process.cwd()) => {
7+
const root = path.join(cwd, "package.json");
8+
const pkgContent = await fs.readFile(root, { encoding: "utf-8" });
9+
const scripts = JSON.parse(pkgContent)?.scripts;
10+
const scriptChoices = Object.keys(scripts).map((key) => {
11+
console.log(key, scripts[key]);
12+
return {
13+
name: scripts[key],
14+
message: key,
15+
};
16+
});
17+
18+
const result = await enquirer.prompt<{ script: string }>([
19+
{
20+
name: "script",
21+
type: "select",
22+
message: "请选择运行脚本",
23+
choices: scriptChoices,
24+
},
25+
]);
26+
27+
if (result.script) {
28+
const script = result.script;
29+
const cmd = script.split(" ")[0];
30+
const args = script.split(" ").slice(1);
31+
execCommand(cmd, args, { stdio: "inherit" });
32+
}
33+
};

src/setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { gitCommitVerify } from "./command/git-commit-verify";
77
import { gitCommit } from "./command/git-commit";
88
import { gitInitSimpleHooks } from "./command/git-init-hooks";
99
import { cleanUp } from "./command/cheanup";
10+
import { npmRun } from "./command/npm-run";
1011

1112
export const setupSet: SetupSet = {
1213
cmSetup: (cli: CAC) => {
@@ -53,4 +54,9 @@ export const setupSet: SetupSet = {
5354
await gitInitSimpleHooks();
5455
});
5556
},
57+
npmRunSetup: (cli: CAC) => {
58+
cli.command("run", "Run the script listed").action(async () => {
59+
await npmRun();
60+
});
61+
},
5662
};

0 commit comments

Comments
 (0)