Skip to content

Commit d4ec683

Browse files
committed
feat(cz-git): configure loader extract, perf load speed
use cosmiconfig extract common BREAKING CHANGE : No supprt typescript config define file. `commitlint.config.ts` link #24, #25
1 parent 9efe275 commit d4ec683

8 files changed

Lines changed: 176 additions & 389 deletions

File tree

‎packages/@cz-git/plugin-loader/package.json‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@
2828
"build:dev": "pnpm clean && tsc -b",
2929
"clean": "rimraf *.tsbuildinfo lib dist"
3030
},
31+
"config": {
32+
"commitizen": {
33+
"path": "node_modules/cz-git",
34+
"useEmoji": true
35+
}
36+
},
3137
"devDependencies": {
32-
"@commitlint/load": "16.2.1",
33-
"glob": "^7.1.3",
34-
"strip-json-comments": "3.1.1"
38+
"@commitlint/resolve-extends": "^16.2.1",
39+
"@commitlint/types": "^16.2.1",
40+
"cosmiconfig": "^7.0.1"
3541
},
3642
"publishConfig": {
3743
"access": "public"

‎packages/@cz-git/plugin-loader/src/commitilint/index.ts‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎packages/@cz-git/plugin-loader/src/commitizen/index.ts‎

Lines changed: 0 additions & 271 deletions
This file was deleted.
Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,85 @@
1-
export * from "./commitilint";
2-
export * from "./commitizen";
1+
import "@commitlint/types";
2+
import resolveExtends from "@commitlint/resolve-extends";
3+
import { cosmiconfig } from "cosmiconfig";
4+
import path from "path";
5+
6+
export interface LoaderOptions {
7+
moduleName: string;
8+
cwd?: string;
9+
stopDir?: string;
10+
explicitPath?: string;
11+
searchPlaces?: string[];
12+
packageProp?: string[];
13+
}
14+
15+
export const loader = async (options: LoaderOptions) => {
16+
const cwd = options.cwd || process.cwd();
17+
const cosmiconfigFn = cosmiconfig(options.moduleName, {
18+
searchPlaces: options.searchPlaces || [],
19+
packageProp: options.packageProp || options.moduleName,
20+
stopDir: options.stopDir,
21+
ignoreEmptySearchPlaces: true,
22+
cache: true
23+
});
24+
25+
const resultPath = options.explicitPath ? path.resolve(cwd, options.explicitPath) : undefined;
26+
const resultFn = resultPath ? cosmiconfigFn.load : cosmiconfigFn.search;
27+
const searchPath = resultPath ? resultPath : cwd;
28+
const result = await resultFn(searchPath);
29+
30+
return result ?? null;
31+
};
32+
33+
export const clLoader = async () => {
34+
const moduleName = "commitlint";
35+
const options = {
36+
moduleName,
37+
searchPlaces: [
38+
"package.json",
39+
`.${moduleName}rc`,
40+
`.${moduleName}rc.json`,
41+
`.${moduleName}rc.yaml`,
42+
`.${moduleName}rc.yml`,
43+
`.${moduleName}rc.js`,
44+
`.${moduleName}rc.cjs`,
45+
`${moduleName}.config.js`,
46+
`${moduleName}.config.cjs`
47+
]
48+
};
49+
const data = await loader(options);
50+
if (data === null) return {};
51+
52+
// resolve extends
53+
const extended = resolveExtends(data.config, {
54+
prefix: "commitlint-config",
55+
cwd: data.filepath
56+
});
57+
return extended;
58+
};
59+
60+
export const czLoader = async () => {
61+
const moduleName = "commitizen";
62+
const options = {
63+
moduleName,
64+
searchPlaces: ["package.json", ".czrc", ".cz.json"],
65+
packageProp: ["config", "commitizen"]
66+
};
67+
const data = await loader(options);
68+
return data === null ? {} : data.config || data;
69+
};
70+
71+
/**
72+
* @description: Main Func: both loader commitizen config and commitlint config
73+
*/
74+
export const configLoader = async () => {
75+
return Promise.all([clLoader(), czLoader()]).then(([clData, czData]) => {
76+
const clPrompt = clData.prompt || {};
77+
return {
78+
...clData,
79+
prompt: {
80+
...czData,
81+
...clPrompt
82+
}
83+
};
84+
});
85+
};

0 commit comments

Comments
 (0)