Skip to content

Commit 61a467f

Browse files
committed
feat(feature): 添加prettier命令
1 parent e430fbe commit 61a467f

File tree

13 files changed

+149
-31
lines changed

13 files changed

+149
-31
lines changed

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ npm i -g code-genius
1818

1919
## 终端命令
2020

21-
| 命令 | 参数 | 功能描述 |
22-
| ------ | ---------------------------------------------- | ---------------------------------------------- |
23-
| cm | --noEmoji | 帮助生成规范的 git 提交内容 |
24-
| cmv | -- | 帮助验证 git commit 的内容是否符合规范 |
25-
| cup | --ignore \<path\> | 清理运行时生成的文件 |
26-
| ihooks | -- | 使用且有修改 simple-git-hooks 后需要重新初始化 |
27-
| run | -- | 运行列出的脚本 |
28-
| lint | --eslintrc \<file\>, --path \<path\>, --staged, --suffix \<suffix\> | 检查代码并尝试修复 |
21+
| 命令 | 参数 | 功能描述 |
22+
| -------- | ------------------------------------------------------------------- | ---------------------------------------------- |
23+
| cm | --noEmoji | 帮助生成规范的 git 提交内容 |
24+
| cmv | -- | 帮助验证 git commit 的内容是否符合规范 |
25+
| cup | --ignore \<path\> | 清理运行时生成的文件 |
26+
| ihooks | -- | 使用且有修改 simple-git-hooks 后需要重新初始化 |
27+
| run | -- | 运行列出的脚本 |
28+
| lint | --eslintrc \<file\>, --path \<path\>, --staged, --suffix \<suffix\> | 检查代码并尝试修复 |
29+
| prettier | --path \<path\>, --staged, --suffix \<suffix\> | 格式化代码高风格 |
2930

3031
## API
3132

@@ -37,6 +38,7 @@ npm i -g code-genius
3738
| 4 | `gitInitSimpleHooks(cwd)` | `cwd?: string` | `Promise<void>` |
3839
| 5 | `npmRun(cwd)` | `cwd?: string` | `Promise<void>` |
3940
| 6 | `eslintFix(cwd, options)` | `cwd?: string`,` options:(EsLintOptions)` | `Promise<void>` |
41+
| 7 | `prettierFormat(cwd, options)` | `cwd?: string`,` options:(PrettierFormatOptions)` | `Promise<void>` |
4042

4143
## API 示例
4244

@@ -112,6 +114,18 @@ await eslintFix(cwd, esLintOptions);
112114
npx esno index.ts
113115
```
114116

117+
prettierFormat()
118+
119+
```typescript
120+
// ./index.ts
121+
import { prettierFormat, cwd, prettierFormatOptions } from "code-genius";
122+
123+
await prettierFormat(cwd, prettierFormatOptions);
124+
125+
// 运行
126+
npx esno index.ts
127+
```
128+
115129
## 执照
116130

117131
MIT License

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"rimraf": "^5.0.1"
7878
},
7979
"simple-git-hooks": {
80-
"pre-commit": "npx esno scripts/lint-script.ts",
81-
"commit-msg": "npx esno scripts/cmv-script.ts"
80+
"pre-commit": "npx cross-env CG_DEBUG=activation esno scripts/lint-script.ts",
81+
"commit-msg": "npx cross-env CG_DEBUG=activation esno scripts/cmv-script.ts"
8282
}
8383
}

scripts/format-script.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { loggerWarring } from "../src/shared";
2+
import { prettierFormatOptions } from "../src/shared/config";
3+
import { prettierFormat } from "../src/command/prettier-format";
4+
5+
async function format() {
6+
try {
7+
await Promise.all([prettierFormat(process.cwd(), prettierFormatOptions)]);
8+
} catch (error) {
9+
loggerWarring(error);
10+
}
11+
}
12+
13+
format();

scripts/lint-script.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { eslintFix } from "./../src/command/eslint-fix";
22
import { loggerWarring } from "../src/shared";
3-
import { esLintOptions } from "../src/shared/config";
3+
import { esLintOptions, prettierFormatOptions } from "../src/shared/config";
4+
import { prettierFormat } from "../src/command/prettier-format";
45

56
async function lint() {
67
try {
7-
await Promise.all([eslintFix(process.cwd(), esLintOptions)]);
8+
await prettierFormat(process.cwd(), prettierFormatOptions);
9+
await eslintFix(process.cwd(), esLintOptions);
810
} catch (error) {
911
loggerWarring(error);
1012
}

src/command/eslint-fix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { EsLintOptions } from "../shared/types";
1212

1313
export const eslintFix = async (
1414
cwd = process.cwd(),
15-
options: EsLintOptions
15+
options: EsLintOptions,
1616
) => {
1717
try {
1818
const { eslintrc, staged, paths, suffix } = options;

src/command/git-commit-verify.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import path from "node:path";
44
import { execCommand } from "../shared";
55

66
export const gitCommitVerify = async () => {
7-
const dogit = await execCommand("git", ["rev-parse", "--show-toplevel"]);
7+
const dogit = await execCommand("git", ["rev-parse", "--show-toplevel"], {
8+
stdio: "inherit",
9+
});
810
const root = path.join(dogit || "", ".git", "COMMIT_EDITMSG");
911
const content = readFileSync(root, { encoding: "utf-8" }).trim();
1012
const REG_EXP =

src/command/git-commit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface GitCommitOptions {
1616
export const gitCommit = async (
1717
types: Array<CommitType>,
1818
scopes: Array<CommitScope>,
19-
options: GitCommitOptions
19+
options: GitCommitOptions,
2020
) => {
2121
if (ACTIVATION) {
2222
loggerInfo("gitCommit 参数信息: \n");

src/command/prettier-format.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
execCommand,
3+
getLintFiles,
4+
loggerError,
5+
loggerInfo,
6+
} from "./../shared/index";
7+
import { PrettierFormatOptions } from "../shared/types";
8+
import { ACTIVATION } from "../shared/config";
9+
10+
export const prettierFormat = async (
11+
cwd = process.cwd(),
12+
options: PrettierFormatOptions,
13+
) => {
14+
const { staged, paths, suffix } = options;
15+
16+
if (ACTIVATION) {
17+
loggerInfo("prettierFormat 参数信息: \n");
18+
console.table(cwd);
19+
console.table(options);
20+
}
21+
22+
try {
23+
const files = await getLintFiles(cwd, staged, paths, suffix);
24+
if (files.length > 0) {
25+
const result = await execCommand("npx", [
26+
"prettier",
27+
"--debug-check",
28+
...files,
29+
]);
30+
if (result) {
31+
await execCommand(
32+
"npx",
33+
["prettier", "--write", ...result.split("\n")],
34+
{
35+
stdio: "inherit",
36+
},
37+
);
38+
await execCommand("git", ["add", ...result.split("\n")], {
39+
stdio: "inherit",
40+
});
41+
}
42+
}
43+
} catch (error) {
44+
loggerError(error);
45+
}
46+
};

src/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
export * from './command/cheanup';
2-
export * from './command/git-commit';
3-
export * from './command/git-commit-verify';
4-
export * from './command/git-init-hooks';
5-
export * from './command/npm-run';
6-
export * from './command/eslint-fix';
1+
export * from "./command/cheanup";
2+
export * from "./command/git-commit";
3+
export * from "./command/git-commit-verify";
4+
export * from "./command/git-init-hooks";
5+
export * from "./command/npm-run";
6+
export * from "./command/eslint-fix";
7+
export * from "./command/prettier-format";
78

8-
export * from './shared/index';
9-
export * from './shared/config';
10-
export * from './shared/types';
9+
export * from "./shared/index";
10+
export * from "./shared/config";
11+
export * from "./shared/types";
1112

12-
export * from './setup';
13+
export * from "./setup";

0 commit comments

Comments
 (0)