bun-git-hooks 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -0
- package/dist/cli.js +1082 -0
- package/dist/config.d.ts +4 -0
- package/dist/git-hooks.d.ts +50 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +831 -0
- package/dist/types.d.ts +43 -0
- package/package.json +92 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1082 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// bin/cli.ts
|
|
5
|
+
import process3 from "process";
|
|
6
|
+
|
|
7
|
+
// node_modules/cac/dist/index.mjs
|
|
8
|
+
import { EventEmitter } from "events";
|
|
9
|
+
function toArr(any) {
|
|
10
|
+
return any == null ? [] : Array.isArray(any) ? any : [any];
|
|
11
|
+
}
|
|
12
|
+
function toVal(out, key, val, opts) {
|
|
13
|
+
var x, old = out[key], nxt = ~opts.string.indexOf(key) ? val == null || val === true ? "" : String(val) : typeof val === "boolean" ? val : ~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
|
|
14
|
+
out[key] = old == null ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
|
|
15
|
+
}
|
|
16
|
+
function mri2(args, opts) {
|
|
17
|
+
args = args || [];
|
|
18
|
+
opts = opts || {};
|
|
19
|
+
var k, arr, arg, name, val, out = { _: [] };
|
|
20
|
+
var i = 0, j = 0, idx = 0, len = args.length;
|
|
21
|
+
const alibi = opts.alias !== undefined;
|
|
22
|
+
const strict = opts.unknown !== undefined;
|
|
23
|
+
const defaults = opts.default !== undefined;
|
|
24
|
+
opts.alias = opts.alias || {};
|
|
25
|
+
opts.string = toArr(opts.string);
|
|
26
|
+
opts.boolean = toArr(opts.boolean);
|
|
27
|
+
if (alibi) {
|
|
28
|
+
for (k in opts.alias) {
|
|
29
|
+
arr = opts.alias[k] = toArr(opts.alias[k]);
|
|
30
|
+
for (i = 0;i < arr.length; i++) {
|
|
31
|
+
(opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (i = opts.boolean.length;i-- > 0; ) {
|
|
36
|
+
arr = opts.alias[opts.boolean[i]] || [];
|
|
37
|
+
for (j = arr.length;j-- > 0; )
|
|
38
|
+
opts.boolean.push(arr[j]);
|
|
39
|
+
}
|
|
40
|
+
for (i = opts.string.length;i-- > 0; ) {
|
|
41
|
+
arr = opts.alias[opts.string[i]] || [];
|
|
42
|
+
for (j = arr.length;j-- > 0; )
|
|
43
|
+
opts.string.push(arr[j]);
|
|
44
|
+
}
|
|
45
|
+
if (defaults) {
|
|
46
|
+
for (k in opts.default) {
|
|
47
|
+
name = typeof opts.default[k];
|
|
48
|
+
arr = opts.alias[k] = opts.alias[k] || [];
|
|
49
|
+
if (opts[name] !== undefined) {
|
|
50
|
+
opts[name].push(k);
|
|
51
|
+
for (i = 0;i < arr.length; i++) {
|
|
52
|
+
opts[name].push(arr[i]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const keys = strict ? Object.keys(opts.alias) : [];
|
|
58
|
+
for (i = 0;i < len; i++) {
|
|
59
|
+
arg = args[i];
|
|
60
|
+
if (arg === "--") {
|
|
61
|
+
out._ = out._.concat(args.slice(++i));
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
for (j = 0;j < arg.length; j++) {
|
|
65
|
+
if (arg.charCodeAt(j) !== 45)
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
if (j === 0) {
|
|
69
|
+
out._.push(arg);
|
|
70
|
+
} else if (arg.substring(j, j + 3) === "no-") {
|
|
71
|
+
name = arg.substring(j + 3);
|
|
72
|
+
if (strict && !~keys.indexOf(name)) {
|
|
73
|
+
return opts.unknown(arg);
|
|
74
|
+
}
|
|
75
|
+
out[name] = false;
|
|
76
|
+
} else {
|
|
77
|
+
for (idx = j + 1;idx < arg.length; idx++) {
|
|
78
|
+
if (arg.charCodeAt(idx) === 61)
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
name = arg.substring(j, idx);
|
|
82
|
+
val = arg.substring(++idx) || (i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i]);
|
|
83
|
+
arr = j === 2 ? [name] : name;
|
|
84
|
+
for (idx = 0;idx < arr.length; idx++) {
|
|
85
|
+
name = arr[idx];
|
|
86
|
+
if (strict && !~keys.indexOf(name))
|
|
87
|
+
return opts.unknown("-".repeat(j) + name);
|
|
88
|
+
toVal(out, name, idx + 1 < arr.length || val, opts);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (defaults) {
|
|
93
|
+
for (k in opts.default) {
|
|
94
|
+
if (out[k] === undefined) {
|
|
95
|
+
out[k] = opts.default[k];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (alibi) {
|
|
100
|
+
for (k in out) {
|
|
101
|
+
arr = opts.alias[k] || [];
|
|
102
|
+
while (arr.length > 0) {
|
|
103
|
+
out[arr.shift()] = out[k];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
var removeBrackets = (v) => v.replace(/[<[].+/, "").trim();
|
|
110
|
+
var findAllBrackets = (v) => {
|
|
111
|
+
const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
|
|
112
|
+
const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
|
|
113
|
+
const res = [];
|
|
114
|
+
const parse = (match) => {
|
|
115
|
+
let variadic = false;
|
|
116
|
+
let value = match[1];
|
|
117
|
+
if (value.startsWith("...")) {
|
|
118
|
+
value = value.slice(3);
|
|
119
|
+
variadic = true;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
required: match[0].startsWith("<"),
|
|
123
|
+
value,
|
|
124
|
+
variadic
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
let angledMatch;
|
|
128
|
+
while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v)) {
|
|
129
|
+
res.push(parse(angledMatch));
|
|
130
|
+
}
|
|
131
|
+
let squareMatch;
|
|
132
|
+
while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) {
|
|
133
|
+
res.push(parse(squareMatch));
|
|
134
|
+
}
|
|
135
|
+
return res;
|
|
136
|
+
};
|
|
137
|
+
var getMriOptions = (options) => {
|
|
138
|
+
const result = { alias: {}, boolean: [] };
|
|
139
|
+
for (const [index, option] of options.entries()) {
|
|
140
|
+
if (option.names.length > 1) {
|
|
141
|
+
result.alias[option.names[0]] = option.names.slice(1);
|
|
142
|
+
}
|
|
143
|
+
if (option.isBoolean) {
|
|
144
|
+
if (option.negated) {
|
|
145
|
+
const hasStringTypeOption = options.some((o, i) => {
|
|
146
|
+
return i !== index && o.names.some((name) => option.names.includes(name)) && typeof o.required === "boolean";
|
|
147
|
+
});
|
|
148
|
+
if (!hasStringTypeOption) {
|
|
149
|
+
result.boolean.push(option.names[0]);
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
result.boolean.push(option.names[0]);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
};
|
|
158
|
+
var findLongest = (arr) => {
|
|
159
|
+
return arr.sort((a, b) => {
|
|
160
|
+
return a.length > b.length ? -1 : 1;
|
|
161
|
+
})[0];
|
|
162
|
+
};
|
|
163
|
+
var padRight = (str, length) => {
|
|
164
|
+
return str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
|
|
165
|
+
};
|
|
166
|
+
var camelcase = (input) => {
|
|
167
|
+
return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
|
|
168
|
+
return p1 + p2.toUpperCase();
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
var setDotProp = (obj, keys, val) => {
|
|
172
|
+
let i = 0;
|
|
173
|
+
let length = keys.length;
|
|
174
|
+
let t = obj;
|
|
175
|
+
let x;
|
|
176
|
+
for (;i < length; ++i) {
|
|
177
|
+
x = t[keys[i]];
|
|
178
|
+
t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf(".") || !(+keys[i + 1] > -1) ? {} : [];
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var setByType = (obj, transforms) => {
|
|
182
|
+
for (const key of Object.keys(transforms)) {
|
|
183
|
+
const transform = transforms[key];
|
|
184
|
+
if (transform.shouldTransform) {
|
|
185
|
+
obj[key] = Array.prototype.concat.call([], obj[key]);
|
|
186
|
+
if (typeof transform.transformFunction === "function") {
|
|
187
|
+
obj[key] = obj[key].map(transform.transformFunction);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
var getFileName = (input) => {
|
|
193
|
+
const m = /([^\\\/]+)$/.exec(input);
|
|
194
|
+
return m ? m[1] : "";
|
|
195
|
+
};
|
|
196
|
+
var camelcaseOptionName = (name) => {
|
|
197
|
+
return name.split(".").map((v, i) => {
|
|
198
|
+
return i === 0 ? camelcase(v) : v;
|
|
199
|
+
}).join(".");
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
class CACError extends Error {
|
|
203
|
+
constructor(message) {
|
|
204
|
+
super(message);
|
|
205
|
+
this.name = this.constructor.name;
|
|
206
|
+
if (typeof Error.captureStackTrace === "function") {
|
|
207
|
+
Error.captureStackTrace(this, this.constructor);
|
|
208
|
+
} else {
|
|
209
|
+
this.stack = new Error(message).stack;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
class Option {
|
|
215
|
+
constructor(rawName, description, config) {
|
|
216
|
+
this.rawName = rawName;
|
|
217
|
+
this.description = description;
|
|
218
|
+
this.config = Object.assign({}, config);
|
|
219
|
+
rawName = rawName.replace(/\.\*/g, "");
|
|
220
|
+
this.negated = false;
|
|
221
|
+
this.names = removeBrackets(rawName).split(",").map((v) => {
|
|
222
|
+
let name = v.trim().replace(/^-{1,2}/, "");
|
|
223
|
+
if (name.startsWith("no-")) {
|
|
224
|
+
this.negated = true;
|
|
225
|
+
name = name.replace(/^no-/, "");
|
|
226
|
+
}
|
|
227
|
+
return camelcaseOptionName(name);
|
|
228
|
+
}).sort((a, b) => a.length > b.length ? 1 : -1);
|
|
229
|
+
this.name = this.names[this.names.length - 1];
|
|
230
|
+
if (this.negated && this.config.default == null) {
|
|
231
|
+
this.config.default = true;
|
|
232
|
+
}
|
|
233
|
+
if (rawName.includes("<")) {
|
|
234
|
+
this.required = true;
|
|
235
|
+
} else if (rawName.includes("[")) {
|
|
236
|
+
this.required = false;
|
|
237
|
+
} else {
|
|
238
|
+
this.isBoolean = true;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
var processArgs = process.argv;
|
|
243
|
+
var platformInfo = `${process.platform}-${process.arch} node-${process.version}`;
|
|
244
|
+
|
|
245
|
+
class Command {
|
|
246
|
+
constructor(rawName, description, config = {}, cli) {
|
|
247
|
+
this.rawName = rawName;
|
|
248
|
+
this.description = description;
|
|
249
|
+
this.config = config;
|
|
250
|
+
this.cli = cli;
|
|
251
|
+
this.options = [];
|
|
252
|
+
this.aliasNames = [];
|
|
253
|
+
this.name = removeBrackets(rawName);
|
|
254
|
+
this.args = findAllBrackets(rawName);
|
|
255
|
+
this.examples = [];
|
|
256
|
+
}
|
|
257
|
+
usage(text) {
|
|
258
|
+
this.usageText = text;
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
allowUnknownOptions() {
|
|
262
|
+
this.config.allowUnknownOptions = true;
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
ignoreOptionDefaultValue() {
|
|
266
|
+
this.config.ignoreOptionDefaultValue = true;
|
|
267
|
+
return this;
|
|
268
|
+
}
|
|
269
|
+
version(version, customFlags = "-v, --version") {
|
|
270
|
+
this.versionNumber = version;
|
|
271
|
+
this.option(customFlags, "Display version number");
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
example(example) {
|
|
275
|
+
this.examples.push(example);
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
option(rawName, description, config) {
|
|
279
|
+
const option = new Option(rawName, description, config);
|
|
280
|
+
this.options.push(option);
|
|
281
|
+
return this;
|
|
282
|
+
}
|
|
283
|
+
alias(name) {
|
|
284
|
+
this.aliasNames.push(name);
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
action(callback) {
|
|
288
|
+
this.commandAction = callback;
|
|
289
|
+
return this;
|
|
290
|
+
}
|
|
291
|
+
isMatched(name) {
|
|
292
|
+
return this.name === name || this.aliasNames.includes(name);
|
|
293
|
+
}
|
|
294
|
+
get isDefaultCommand() {
|
|
295
|
+
return this.name === "" || this.aliasNames.includes("!");
|
|
296
|
+
}
|
|
297
|
+
get isGlobalCommand() {
|
|
298
|
+
return this instanceof GlobalCommand;
|
|
299
|
+
}
|
|
300
|
+
hasOption(name) {
|
|
301
|
+
name = name.split(".")[0];
|
|
302
|
+
return this.options.find((option) => {
|
|
303
|
+
return option.names.includes(name);
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
outputHelp() {
|
|
307
|
+
const { name, commands } = this.cli;
|
|
308
|
+
const {
|
|
309
|
+
versionNumber,
|
|
310
|
+
options: globalOptions,
|
|
311
|
+
helpCallback
|
|
312
|
+
} = this.cli.globalCommand;
|
|
313
|
+
let sections = [
|
|
314
|
+
{
|
|
315
|
+
body: `${name}${versionNumber ? `/${versionNumber}` : ""}`
|
|
316
|
+
}
|
|
317
|
+
];
|
|
318
|
+
sections.push({
|
|
319
|
+
title: "Usage",
|
|
320
|
+
body: ` $ ${name} ${this.usageText || this.rawName}`
|
|
321
|
+
});
|
|
322
|
+
const showCommands = (this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0;
|
|
323
|
+
if (showCommands) {
|
|
324
|
+
const longestCommandName = findLongest(commands.map((command) => command.rawName));
|
|
325
|
+
sections.push({
|
|
326
|
+
title: "Commands",
|
|
327
|
+
body: commands.map((command) => {
|
|
328
|
+
return ` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`;
|
|
329
|
+
}).join(`
|
|
330
|
+
`)
|
|
331
|
+
});
|
|
332
|
+
sections.push({
|
|
333
|
+
title: `For more info, run any command with the \`--help\` flag`,
|
|
334
|
+
body: commands.map((command) => ` $ ${name}${command.name === "" ? "" : ` ${command.name}`} --help`).join(`
|
|
335
|
+
`)
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
let options = this.isGlobalCommand ? globalOptions : [...this.options, ...globalOptions || []];
|
|
339
|
+
if (!this.isGlobalCommand && !this.isDefaultCommand) {
|
|
340
|
+
options = options.filter((option) => option.name !== "version");
|
|
341
|
+
}
|
|
342
|
+
if (options.length > 0) {
|
|
343
|
+
const longestOptionName = findLongest(options.map((option) => option.rawName));
|
|
344
|
+
sections.push({
|
|
345
|
+
title: "Options",
|
|
346
|
+
body: options.map((option) => {
|
|
347
|
+
return ` ${padRight(option.rawName, longestOptionName.length)} ${option.description} ${option.config.default === undefined ? "" : `(default: ${option.config.default})`}`;
|
|
348
|
+
}).join(`
|
|
349
|
+
`)
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
if (this.examples.length > 0) {
|
|
353
|
+
sections.push({
|
|
354
|
+
title: "Examples",
|
|
355
|
+
body: this.examples.map((example) => {
|
|
356
|
+
if (typeof example === "function") {
|
|
357
|
+
return example(name);
|
|
358
|
+
}
|
|
359
|
+
return example;
|
|
360
|
+
}).join(`
|
|
361
|
+
`)
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
if (helpCallback) {
|
|
365
|
+
sections = helpCallback(sections) || sections;
|
|
366
|
+
}
|
|
367
|
+
console.log(sections.map((section) => {
|
|
368
|
+
return section.title ? `${section.title}:
|
|
369
|
+
${section.body}` : section.body;
|
|
370
|
+
}).join(`
|
|
371
|
+
|
|
372
|
+
`));
|
|
373
|
+
}
|
|
374
|
+
outputVersion() {
|
|
375
|
+
const { name } = this.cli;
|
|
376
|
+
const { versionNumber } = this.cli.globalCommand;
|
|
377
|
+
if (versionNumber) {
|
|
378
|
+
console.log(`${name}/${versionNumber} ${platformInfo}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
checkRequiredArgs() {
|
|
382
|
+
const minimalArgsCount = this.args.filter((arg) => arg.required).length;
|
|
383
|
+
if (this.cli.args.length < minimalArgsCount) {
|
|
384
|
+
throw new CACError(`missing required args for command \`${this.rawName}\``);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
checkUnknownOptions() {
|
|
388
|
+
const { options, globalCommand } = this.cli;
|
|
389
|
+
if (!this.config.allowUnknownOptions) {
|
|
390
|
+
for (const name of Object.keys(options)) {
|
|
391
|
+
if (name !== "--" && !this.hasOption(name) && !globalCommand.hasOption(name)) {
|
|
392
|
+
throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
checkOptionValue() {
|
|
398
|
+
const { options: parsedOptions, globalCommand } = this.cli;
|
|
399
|
+
const options = [...globalCommand.options, ...this.options];
|
|
400
|
+
for (const option of options) {
|
|
401
|
+
const value = parsedOptions[option.name.split(".")[0]];
|
|
402
|
+
if (option.required) {
|
|
403
|
+
const hasNegated = options.some((o) => o.negated && o.names.includes(option.name));
|
|
404
|
+
if (value === true || value === false && !hasNegated) {
|
|
405
|
+
throw new CACError(`option \`${option.rawName}\` value is missing`);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
class GlobalCommand extends Command {
|
|
413
|
+
constructor(cli) {
|
|
414
|
+
super("@@global@@", "", {}, cli);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
var __assign = Object.assign;
|
|
418
|
+
|
|
419
|
+
class CAC extends EventEmitter {
|
|
420
|
+
constructor(name = "") {
|
|
421
|
+
super();
|
|
422
|
+
this.name = name;
|
|
423
|
+
this.commands = [];
|
|
424
|
+
this.rawArgs = [];
|
|
425
|
+
this.args = [];
|
|
426
|
+
this.options = {};
|
|
427
|
+
this.globalCommand = new GlobalCommand(this);
|
|
428
|
+
this.globalCommand.usage("<command> [options]");
|
|
429
|
+
}
|
|
430
|
+
usage(text) {
|
|
431
|
+
this.globalCommand.usage(text);
|
|
432
|
+
return this;
|
|
433
|
+
}
|
|
434
|
+
command(rawName, description, config) {
|
|
435
|
+
const command = new Command(rawName, description || "", config, this);
|
|
436
|
+
command.globalCommand = this.globalCommand;
|
|
437
|
+
this.commands.push(command);
|
|
438
|
+
return command;
|
|
439
|
+
}
|
|
440
|
+
option(rawName, description, config) {
|
|
441
|
+
this.globalCommand.option(rawName, description, config);
|
|
442
|
+
return this;
|
|
443
|
+
}
|
|
444
|
+
help(callback) {
|
|
445
|
+
this.globalCommand.option("-h, --help", "Display this message");
|
|
446
|
+
this.globalCommand.helpCallback = callback;
|
|
447
|
+
this.showHelpOnExit = true;
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
version(version, customFlags = "-v, --version") {
|
|
451
|
+
this.globalCommand.version(version, customFlags);
|
|
452
|
+
this.showVersionOnExit = true;
|
|
453
|
+
return this;
|
|
454
|
+
}
|
|
455
|
+
example(example) {
|
|
456
|
+
this.globalCommand.example(example);
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
459
|
+
outputHelp() {
|
|
460
|
+
if (this.matchedCommand) {
|
|
461
|
+
this.matchedCommand.outputHelp();
|
|
462
|
+
} else {
|
|
463
|
+
this.globalCommand.outputHelp();
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
outputVersion() {
|
|
467
|
+
this.globalCommand.outputVersion();
|
|
468
|
+
}
|
|
469
|
+
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) {
|
|
470
|
+
this.args = args;
|
|
471
|
+
this.options = options;
|
|
472
|
+
if (matchedCommand) {
|
|
473
|
+
this.matchedCommand = matchedCommand;
|
|
474
|
+
}
|
|
475
|
+
if (matchedCommandName) {
|
|
476
|
+
this.matchedCommandName = matchedCommandName;
|
|
477
|
+
}
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
480
|
+
unsetMatchedCommand() {
|
|
481
|
+
this.matchedCommand = undefined;
|
|
482
|
+
this.matchedCommandName = undefined;
|
|
483
|
+
}
|
|
484
|
+
parse(argv = processArgs, {
|
|
485
|
+
run = true
|
|
486
|
+
} = {}) {
|
|
487
|
+
this.rawArgs = argv;
|
|
488
|
+
if (!this.name) {
|
|
489
|
+
this.name = argv[1] ? getFileName(argv[1]) : "cli";
|
|
490
|
+
}
|
|
491
|
+
let shouldParse = true;
|
|
492
|
+
for (const command of this.commands) {
|
|
493
|
+
const parsed = this.mri(argv.slice(2), command);
|
|
494
|
+
const commandName = parsed.args[0];
|
|
495
|
+
if (command.isMatched(commandName)) {
|
|
496
|
+
shouldParse = false;
|
|
497
|
+
const parsedInfo = __assign(__assign({}, parsed), {
|
|
498
|
+
args: parsed.args.slice(1)
|
|
499
|
+
});
|
|
500
|
+
this.setParsedInfo(parsedInfo, command, commandName);
|
|
501
|
+
this.emit(`command:${commandName}`, command);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
if (shouldParse) {
|
|
505
|
+
for (const command of this.commands) {
|
|
506
|
+
if (command.name === "") {
|
|
507
|
+
shouldParse = false;
|
|
508
|
+
const parsed = this.mri(argv.slice(2), command);
|
|
509
|
+
this.setParsedInfo(parsed, command);
|
|
510
|
+
this.emit(`command:!`, command);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (shouldParse) {
|
|
515
|
+
const parsed = this.mri(argv.slice(2));
|
|
516
|
+
this.setParsedInfo(parsed);
|
|
517
|
+
}
|
|
518
|
+
if (this.options.help && this.showHelpOnExit) {
|
|
519
|
+
this.outputHelp();
|
|
520
|
+
run = false;
|
|
521
|
+
this.unsetMatchedCommand();
|
|
522
|
+
}
|
|
523
|
+
if (this.options.version && this.showVersionOnExit && this.matchedCommandName == null) {
|
|
524
|
+
this.outputVersion();
|
|
525
|
+
run = false;
|
|
526
|
+
this.unsetMatchedCommand();
|
|
527
|
+
}
|
|
528
|
+
const parsedArgv = { args: this.args, options: this.options };
|
|
529
|
+
if (run) {
|
|
530
|
+
this.runMatchedCommand();
|
|
531
|
+
}
|
|
532
|
+
if (!this.matchedCommand && this.args[0]) {
|
|
533
|
+
this.emit("command:*");
|
|
534
|
+
}
|
|
535
|
+
return parsedArgv;
|
|
536
|
+
}
|
|
537
|
+
mri(argv, command) {
|
|
538
|
+
const cliOptions = [
|
|
539
|
+
...this.globalCommand.options,
|
|
540
|
+
...command ? command.options : []
|
|
541
|
+
];
|
|
542
|
+
const mriOptions = getMriOptions(cliOptions);
|
|
543
|
+
let argsAfterDoubleDashes = [];
|
|
544
|
+
const doubleDashesIndex = argv.indexOf("--");
|
|
545
|
+
if (doubleDashesIndex > -1) {
|
|
546
|
+
argsAfterDoubleDashes = argv.slice(doubleDashesIndex + 1);
|
|
547
|
+
argv = argv.slice(0, doubleDashesIndex);
|
|
548
|
+
}
|
|
549
|
+
let parsed = mri2(argv, mriOptions);
|
|
550
|
+
parsed = Object.keys(parsed).reduce((res, name) => {
|
|
551
|
+
return __assign(__assign({}, res), {
|
|
552
|
+
[camelcaseOptionName(name)]: parsed[name]
|
|
553
|
+
});
|
|
554
|
+
}, { _: [] });
|
|
555
|
+
const args = parsed._;
|
|
556
|
+
const options = {
|
|
557
|
+
"--": argsAfterDoubleDashes
|
|
558
|
+
};
|
|
559
|
+
const ignoreDefault = command && command.config.ignoreOptionDefaultValue ? command.config.ignoreOptionDefaultValue : this.globalCommand.config.ignoreOptionDefaultValue;
|
|
560
|
+
let transforms = Object.create(null);
|
|
561
|
+
for (const cliOption of cliOptions) {
|
|
562
|
+
if (!ignoreDefault && cliOption.config.default !== undefined) {
|
|
563
|
+
for (const name of cliOption.names) {
|
|
564
|
+
options[name] = cliOption.config.default;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (Array.isArray(cliOption.config.type)) {
|
|
568
|
+
if (transforms[cliOption.name] === undefined) {
|
|
569
|
+
transforms[cliOption.name] = Object.create(null);
|
|
570
|
+
transforms[cliOption.name]["shouldTransform"] = true;
|
|
571
|
+
transforms[cliOption.name]["transformFunction"] = cliOption.config.type[0];
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
for (const key of Object.keys(parsed)) {
|
|
576
|
+
if (key !== "_") {
|
|
577
|
+
const keys = key.split(".");
|
|
578
|
+
setDotProp(options, keys, parsed[key]);
|
|
579
|
+
setByType(options, transforms);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return {
|
|
583
|
+
args,
|
|
584
|
+
options
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
runMatchedCommand() {
|
|
588
|
+
const { args, options, matchedCommand: command } = this;
|
|
589
|
+
if (!command || !command.commandAction)
|
|
590
|
+
return;
|
|
591
|
+
command.checkUnknownOptions();
|
|
592
|
+
command.checkOptionValue();
|
|
593
|
+
command.checkRequiredArgs();
|
|
594
|
+
const actionArgs = [];
|
|
595
|
+
command.args.forEach((arg, index) => {
|
|
596
|
+
if (arg.variadic) {
|
|
597
|
+
actionArgs.push(args.slice(index));
|
|
598
|
+
} else {
|
|
599
|
+
actionArgs.push(args[index]);
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
actionArgs.push(options);
|
|
603
|
+
return command.commandAction.apply(this, actionArgs);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
// package.json
|
|
607
|
+
var version = "0.1.0";
|
|
608
|
+
|
|
609
|
+
// src/git-hooks.ts
|
|
610
|
+
import fs from "fs";
|
|
611
|
+
import path from "path";
|
|
612
|
+
import process2 from "process";
|
|
613
|
+
|
|
614
|
+
// src/config.ts
|
|
615
|
+
import { resolve } from "path";
|
|
616
|
+
|
|
617
|
+
// node_modules/bunfig/dist/index.js
|
|
618
|
+
var L = Object.create;
|
|
619
|
+
var h = Object.defineProperty;
|
|
620
|
+
var D = Object.getOwnPropertyDescriptor;
|
|
621
|
+
var T = Object.getOwnPropertyNames;
|
|
622
|
+
var _ = Object.getPrototypeOf;
|
|
623
|
+
var E = Object.prototype.hasOwnProperty;
|
|
624
|
+
var R = (s, e) => () => (e || s((e = { exports: {} }).exports, e), e.exports);
|
|
625
|
+
var N = (s, e, r, t) => {
|
|
626
|
+
if (e && typeof e == "object" || typeof e == "function")
|
|
627
|
+
for (let i of T(e))
|
|
628
|
+
!E.call(s, i) && i !== r && h(s, i, { get: () => e[i], enumerable: !(t = D(e, i)) || t.enumerable });
|
|
629
|
+
return s;
|
|
630
|
+
};
|
|
631
|
+
var j = (s, e, r) => (r = s != null ? L(_(s)) : {}, N(e || !s || !s.__esModule ? h(r, "default", { value: s, enumerable: true }) : r, s));
|
|
632
|
+
var k = R((W, w) => {
|
|
633
|
+
function v(s) {
|
|
634
|
+
if (typeof s != "string")
|
|
635
|
+
throw new TypeError("Path must be a string. Received " + JSON.stringify(s));
|
|
636
|
+
}
|
|
637
|
+
function C(s, e) {
|
|
638
|
+
for (var r = "", t = 0, i = -1, a = 0, n, l = 0;l <= s.length; ++l) {
|
|
639
|
+
if (l < s.length)
|
|
640
|
+
n = s.charCodeAt(l);
|
|
641
|
+
else {
|
|
642
|
+
if (n === 47)
|
|
643
|
+
break;
|
|
644
|
+
n = 47;
|
|
645
|
+
}
|
|
646
|
+
if (n === 47) {
|
|
647
|
+
if (!(i === l - 1 || a === 1))
|
|
648
|
+
if (i !== l - 1 && a === 2) {
|
|
649
|
+
if (r.length < 2 || t !== 2 || r.charCodeAt(r.length - 1) !== 46 || r.charCodeAt(r.length - 2) !== 46) {
|
|
650
|
+
if (r.length > 2) {
|
|
651
|
+
var f = r.lastIndexOf("/");
|
|
652
|
+
if (f !== r.length - 1) {
|
|
653
|
+
f === -1 ? (r = "", t = 0) : (r = r.slice(0, f), t = r.length - 1 - r.lastIndexOf("/")), i = l, a = 0;
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
} else if (r.length === 2 || r.length === 1) {
|
|
657
|
+
r = "", t = 0, i = l, a = 0;
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
e && (r.length > 0 ? r += "/.." : r = "..", t = 2);
|
|
662
|
+
} else
|
|
663
|
+
r.length > 0 ? r += "/" + s.slice(i + 1, l) : r = s.slice(i + 1, l), t = l - i - 1;
|
|
664
|
+
i = l, a = 0;
|
|
665
|
+
} else
|
|
666
|
+
n === 46 && a !== -1 ? ++a : a = -1;
|
|
667
|
+
}
|
|
668
|
+
return r;
|
|
669
|
+
}
|
|
670
|
+
function F(s, e) {
|
|
671
|
+
var r = e.dir || e.root, t = e.base || (e.name || "") + (e.ext || "");
|
|
672
|
+
return r ? r === e.root ? r + t : r + s + t : t;
|
|
673
|
+
}
|
|
674
|
+
var m = { resolve: function() {
|
|
675
|
+
for (var e = "", r = false, t, i = arguments.length - 1;i >= -1 && !r; i--) {
|
|
676
|
+
var a;
|
|
677
|
+
i >= 0 ? a = arguments[i] : (t === undefined && (t = process.cwd()), a = t), v(a), a.length !== 0 && (e = a + "/" + e, r = a.charCodeAt(0) === 47);
|
|
678
|
+
}
|
|
679
|
+
return e = C(e, !r), r ? e.length > 0 ? "/" + e : "/" : e.length > 0 ? e : ".";
|
|
680
|
+
}, normalize: function(e) {
|
|
681
|
+
if (v(e), e.length === 0)
|
|
682
|
+
return ".";
|
|
683
|
+
var r = e.charCodeAt(0) === 47, t = e.charCodeAt(e.length - 1) === 47;
|
|
684
|
+
return e = C(e, !r), e.length === 0 && !r && (e = "."), e.length > 0 && t && (e += "/"), r ? "/" + e : e;
|
|
685
|
+
}, isAbsolute: function(e) {
|
|
686
|
+
return v(e), e.length > 0 && e.charCodeAt(0) === 47;
|
|
687
|
+
}, join: function() {
|
|
688
|
+
if (arguments.length === 0)
|
|
689
|
+
return ".";
|
|
690
|
+
for (var e, r = 0;r < arguments.length; ++r) {
|
|
691
|
+
var t = arguments[r];
|
|
692
|
+
v(t), t.length > 0 && (e === undefined ? e = t : e += "/" + t);
|
|
693
|
+
}
|
|
694
|
+
return e === undefined ? "." : m.normalize(e);
|
|
695
|
+
}, relative: function(e, r) {
|
|
696
|
+
if (v(e), v(r), e === r || (e = m.resolve(e), r = m.resolve(r), e === r))
|
|
697
|
+
return "";
|
|
698
|
+
for (var t = 1;t < e.length && e.charCodeAt(t) === 47; ++t)
|
|
699
|
+
;
|
|
700
|
+
for (var i = e.length, a = i - t, n = 1;n < r.length && r.charCodeAt(n) === 47; ++n)
|
|
701
|
+
;
|
|
702
|
+
for (var l = r.length, f = l - n, c = a < f ? a : f, d = -1, o = 0;o <= c; ++o) {
|
|
703
|
+
if (o === c) {
|
|
704
|
+
if (f > c) {
|
|
705
|
+
if (r.charCodeAt(n + o) === 47)
|
|
706
|
+
return r.slice(n + o + 1);
|
|
707
|
+
if (o === 0)
|
|
708
|
+
return r.slice(n + o);
|
|
709
|
+
} else
|
|
710
|
+
a > c && (e.charCodeAt(t + o) === 47 ? d = o : o === 0 && (d = 0));
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
var A = e.charCodeAt(t + o), z = r.charCodeAt(n + o);
|
|
714
|
+
if (A !== z)
|
|
715
|
+
break;
|
|
716
|
+
A === 47 && (d = o);
|
|
717
|
+
}
|
|
718
|
+
var b = "";
|
|
719
|
+
for (o = t + d + 1;o <= i; ++o)
|
|
720
|
+
(o === i || e.charCodeAt(o) === 47) && (b.length === 0 ? b += ".." : b += "/..");
|
|
721
|
+
return b.length > 0 ? b + r.slice(n + d) : (n += d, r.charCodeAt(n) === 47 && ++n, r.slice(n));
|
|
722
|
+
}, _makeLong: function(e) {
|
|
723
|
+
return e;
|
|
724
|
+
}, dirname: function(e) {
|
|
725
|
+
if (v(e), e.length === 0)
|
|
726
|
+
return ".";
|
|
727
|
+
for (var r = e.charCodeAt(0), t = r === 47, i = -1, a = true, n = e.length - 1;n >= 1; --n)
|
|
728
|
+
if (r = e.charCodeAt(n), r === 47) {
|
|
729
|
+
if (!a) {
|
|
730
|
+
i = n;
|
|
731
|
+
break;
|
|
732
|
+
}
|
|
733
|
+
} else
|
|
734
|
+
a = false;
|
|
735
|
+
return i === -1 ? t ? "/" : "." : t && i === 1 ? "//" : e.slice(0, i);
|
|
736
|
+
}, basename: function(e, r) {
|
|
737
|
+
if (r !== undefined && typeof r != "string")
|
|
738
|
+
throw new TypeError('"ext" argument must be a string');
|
|
739
|
+
v(e);
|
|
740
|
+
var t = 0, i = -1, a = true, n;
|
|
741
|
+
if (r !== undefined && r.length > 0 && r.length <= e.length) {
|
|
742
|
+
if (r.length === e.length && r === e)
|
|
743
|
+
return "";
|
|
744
|
+
var l = r.length - 1, f = -1;
|
|
745
|
+
for (n = e.length - 1;n >= 0; --n) {
|
|
746
|
+
var c = e.charCodeAt(n);
|
|
747
|
+
if (c === 47) {
|
|
748
|
+
if (!a) {
|
|
749
|
+
t = n + 1;
|
|
750
|
+
break;
|
|
751
|
+
}
|
|
752
|
+
} else
|
|
753
|
+
f === -1 && (a = false, f = n + 1), l >= 0 && (c === r.charCodeAt(l) ? --l === -1 && (i = n) : (l = -1, i = f));
|
|
754
|
+
}
|
|
755
|
+
return t === i ? i = f : i === -1 && (i = e.length), e.slice(t, i);
|
|
756
|
+
} else {
|
|
757
|
+
for (n = e.length - 1;n >= 0; --n)
|
|
758
|
+
if (e.charCodeAt(n) === 47) {
|
|
759
|
+
if (!a) {
|
|
760
|
+
t = n + 1;
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
763
|
+
} else
|
|
764
|
+
i === -1 && (a = false, i = n + 1);
|
|
765
|
+
return i === -1 ? "" : e.slice(t, i);
|
|
766
|
+
}
|
|
767
|
+
}, extname: function(e) {
|
|
768
|
+
v(e);
|
|
769
|
+
for (var r = -1, t = 0, i = -1, a = true, n = 0, l = e.length - 1;l >= 0; --l) {
|
|
770
|
+
var f = e.charCodeAt(l);
|
|
771
|
+
if (f === 47) {
|
|
772
|
+
if (!a) {
|
|
773
|
+
t = l + 1;
|
|
774
|
+
break;
|
|
775
|
+
}
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
i === -1 && (a = false, i = l + 1), f === 46 ? r === -1 ? r = l : n !== 1 && (n = 1) : r !== -1 && (n = -1);
|
|
779
|
+
}
|
|
780
|
+
return r === -1 || i === -1 || n === 0 || n === 1 && r === i - 1 && r === t + 1 ? "" : e.slice(r, i);
|
|
781
|
+
}, format: function(e) {
|
|
782
|
+
if (e === null || typeof e != "object")
|
|
783
|
+
throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof e);
|
|
784
|
+
return F("/", e);
|
|
785
|
+
}, parse: function(e) {
|
|
786
|
+
v(e);
|
|
787
|
+
var r = { root: "", dir: "", base: "", ext: "", name: "" };
|
|
788
|
+
if (e.length === 0)
|
|
789
|
+
return r;
|
|
790
|
+
var t = e.charCodeAt(0), i = t === 47, a;
|
|
791
|
+
i ? (r.root = "/", a = 1) : a = 0;
|
|
792
|
+
for (var n = -1, l = 0, f = -1, c = true, d = e.length - 1, o = 0;d >= a; --d) {
|
|
793
|
+
if (t = e.charCodeAt(d), t === 47) {
|
|
794
|
+
if (!c) {
|
|
795
|
+
l = d + 1;
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
f === -1 && (c = false, f = d + 1), t === 46 ? n === -1 ? n = d : o !== 1 && (o = 1) : n !== -1 && (o = -1);
|
|
801
|
+
}
|
|
802
|
+
return n === -1 || f === -1 || o === 0 || o === 1 && n === f - 1 && n === l + 1 ? f !== -1 && (l === 0 && i ? r.base = r.name = e.slice(1, f) : r.base = r.name = e.slice(l, f)) : (l === 0 && i ? (r.name = e.slice(1, n), r.base = e.slice(1, f)) : (r.name = e.slice(l, n), r.base = e.slice(l, f)), r.ext = e.slice(n, f)), l > 0 ? r.dir = e.slice(0, l - 1) : i && (r.dir = "/"), r;
|
|
803
|
+
}, sep: "/", delimiter: ":", win32: null, posix: null };
|
|
804
|
+
m.posix = m;
|
|
805
|
+
w.exports = m;
|
|
806
|
+
});
|
|
807
|
+
var x = j(k());
|
|
808
|
+
var u = x;
|
|
809
|
+
var J = x;
|
|
810
|
+
var P = function(s) {
|
|
811
|
+
return s;
|
|
812
|
+
};
|
|
813
|
+
var S = function() {
|
|
814
|
+
throw new Error("Not implemented");
|
|
815
|
+
};
|
|
816
|
+
u.parse ??= S;
|
|
817
|
+
J.parse ??= S;
|
|
818
|
+
var g = { resolve: u.resolve.bind(u), normalize: u.normalize.bind(u), isAbsolute: u.isAbsolute.bind(u), join: u.join.bind(u), relative: u.relative.bind(u), toNamespacedPath: P, dirname: u.dirname.bind(u), basename: u.basename.bind(u), extname: u.extname.bind(u), format: u.format.bind(u), parse: u.parse.bind(u), sep: "/", delimiter: ":", win32: undefined, posix: undefined, _makeLong: P };
|
|
819
|
+
var y = { sep: "\\", delimiter: ";", win32: undefined, ...g, posix: g };
|
|
820
|
+
g.win32 = y.win32 = y;
|
|
821
|
+
g.posix = g;
|
|
822
|
+
var { resolve: B, normalize: G, isAbsolute: H, join: K, relative: Q, toNamespacedPath: U, dirname: V, basename: X, extname: Y, format: Z, parse: $, sep: I, delimiter: O } = g;
|
|
823
|
+
function deepMerge(target, source) {
|
|
824
|
+
if (Array.isArray(source) && !Array.isArray(target)) {
|
|
825
|
+
return source;
|
|
826
|
+
}
|
|
827
|
+
if (Array.isArray(source) && Array.isArray(target)) {
|
|
828
|
+
return source.map((sourceItem, index) => {
|
|
829
|
+
const targetItem = target[index];
|
|
830
|
+
if (isObject(sourceItem) && isObject(targetItem)) {
|
|
831
|
+
return deepMerge(targetItem, sourceItem);
|
|
832
|
+
}
|
|
833
|
+
return sourceItem;
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
if (!isObject(source) || !isObject(target)) {
|
|
837
|
+
return source;
|
|
838
|
+
}
|
|
839
|
+
const merged = { ...target };
|
|
840
|
+
for (const key in source) {
|
|
841
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
842
|
+
const sourceValue = source[key];
|
|
843
|
+
const targetValue = merged[key];
|
|
844
|
+
if (sourceValue === null || sourceValue === undefined) {
|
|
845
|
+
merged[key] = sourceValue;
|
|
846
|
+
} else if (isObject(sourceValue) && isObject(targetValue)) {
|
|
847
|
+
merged[key] = deepMerge(targetValue, sourceValue);
|
|
848
|
+
} else {
|
|
849
|
+
merged[key] = sourceValue;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
return merged;
|
|
854
|
+
}
|
|
855
|
+
function isObject(item) {
|
|
856
|
+
return Boolean(item && typeof item === "object" && !Array.isArray(item));
|
|
857
|
+
}
|
|
858
|
+
async function tryLoadConfig(configPath, defaultConfig) {
|
|
859
|
+
try {
|
|
860
|
+
const importedConfig = await import(configPath);
|
|
861
|
+
const loadedConfig = importedConfig.default || importedConfig;
|
|
862
|
+
return deepMerge(defaultConfig, loadedConfig);
|
|
863
|
+
} catch {
|
|
864
|
+
return null;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
async function loadConfig({
|
|
868
|
+
name,
|
|
869
|
+
cwd,
|
|
870
|
+
defaultConfig,
|
|
871
|
+
endpoint,
|
|
872
|
+
headers = {
|
|
873
|
+
Accept: "application/json",
|
|
874
|
+
"Content-Type": "application/json"
|
|
875
|
+
}
|
|
876
|
+
}) {
|
|
877
|
+
if (typeof window === "undefined") {
|
|
878
|
+
const baseDir = cwd || "../../../";
|
|
879
|
+
const configPaths = [
|
|
880
|
+
`${name}.config`,
|
|
881
|
+
`.${name}.config`,
|
|
882
|
+
name,
|
|
883
|
+
`.${name}`
|
|
884
|
+
];
|
|
885
|
+
for (const configPath of configPaths) {
|
|
886
|
+
const fullPath = B(baseDir, configPath);
|
|
887
|
+
const config = await tryLoadConfig(fullPath, defaultConfig);
|
|
888
|
+
if (config !== null) {
|
|
889
|
+
return config;
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
console.error("Failed to load client config from any expected location");
|
|
893
|
+
return defaultConfig;
|
|
894
|
+
}
|
|
895
|
+
if (!endpoint) {
|
|
896
|
+
console.warn("An API endpoint is required to load the client config.");
|
|
897
|
+
return defaultConfig;
|
|
898
|
+
}
|
|
899
|
+
try {
|
|
900
|
+
const response = await fetch(endpoint, {
|
|
901
|
+
method: "GET",
|
|
902
|
+
headers
|
|
903
|
+
});
|
|
904
|
+
if (!response.ok) {
|
|
905
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
906
|
+
}
|
|
907
|
+
const loadedConfig = await response.json();
|
|
908
|
+
return deepMerge(defaultConfig, loadedConfig);
|
|
909
|
+
} catch (error) {
|
|
910
|
+
console.error("Failed to load client config:", error);
|
|
911
|
+
return defaultConfig;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// src/config.ts
|
|
916
|
+
var __dirname = "/home/runner/work/bun-git-hooks/bun-git-hooks/src";
|
|
917
|
+
var defaultConfig = {
|
|
918
|
+
verbose: true
|
|
919
|
+
};
|
|
920
|
+
var config = await loadConfig({
|
|
921
|
+
name: "git-hooks",
|
|
922
|
+
cwd: resolve(__dirname, ".."),
|
|
923
|
+
defaultConfig
|
|
924
|
+
});
|
|
925
|
+
|
|
926
|
+
// src/git-hooks.ts
|
|
927
|
+
var VALID_GIT_HOOKS = [
|
|
928
|
+
"applypatch-msg",
|
|
929
|
+
"pre-applypatch",
|
|
930
|
+
"post-applypatch",
|
|
931
|
+
"pre-commit",
|
|
932
|
+
"pre-merge-commit",
|
|
933
|
+
"prepare-commit-msg",
|
|
934
|
+
"commit-msg",
|
|
935
|
+
"post-commit",
|
|
936
|
+
"pre-rebase",
|
|
937
|
+
"post-checkout",
|
|
938
|
+
"post-merge",
|
|
939
|
+
"pre-push",
|
|
940
|
+
"pre-receive",
|
|
941
|
+
"update",
|
|
942
|
+
"proc-receive",
|
|
943
|
+
"post-receive",
|
|
944
|
+
"post-update",
|
|
945
|
+
"reference-transaction",
|
|
946
|
+
"push-to-checkout",
|
|
947
|
+
"pre-auto-gc",
|
|
948
|
+
"post-rewrite",
|
|
949
|
+
"sendemail-validate",
|
|
950
|
+
"fsmonitor-watchman",
|
|
951
|
+
"p4-changelist",
|
|
952
|
+
"p4-prepare-changelist",
|
|
953
|
+
"p4-post-changelist",
|
|
954
|
+
"p4-pre-submit",
|
|
955
|
+
"post-index-change"
|
|
956
|
+
];
|
|
957
|
+
var PREPEND_SCRIPT = `#!/bin/sh
|
|
958
|
+
|
|
959
|
+
if [ "$SKIP_BUN_GIT_HOOKS" = "1" ]; then
|
|
960
|
+
echo "[INFO] SKIP_BUN_GIT_HOOKS is set to 1, skipping hook."
|
|
961
|
+
exit 0
|
|
962
|
+
fi
|
|
963
|
+
|
|
964
|
+
if [ -f "$BUN_GIT_HOOKS_RC" ]; then
|
|
965
|
+
. "$BUN_GIT_HOOKS_RC"
|
|
966
|
+
fi
|
|
967
|
+
|
|
968
|
+
`;
|
|
969
|
+
function getGitProjectRoot(directory = process2.cwd()) {
|
|
970
|
+
if (directory.endsWith(".git")) {
|
|
971
|
+
return path.normalize(directory);
|
|
972
|
+
}
|
|
973
|
+
let start = path.normalize(directory);
|
|
974
|
+
if (!start || start === path.sep || start === ".") {
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
const fullPath = path.join(start, ".git");
|
|
978
|
+
if (fs.existsSync(fullPath)) {
|
|
979
|
+
if (!fs.lstatSync(fullPath).isDirectory()) {
|
|
980
|
+
const content = fs.readFileSync(fullPath, { encoding: "utf-8" });
|
|
981
|
+
const match = /^gitdir: (.*)\s*$/.exec(content);
|
|
982
|
+
if (match) {
|
|
983
|
+
const gitDir = match[1];
|
|
984
|
+
let commonDir = path.join(gitDir, "commondir");
|
|
985
|
+
if (fs.existsSync(commonDir)) {
|
|
986
|
+
commonDir = fs.readFileSync(commonDir, "utf8").trim();
|
|
987
|
+
return path.resolve(gitDir, commonDir);
|
|
988
|
+
}
|
|
989
|
+
return path.normalize(gitDir);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
return path.normalize(fullPath);
|
|
993
|
+
}
|
|
994
|
+
const parentDir = path.dirname(start);
|
|
995
|
+
if (parentDir === start) {
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
return getGitProjectRoot(parentDir);
|
|
999
|
+
}
|
|
1000
|
+
function setHooksFromConfig(projectRootPath = process2.cwd(), options = {}) {
|
|
1001
|
+
if (!config) {
|
|
1002
|
+
throw new Error("[ERROR] Config was not found! Please add `.git-hooks.config.{ts,js,mjs,cjs,mts,cts,json}` or `git-hooks.config.{ts,js,mjs,cjs,mts,cts,json}` or the `git-hooks` entry in your package.json.\r\nCheck README for details");
|
|
1003
|
+
}
|
|
1004
|
+
const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : config.preserveUnused ? VALID_GIT_HOOKS : [];
|
|
1005
|
+
for (const hook of VALID_GIT_HOOKS) {
|
|
1006
|
+
if (Object.prototype.hasOwnProperty.call(config, hook)) {
|
|
1007
|
+
if (!config[hook]) {
|
|
1008
|
+
throw new Error(`[ERROR] Command for ${hook} is not set`);
|
|
1009
|
+
}
|
|
1010
|
+
_setHook(hook, config[hook], projectRootPath);
|
|
1011
|
+
} else if (!preserveUnused.includes(hook)) {
|
|
1012
|
+
_removeHook(hook, projectRootPath);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
function _setHook(hook, command, projectRoot = process2.cwd()) {
|
|
1017
|
+
const gitRoot = getGitProjectRoot(projectRoot);
|
|
1018
|
+
if (!gitRoot) {
|
|
1019
|
+
console.info("[INFO] No `.git` root folder found, skipping");
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
const hookCommand = PREPEND_SCRIPT + command;
|
|
1023
|
+
const hookDirectory = `${gitRoot}/hooks/`;
|
|
1024
|
+
const hookPath = path.normalize(hookDirectory + hook);
|
|
1025
|
+
const normalizedHookDirectory = path.normalize(hookDirectory);
|
|
1026
|
+
if (!fs.existsSync(normalizedHookDirectory))
|
|
1027
|
+
fs.mkdirSync(normalizedHookDirectory);
|
|
1028
|
+
fs.writeFileSync(hookPath, hookCommand);
|
|
1029
|
+
fs.chmodSync(hookPath, 493);
|
|
1030
|
+
console.info(`[INFO] Successfully set the ${hook} with command: ${command}`);
|
|
1031
|
+
}
|
|
1032
|
+
function removeHooks(projectRoot = process2.cwd(), verbose = false) {
|
|
1033
|
+
for (const configEntry of VALID_GIT_HOOKS) {
|
|
1034
|
+
_removeHook(configEntry, projectRoot, verbose);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
function _removeHook(hook, projectRoot = process2.cwd(), verbose = false) {
|
|
1038
|
+
const gitRoot = getGitProjectRoot(projectRoot);
|
|
1039
|
+
const hookPath = path.normalize(`${gitRoot}/hooks/${hook}`);
|
|
1040
|
+
if (fs.existsSync(hookPath)) {
|
|
1041
|
+
fs.unlinkSync(hookPath);
|
|
1042
|
+
}
|
|
1043
|
+
if (verbose) {
|
|
1044
|
+
console.info(`[INFO] Successfully removed the ${hook} hook`);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// bin/cli.ts
|
|
1049
|
+
var cli = new CAC("git-hooks");
|
|
1050
|
+
var { SKIP_INSTALL_GIT_HOOKS } = process3.env;
|
|
1051
|
+
if (["1", "true"].includes(SKIP_INSTALL_GIT_HOOKS || "")) {
|
|
1052
|
+
console.log(`[INFO] SKIP_INSTALL_GIT_HOOKS is set to "${SKIP_INSTALL_GIT_HOOKS}", skipping installing hooks.`);
|
|
1053
|
+
process3.exit(0);
|
|
1054
|
+
}
|
|
1055
|
+
cli.command("[configPath]", "Install git hooks, optionally from specified config file").option("--verbose", "Enable verbose logging").example("bun-git-hooks").example("bun-git-hooks ./config.ts").example("bun-git-hooks --verbose").action(async (configPath, options) => {
|
|
1056
|
+
try {
|
|
1057
|
+
if (options?.verbose) {
|
|
1058
|
+
console.log("[DEBUG] Config path:", configPath || "using default");
|
|
1059
|
+
console.log("[DEBUG] Working directory:", process3.cwd());
|
|
1060
|
+
}
|
|
1061
|
+
setHooksFromConfig(process3.cwd());
|
|
1062
|
+
console.log("[INFO] Successfully set all git hooks");
|
|
1063
|
+
} catch (err) {
|
|
1064
|
+
console.error("[ERROR] Was not able to set git hooks. Error:", err);
|
|
1065
|
+
process3.exit(1);
|
|
1066
|
+
}
|
|
1067
|
+
});
|
|
1068
|
+
cli.command("uninstall", "Remove all git hooks").alias("remove").option("--verbose", "Enable verbose logging").example("bun-git-hooks uninstall").example("bunx bun-git-hooks remove").example("bunx git-hooks uninstall --verbose").action(async (options) => {
|
|
1069
|
+
try {
|
|
1070
|
+
if (options?.verbose) {
|
|
1071
|
+
console.log("[DEBUG] Removing hooks from:", process3.cwd());
|
|
1072
|
+
}
|
|
1073
|
+
removeHooks(process3.cwd(), options?.verbose);
|
|
1074
|
+
console.log("[INFO] Successfully removed all git hooks");
|
|
1075
|
+
} catch (err) {
|
|
1076
|
+
console.error("[ERROR] Was not able to remove git hooks. Error:", err);
|
|
1077
|
+
process3.exit(1);
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
cli.version(version);
|
|
1081
|
+
cli.help();
|
|
1082
|
+
cli.parse();
|