Skip to content

Commit

Permalink
feat(cz-git): configure loader extract, perf load speed
Browse files Browse the repository at this point in the history
use cosmiconfig extract common

BREAKING CHANGE :
No supprt typescript config define file. `commitlint.config.ts`

link #24, #25
  • Loading branch information
Zhengqbbb committed May 14, 2022
1 parent 9efe275 commit d4ec683
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 389 deletions.
12 changes: 9 additions & 3 deletions packages/@cz-git/plugin-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
"build:dev": "pnpm clean && tsc -b",
"clean": "rimraf *.tsbuildinfo lib dist"
},
"config": {
"commitizen": {
"path": "node_modules/cz-git",
"useEmoji": true
}
},
"devDependencies": {
"@commitlint/load": "16.2.1",
"glob": "^7.1.3",
"strip-json-comments": "3.1.1"
"@commitlint/resolve-extends": "^16.2.1",
"@commitlint/types": "^16.2.1",
"cosmiconfig": "^7.0.1"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 0 additions & 3 deletions packages/@cz-git/plugin-loader/src/commitilint/index.ts

This file was deleted.

271 changes: 0 additions & 271 deletions packages/@cz-git/plugin-loader/src/commitizen/index.ts

This file was deleted.

87 changes: 85 additions & 2 deletions packages/@cz-git/plugin-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,85 @@
export * from "./commitilint";
export * from "./commitizen";
import "@commitlint/types";
import resolveExtends from "@commitlint/resolve-extends";
import { cosmiconfig } from "cosmiconfig";
import path from "path";

export interface LoaderOptions {
moduleName: string;
cwd?: string;
stopDir?: string;
explicitPath?: string;
searchPlaces?: string[];
packageProp?: string[];
}

export const loader = async (options: LoaderOptions) => {
const cwd = options.cwd || process.cwd();
const cosmiconfigFn = cosmiconfig(options.moduleName, {
searchPlaces: options.searchPlaces || [],
packageProp: options.packageProp || options.moduleName,
stopDir: options.stopDir,
ignoreEmptySearchPlaces: true,
cache: true
});

const resultPath = options.explicitPath ? path.resolve(cwd, options.explicitPath) : undefined;
const resultFn = resultPath ? cosmiconfigFn.load : cosmiconfigFn.search;
const searchPath = resultPath ? resultPath : cwd;
const result = await resultFn(searchPath);

return result ?? null;
};

export const clLoader = async () => {
const moduleName = "commitlint";
const options = {
moduleName,
searchPlaces: [
"package.json",
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`.${moduleName}rc.cjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`
]
};
const data = await loader(options);
if (data === null) return {};

// resolve extends
const extended = resolveExtends(data.config, {
prefix: "commitlint-config",
cwd: data.filepath
});
return extended;
};

export const czLoader = async () => {
const moduleName = "commitizen";
const options = {
moduleName,
searchPlaces: ["package.json", ".czrc", ".cz.json"],
packageProp: ["config", "commitizen"]
};
const data = await loader(options);
return data === null ? {} : data.config || data;
};

/**
* @description: Main Func: both loader commitizen config and commitlint config
*/
export const configLoader = async () => {
return Promise.all([clLoader(), czLoader()]).then(([clData, czData]) => {
const clPrompt = clData.prompt || {};
return {
...clData,
prompt: {
...czData,
...clPrompt
}
};
});
};
Loading

0 comments on commit d4ec683

Please sign in to comment.