-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.ts
More file actions
221 lines (207 loc) · 8.61 KB
/
Copy pathconfig.ts
File metadata and controls
221 lines (207 loc) · 8.61 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {join} from "node:path";
import {pathToFileURL} from "node:url";
import {access} from "node:fs/promises";
import type {ParseArgsOptionsConfig} from "node:util";
import {validRange} from "./utils/semver.ts";
import {commaSeparatedToArray, patternToRegex, walkUp, memoizeAsync} from "./utils/utils.ts";
import type {RenovateImportOptions} from "./utils/renovate.ts";
import {loadRenovateConfig} from "./utils/renovate.ts";
export type Config = {
/** Array of dependencies to include */
include?: Array<string | RegExp>;
/** Array of dependencies to exclude */
exclude?: Array<string | RegExp>;
/** Array of package types to use */
types?: Array<string>;
/** URL to npm registry */
registry?: string;
/** Minimum dependency age, e.g. 7 (days), "1w", "2d", "6h" */
cooldown?: number | string;
/** Pin dependencies to semver ranges */
pin?: Record<string, string>;
/**
* @internal Not a user option. Set by the renovate importer for the `pin` entries that came
* from `allowedVersions`, which filter but never downgrade. The importer marks the whole map
* with `true`, loadConfig narrows it to the names renovate still owns, and only the
* per-directory config is ever read.
*/
pinNoDowngrade?: boolean | Array<string>;
/** File or directory paths to use */
files?: Array<string>;
/** Which modes to enable */
modes?: Array<string>;
/** Update versions and write dependency files */
update?: boolean;
/** Include indirect Go dependencies */
indirect?: boolean;
/** Output a JSON object */
json?: boolean;
/** Print verbose output to stderr */
verbose?: boolean;
/** Exit with code 2 when updates are available */
errorOnOutdated?: boolean;
/** Exit with code 0 when updates are available and 2 when not */
errorOnUnchanged?: boolean;
/** Force color output */
color?: boolean;
/** Disable color output */
noColor?: boolean;
/** Disable HTTP cache */
noCache?: boolean;
/** Network request timeout in ms, go probes use half */
timeout?: number;
/** Maximum number of parallel HTTP sockets */
sockets?: number;
/** Ignore the `latest` tag and take the greatest release */
greatest?: boolean | Array<string | RegExp>;
/** Consider prereleases, implying `greatest` */
prerelease?: boolean | Array<string | RegExp>;
/** Never consider prereleases */
release?: boolean | Array<string | RegExp>;
/** Consider only up to semver-patch */
patch?: boolean | Array<string | RegExp>;
/** Consider only up to semver-minor */
minor?: boolean | Array<string | RegExp>;
/** Allow downgrading onto a lower `latest` tag */
allowDowngrade?: boolean | Array<string | RegExp>;
/** Per-package option overrides, matched by name; last matching override wins */
overrides?: Array<Override>;
/** Opt-in to inheriting select fields from other tools' configs */
inherit?: {renovate?: RenovateImportOptions};
};
/** Options applied to dependencies whose name matches an override's patterns. */
export type Override = {
/** Name patterns this override applies to (glob or RegExp). Omit to match all. */
include?: Array<string | RegExp>;
/** Name patterns excluded from this override */
exclude?: Array<string | RegExp>;
/** Minimum dependency age, e.g. 7 (days), "1w", "2d", "6h"; 0 disables a global cooldown */
cooldown?: number | string;
/** Ignore the `latest` tag and take the greatest release */
greatest?: boolean;
/** Consider prereleases, implying `greatest` */
prerelease?: boolean;
/** Never consider prereleases */
release?: boolean;
/** Consider only up to semver-patch */
patch?: boolean;
/** Consider only up to semver-minor */
minor?: boolean;
/** Allow downgrading onto a lower `latest` tag */
allowDowngrade?: boolean;
};
export type Arg = string | boolean | Array<string | boolean> | undefined;
export const cliBaseConfig = Symbol("cliBaseConfig");
export const options: ParseArgsOptionsConfig = {
"allow-downgrade": {short: "d", type: "string", multiple: true},
"error-on-outdated": {short: "E", type: "boolean"},
"error-on-unchanged": {short: "U", type: "boolean"},
"exclude": {short: "e", type: "string", multiple: true},
"file": {short: "f", type: "string", multiple: true},
"forgeapi": {type: "string"}, // test-only
"goproxy": {type: "string"}, // test-only
"cargoapi": {type: "string"}, // test-only
"dockerapi": {type: "string"}, // test-only
"greatest": {short: "g", type: "string", multiple: true},
"help": {short: "h", type: "boolean"},
"include": {short: "i", type: "string", multiple: true},
"indirect": {short: "I", type: "boolean"},
"json": {short: "j", type: "boolean"},
"jsrapi": {type: "string"}, // test-only
"cooldown": {short: "C", type: "string"},
"minor": {short: "m", type: "string", multiple: true},
"modes": {short: "M", type: "string", multiple: true},
"color": {short: "c", type: "boolean"},
"no-cache": {short: "x", type: "boolean"},
"no-color": {short: "n", type: "boolean"},
"patch": {short: "P", type: "string", multiple: true},
"pin": {short: "l", type: "string", multiple: true},
"prerelease": {short: "p", type: "string", multiple: true},
"pypiapi": {type: "string"}, // test-only
"registry": {short: "r", type: "string"},
"release": {short: "R", type: "string", multiple: true},
"sockets": {short: "s", type: "string"},
"timeout": {short: "T", type: "string"},
"types": {short: "t", type: "string", multiple: true},
"update": {short: "u", type: "boolean"},
"verbose": {short: "V", type: "boolean"},
"version": {short: "v", type: "boolean"},
};
// the `[<dep,...>]` options in --help, valid on their own
export const optionalValueOptions = new Set(["allow-downgrade", "greatest", "minor", "patch", "prerelease", "release"]);
export function parseMixedArg(arg: Arg): boolean | Set<string> {
if (Array.isArray(arg)) {
return !arg.length || arg.includes(true) ? true :
new Set(arg.filter(val => typeof val === "string").flatMap(commaSeparatedToArray));
}
if (typeof arg === "string") return new Set([arg]);
if (typeof arg === "boolean") return arg;
return false;
}
export function getOptionKey(name: string): string {
for (const [key, {short}] of Object.entries(options)) {
if (key === name || short === name) return key;
}
return "";
}
export function patternsToRegexSet(patterns: Array<string | RegExp>): Set<RegExp> {
return new Set(patterns.map(patternToRegex));
}
export function parseArgList(arg: Arg): Array<string> {
if (Array.isArray(arg)) {
return arg.filter(v => typeof v === "string").flatMap(commaSeparatedToArray);
}
return [];
}
export function validatePin(pin: Config["pin"]): void {
for (const [pkg, range] of Object.entries(pin ?? {})) {
if (!validRange(range)) throw new Error(`Invalid pin range for ${pkg}: ${range}`);
}
}
export function parsePinArg(arg: Arg): Record<string, string> {
const result: Record<string, string> = {};
for (const val of Array.isArray(arg) ? arg : [arg]) {
if (typeof val !== "string") continue;
const eq = val.indexOf("=");
if (eq < 1) throw new Error(`Invalid pin: ${val}, expected <dep>=<range>`);
result[val.slice(0, eq)] = val.slice(eq + 1);
}
validatePin(result);
return result;
}
export function configMixedToRegexes(val: boolean | Array<string | RegExp> | undefined): Set<RegExp> | boolean {
if (typeof val === "boolean") return val;
if (!Array.isArray(val) || !val.length) return false;
return patternsToRegexSet(val);
}
const findConfigUp = memoizeAsync((startDir: string) => walkUp(startDir, async dir => {
for (const ext of ["js", "ts", "mjs", "mts"]) {
const filename = `updates.config.${ext}`;
const fullPath = join(dir, filename);
try {
await access(fullPath);
} catch (err: any) {
if (err.code === "ENOENT") continue;
throw new Error(`Unable to load config file ${filename}: ${err.message}`);
}
try {
const mod = await import(pathToFileURL(fullPath).href);
return mod.default ?? {};
} catch (err: any) {
throw new Error(`Unable to parse config file ${filename}: ${err?.message ?? err}`);
}
}
return null;
}));
export const loadConfig = memoizeAsync(async (startDir: string): Promise<Config> => {
const raw = await findConfigUp(startDir) ?? {};
const renovateConfig = await loadRenovateConfig(startDir, raw.inherit?.renovate);
const config: Config = {...renovateConfig, ...raw};
if (renovateConfig.pin) {
config.pin = {...renovateConfig.pin, ...raw.pin};
config.pinNoDowngrade = Object.keys(renovateConfig.pin).filter(name => !raw.pin?.[name]);
}
if (renovateConfig.overrides?.length) config.overrides = [...renovateConfig.overrides, ...(raw.overrides ?? [])];
validatePin(config.pin);
return config;
});