|
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