File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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);
8486npx 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
89103MIT License
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { gitCommitVerify } from "./command/git-commit-verify";
77import { gitCommit } from "./command/git-commit" ;
88import { gitInitSimpleHooks } from "./command/git-init-hooks" ;
99import { cleanUp } from "./command/cheanup" ;
10+ import { npmRun } from "./command/npm-run" ;
1011
1112export 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} ;
You can’t perform that action at this time.
0 commit comments