-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.ts
186 lines (170 loc) · 4.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import '@commitlint/types'
import path from 'node:path'
import os from 'node:os'
import resolveExtends from '@commitlint/resolve-extends'
import { cosmiconfig } from 'cosmiconfig'
import type { RulesConfig } from '@commitlint/types'
type ExectableConfig<T> = (() => T) | (() => Promise<T>)
type Config<T> = T | Promise<T> | ExectableConfig<T>
type ExecutedConfig<T> = readonly [string, T]
interface CommitlintOptions {
rules?: Partial<RulesConfig>
prompt?: any
}
export interface LoaderOptions {
moduleName: string
cwd?: string
stopDir?: string
explicitPath?: string
searchPlaces?: string[]
packageProp?: string[]
}
export async function loader(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 || cwd
const result = await resultFn(searchPath)
return result ?? null
}
function executable<T>(config: Config<T>, name: string): config is ExectableConfig<T> {
return typeof config === 'function' && !name.endsWith('CB')
}
async function configExecute<T>(
isRule: boolean,
configItem?: [string, Config<T>],
): Promise<ExecutedConfig<T> | null> {
if (isRule && !Array.isArray(configItem))
return null
const [name, config] = configItem as [string, Config<T>]
const fn = executable(config, name) ? config : async () => config
return [name, await fn()]
}
async function execute<T>(config: Config<T>, isRule = true): Promise<T> {
return (
await Promise.all(Object.entries(config || {}).map(entry => configExecute(isRule, entry)))
).reduce((registry: any, item) => {
const [key, value] = item!
registry[key] = value
return registry
}, {})
}
export async function clLoader(cwd?: string): Promise<CommitlintOptions> {
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`,
],
cwd,
}
const data = await loader(options)
if (data === null)
return {}
// resolve extends
const base = (data && data.filepath) ? path.dirname(data.filepath) : process.cwd()
const extended = resolveExtends(data.config, {
prefix: 'commitlint-config',
cwd: base,
})
return Promise.all([
execute(extended.rules || {}, true),
execute(extended.prompt || {}, false),
]).then(([rules, prompt]) => {
return {
rules: rules || {},
prompt: prompt || {},
}
})
}
export async function czLoader(cwd?: string) {
const moduleName = 'cz'
const options = {
moduleName,
searchPlaces: [
`.${moduleName}rc`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
'package.json',
],
packageProp: ['config', 'commitizen'],
cwd,
}
let data = await loader(options)
if (!data)
return {}
if (typeof data.config.czConfig === 'string') {
const base = (data && data.filepath) ? path.dirname(data.filepath) : process.cwd()
data = await cosmiconfig('commitizen', {
ignoreEmptySearchPlaces: true,
cache: true,
}).load(path.resolve(base, data.config.czConfig))
}
return await execute(data?.config || data || {}, true)
}
export async function aiLoader() {
const cwd = os.homedir()
const options = {
moduleName: 'czrc',
searchPlaces: [
'.config/.czrc',
'.czrc',
],
cwd,
stopDir: cwd,
}
const data = await loader(options)
return {
openAIToken: data?.config?.openAIToken || '',
apiProxy: data?.config?.apiProxy || '',
}
}
export interface UserOptions {
/** Debug mode path */
cwd?: string
/** Directly specify the configuration path */
configPath?: string
}
/**
* @description: Main Func: both loader commitizen config and commitlint config
*/
export async function configLoader(options?: UserOptions) {
// provide cli config loader
if (typeof options?.configPath === 'string') {
const czData = await cosmiconfig('commitizen', {
ignoreEmptySearchPlaces: true,
cache: true,
}).load(path.resolve(options.cwd || process.cwd(), options.configPath))
return { prompt: await execute(czData?.config || czData || {}, true) }
}
else {
return Promise.all([clLoader(options?.cwd), czLoader(options?.cwd), aiLoader()]).then(
([clData, czData, rootData]) => {
const clPrompt = clData.prompt || {}
return {
...clData,
prompt: {
...czData,
...clPrompt,
...rootData,
},
}
},
)
}
}