@hasna/invoices 0.0.1
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/LICENSE +190 -0
- package/README.md +125 -0
- package/dashboard/dist/assets/index-BhR1BMmj.js +139 -0
- package/dashboard/dist/assets/index-khzrvnCp.css +1 -0
- package/dashboard/dist/index.html +13 -0
- package/dashboard/dist/logo.jpg +0 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +2757 -0
- package/dist/cli/tui.d.ts +2 -0
- package/dist/cli/tui.d.ts.map +1 -0
- package/dist/db/agents.d.ts +19 -0
- package/dist/db/agents.d.ts.map +1 -0
- package/dist/db/core.test.d.ts +2 -0
- package/dist/db/core.test.d.ts.map +1 -0
- package/dist/db/database.d.ts +7 -0
- package/dist/db/database.d.ts.map +1 -0
- package/dist/db/invoices.d.ts +92 -0
- package/dist/db/invoices.d.ts.map +1 -0
- package/dist/db/migrate.d.ts +5 -0
- package/dist/db/migrate.d.ts.map +1 -0
- package/dist/db/schema.d.ts +6 -0
- package/dist/db/schema.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +457 -0
- package/dist/lib/version.d.ts +2 -0
- package/dist/lib/version.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +3 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +4675 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +4691 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +77 -0
|
@@ -0,0 +1,2757 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
function __accessProp(key) {
|
|
9
|
+
return this[key];
|
|
10
|
+
}
|
|
11
|
+
var __toESMCache_node;
|
|
12
|
+
var __toESMCache_esm;
|
|
13
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
14
|
+
var canCache = mod != null && typeof mod === "object";
|
|
15
|
+
if (canCache) {
|
|
16
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
17
|
+
var cached = cache.get(mod);
|
|
18
|
+
if (cached)
|
|
19
|
+
return cached;
|
|
20
|
+
}
|
|
21
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
22
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
if (canCache)
|
|
30
|
+
cache.set(mod, to);
|
|
31
|
+
return to;
|
|
32
|
+
};
|
|
33
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
34
|
+
var __returnValue = (v) => v;
|
|
35
|
+
function __exportSetter(name, newValue) {
|
|
36
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
37
|
+
}
|
|
38
|
+
var __export = (target, all) => {
|
|
39
|
+
for (var name in all)
|
|
40
|
+
__defProp(target, name, {
|
|
41
|
+
get: all[name],
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
set: __exportSetter.bind(all, name)
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
48
|
+
var __require = import.meta.require;
|
|
49
|
+
|
|
50
|
+
// node_modules/commander/lib/error.js
|
|
51
|
+
var require_error = __commonJS((exports) => {
|
|
52
|
+
class CommanderError extends Error {
|
|
53
|
+
constructor(exitCode, code, message) {
|
|
54
|
+
super(message);
|
|
55
|
+
Error.captureStackTrace(this, this.constructor);
|
|
56
|
+
this.name = this.constructor.name;
|
|
57
|
+
this.code = code;
|
|
58
|
+
this.exitCode = exitCode;
|
|
59
|
+
this.nestedError = undefined;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class InvalidArgumentError extends CommanderError {
|
|
64
|
+
constructor(message) {
|
|
65
|
+
super(1, "commander.invalidArgument", message);
|
|
66
|
+
Error.captureStackTrace(this, this.constructor);
|
|
67
|
+
this.name = this.constructor.name;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.CommanderError = CommanderError;
|
|
71
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// node_modules/commander/lib/argument.js
|
|
75
|
+
var require_argument = __commonJS((exports) => {
|
|
76
|
+
var { InvalidArgumentError } = require_error();
|
|
77
|
+
|
|
78
|
+
class Argument {
|
|
79
|
+
constructor(name, description) {
|
|
80
|
+
this.description = description || "";
|
|
81
|
+
this.variadic = false;
|
|
82
|
+
this.parseArg = undefined;
|
|
83
|
+
this.defaultValue = undefined;
|
|
84
|
+
this.defaultValueDescription = undefined;
|
|
85
|
+
this.argChoices = undefined;
|
|
86
|
+
switch (name[0]) {
|
|
87
|
+
case "<":
|
|
88
|
+
this.required = true;
|
|
89
|
+
this._name = name.slice(1, -1);
|
|
90
|
+
break;
|
|
91
|
+
case "[":
|
|
92
|
+
this.required = false;
|
|
93
|
+
this._name = name.slice(1, -1);
|
|
94
|
+
break;
|
|
95
|
+
default:
|
|
96
|
+
this.required = true;
|
|
97
|
+
this._name = name;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
101
|
+
this.variadic = true;
|
|
102
|
+
this._name = this._name.slice(0, -3);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
name() {
|
|
106
|
+
return this._name;
|
|
107
|
+
}
|
|
108
|
+
_concatValue(value, previous) {
|
|
109
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
110
|
+
return [value];
|
|
111
|
+
}
|
|
112
|
+
return previous.concat(value);
|
|
113
|
+
}
|
|
114
|
+
default(value, description) {
|
|
115
|
+
this.defaultValue = value;
|
|
116
|
+
this.defaultValueDescription = description;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
argParser(fn) {
|
|
120
|
+
this.parseArg = fn;
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
choices(values) {
|
|
124
|
+
this.argChoices = values.slice();
|
|
125
|
+
this.parseArg = (arg, previous) => {
|
|
126
|
+
if (!this.argChoices.includes(arg)) {
|
|
127
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
128
|
+
}
|
|
129
|
+
if (this.variadic) {
|
|
130
|
+
return this._concatValue(arg, previous);
|
|
131
|
+
}
|
|
132
|
+
return arg;
|
|
133
|
+
};
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
argRequired() {
|
|
137
|
+
this.required = true;
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
argOptional() {
|
|
141
|
+
this.required = false;
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function humanReadableArgName(arg) {
|
|
146
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
147
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
148
|
+
}
|
|
149
|
+
exports.Argument = Argument;
|
|
150
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// node_modules/commander/lib/help.js
|
|
154
|
+
var require_help = __commonJS((exports) => {
|
|
155
|
+
var { humanReadableArgName } = require_argument();
|
|
156
|
+
|
|
157
|
+
class Help {
|
|
158
|
+
constructor() {
|
|
159
|
+
this.helpWidth = undefined;
|
|
160
|
+
this.minWidthToWrap = 40;
|
|
161
|
+
this.sortSubcommands = false;
|
|
162
|
+
this.sortOptions = false;
|
|
163
|
+
this.showGlobalOptions = false;
|
|
164
|
+
}
|
|
165
|
+
prepareContext(contextOptions) {
|
|
166
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
167
|
+
}
|
|
168
|
+
visibleCommands(cmd) {
|
|
169
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
170
|
+
const helpCommand = cmd._getHelpCommand();
|
|
171
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
172
|
+
visibleCommands.push(helpCommand);
|
|
173
|
+
}
|
|
174
|
+
if (this.sortSubcommands) {
|
|
175
|
+
visibleCommands.sort((a, b) => {
|
|
176
|
+
return a.name().localeCompare(b.name());
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return visibleCommands;
|
|
180
|
+
}
|
|
181
|
+
compareOptions(a, b) {
|
|
182
|
+
const getSortKey = (option) => {
|
|
183
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
184
|
+
};
|
|
185
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
186
|
+
}
|
|
187
|
+
visibleOptions(cmd) {
|
|
188
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
189
|
+
const helpOption = cmd._getHelpOption();
|
|
190
|
+
if (helpOption && !helpOption.hidden) {
|
|
191
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
192
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
193
|
+
if (!removeShort && !removeLong) {
|
|
194
|
+
visibleOptions.push(helpOption);
|
|
195
|
+
} else if (helpOption.long && !removeLong) {
|
|
196
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
197
|
+
} else if (helpOption.short && !removeShort) {
|
|
198
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (this.sortOptions) {
|
|
202
|
+
visibleOptions.sort(this.compareOptions);
|
|
203
|
+
}
|
|
204
|
+
return visibleOptions;
|
|
205
|
+
}
|
|
206
|
+
visibleGlobalOptions(cmd) {
|
|
207
|
+
if (!this.showGlobalOptions)
|
|
208
|
+
return [];
|
|
209
|
+
const globalOptions = [];
|
|
210
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
211
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
212
|
+
globalOptions.push(...visibleOptions);
|
|
213
|
+
}
|
|
214
|
+
if (this.sortOptions) {
|
|
215
|
+
globalOptions.sort(this.compareOptions);
|
|
216
|
+
}
|
|
217
|
+
return globalOptions;
|
|
218
|
+
}
|
|
219
|
+
visibleArguments(cmd) {
|
|
220
|
+
if (cmd._argsDescription) {
|
|
221
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
222
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
226
|
+
return cmd.registeredArguments;
|
|
227
|
+
}
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
subcommandTerm(cmd) {
|
|
231
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
232
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
233
|
+
}
|
|
234
|
+
optionTerm(option) {
|
|
235
|
+
return option.flags;
|
|
236
|
+
}
|
|
237
|
+
argumentTerm(argument) {
|
|
238
|
+
return argument.name();
|
|
239
|
+
}
|
|
240
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
241
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
242
|
+
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
243
|
+
}, 0);
|
|
244
|
+
}
|
|
245
|
+
longestOptionTermLength(cmd, helper) {
|
|
246
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
247
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
248
|
+
}, 0);
|
|
249
|
+
}
|
|
250
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
251
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
252
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
253
|
+
}, 0);
|
|
254
|
+
}
|
|
255
|
+
longestArgumentTermLength(cmd, helper) {
|
|
256
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
257
|
+
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
258
|
+
}, 0);
|
|
259
|
+
}
|
|
260
|
+
commandUsage(cmd) {
|
|
261
|
+
let cmdName = cmd._name;
|
|
262
|
+
if (cmd._aliases[0]) {
|
|
263
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
264
|
+
}
|
|
265
|
+
let ancestorCmdNames = "";
|
|
266
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
267
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
268
|
+
}
|
|
269
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
270
|
+
}
|
|
271
|
+
commandDescription(cmd) {
|
|
272
|
+
return cmd.description();
|
|
273
|
+
}
|
|
274
|
+
subcommandDescription(cmd) {
|
|
275
|
+
return cmd.summary() || cmd.description();
|
|
276
|
+
}
|
|
277
|
+
optionDescription(option) {
|
|
278
|
+
const extraInfo = [];
|
|
279
|
+
if (option.argChoices) {
|
|
280
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
281
|
+
}
|
|
282
|
+
if (option.defaultValue !== undefined) {
|
|
283
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
284
|
+
if (showDefault) {
|
|
285
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
289
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
290
|
+
}
|
|
291
|
+
if (option.envVar !== undefined) {
|
|
292
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
293
|
+
}
|
|
294
|
+
if (extraInfo.length > 0) {
|
|
295
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
296
|
+
}
|
|
297
|
+
return option.description;
|
|
298
|
+
}
|
|
299
|
+
argumentDescription(argument) {
|
|
300
|
+
const extraInfo = [];
|
|
301
|
+
if (argument.argChoices) {
|
|
302
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
303
|
+
}
|
|
304
|
+
if (argument.defaultValue !== undefined) {
|
|
305
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
306
|
+
}
|
|
307
|
+
if (extraInfo.length > 0) {
|
|
308
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
309
|
+
if (argument.description) {
|
|
310
|
+
return `${argument.description} ${extraDescription}`;
|
|
311
|
+
}
|
|
312
|
+
return extraDescription;
|
|
313
|
+
}
|
|
314
|
+
return argument.description;
|
|
315
|
+
}
|
|
316
|
+
formatHelp(cmd, helper) {
|
|
317
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
318
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
319
|
+
function callFormatItem(term, description) {
|
|
320
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
321
|
+
}
|
|
322
|
+
let output = [
|
|
323
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
324
|
+
""
|
|
325
|
+
];
|
|
326
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
327
|
+
if (commandDescription.length > 0) {
|
|
328
|
+
output = output.concat([
|
|
329
|
+
helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
|
|
330
|
+
""
|
|
331
|
+
]);
|
|
332
|
+
}
|
|
333
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
334
|
+
return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
|
|
335
|
+
});
|
|
336
|
+
if (argumentList.length > 0) {
|
|
337
|
+
output = output.concat([
|
|
338
|
+
helper.styleTitle("Arguments:"),
|
|
339
|
+
...argumentList,
|
|
340
|
+
""
|
|
341
|
+
]);
|
|
342
|
+
}
|
|
343
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
344
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
345
|
+
});
|
|
346
|
+
if (optionList.length > 0) {
|
|
347
|
+
output = output.concat([
|
|
348
|
+
helper.styleTitle("Options:"),
|
|
349
|
+
...optionList,
|
|
350
|
+
""
|
|
351
|
+
]);
|
|
352
|
+
}
|
|
353
|
+
if (helper.showGlobalOptions) {
|
|
354
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
355
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
356
|
+
});
|
|
357
|
+
if (globalOptionList.length > 0) {
|
|
358
|
+
output = output.concat([
|
|
359
|
+
helper.styleTitle("Global Options:"),
|
|
360
|
+
...globalOptionList,
|
|
361
|
+
""
|
|
362
|
+
]);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
366
|
+
return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)), helper.styleSubcommandDescription(helper.subcommandDescription(cmd2)));
|
|
367
|
+
});
|
|
368
|
+
if (commandList.length > 0) {
|
|
369
|
+
output = output.concat([
|
|
370
|
+
helper.styleTitle("Commands:"),
|
|
371
|
+
...commandList,
|
|
372
|
+
""
|
|
373
|
+
]);
|
|
374
|
+
}
|
|
375
|
+
return output.join(`
|
|
376
|
+
`);
|
|
377
|
+
}
|
|
378
|
+
displayWidth(str) {
|
|
379
|
+
return stripColor(str).length;
|
|
380
|
+
}
|
|
381
|
+
styleTitle(str) {
|
|
382
|
+
return str;
|
|
383
|
+
}
|
|
384
|
+
styleUsage(str) {
|
|
385
|
+
return str.split(" ").map((word) => {
|
|
386
|
+
if (word === "[options]")
|
|
387
|
+
return this.styleOptionText(word);
|
|
388
|
+
if (word === "[command]")
|
|
389
|
+
return this.styleSubcommandText(word);
|
|
390
|
+
if (word[0] === "[" || word[0] === "<")
|
|
391
|
+
return this.styleArgumentText(word);
|
|
392
|
+
return this.styleCommandText(word);
|
|
393
|
+
}).join(" ");
|
|
394
|
+
}
|
|
395
|
+
styleCommandDescription(str) {
|
|
396
|
+
return this.styleDescriptionText(str);
|
|
397
|
+
}
|
|
398
|
+
styleOptionDescription(str) {
|
|
399
|
+
return this.styleDescriptionText(str);
|
|
400
|
+
}
|
|
401
|
+
styleSubcommandDescription(str) {
|
|
402
|
+
return this.styleDescriptionText(str);
|
|
403
|
+
}
|
|
404
|
+
styleArgumentDescription(str) {
|
|
405
|
+
return this.styleDescriptionText(str);
|
|
406
|
+
}
|
|
407
|
+
styleDescriptionText(str) {
|
|
408
|
+
return str;
|
|
409
|
+
}
|
|
410
|
+
styleOptionTerm(str) {
|
|
411
|
+
return this.styleOptionText(str);
|
|
412
|
+
}
|
|
413
|
+
styleSubcommandTerm(str) {
|
|
414
|
+
return str.split(" ").map((word) => {
|
|
415
|
+
if (word === "[options]")
|
|
416
|
+
return this.styleOptionText(word);
|
|
417
|
+
if (word[0] === "[" || word[0] === "<")
|
|
418
|
+
return this.styleArgumentText(word);
|
|
419
|
+
return this.styleSubcommandText(word);
|
|
420
|
+
}).join(" ");
|
|
421
|
+
}
|
|
422
|
+
styleArgumentTerm(str) {
|
|
423
|
+
return this.styleArgumentText(str);
|
|
424
|
+
}
|
|
425
|
+
styleOptionText(str) {
|
|
426
|
+
return str;
|
|
427
|
+
}
|
|
428
|
+
styleArgumentText(str) {
|
|
429
|
+
return str;
|
|
430
|
+
}
|
|
431
|
+
styleSubcommandText(str) {
|
|
432
|
+
return str;
|
|
433
|
+
}
|
|
434
|
+
styleCommandText(str) {
|
|
435
|
+
return str;
|
|
436
|
+
}
|
|
437
|
+
padWidth(cmd, helper) {
|
|
438
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
439
|
+
}
|
|
440
|
+
preformatted(str) {
|
|
441
|
+
return /\n[^\S\r\n]/.test(str);
|
|
442
|
+
}
|
|
443
|
+
formatItem(term, termWidth, description, helper) {
|
|
444
|
+
const itemIndent = 2;
|
|
445
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
446
|
+
if (!description)
|
|
447
|
+
return itemIndentStr + term;
|
|
448
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
449
|
+
const spacerWidth = 2;
|
|
450
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
451
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
452
|
+
let formattedDescription;
|
|
453
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
454
|
+
formattedDescription = description;
|
|
455
|
+
} else {
|
|
456
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
457
|
+
formattedDescription = wrappedDescription.replace(/\n/g, `
|
|
458
|
+
` + " ".repeat(termWidth + spacerWidth));
|
|
459
|
+
}
|
|
460
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
461
|
+
${itemIndentStr}`);
|
|
462
|
+
}
|
|
463
|
+
boxWrap(str, width) {
|
|
464
|
+
if (width < this.minWidthToWrap)
|
|
465
|
+
return str;
|
|
466
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
467
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
468
|
+
const wrappedLines = [];
|
|
469
|
+
rawLines.forEach((line) => {
|
|
470
|
+
const chunks = line.match(chunkPattern);
|
|
471
|
+
if (chunks === null) {
|
|
472
|
+
wrappedLines.push("");
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
let sumChunks = [chunks.shift()];
|
|
476
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
477
|
+
chunks.forEach((chunk) => {
|
|
478
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
479
|
+
if (sumWidth + visibleWidth <= width) {
|
|
480
|
+
sumChunks.push(chunk);
|
|
481
|
+
sumWidth += visibleWidth;
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
wrappedLines.push(sumChunks.join(""));
|
|
485
|
+
const nextChunk = chunk.trimStart();
|
|
486
|
+
sumChunks = [nextChunk];
|
|
487
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
488
|
+
});
|
|
489
|
+
wrappedLines.push(sumChunks.join(""));
|
|
490
|
+
});
|
|
491
|
+
return wrappedLines.join(`
|
|
492
|
+
`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function stripColor(str) {
|
|
496
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
497
|
+
return str.replace(sgrPattern, "");
|
|
498
|
+
}
|
|
499
|
+
exports.Help = Help;
|
|
500
|
+
exports.stripColor = stripColor;
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
// node_modules/commander/lib/option.js
|
|
504
|
+
var require_option = __commonJS((exports) => {
|
|
505
|
+
var { InvalidArgumentError } = require_error();
|
|
506
|
+
|
|
507
|
+
class Option {
|
|
508
|
+
constructor(flags, description) {
|
|
509
|
+
this.flags = flags;
|
|
510
|
+
this.description = description || "";
|
|
511
|
+
this.required = flags.includes("<");
|
|
512
|
+
this.optional = flags.includes("[");
|
|
513
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
514
|
+
this.mandatory = false;
|
|
515
|
+
const optionFlags = splitOptionFlags(flags);
|
|
516
|
+
this.short = optionFlags.shortFlag;
|
|
517
|
+
this.long = optionFlags.longFlag;
|
|
518
|
+
this.negate = false;
|
|
519
|
+
if (this.long) {
|
|
520
|
+
this.negate = this.long.startsWith("--no-");
|
|
521
|
+
}
|
|
522
|
+
this.defaultValue = undefined;
|
|
523
|
+
this.defaultValueDescription = undefined;
|
|
524
|
+
this.presetArg = undefined;
|
|
525
|
+
this.envVar = undefined;
|
|
526
|
+
this.parseArg = undefined;
|
|
527
|
+
this.hidden = false;
|
|
528
|
+
this.argChoices = undefined;
|
|
529
|
+
this.conflictsWith = [];
|
|
530
|
+
this.implied = undefined;
|
|
531
|
+
}
|
|
532
|
+
default(value, description) {
|
|
533
|
+
this.defaultValue = value;
|
|
534
|
+
this.defaultValueDescription = description;
|
|
535
|
+
return this;
|
|
536
|
+
}
|
|
537
|
+
preset(arg) {
|
|
538
|
+
this.presetArg = arg;
|
|
539
|
+
return this;
|
|
540
|
+
}
|
|
541
|
+
conflicts(names) {
|
|
542
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
543
|
+
return this;
|
|
544
|
+
}
|
|
545
|
+
implies(impliedOptionValues) {
|
|
546
|
+
let newImplied = impliedOptionValues;
|
|
547
|
+
if (typeof impliedOptionValues === "string") {
|
|
548
|
+
newImplied = { [impliedOptionValues]: true };
|
|
549
|
+
}
|
|
550
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
551
|
+
return this;
|
|
552
|
+
}
|
|
553
|
+
env(name) {
|
|
554
|
+
this.envVar = name;
|
|
555
|
+
return this;
|
|
556
|
+
}
|
|
557
|
+
argParser(fn) {
|
|
558
|
+
this.parseArg = fn;
|
|
559
|
+
return this;
|
|
560
|
+
}
|
|
561
|
+
makeOptionMandatory(mandatory = true) {
|
|
562
|
+
this.mandatory = !!mandatory;
|
|
563
|
+
return this;
|
|
564
|
+
}
|
|
565
|
+
hideHelp(hide = true) {
|
|
566
|
+
this.hidden = !!hide;
|
|
567
|
+
return this;
|
|
568
|
+
}
|
|
569
|
+
_concatValue(value, previous) {
|
|
570
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
571
|
+
return [value];
|
|
572
|
+
}
|
|
573
|
+
return previous.concat(value);
|
|
574
|
+
}
|
|
575
|
+
choices(values) {
|
|
576
|
+
this.argChoices = values.slice();
|
|
577
|
+
this.parseArg = (arg, previous) => {
|
|
578
|
+
if (!this.argChoices.includes(arg)) {
|
|
579
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
580
|
+
}
|
|
581
|
+
if (this.variadic) {
|
|
582
|
+
return this._concatValue(arg, previous);
|
|
583
|
+
}
|
|
584
|
+
return arg;
|
|
585
|
+
};
|
|
586
|
+
return this;
|
|
587
|
+
}
|
|
588
|
+
name() {
|
|
589
|
+
if (this.long) {
|
|
590
|
+
return this.long.replace(/^--/, "");
|
|
591
|
+
}
|
|
592
|
+
return this.short.replace(/^-/, "");
|
|
593
|
+
}
|
|
594
|
+
attributeName() {
|
|
595
|
+
if (this.negate) {
|
|
596
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
597
|
+
}
|
|
598
|
+
return camelcase(this.name());
|
|
599
|
+
}
|
|
600
|
+
is(arg) {
|
|
601
|
+
return this.short === arg || this.long === arg;
|
|
602
|
+
}
|
|
603
|
+
isBoolean() {
|
|
604
|
+
return !this.required && !this.optional && !this.negate;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
class DualOptions {
|
|
609
|
+
constructor(options) {
|
|
610
|
+
this.positiveOptions = new Map;
|
|
611
|
+
this.negativeOptions = new Map;
|
|
612
|
+
this.dualOptions = new Set;
|
|
613
|
+
options.forEach((option) => {
|
|
614
|
+
if (option.negate) {
|
|
615
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
616
|
+
} else {
|
|
617
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
this.negativeOptions.forEach((value, key) => {
|
|
621
|
+
if (this.positiveOptions.has(key)) {
|
|
622
|
+
this.dualOptions.add(key);
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
valueFromOption(value, option) {
|
|
627
|
+
const optionKey = option.attributeName();
|
|
628
|
+
if (!this.dualOptions.has(optionKey))
|
|
629
|
+
return true;
|
|
630
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
631
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
632
|
+
return option.negate === (negativeValue === value);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function camelcase(str) {
|
|
636
|
+
return str.split("-").reduce((str2, word) => {
|
|
637
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
function splitOptionFlags(flags) {
|
|
641
|
+
let shortFlag;
|
|
642
|
+
let longFlag;
|
|
643
|
+
const shortFlagExp = /^-[^-]$/;
|
|
644
|
+
const longFlagExp = /^--[^-]/;
|
|
645
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
646
|
+
if (shortFlagExp.test(flagParts[0]))
|
|
647
|
+
shortFlag = flagParts.shift();
|
|
648
|
+
if (longFlagExp.test(flagParts[0]))
|
|
649
|
+
longFlag = flagParts.shift();
|
|
650
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
651
|
+
shortFlag = flagParts.shift();
|
|
652
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
653
|
+
shortFlag = longFlag;
|
|
654
|
+
longFlag = flagParts.shift();
|
|
655
|
+
}
|
|
656
|
+
if (flagParts[0].startsWith("-")) {
|
|
657
|
+
const unsupportedFlag = flagParts[0];
|
|
658
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
659
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
660
|
+
throw new Error(`${baseError}
|
|
661
|
+
- a short flag is a single dash and a single character
|
|
662
|
+
- either use a single dash and a single character (for a short flag)
|
|
663
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
664
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
665
|
+
throw new Error(`${baseError}
|
|
666
|
+
- too many short flags`);
|
|
667
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
668
|
+
throw new Error(`${baseError}
|
|
669
|
+
- too many long flags`);
|
|
670
|
+
throw new Error(`${baseError}
|
|
671
|
+
- unrecognised flag format`);
|
|
672
|
+
}
|
|
673
|
+
if (shortFlag === undefined && longFlag === undefined)
|
|
674
|
+
throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
675
|
+
return { shortFlag, longFlag };
|
|
676
|
+
}
|
|
677
|
+
exports.Option = Option;
|
|
678
|
+
exports.DualOptions = DualOptions;
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
682
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
683
|
+
var maxDistance = 3;
|
|
684
|
+
function editDistance(a, b) {
|
|
685
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
686
|
+
return Math.max(a.length, b.length);
|
|
687
|
+
const d = [];
|
|
688
|
+
for (let i = 0;i <= a.length; i++) {
|
|
689
|
+
d[i] = [i];
|
|
690
|
+
}
|
|
691
|
+
for (let j = 0;j <= b.length; j++) {
|
|
692
|
+
d[0][j] = j;
|
|
693
|
+
}
|
|
694
|
+
for (let j = 1;j <= b.length; j++) {
|
|
695
|
+
for (let i = 1;i <= a.length; i++) {
|
|
696
|
+
let cost = 1;
|
|
697
|
+
if (a[i - 1] === b[j - 1]) {
|
|
698
|
+
cost = 0;
|
|
699
|
+
} else {
|
|
700
|
+
cost = 1;
|
|
701
|
+
}
|
|
702
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
703
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
704
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return d[a.length][b.length];
|
|
709
|
+
}
|
|
710
|
+
function suggestSimilar(word, candidates) {
|
|
711
|
+
if (!candidates || candidates.length === 0)
|
|
712
|
+
return "";
|
|
713
|
+
candidates = Array.from(new Set(candidates));
|
|
714
|
+
const searchingOptions = word.startsWith("--");
|
|
715
|
+
if (searchingOptions) {
|
|
716
|
+
word = word.slice(2);
|
|
717
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
718
|
+
}
|
|
719
|
+
let similar = [];
|
|
720
|
+
let bestDistance = maxDistance;
|
|
721
|
+
const minSimilarity = 0.4;
|
|
722
|
+
candidates.forEach((candidate) => {
|
|
723
|
+
if (candidate.length <= 1)
|
|
724
|
+
return;
|
|
725
|
+
const distance = editDistance(word, candidate);
|
|
726
|
+
const length = Math.max(word.length, candidate.length);
|
|
727
|
+
const similarity = (length - distance) / length;
|
|
728
|
+
if (similarity > minSimilarity) {
|
|
729
|
+
if (distance < bestDistance) {
|
|
730
|
+
bestDistance = distance;
|
|
731
|
+
similar = [candidate];
|
|
732
|
+
} else if (distance === bestDistance) {
|
|
733
|
+
similar.push(candidate);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
738
|
+
if (searchingOptions) {
|
|
739
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
740
|
+
}
|
|
741
|
+
if (similar.length > 1) {
|
|
742
|
+
return `
|
|
743
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
744
|
+
}
|
|
745
|
+
if (similar.length === 1) {
|
|
746
|
+
return `
|
|
747
|
+
(Did you mean ${similar[0]}?)`;
|
|
748
|
+
}
|
|
749
|
+
return "";
|
|
750
|
+
}
|
|
751
|
+
exports.suggestSimilar = suggestSimilar;
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
// node_modules/commander/lib/command.js
|
|
755
|
+
var require_command = __commonJS((exports) => {
|
|
756
|
+
var EventEmitter = __require("events").EventEmitter;
|
|
757
|
+
var childProcess = __require("child_process");
|
|
758
|
+
var path = __require("path");
|
|
759
|
+
var fs = __require("fs");
|
|
760
|
+
var process2 = __require("process");
|
|
761
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
762
|
+
var { CommanderError } = require_error();
|
|
763
|
+
var { Help, stripColor } = require_help();
|
|
764
|
+
var { Option, DualOptions } = require_option();
|
|
765
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
766
|
+
|
|
767
|
+
class Command extends EventEmitter {
|
|
768
|
+
constructor(name) {
|
|
769
|
+
super();
|
|
770
|
+
this.commands = [];
|
|
771
|
+
this.options = [];
|
|
772
|
+
this.parent = null;
|
|
773
|
+
this._allowUnknownOption = false;
|
|
774
|
+
this._allowExcessArguments = false;
|
|
775
|
+
this.registeredArguments = [];
|
|
776
|
+
this._args = this.registeredArguments;
|
|
777
|
+
this.args = [];
|
|
778
|
+
this.rawArgs = [];
|
|
779
|
+
this.processedArgs = [];
|
|
780
|
+
this._scriptPath = null;
|
|
781
|
+
this._name = name || "";
|
|
782
|
+
this._optionValues = {};
|
|
783
|
+
this._optionValueSources = {};
|
|
784
|
+
this._storeOptionsAsProperties = false;
|
|
785
|
+
this._actionHandler = null;
|
|
786
|
+
this._executableHandler = false;
|
|
787
|
+
this._executableFile = null;
|
|
788
|
+
this._executableDir = null;
|
|
789
|
+
this._defaultCommandName = null;
|
|
790
|
+
this._exitCallback = null;
|
|
791
|
+
this._aliases = [];
|
|
792
|
+
this._combineFlagAndOptionalValue = true;
|
|
793
|
+
this._description = "";
|
|
794
|
+
this._summary = "";
|
|
795
|
+
this._argsDescription = undefined;
|
|
796
|
+
this._enablePositionalOptions = false;
|
|
797
|
+
this._passThroughOptions = false;
|
|
798
|
+
this._lifeCycleHooks = {};
|
|
799
|
+
this._showHelpAfterError = false;
|
|
800
|
+
this._showSuggestionAfterError = true;
|
|
801
|
+
this._savedState = null;
|
|
802
|
+
this._outputConfiguration = {
|
|
803
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
804
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
805
|
+
outputError: (str, write) => write(str),
|
|
806
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
807
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
808
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
809
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
810
|
+
stripColor: (str) => stripColor(str)
|
|
811
|
+
};
|
|
812
|
+
this._hidden = false;
|
|
813
|
+
this._helpOption = undefined;
|
|
814
|
+
this._addImplicitHelpCommand = undefined;
|
|
815
|
+
this._helpCommand = undefined;
|
|
816
|
+
this._helpConfiguration = {};
|
|
817
|
+
}
|
|
818
|
+
copyInheritedSettings(sourceCommand) {
|
|
819
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
820
|
+
this._helpOption = sourceCommand._helpOption;
|
|
821
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
822
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
823
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
824
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
825
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
826
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
827
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
828
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
829
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
830
|
+
return this;
|
|
831
|
+
}
|
|
832
|
+
_getCommandAndAncestors() {
|
|
833
|
+
const result = [];
|
|
834
|
+
for (let command = this;command; command = command.parent) {
|
|
835
|
+
result.push(command);
|
|
836
|
+
}
|
|
837
|
+
return result;
|
|
838
|
+
}
|
|
839
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
840
|
+
let desc = actionOptsOrExecDesc;
|
|
841
|
+
let opts = execOpts;
|
|
842
|
+
if (typeof desc === "object" && desc !== null) {
|
|
843
|
+
opts = desc;
|
|
844
|
+
desc = null;
|
|
845
|
+
}
|
|
846
|
+
opts = opts || {};
|
|
847
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
848
|
+
const cmd = this.createCommand(name);
|
|
849
|
+
if (desc) {
|
|
850
|
+
cmd.description(desc);
|
|
851
|
+
cmd._executableHandler = true;
|
|
852
|
+
}
|
|
853
|
+
if (opts.isDefault)
|
|
854
|
+
this._defaultCommandName = cmd._name;
|
|
855
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
856
|
+
cmd._executableFile = opts.executableFile || null;
|
|
857
|
+
if (args)
|
|
858
|
+
cmd.arguments(args);
|
|
859
|
+
this._registerCommand(cmd);
|
|
860
|
+
cmd.parent = this;
|
|
861
|
+
cmd.copyInheritedSettings(this);
|
|
862
|
+
if (desc)
|
|
863
|
+
return this;
|
|
864
|
+
return cmd;
|
|
865
|
+
}
|
|
866
|
+
createCommand(name) {
|
|
867
|
+
return new Command(name);
|
|
868
|
+
}
|
|
869
|
+
createHelp() {
|
|
870
|
+
return Object.assign(new Help, this.configureHelp());
|
|
871
|
+
}
|
|
872
|
+
configureHelp(configuration) {
|
|
873
|
+
if (configuration === undefined)
|
|
874
|
+
return this._helpConfiguration;
|
|
875
|
+
this._helpConfiguration = configuration;
|
|
876
|
+
return this;
|
|
877
|
+
}
|
|
878
|
+
configureOutput(configuration) {
|
|
879
|
+
if (configuration === undefined)
|
|
880
|
+
return this._outputConfiguration;
|
|
881
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
882
|
+
return this;
|
|
883
|
+
}
|
|
884
|
+
showHelpAfterError(displayHelp = true) {
|
|
885
|
+
if (typeof displayHelp !== "string")
|
|
886
|
+
displayHelp = !!displayHelp;
|
|
887
|
+
this._showHelpAfterError = displayHelp;
|
|
888
|
+
return this;
|
|
889
|
+
}
|
|
890
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
891
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
892
|
+
return this;
|
|
893
|
+
}
|
|
894
|
+
addCommand(cmd, opts) {
|
|
895
|
+
if (!cmd._name) {
|
|
896
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
897
|
+
- specify the name in Command constructor or using .name()`);
|
|
898
|
+
}
|
|
899
|
+
opts = opts || {};
|
|
900
|
+
if (opts.isDefault)
|
|
901
|
+
this._defaultCommandName = cmd._name;
|
|
902
|
+
if (opts.noHelp || opts.hidden)
|
|
903
|
+
cmd._hidden = true;
|
|
904
|
+
this._registerCommand(cmd);
|
|
905
|
+
cmd.parent = this;
|
|
906
|
+
cmd._checkForBrokenPassThrough();
|
|
907
|
+
return this;
|
|
908
|
+
}
|
|
909
|
+
createArgument(name, description) {
|
|
910
|
+
return new Argument(name, description);
|
|
911
|
+
}
|
|
912
|
+
argument(name, description, fn, defaultValue) {
|
|
913
|
+
const argument = this.createArgument(name, description);
|
|
914
|
+
if (typeof fn === "function") {
|
|
915
|
+
argument.default(defaultValue).argParser(fn);
|
|
916
|
+
} else {
|
|
917
|
+
argument.default(fn);
|
|
918
|
+
}
|
|
919
|
+
this.addArgument(argument);
|
|
920
|
+
return this;
|
|
921
|
+
}
|
|
922
|
+
arguments(names) {
|
|
923
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
924
|
+
this.argument(detail);
|
|
925
|
+
});
|
|
926
|
+
return this;
|
|
927
|
+
}
|
|
928
|
+
addArgument(argument) {
|
|
929
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
930
|
+
if (previousArgument && previousArgument.variadic) {
|
|
931
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
932
|
+
}
|
|
933
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
934
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
935
|
+
}
|
|
936
|
+
this.registeredArguments.push(argument);
|
|
937
|
+
return this;
|
|
938
|
+
}
|
|
939
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
940
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
941
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
942
|
+
return this;
|
|
943
|
+
}
|
|
944
|
+
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
945
|
+
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
946
|
+
const helpDescription = description ?? "display help for command";
|
|
947
|
+
const helpCommand = this.createCommand(helpName);
|
|
948
|
+
helpCommand.helpOption(false);
|
|
949
|
+
if (helpArgs)
|
|
950
|
+
helpCommand.arguments(helpArgs);
|
|
951
|
+
if (helpDescription)
|
|
952
|
+
helpCommand.description(helpDescription);
|
|
953
|
+
this._addImplicitHelpCommand = true;
|
|
954
|
+
this._helpCommand = helpCommand;
|
|
955
|
+
return this;
|
|
956
|
+
}
|
|
957
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
958
|
+
if (typeof helpCommand !== "object") {
|
|
959
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
960
|
+
return this;
|
|
961
|
+
}
|
|
962
|
+
this._addImplicitHelpCommand = true;
|
|
963
|
+
this._helpCommand = helpCommand;
|
|
964
|
+
return this;
|
|
965
|
+
}
|
|
966
|
+
_getHelpCommand() {
|
|
967
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
968
|
+
if (hasImplicitHelpCommand) {
|
|
969
|
+
if (this._helpCommand === undefined) {
|
|
970
|
+
this.helpCommand(undefined, undefined);
|
|
971
|
+
}
|
|
972
|
+
return this._helpCommand;
|
|
973
|
+
}
|
|
974
|
+
return null;
|
|
975
|
+
}
|
|
976
|
+
hook(event, listener) {
|
|
977
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
978
|
+
if (!allowedValues.includes(event)) {
|
|
979
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
980
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
981
|
+
}
|
|
982
|
+
if (this._lifeCycleHooks[event]) {
|
|
983
|
+
this._lifeCycleHooks[event].push(listener);
|
|
984
|
+
} else {
|
|
985
|
+
this._lifeCycleHooks[event] = [listener];
|
|
986
|
+
}
|
|
987
|
+
return this;
|
|
988
|
+
}
|
|
989
|
+
exitOverride(fn) {
|
|
990
|
+
if (fn) {
|
|
991
|
+
this._exitCallback = fn;
|
|
992
|
+
} else {
|
|
993
|
+
this._exitCallback = (err) => {
|
|
994
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
995
|
+
throw err;
|
|
996
|
+
} else {}
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
return this;
|
|
1000
|
+
}
|
|
1001
|
+
_exit(exitCode, code, message) {
|
|
1002
|
+
if (this._exitCallback) {
|
|
1003
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
1004
|
+
}
|
|
1005
|
+
process2.exit(exitCode);
|
|
1006
|
+
}
|
|
1007
|
+
action(fn) {
|
|
1008
|
+
const listener = (args) => {
|
|
1009
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
1010
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
1011
|
+
if (this._storeOptionsAsProperties) {
|
|
1012
|
+
actionArgs[expectedArgsCount] = this;
|
|
1013
|
+
} else {
|
|
1014
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
1015
|
+
}
|
|
1016
|
+
actionArgs.push(this);
|
|
1017
|
+
return fn.apply(this, actionArgs);
|
|
1018
|
+
};
|
|
1019
|
+
this._actionHandler = listener;
|
|
1020
|
+
return this;
|
|
1021
|
+
}
|
|
1022
|
+
createOption(flags, description) {
|
|
1023
|
+
return new Option(flags, description);
|
|
1024
|
+
}
|
|
1025
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
1026
|
+
try {
|
|
1027
|
+
return target.parseArg(value, previous);
|
|
1028
|
+
} catch (err) {
|
|
1029
|
+
if (err.code === "commander.invalidArgument") {
|
|
1030
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1031
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1032
|
+
}
|
|
1033
|
+
throw err;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
_registerOption(option) {
|
|
1037
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
1038
|
+
if (matchingOption) {
|
|
1039
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
1040
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1041
|
+
- already used by option '${matchingOption.flags}'`);
|
|
1042
|
+
}
|
|
1043
|
+
this.options.push(option);
|
|
1044
|
+
}
|
|
1045
|
+
_registerCommand(command) {
|
|
1046
|
+
const knownBy = (cmd) => {
|
|
1047
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
1048
|
+
};
|
|
1049
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
1050
|
+
if (alreadyUsed) {
|
|
1051
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1052
|
+
const newCmd = knownBy(command).join("|");
|
|
1053
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
1054
|
+
}
|
|
1055
|
+
this.commands.push(command);
|
|
1056
|
+
}
|
|
1057
|
+
addOption(option) {
|
|
1058
|
+
this._registerOption(option);
|
|
1059
|
+
const oname = option.name();
|
|
1060
|
+
const name = option.attributeName();
|
|
1061
|
+
if (option.negate) {
|
|
1062
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1063
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1064
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
1065
|
+
}
|
|
1066
|
+
} else if (option.defaultValue !== undefined) {
|
|
1067
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1068
|
+
}
|
|
1069
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1070
|
+
if (val == null && option.presetArg !== undefined) {
|
|
1071
|
+
val = option.presetArg;
|
|
1072
|
+
}
|
|
1073
|
+
const oldValue = this.getOptionValue(name);
|
|
1074
|
+
if (val !== null && option.parseArg) {
|
|
1075
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1076
|
+
} else if (val !== null && option.variadic) {
|
|
1077
|
+
val = option._concatValue(val, oldValue);
|
|
1078
|
+
}
|
|
1079
|
+
if (val == null) {
|
|
1080
|
+
if (option.negate) {
|
|
1081
|
+
val = false;
|
|
1082
|
+
} else if (option.isBoolean() || option.optional) {
|
|
1083
|
+
val = true;
|
|
1084
|
+
} else {
|
|
1085
|
+
val = "";
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1089
|
+
};
|
|
1090
|
+
this.on("option:" + oname, (val) => {
|
|
1091
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1092
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1093
|
+
});
|
|
1094
|
+
if (option.envVar) {
|
|
1095
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1096
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1097
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
return this;
|
|
1101
|
+
}
|
|
1102
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1103
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
1104
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
1105
|
+
}
|
|
1106
|
+
const option = this.createOption(flags, description);
|
|
1107
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
1108
|
+
if (typeof fn === "function") {
|
|
1109
|
+
option.default(defaultValue).argParser(fn);
|
|
1110
|
+
} else if (fn instanceof RegExp) {
|
|
1111
|
+
const regex = fn;
|
|
1112
|
+
fn = (val, def) => {
|
|
1113
|
+
const m = regex.exec(val);
|
|
1114
|
+
return m ? m[0] : def;
|
|
1115
|
+
};
|
|
1116
|
+
option.default(defaultValue).argParser(fn);
|
|
1117
|
+
} else {
|
|
1118
|
+
option.default(fn);
|
|
1119
|
+
}
|
|
1120
|
+
return this.addOption(option);
|
|
1121
|
+
}
|
|
1122
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1123
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1124
|
+
}
|
|
1125
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1126
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1127
|
+
}
|
|
1128
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1129
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1130
|
+
return this;
|
|
1131
|
+
}
|
|
1132
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1133
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1134
|
+
return this;
|
|
1135
|
+
}
|
|
1136
|
+
allowExcessArguments(allowExcess = true) {
|
|
1137
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1138
|
+
return this;
|
|
1139
|
+
}
|
|
1140
|
+
enablePositionalOptions(positional = true) {
|
|
1141
|
+
this._enablePositionalOptions = !!positional;
|
|
1142
|
+
return this;
|
|
1143
|
+
}
|
|
1144
|
+
passThroughOptions(passThrough = true) {
|
|
1145
|
+
this._passThroughOptions = !!passThrough;
|
|
1146
|
+
this._checkForBrokenPassThrough();
|
|
1147
|
+
return this;
|
|
1148
|
+
}
|
|
1149
|
+
_checkForBrokenPassThrough() {
|
|
1150
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1151
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1155
|
+
if (this.options.length) {
|
|
1156
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1157
|
+
}
|
|
1158
|
+
if (Object.keys(this._optionValues).length) {
|
|
1159
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1160
|
+
}
|
|
1161
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1162
|
+
return this;
|
|
1163
|
+
}
|
|
1164
|
+
getOptionValue(key) {
|
|
1165
|
+
if (this._storeOptionsAsProperties) {
|
|
1166
|
+
return this[key];
|
|
1167
|
+
}
|
|
1168
|
+
return this._optionValues[key];
|
|
1169
|
+
}
|
|
1170
|
+
setOptionValue(key, value) {
|
|
1171
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1172
|
+
}
|
|
1173
|
+
setOptionValueWithSource(key, value, source) {
|
|
1174
|
+
if (this._storeOptionsAsProperties) {
|
|
1175
|
+
this[key] = value;
|
|
1176
|
+
} else {
|
|
1177
|
+
this._optionValues[key] = value;
|
|
1178
|
+
}
|
|
1179
|
+
this._optionValueSources[key] = source;
|
|
1180
|
+
return this;
|
|
1181
|
+
}
|
|
1182
|
+
getOptionValueSource(key) {
|
|
1183
|
+
return this._optionValueSources[key];
|
|
1184
|
+
}
|
|
1185
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1186
|
+
let source;
|
|
1187
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1188
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1189
|
+
source = cmd.getOptionValueSource(key);
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
return source;
|
|
1193
|
+
}
|
|
1194
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1195
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1196
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1197
|
+
}
|
|
1198
|
+
parseOptions = parseOptions || {};
|
|
1199
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1200
|
+
if (process2.versions?.electron) {
|
|
1201
|
+
parseOptions.from = "electron";
|
|
1202
|
+
}
|
|
1203
|
+
const execArgv = process2.execArgv ?? [];
|
|
1204
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1205
|
+
parseOptions.from = "eval";
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (argv === undefined) {
|
|
1209
|
+
argv = process2.argv;
|
|
1210
|
+
}
|
|
1211
|
+
this.rawArgs = argv.slice();
|
|
1212
|
+
let userArgs;
|
|
1213
|
+
switch (parseOptions.from) {
|
|
1214
|
+
case undefined:
|
|
1215
|
+
case "node":
|
|
1216
|
+
this._scriptPath = argv[1];
|
|
1217
|
+
userArgs = argv.slice(2);
|
|
1218
|
+
break;
|
|
1219
|
+
case "electron":
|
|
1220
|
+
if (process2.defaultApp) {
|
|
1221
|
+
this._scriptPath = argv[1];
|
|
1222
|
+
userArgs = argv.slice(2);
|
|
1223
|
+
} else {
|
|
1224
|
+
userArgs = argv.slice(1);
|
|
1225
|
+
}
|
|
1226
|
+
break;
|
|
1227
|
+
case "user":
|
|
1228
|
+
userArgs = argv.slice(0);
|
|
1229
|
+
break;
|
|
1230
|
+
case "eval":
|
|
1231
|
+
userArgs = argv.slice(1);
|
|
1232
|
+
break;
|
|
1233
|
+
default:
|
|
1234
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1235
|
+
}
|
|
1236
|
+
if (!this._name && this._scriptPath)
|
|
1237
|
+
this.nameFromFilename(this._scriptPath);
|
|
1238
|
+
this._name = this._name || "program";
|
|
1239
|
+
return userArgs;
|
|
1240
|
+
}
|
|
1241
|
+
parse(argv, parseOptions) {
|
|
1242
|
+
this._prepareForParse();
|
|
1243
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1244
|
+
this._parseCommand([], userArgs);
|
|
1245
|
+
return this;
|
|
1246
|
+
}
|
|
1247
|
+
async parseAsync(argv, parseOptions) {
|
|
1248
|
+
this._prepareForParse();
|
|
1249
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1250
|
+
await this._parseCommand([], userArgs);
|
|
1251
|
+
return this;
|
|
1252
|
+
}
|
|
1253
|
+
_prepareForParse() {
|
|
1254
|
+
if (this._savedState === null) {
|
|
1255
|
+
this.saveStateBeforeParse();
|
|
1256
|
+
} else {
|
|
1257
|
+
this.restoreStateBeforeParse();
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
saveStateBeforeParse() {
|
|
1261
|
+
this._savedState = {
|
|
1262
|
+
_name: this._name,
|
|
1263
|
+
_optionValues: { ...this._optionValues },
|
|
1264
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
1265
|
+
};
|
|
1266
|
+
}
|
|
1267
|
+
restoreStateBeforeParse() {
|
|
1268
|
+
if (this._storeOptionsAsProperties)
|
|
1269
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
1270
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1271
|
+
this._name = this._savedState._name;
|
|
1272
|
+
this._scriptPath = null;
|
|
1273
|
+
this.rawArgs = [];
|
|
1274
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
1275
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
1276
|
+
this.args = [];
|
|
1277
|
+
this.processedArgs = [];
|
|
1278
|
+
}
|
|
1279
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1280
|
+
if (fs.existsSync(executableFile))
|
|
1281
|
+
return;
|
|
1282
|
+
const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
|
|
1283
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1284
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1285
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1286
|
+
- ${executableDirMessage}`;
|
|
1287
|
+
throw new Error(executableMissing);
|
|
1288
|
+
}
|
|
1289
|
+
_executeSubCommand(subcommand, args) {
|
|
1290
|
+
args = args.slice();
|
|
1291
|
+
let launchWithNode = false;
|
|
1292
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1293
|
+
function findFile(baseDir, baseName) {
|
|
1294
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1295
|
+
if (fs.existsSync(localBin))
|
|
1296
|
+
return localBin;
|
|
1297
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1298
|
+
return;
|
|
1299
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1300
|
+
if (foundExt)
|
|
1301
|
+
return `${localBin}${foundExt}`;
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
1304
|
+
this._checkForMissingMandatoryOptions();
|
|
1305
|
+
this._checkForConflictingOptions();
|
|
1306
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1307
|
+
let executableDir = this._executableDir || "";
|
|
1308
|
+
if (this._scriptPath) {
|
|
1309
|
+
let resolvedScriptPath;
|
|
1310
|
+
try {
|
|
1311
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1312
|
+
} catch {
|
|
1313
|
+
resolvedScriptPath = this._scriptPath;
|
|
1314
|
+
}
|
|
1315
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1316
|
+
}
|
|
1317
|
+
if (executableDir) {
|
|
1318
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1319
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1320
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1321
|
+
if (legacyName !== this._name) {
|
|
1322
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
executableFile = localFile || executableFile;
|
|
1326
|
+
}
|
|
1327
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1328
|
+
let proc;
|
|
1329
|
+
if (process2.platform !== "win32") {
|
|
1330
|
+
if (launchWithNode) {
|
|
1331
|
+
args.unshift(executableFile);
|
|
1332
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1333
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1334
|
+
} else {
|
|
1335
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1336
|
+
}
|
|
1337
|
+
} else {
|
|
1338
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1339
|
+
args.unshift(executableFile);
|
|
1340
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1341
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1342
|
+
}
|
|
1343
|
+
if (!proc.killed) {
|
|
1344
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1345
|
+
signals.forEach((signal) => {
|
|
1346
|
+
process2.on(signal, () => {
|
|
1347
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1348
|
+
proc.kill(signal);
|
|
1349
|
+
}
|
|
1350
|
+
});
|
|
1351
|
+
});
|
|
1352
|
+
}
|
|
1353
|
+
const exitCallback = this._exitCallback;
|
|
1354
|
+
proc.on("close", (code) => {
|
|
1355
|
+
code = code ?? 1;
|
|
1356
|
+
if (!exitCallback) {
|
|
1357
|
+
process2.exit(code);
|
|
1358
|
+
} else {
|
|
1359
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1360
|
+
}
|
|
1361
|
+
});
|
|
1362
|
+
proc.on("error", (err) => {
|
|
1363
|
+
if (err.code === "ENOENT") {
|
|
1364
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1365
|
+
} else if (err.code === "EACCES") {
|
|
1366
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1367
|
+
}
|
|
1368
|
+
if (!exitCallback) {
|
|
1369
|
+
process2.exit(1);
|
|
1370
|
+
} else {
|
|
1371
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1372
|
+
wrappedError.nestedError = err;
|
|
1373
|
+
exitCallback(wrappedError);
|
|
1374
|
+
}
|
|
1375
|
+
});
|
|
1376
|
+
this.runningCommand = proc;
|
|
1377
|
+
}
|
|
1378
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1379
|
+
const subCommand = this._findCommand(commandName);
|
|
1380
|
+
if (!subCommand)
|
|
1381
|
+
this.help({ error: true });
|
|
1382
|
+
subCommand._prepareForParse();
|
|
1383
|
+
let promiseChain;
|
|
1384
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1385
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1386
|
+
if (subCommand._executableHandler) {
|
|
1387
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1388
|
+
} else {
|
|
1389
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
return promiseChain;
|
|
1393
|
+
}
|
|
1394
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1395
|
+
if (!subcommandName) {
|
|
1396
|
+
this.help();
|
|
1397
|
+
}
|
|
1398
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1399
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1400
|
+
subCommand.help();
|
|
1401
|
+
}
|
|
1402
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1403
|
+
}
|
|
1404
|
+
_checkNumberOfArguments() {
|
|
1405
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1406
|
+
if (arg.required && this.args[i] == null) {
|
|
1407
|
+
this.missingArgument(arg.name());
|
|
1408
|
+
}
|
|
1409
|
+
});
|
|
1410
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1413
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1414
|
+
this._excessArguments(this.args);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
_processArguments() {
|
|
1418
|
+
const myParseArg = (argument, value, previous) => {
|
|
1419
|
+
let parsedValue = value;
|
|
1420
|
+
if (value !== null && argument.parseArg) {
|
|
1421
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1422
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1423
|
+
}
|
|
1424
|
+
return parsedValue;
|
|
1425
|
+
};
|
|
1426
|
+
this._checkNumberOfArguments();
|
|
1427
|
+
const processedArgs = [];
|
|
1428
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1429
|
+
let value = declaredArg.defaultValue;
|
|
1430
|
+
if (declaredArg.variadic) {
|
|
1431
|
+
if (index < this.args.length) {
|
|
1432
|
+
value = this.args.slice(index);
|
|
1433
|
+
if (declaredArg.parseArg) {
|
|
1434
|
+
value = value.reduce((processed, v) => {
|
|
1435
|
+
return myParseArg(declaredArg, v, processed);
|
|
1436
|
+
}, declaredArg.defaultValue);
|
|
1437
|
+
}
|
|
1438
|
+
} else if (value === undefined) {
|
|
1439
|
+
value = [];
|
|
1440
|
+
}
|
|
1441
|
+
} else if (index < this.args.length) {
|
|
1442
|
+
value = this.args[index];
|
|
1443
|
+
if (declaredArg.parseArg) {
|
|
1444
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
processedArgs[index] = value;
|
|
1448
|
+
});
|
|
1449
|
+
this.processedArgs = processedArgs;
|
|
1450
|
+
}
|
|
1451
|
+
_chainOrCall(promise, fn) {
|
|
1452
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1453
|
+
return promise.then(() => fn());
|
|
1454
|
+
}
|
|
1455
|
+
return fn();
|
|
1456
|
+
}
|
|
1457
|
+
_chainOrCallHooks(promise, event) {
|
|
1458
|
+
let result = promise;
|
|
1459
|
+
const hooks = [];
|
|
1460
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1461
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1462
|
+
hooks.push({ hookedCommand, callback });
|
|
1463
|
+
});
|
|
1464
|
+
});
|
|
1465
|
+
if (event === "postAction") {
|
|
1466
|
+
hooks.reverse();
|
|
1467
|
+
}
|
|
1468
|
+
hooks.forEach((hookDetail) => {
|
|
1469
|
+
result = this._chainOrCall(result, () => {
|
|
1470
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1471
|
+
});
|
|
1472
|
+
});
|
|
1473
|
+
return result;
|
|
1474
|
+
}
|
|
1475
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1476
|
+
let result = promise;
|
|
1477
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1478
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1479
|
+
result = this._chainOrCall(result, () => {
|
|
1480
|
+
return hook(this, subCommand);
|
|
1481
|
+
});
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
return result;
|
|
1485
|
+
}
|
|
1486
|
+
_parseCommand(operands, unknown) {
|
|
1487
|
+
const parsed = this.parseOptions(unknown);
|
|
1488
|
+
this._parseOptionsEnv();
|
|
1489
|
+
this._parseOptionsImplied();
|
|
1490
|
+
operands = operands.concat(parsed.operands);
|
|
1491
|
+
unknown = parsed.unknown;
|
|
1492
|
+
this.args = operands.concat(unknown);
|
|
1493
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1494
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1495
|
+
}
|
|
1496
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1497
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1498
|
+
}
|
|
1499
|
+
if (this._defaultCommandName) {
|
|
1500
|
+
this._outputHelpIfRequested(unknown);
|
|
1501
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1502
|
+
}
|
|
1503
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1504
|
+
this.help({ error: true });
|
|
1505
|
+
}
|
|
1506
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1507
|
+
this._checkForMissingMandatoryOptions();
|
|
1508
|
+
this._checkForConflictingOptions();
|
|
1509
|
+
const checkForUnknownOptions = () => {
|
|
1510
|
+
if (parsed.unknown.length > 0) {
|
|
1511
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1512
|
+
}
|
|
1513
|
+
};
|
|
1514
|
+
const commandEvent = `command:${this.name()}`;
|
|
1515
|
+
if (this._actionHandler) {
|
|
1516
|
+
checkForUnknownOptions();
|
|
1517
|
+
this._processArguments();
|
|
1518
|
+
let promiseChain;
|
|
1519
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1520
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1521
|
+
if (this.parent) {
|
|
1522
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1523
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1524
|
+
});
|
|
1525
|
+
}
|
|
1526
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1527
|
+
return promiseChain;
|
|
1528
|
+
}
|
|
1529
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1530
|
+
checkForUnknownOptions();
|
|
1531
|
+
this._processArguments();
|
|
1532
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1533
|
+
} else if (operands.length) {
|
|
1534
|
+
if (this._findCommand("*")) {
|
|
1535
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1536
|
+
}
|
|
1537
|
+
if (this.listenerCount("command:*")) {
|
|
1538
|
+
this.emit("command:*", operands, unknown);
|
|
1539
|
+
} else if (this.commands.length) {
|
|
1540
|
+
this.unknownCommand();
|
|
1541
|
+
} else {
|
|
1542
|
+
checkForUnknownOptions();
|
|
1543
|
+
this._processArguments();
|
|
1544
|
+
}
|
|
1545
|
+
} else if (this.commands.length) {
|
|
1546
|
+
checkForUnknownOptions();
|
|
1547
|
+
this.help({ error: true });
|
|
1548
|
+
} else {
|
|
1549
|
+
checkForUnknownOptions();
|
|
1550
|
+
this._processArguments();
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
_findCommand(name) {
|
|
1554
|
+
if (!name)
|
|
1555
|
+
return;
|
|
1556
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1557
|
+
}
|
|
1558
|
+
_findOption(arg) {
|
|
1559
|
+
return this.options.find((option) => option.is(arg));
|
|
1560
|
+
}
|
|
1561
|
+
_checkForMissingMandatoryOptions() {
|
|
1562
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1563
|
+
cmd.options.forEach((anOption) => {
|
|
1564
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1565
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1566
|
+
}
|
|
1567
|
+
});
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
_checkForConflictingLocalOptions() {
|
|
1571
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1572
|
+
const optionKey = option.attributeName();
|
|
1573
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1574
|
+
return false;
|
|
1575
|
+
}
|
|
1576
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1577
|
+
});
|
|
1578
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1579
|
+
optionsWithConflicting.forEach((option) => {
|
|
1580
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1581
|
+
if (conflictingAndDefined) {
|
|
1582
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1583
|
+
}
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1586
|
+
_checkForConflictingOptions() {
|
|
1587
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1588
|
+
cmd._checkForConflictingLocalOptions();
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
parseOptions(argv) {
|
|
1592
|
+
const operands = [];
|
|
1593
|
+
const unknown = [];
|
|
1594
|
+
let dest = operands;
|
|
1595
|
+
const args = argv.slice();
|
|
1596
|
+
function maybeOption(arg) {
|
|
1597
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1598
|
+
}
|
|
1599
|
+
let activeVariadicOption = null;
|
|
1600
|
+
while (args.length) {
|
|
1601
|
+
const arg = args.shift();
|
|
1602
|
+
if (arg === "--") {
|
|
1603
|
+
if (dest === unknown)
|
|
1604
|
+
dest.push(arg);
|
|
1605
|
+
dest.push(...args);
|
|
1606
|
+
break;
|
|
1607
|
+
}
|
|
1608
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1609
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1610
|
+
continue;
|
|
1611
|
+
}
|
|
1612
|
+
activeVariadicOption = null;
|
|
1613
|
+
if (maybeOption(arg)) {
|
|
1614
|
+
const option = this._findOption(arg);
|
|
1615
|
+
if (option) {
|
|
1616
|
+
if (option.required) {
|
|
1617
|
+
const value = args.shift();
|
|
1618
|
+
if (value === undefined)
|
|
1619
|
+
this.optionMissingArgument(option);
|
|
1620
|
+
this.emit(`option:${option.name()}`, value);
|
|
1621
|
+
} else if (option.optional) {
|
|
1622
|
+
let value = null;
|
|
1623
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1624
|
+
value = args.shift();
|
|
1625
|
+
}
|
|
1626
|
+
this.emit(`option:${option.name()}`, value);
|
|
1627
|
+
} else {
|
|
1628
|
+
this.emit(`option:${option.name()}`);
|
|
1629
|
+
}
|
|
1630
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1631
|
+
continue;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1635
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1636
|
+
if (option) {
|
|
1637
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1638
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1639
|
+
} else {
|
|
1640
|
+
this.emit(`option:${option.name()}`);
|
|
1641
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1642
|
+
}
|
|
1643
|
+
continue;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1647
|
+
const index = arg.indexOf("=");
|
|
1648
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1649
|
+
if (option && (option.required || option.optional)) {
|
|
1650
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1651
|
+
continue;
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
if (maybeOption(arg)) {
|
|
1655
|
+
dest = unknown;
|
|
1656
|
+
}
|
|
1657
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1658
|
+
if (this._findCommand(arg)) {
|
|
1659
|
+
operands.push(arg);
|
|
1660
|
+
if (args.length > 0)
|
|
1661
|
+
unknown.push(...args);
|
|
1662
|
+
break;
|
|
1663
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1664
|
+
operands.push(arg);
|
|
1665
|
+
if (args.length > 0)
|
|
1666
|
+
operands.push(...args);
|
|
1667
|
+
break;
|
|
1668
|
+
} else if (this._defaultCommandName) {
|
|
1669
|
+
unknown.push(arg);
|
|
1670
|
+
if (args.length > 0)
|
|
1671
|
+
unknown.push(...args);
|
|
1672
|
+
break;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
if (this._passThroughOptions) {
|
|
1676
|
+
dest.push(arg);
|
|
1677
|
+
if (args.length > 0)
|
|
1678
|
+
dest.push(...args);
|
|
1679
|
+
break;
|
|
1680
|
+
}
|
|
1681
|
+
dest.push(arg);
|
|
1682
|
+
}
|
|
1683
|
+
return { operands, unknown };
|
|
1684
|
+
}
|
|
1685
|
+
opts() {
|
|
1686
|
+
if (this._storeOptionsAsProperties) {
|
|
1687
|
+
const result = {};
|
|
1688
|
+
const len = this.options.length;
|
|
1689
|
+
for (let i = 0;i < len; i++) {
|
|
1690
|
+
const key = this.options[i].attributeName();
|
|
1691
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1692
|
+
}
|
|
1693
|
+
return result;
|
|
1694
|
+
}
|
|
1695
|
+
return this._optionValues;
|
|
1696
|
+
}
|
|
1697
|
+
optsWithGlobals() {
|
|
1698
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1699
|
+
}
|
|
1700
|
+
error(message, errorOptions) {
|
|
1701
|
+
this._outputConfiguration.outputError(`${message}
|
|
1702
|
+
`, this._outputConfiguration.writeErr);
|
|
1703
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1704
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1705
|
+
`);
|
|
1706
|
+
} else if (this._showHelpAfterError) {
|
|
1707
|
+
this._outputConfiguration.writeErr(`
|
|
1708
|
+
`);
|
|
1709
|
+
this.outputHelp({ error: true });
|
|
1710
|
+
}
|
|
1711
|
+
const config = errorOptions || {};
|
|
1712
|
+
const exitCode = config.exitCode || 1;
|
|
1713
|
+
const code = config.code || "commander.error";
|
|
1714
|
+
this._exit(exitCode, code, message);
|
|
1715
|
+
}
|
|
1716
|
+
_parseOptionsEnv() {
|
|
1717
|
+
this.options.forEach((option) => {
|
|
1718
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1719
|
+
const optionKey = option.attributeName();
|
|
1720
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1721
|
+
if (option.required || option.optional) {
|
|
1722
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1723
|
+
} else {
|
|
1724
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
});
|
|
1729
|
+
}
|
|
1730
|
+
_parseOptionsImplied() {
|
|
1731
|
+
const dualHelper = new DualOptions(this.options);
|
|
1732
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1733
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1734
|
+
};
|
|
1735
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1736
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1737
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1738
|
+
});
|
|
1739
|
+
});
|
|
1740
|
+
}
|
|
1741
|
+
missingArgument(name) {
|
|
1742
|
+
const message = `error: missing required argument '${name}'`;
|
|
1743
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1744
|
+
}
|
|
1745
|
+
optionMissingArgument(option) {
|
|
1746
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1747
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1748
|
+
}
|
|
1749
|
+
missingMandatoryOptionValue(option) {
|
|
1750
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1751
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1752
|
+
}
|
|
1753
|
+
_conflictingOption(option, conflictingOption) {
|
|
1754
|
+
const findBestOptionFromValue = (option2) => {
|
|
1755
|
+
const optionKey = option2.attributeName();
|
|
1756
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1757
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1758
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1759
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1760
|
+
return negativeOption;
|
|
1761
|
+
}
|
|
1762
|
+
return positiveOption || option2;
|
|
1763
|
+
};
|
|
1764
|
+
const getErrorMessage = (option2) => {
|
|
1765
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1766
|
+
const optionKey = bestOption.attributeName();
|
|
1767
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1768
|
+
if (source === "env") {
|
|
1769
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1770
|
+
}
|
|
1771
|
+
return `option '${bestOption.flags}'`;
|
|
1772
|
+
};
|
|
1773
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1774
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1775
|
+
}
|
|
1776
|
+
unknownOption(flag) {
|
|
1777
|
+
if (this._allowUnknownOption)
|
|
1778
|
+
return;
|
|
1779
|
+
let suggestion = "";
|
|
1780
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1781
|
+
let candidateFlags = [];
|
|
1782
|
+
let command = this;
|
|
1783
|
+
do {
|
|
1784
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1785
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1786
|
+
command = command.parent;
|
|
1787
|
+
} while (command && !command._enablePositionalOptions);
|
|
1788
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1789
|
+
}
|
|
1790
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1791
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1792
|
+
}
|
|
1793
|
+
_excessArguments(receivedArgs) {
|
|
1794
|
+
if (this._allowExcessArguments)
|
|
1795
|
+
return;
|
|
1796
|
+
const expected = this.registeredArguments.length;
|
|
1797
|
+
const s = expected === 1 ? "" : "s";
|
|
1798
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1799
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1800
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1801
|
+
}
|
|
1802
|
+
unknownCommand() {
|
|
1803
|
+
const unknownName = this.args[0];
|
|
1804
|
+
let suggestion = "";
|
|
1805
|
+
if (this._showSuggestionAfterError) {
|
|
1806
|
+
const candidateNames = [];
|
|
1807
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1808
|
+
candidateNames.push(command.name());
|
|
1809
|
+
if (command.alias())
|
|
1810
|
+
candidateNames.push(command.alias());
|
|
1811
|
+
});
|
|
1812
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1813
|
+
}
|
|
1814
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1815
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1816
|
+
}
|
|
1817
|
+
version(str, flags, description) {
|
|
1818
|
+
if (str === undefined)
|
|
1819
|
+
return this._version;
|
|
1820
|
+
this._version = str;
|
|
1821
|
+
flags = flags || "-V, --version";
|
|
1822
|
+
description = description || "output the version number";
|
|
1823
|
+
const versionOption = this.createOption(flags, description);
|
|
1824
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1825
|
+
this._registerOption(versionOption);
|
|
1826
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1827
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1828
|
+
`);
|
|
1829
|
+
this._exit(0, "commander.version", str);
|
|
1830
|
+
});
|
|
1831
|
+
return this;
|
|
1832
|
+
}
|
|
1833
|
+
description(str, argsDescription) {
|
|
1834
|
+
if (str === undefined && argsDescription === undefined)
|
|
1835
|
+
return this._description;
|
|
1836
|
+
this._description = str;
|
|
1837
|
+
if (argsDescription) {
|
|
1838
|
+
this._argsDescription = argsDescription;
|
|
1839
|
+
}
|
|
1840
|
+
return this;
|
|
1841
|
+
}
|
|
1842
|
+
summary(str) {
|
|
1843
|
+
if (str === undefined)
|
|
1844
|
+
return this._summary;
|
|
1845
|
+
this._summary = str;
|
|
1846
|
+
return this;
|
|
1847
|
+
}
|
|
1848
|
+
alias(alias) {
|
|
1849
|
+
if (alias === undefined)
|
|
1850
|
+
return this._aliases[0];
|
|
1851
|
+
let command = this;
|
|
1852
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1853
|
+
command = this.commands[this.commands.length - 1];
|
|
1854
|
+
}
|
|
1855
|
+
if (alias === command._name)
|
|
1856
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1857
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1858
|
+
if (matchingCommand) {
|
|
1859
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1860
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1861
|
+
}
|
|
1862
|
+
command._aliases.push(alias);
|
|
1863
|
+
return this;
|
|
1864
|
+
}
|
|
1865
|
+
aliases(aliases) {
|
|
1866
|
+
if (aliases === undefined)
|
|
1867
|
+
return this._aliases;
|
|
1868
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1869
|
+
return this;
|
|
1870
|
+
}
|
|
1871
|
+
usage(str) {
|
|
1872
|
+
if (str === undefined) {
|
|
1873
|
+
if (this._usage)
|
|
1874
|
+
return this._usage;
|
|
1875
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1876
|
+
return humanReadableArgName(arg);
|
|
1877
|
+
});
|
|
1878
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1879
|
+
}
|
|
1880
|
+
this._usage = str;
|
|
1881
|
+
return this;
|
|
1882
|
+
}
|
|
1883
|
+
name(str) {
|
|
1884
|
+
if (str === undefined)
|
|
1885
|
+
return this._name;
|
|
1886
|
+
this._name = str;
|
|
1887
|
+
return this;
|
|
1888
|
+
}
|
|
1889
|
+
nameFromFilename(filename) {
|
|
1890
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1891
|
+
return this;
|
|
1892
|
+
}
|
|
1893
|
+
executableDir(path2) {
|
|
1894
|
+
if (path2 === undefined)
|
|
1895
|
+
return this._executableDir;
|
|
1896
|
+
this._executableDir = path2;
|
|
1897
|
+
return this;
|
|
1898
|
+
}
|
|
1899
|
+
helpInformation(contextOptions) {
|
|
1900
|
+
const helper = this.createHelp();
|
|
1901
|
+
const context = this._getOutputContext(contextOptions);
|
|
1902
|
+
helper.prepareContext({
|
|
1903
|
+
error: context.error,
|
|
1904
|
+
helpWidth: context.helpWidth,
|
|
1905
|
+
outputHasColors: context.hasColors
|
|
1906
|
+
});
|
|
1907
|
+
const text = helper.formatHelp(this, helper);
|
|
1908
|
+
if (context.hasColors)
|
|
1909
|
+
return text;
|
|
1910
|
+
return this._outputConfiguration.stripColor(text);
|
|
1911
|
+
}
|
|
1912
|
+
_getOutputContext(contextOptions) {
|
|
1913
|
+
contextOptions = contextOptions || {};
|
|
1914
|
+
const error = !!contextOptions.error;
|
|
1915
|
+
let baseWrite;
|
|
1916
|
+
let hasColors;
|
|
1917
|
+
let helpWidth;
|
|
1918
|
+
if (error) {
|
|
1919
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
1920
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1921
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1922
|
+
} else {
|
|
1923
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
1924
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1925
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1926
|
+
}
|
|
1927
|
+
const write = (str) => {
|
|
1928
|
+
if (!hasColors)
|
|
1929
|
+
str = this._outputConfiguration.stripColor(str);
|
|
1930
|
+
return baseWrite(str);
|
|
1931
|
+
};
|
|
1932
|
+
return { error, write, hasColors, helpWidth };
|
|
1933
|
+
}
|
|
1934
|
+
outputHelp(contextOptions) {
|
|
1935
|
+
let deprecatedCallback;
|
|
1936
|
+
if (typeof contextOptions === "function") {
|
|
1937
|
+
deprecatedCallback = contextOptions;
|
|
1938
|
+
contextOptions = undefined;
|
|
1939
|
+
}
|
|
1940
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1941
|
+
const eventContext = {
|
|
1942
|
+
error: outputContext.error,
|
|
1943
|
+
write: outputContext.write,
|
|
1944
|
+
command: this
|
|
1945
|
+
};
|
|
1946
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
|
|
1947
|
+
this.emit("beforeHelp", eventContext);
|
|
1948
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
1949
|
+
if (deprecatedCallback) {
|
|
1950
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1951
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1952
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
outputContext.write(helpInformation);
|
|
1956
|
+
if (this._getHelpOption()?.long) {
|
|
1957
|
+
this.emit(this._getHelpOption().long);
|
|
1958
|
+
}
|
|
1959
|
+
this.emit("afterHelp", eventContext);
|
|
1960
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
|
|
1961
|
+
}
|
|
1962
|
+
helpOption(flags, description) {
|
|
1963
|
+
if (typeof flags === "boolean") {
|
|
1964
|
+
if (flags) {
|
|
1965
|
+
this._helpOption = this._helpOption ?? undefined;
|
|
1966
|
+
} else {
|
|
1967
|
+
this._helpOption = null;
|
|
1968
|
+
}
|
|
1969
|
+
return this;
|
|
1970
|
+
}
|
|
1971
|
+
flags = flags ?? "-h, --help";
|
|
1972
|
+
description = description ?? "display help for command";
|
|
1973
|
+
this._helpOption = this.createOption(flags, description);
|
|
1974
|
+
return this;
|
|
1975
|
+
}
|
|
1976
|
+
_getHelpOption() {
|
|
1977
|
+
if (this._helpOption === undefined) {
|
|
1978
|
+
this.helpOption(undefined, undefined);
|
|
1979
|
+
}
|
|
1980
|
+
return this._helpOption;
|
|
1981
|
+
}
|
|
1982
|
+
addHelpOption(option) {
|
|
1983
|
+
this._helpOption = option;
|
|
1984
|
+
return this;
|
|
1985
|
+
}
|
|
1986
|
+
help(contextOptions) {
|
|
1987
|
+
this.outputHelp(contextOptions);
|
|
1988
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
1989
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1990
|
+
exitCode = 1;
|
|
1991
|
+
}
|
|
1992
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1993
|
+
}
|
|
1994
|
+
addHelpText(position, text) {
|
|
1995
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1996
|
+
if (!allowedValues.includes(position)) {
|
|
1997
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1998
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1999
|
+
}
|
|
2000
|
+
const helpEvent = `${position}Help`;
|
|
2001
|
+
this.on(helpEvent, (context) => {
|
|
2002
|
+
let helpStr;
|
|
2003
|
+
if (typeof text === "function") {
|
|
2004
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
2005
|
+
} else {
|
|
2006
|
+
helpStr = text;
|
|
2007
|
+
}
|
|
2008
|
+
if (helpStr) {
|
|
2009
|
+
context.write(`${helpStr}
|
|
2010
|
+
`);
|
|
2011
|
+
}
|
|
2012
|
+
});
|
|
2013
|
+
return this;
|
|
2014
|
+
}
|
|
2015
|
+
_outputHelpIfRequested(args) {
|
|
2016
|
+
const helpOption = this._getHelpOption();
|
|
2017
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
2018
|
+
if (helpRequested) {
|
|
2019
|
+
this.outputHelp();
|
|
2020
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
function incrementNodeInspectorPort(args) {
|
|
2025
|
+
return args.map((arg) => {
|
|
2026
|
+
if (!arg.startsWith("--inspect")) {
|
|
2027
|
+
return arg;
|
|
2028
|
+
}
|
|
2029
|
+
let debugOption;
|
|
2030
|
+
let debugHost = "127.0.0.1";
|
|
2031
|
+
let debugPort = "9229";
|
|
2032
|
+
let match;
|
|
2033
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2034
|
+
debugOption = match[1];
|
|
2035
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2036
|
+
debugOption = match[1];
|
|
2037
|
+
if (/^\d+$/.test(match[3])) {
|
|
2038
|
+
debugPort = match[3];
|
|
2039
|
+
} else {
|
|
2040
|
+
debugHost = match[3];
|
|
2041
|
+
}
|
|
2042
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2043
|
+
debugOption = match[1];
|
|
2044
|
+
debugHost = match[3];
|
|
2045
|
+
debugPort = match[4];
|
|
2046
|
+
}
|
|
2047
|
+
if (debugOption && debugPort !== "0") {
|
|
2048
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
2049
|
+
}
|
|
2050
|
+
return arg;
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
function useColor() {
|
|
2054
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
2055
|
+
return false;
|
|
2056
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
|
|
2057
|
+
return true;
|
|
2058
|
+
return;
|
|
2059
|
+
}
|
|
2060
|
+
exports.Command = Command;
|
|
2061
|
+
exports.useColor = useColor;
|
|
2062
|
+
});
|
|
2063
|
+
|
|
2064
|
+
// node_modules/commander/index.js
|
|
2065
|
+
var require_commander = __commonJS((exports) => {
|
|
2066
|
+
var { Argument } = require_argument();
|
|
2067
|
+
var { Command } = require_command();
|
|
2068
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
2069
|
+
var { Help } = require_help();
|
|
2070
|
+
var { Option } = require_option();
|
|
2071
|
+
exports.program = new Command;
|
|
2072
|
+
exports.createCommand = (name) => new Command(name);
|
|
2073
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
2074
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
2075
|
+
exports.Command = Command;
|
|
2076
|
+
exports.Option = Option;
|
|
2077
|
+
exports.Argument = Argument;
|
|
2078
|
+
exports.Help = Help;
|
|
2079
|
+
exports.CommanderError = CommanderError;
|
|
2080
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
2081
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2082
|
+
});
|
|
2083
|
+
|
|
2084
|
+
// src/db/invoices.ts
|
|
2085
|
+
import { randomUUID } from "crypto";
|
|
2086
|
+
function toLineTotalCents(line) {
|
|
2087
|
+
const subtotal = Math.round(line.quantity * line.unitPriceCents);
|
|
2088
|
+
const taxRate = line.taxRateBasisPoints ?? 0;
|
|
2089
|
+
const tax = Math.round(subtotal * taxRate / 1e4);
|
|
2090
|
+
return { lineTotalCents: subtotal, lineTaxCents: tax };
|
|
2091
|
+
}
|
|
2092
|
+
function mapPartyRow(row) {
|
|
2093
|
+
return {
|
|
2094
|
+
id: row.id,
|
|
2095
|
+
kind: row.kind,
|
|
2096
|
+
legalName: row.legal_name,
|
|
2097
|
+
email: row.email ?? undefined,
|
|
2098
|
+
taxId: row.tax_id ?? undefined,
|
|
2099
|
+
country: row.country ?? undefined,
|
|
2100
|
+
address: row.address ?? undefined,
|
|
2101
|
+
createdAt: row.created_at,
|
|
2102
|
+
updatedAt: row.updated_at
|
|
2103
|
+
};
|
|
2104
|
+
}
|
|
2105
|
+
function mapInvoiceRow(row) {
|
|
2106
|
+
return {
|
|
2107
|
+
id: row.id,
|
|
2108
|
+
number: row.number,
|
|
2109
|
+
issuerId: row.issuer_id,
|
|
2110
|
+
customerId: row.customer_id,
|
|
2111
|
+
status: row.status,
|
|
2112
|
+
currency: row.currency,
|
|
2113
|
+
notes: row.notes ?? undefined,
|
|
2114
|
+
issuedAt: row.issued_at,
|
|
2115
|
+
dueAt: row.due_at ?? undefined,
|
|
2116
|
+
subtotalCents: row.subtotal_cents,
|
|
2117
|
+
taxCents: row.tax_cents,
|
|
2118
|
+
totalCents: row.total_cents,
|
|
2119
|
+
createdAt: row.created_at,
|
|
2120
|
+
updatedAt: row.updated_at
|
|
2121
|
+
};
|
|
2122
|
+
}
|
|
2123
|
+
function mapInvoiceLineRow(row) {
|
|
2124
|
+
return {
|
|
2125
|
+
id: row.id,
|
|
2126
|
+
invoiceId: row.invoice_id,
|
|
2127
|
+
position: row.position,
|
|
2128
|
+
description: row.description,
|
|
2129
|
+
quantity: row.quantity,
|
|
2130
|
+
unitPriceCents: row.unit_price_cents,
|
|
2131
|
+
taxRateBasisPoints: row.tax_rate_basis_points,
|
|
2132
|
+
lineTotalCents: row.line_total_cents,
|
|
2133
|
+
createdAt: row.created_at
|
|
2134
|
+
};
|
|
2135
|
+
}
|
|
2136
|
+
function createParty(db, input) {
|
|
2137
|
+
const id = input.id ?? randomUUID();
|
|
2138
|
+
db.query(`
|
|
2139
|
+
INSERT INTO parties (id, kind, legal_name, email, tax_id, country, address)
|
|
2140
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
|
|
2141
|
+
`).run(id, input.kind, input.legalName, input.email ?? null, input.taxId ?? null, input.country ?? null, input.address ?? null);
|
|
2142
|
+
return getPartyById(db, id);
|
|
2143
|
+
}
|
|
2144
|
+
function getPartyById(db, id) {
|
|
2145
|
+
const row = db.query("SELECT * FROM parties WHERE id = ?1").get(id);
|
|
2146
|
+
return row ? mapPartyRow(row) : null;
|
|
2147
|
+
}
|
|
2148
|
+
function listParties(db, kind) {
|
|
2149
|
+
if (kind) {
|
|
2150
|
+
return db.query("SELECT * FROM parties WHERE kind = ?1 ORDER BY legal_name ASC").all(kind).map(mapPartyRow);
|
|
2151
|
+
}
|
|
2152
|
+
return db.query("SELECT * FROM parties ORDER BY legal_name ASC").all().map(mapPartyRow);
|
|
2153
|
+
}
|
|
2154
|
+
function createInvoice(db, input) {
|
|
2155
|
+
const invoiceId = input.id ?? randomUUID();
|
|
2156
|
+
const preparedLines = input.lines.map((line, index) => {
|
|
2157
|
+
const { lineTotalCents, lineTaxCents } = toLineTotalCents(line);
|
|
2158
|
+
return {
|
|
2159
|
+
id: line.id ?? randomUUID(),
|
|
2160
|
+
position: index,
|
|
2161
|
+
description: line.description,
|
|
2162
|
+
quantity: line.quantity,
|
|
2163
|
+
unitPriceCents: line.unitPriceCents,
|
|
2164
|
+
taxRateBasisPoints: line.taxRateBasisPoints ?? 0,
|
|
2165
|
+
lineTotalCents,
|
|
2166
|
+
lineTaxCents
|
|
2167
|
+
};
|
|
2168
|
+
});
|
|
2169
|
+
const subtotalCents = preparedLines.reduce((sum, line) => sum + line.lineTotalCents, 0);
|
|
2170
|
+
const taxCents = preparedLines.reduce((sum, line) => sum + line.lineTaxCents, 0);
|
|
2171
|
+
const totalCents = subtotalCents + taxCents;
|
|
2172
|
+
db.transaction(() => {
|
|
2173
|
+
db.query(`
|
|
2174
|
+
INSERT INTO invoices (
|
|
2175
|
+
id, number, issuer_id, customer_id, status, currency, notes, issued_at, due_at, subtotal_cents, tax_cents, total_cents
|
|
2176
|
+
)
|
|
2177
|
+
VALUES (?1, ?2, ?3, ?4, 'draft', ?5, ?6, ?7, ?8, ?9, ?10, ?11)
|
|
2178
|
+
`).run(invoiceId, input.number, input.issuerId, input.customerId, input.currency, input.notes ?? null, input.issuedAt, input.dueAt ?? null, subtotalCents, taxCents, totalCents);
|
|
2179
|
+
const insertLine = db.query(`
|
|
2180
|
+
INSERT INTO invoice_lines (
|
|
2181
|
+
id, invoice_id, position, description, quantity, unit_price_cents, tax_rate_basis_points, line_total_cents
|
|
2182
|
+
)
|
|
2183
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
|
|
2184
|
+
`);
|
|
2185
|
+
for (const line of preparedLines) {
|
|
2186
|
+
insertLine.run(line.id, invoiceId, line.position, line.description, line.quantity, line.unitPriceCents, line.taxRateBasisPoints, line.lineTotalCents);
|
|
2187
|
+
}
|
|
2188
|
+
})();
|
|
2189
|
+
return getInvoiceById(db, invoiceId);
|
|
2190
|
+
}
|
|
2191
|
+
function getInvoiceById(db, id) {
|
|
2192
|
+
const invoiceRow = db.query("SELECT * FROM invoices WHERE id = ?1").get(id);
|
|
2193
|
+
if (!invoiceRow) {
|
|
2194
|
+
return null;
|
|
2195
|
+
}
|
|
2196
|
+
const lines = db.query("SELECT * FROM invoice_lines WHERE invoice_id = ?1 ORDER BY position ASC").all(id).map(mapInvoiceLineRow);
|
|
2197
|
+
return {
|
|
2198
|
+
...mapInvoiceRow(invoiceRow),
|
|
2199
|
+
lines
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
function listInvoices(db, options = {}) {
|
|
2203
|
+
const limit = options.limit ?? 50;
|
|
2204
|
+
const offset = options.offset ?? 0;
|
|
2205
|
+
if (options.status) {
|
|
2206
|
+
return db.query("SELECT * FROM invoices WHERE status = ?1 ORDER BY issued_at DESC, created_at DESC LIMIT ?2 OFFSET ?3").all(options.status, limit, offset).map(mapInvoiceRow);
|
|
2207
|
+
}
|
|
2208
|
+
return db.query("SELECT * FROM invoices ORDER BY issued_at DESC, created_at DESC LIMIT ?1 OFFSET ?2").all(limit, offset).map(mapInvoiceRow);
|
|
2209
|
+
}
|
|
2210
|
+
function updateInvoiceStatus(db, id, status) {
|
|
2211
|
+
db.query("UPDATE invoices SET status = ?2, updated_at = datetime('now') WHERE id = ?1").run(id, status);
|
|
2212
|
+
const row = db.query("SELECT * FROM invoices WHERE id = ?1").get(id);
|
|
2213
|
+
return row ? mapInvoiceRow(row) : null;
|
|
2214
|
+
}
|
|
2215
|
+
function searchInvoices(db, query, options = {}) {
|
|
2216
|
+
const limit = options.limit ?? 20;
|
|
2217
|
+
const offset = options.offset ?? 0;
|
|
2218
|
+
const escapedPhrase = `"${query.replace(/"/g, '""')}"`;
|
|
2219
|
+
if (options.status) {
|
|
2220
|
+
return db.query(`
|
|
2221
|
+
SELECT i.*
|
|
2222
|
+
FROM invoices_fts f
|
|
2223
|
+
JOIN invoices i ON i.id = f.invoice_id
|
|
2224
|
+
WHERE invoices_fts MATCH ?1
|
|
2225
|
+
AND i.status = ?2
|
|
2226
|
+
ORDER BY bm25(invoices_fts)
|
|
2227
|
+
LIMIT ?3
|
|
2228
|
+
OFFSET ?4
|
|
2229
|
+
`).all(escapedPhrase, options.status, limit, offset).map(mapInvoiceRow);
|
|
2230
|
+
}
|
|
2231
|
+
return db.query(`
|
|
2232
|
+
SELECT i.*
|
|
2233
|
+
FROM invoices_fts f
|
|
2234
|
+
JOIN invoices i ON i.id = f.invoice_id
|
|
2235
|
+
WHERE invoices_fts MATCH ?1
|
|
2236
|
+
ORDER BY bm25(invoices_fts)
|
|
2237
|
+
LIMIT ?2
|
|
2238
|
+
OFFSET ?3
|
|
2239
|
+
`).all(escapedPhrase, limit, offset).map(mapInvoiceRow);
|
|
2240
|
+
}
|
|
2241
|
+
var init_invoices = () => {};
|
|
2242
|
+
|
|
2243
|
+
// src/db/database.ts
|
|
2244
|
+
import { mkdirSync } from "fs";
|
|
2245
|
+
import { dirname, join } from "path";
|
|
2246
|
+
import { Database } from "bun:sqlite";
|
|
2247
|
+
function defaultDatabasePath() {
|
|
2248
|
+
const baseDir = join(process.env.HOME ?? ".", ".hasna", "invoices");
|
|
2249
|
+
mkdirSync(baseDir, { recursive: true });
|
|
2250
|
+
return join(baseDir, "invoices.db");
|
|
2251
|
+
}
|
|
2252
|
+
function openInvoiceDatabase(options = {}) {
|
|
2253
|
+
const dbPath = options.dbPath ?? defaultDatabasePath();
|
|
2254
|
+
mkdirSync(dirname(dbPath), { recursive: true });
|
|
2255
|
+
const db = new Database(dbPath, { create: true, strict: true });
|
|
2256
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
2257
|
+
db.exec("PRAGMA foreign_keys = ON;");
|
|
2258
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
2259
|
+
return db;
|
|
2260
|
+
}
|
|
2261
|
+
var init_database = () => {};
|
|
2262
|
+
|
|
2263
|
+
// src/cli/tui.tsx
|
|
2264
|
+
var exports_tui = {};
|
|
2265
|
+
__export(exports_tui, {
|
|
2266
|
+
runInvoicesTui: () => runInvoicesTui
|
|
2267
|
+
});
|
|
2268
|
+
import React from "react";
|
|
2269
|
+
import { Box, render, Text, useApp, useInput } from "ink";
|
|
2270
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
2271
|
+
function App() {
|
|
2272
|
+
const { exit } = useApp();
|
|
2273
|
+
const db = React.useMemo(() => openInvoiceDatabase(), []);
|
|
2274
|
+
React.useEffect(() => {
|
|
2275
|
+
return () => {
|
|
2276
|
+
db.close();
|
|
2277
|
+
};
|
|
2278
|
+
}, [db]);
|
|
2279
|
+
useInput((input) => {
|
|
2280
|
+
if (input.toLowerCase() === "q") {
|
|
2281
|
+
exit();
|
|
2282
|
+
}
|
|
2283
|
+
});
|
|
2284
|
+
const invoices = React.useMemo(() => listInvoices(db, { limit: 20, offset: 0 }), [db]);
|
|
2285
|
+
const issuers = React.useMemo(() => listParties(db, "issuer"), [db]);
|
|
2286
|
+
const customers = React.useMemo(() => listParties(db, "customer"), [db]);
|
|
2287
|
+
return /* @__PURE__ */ jsxDEV(Box, {
|
|
2288
|
+
flexDirection: "column",
|
|
2289
|
+
padding: 1,
|
|
2290
|
+
children: [
|
|
2291
|
+
/* @__PURE__ */ jsxDEV(Text, {
|
|
2292
|
+
bold: true,
|
|
2293
|
+
color: "cyan",
|
|
2294
|
+
children: "invoices"
|
|
2295
|
+
}, undefined, false, undefined, this),
|
|
2296
|
+
/* @__PURE__ */ jsxDEV(Text, {
|
|
2297
|
+
children: [
|
|
2298
|
+
"Invoices: ",
|
|
2299
|
+
invoices.length
|
|
2300
|
+
]
|
|
2301
|
+
}, undefined, true, undefined, this),
|
|
2302
|
+
/* @__PURE__ */ jsxDEV(Text, {
|
|
2303
|
+
children: [
|
|
2304
|
+
"Issuers: ",
|
|
2305
|
+
issuers.length
|
|
2306
|
+
]
|
|
2307
|
+
}, undefined, true, undefined, this),
|
|
2308
|
+
/* @__PURE__ */ jsxDEV(Text, {
|
|
2309
|
+
children: [
|
|
2310
|
+
"Customers: ",
|
|
2311
|
+
customers.length
|
|
2312
|
+
]
|
|
2313
|
+
}, undefined, true, undefined, this),
|
|
2314
|
+
/* @__PURE__ */ jsxDEV(Text, {
|
|
2315
|
+
color: "gray",
|
|
2316
|
+
children: "Press q to quit"
|
|
2317
|
+
}, undefined, false, undefined, this),
|
|
2318
|
+
/* @__PURE__ */ jsxDEV(Box, {
|
|
2319
|
+
marginTop: 1,
|
|
2320
|
+
flexDirection: "column",
|
|
2321
|
+
children: invoices.slice(0, 10).map((invoice) => /* @__PURE__ */ jsxDEV(Text, {
|
|
2322
|
+
children: [
|
|
2323
|
+
invoice.number,
|
|
2324
|
+
" [",
|
|
2325
|
+
invoice.status,
|
|
2326
|
+
"] ",
|
|
2327
|
+
invoice.totalCents / 100,
|
|
2328
|
+
" ",
|
|
2329
|
+
invoice.currency
|
|
2330
|
+
]
|
|
2331
|
+
}, invoice.id, true, undefined, this))
|
|
2332
|
+
}, undefined, false, undefined, this)
|
|
2333
|
+
]
|
|
2334
|
+
}, undefined, true, undefined, this);
|
|
2335
|
+
}
|
|
2336
|
+
async function runInvoicesTui() {
|
|
2337
|
+
const app = render(/* @__PURE__ */ jsxDEV(App, {}, undefined, false, undefined, this));
|
|
2338
|
+
await app.waitUntilExit();
|
|
2339
|
+
}
|
|
2340
|
+
var init_tui = __esm(() => {
|
|
2341
|
+
init_database();
|
|
2342
|
+
init_invoices();
|
|
2343
|
+
});
|
|
2344
|
+
|
|
2345
|
+
// src/cli/index.ts
|
|
2346
|
+
import chalk from "chalk";
|
|
2347
|
+
|
|
2348
|
+
// node_modules/commander/esm.mjs
|
|
2349
|
+
var import__ = __toESM(require_commander(), 1);
|
|
2350
|
+
var {
|
|
2351
|
+
program,
|
|
2352
|
+
createCommand,
|
|
2353
|
+
createArgument,
|
|
2354
|
+
createOption,
|
|
2355
|
+
CommanderError,
|
|
2356
|
+
InvalidArgumentError,
|
|
2357
|
+
InvalidOptionArgumentError,
|
|
2358
|
+
Command,
|
|
2359
|
+
Argument,
|
|
2360
|
+
Option,
|
|
2361
|
+
Help
|
|
2362
|
+
} = import__.default;
|
|
2363
|
+
|
|
2364
|
+
// src/cli/index.ts
|
|
2365
|
+
init_invoices();
|
|
2366
|
+
|
|
2367
|
+
// src/db/agents.ts
|
|
2368
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
2369
|
+
function mapAgent(row) {
|
|
2370
|
+
return {
|
|
2371
|
+
id: row.id,
|
|
2372
|
+
name: row.name,
|
|
2373
|
+
description: row.description ?? undefined,
|
|
2374
|
+
focus: row.focus ?? undefined,
|
|
2375
|
+
createdAt: row.created_at,
|
|
2376
|
+
lastSeenAt: row.last_seen_at
|
|
2377
|
+
};
|
|
2378
|
+
}
|
|
2379
|
+
function registerAgent(db, input) {
|
|
2380
|
+
const existing = db.query("SELECT * FROM agents WHERE name = ?1").get(input.name);
|
|
2381
|
+
if (existing) {
|
|
2382
|
+
db.query("UPDATE agents SET description = COALESCE(?2, description), last_seen_at = datetime('now') WHERE name = ?1").run(input.name, input.description ?? null);
|
|
2383
|
+
return getAgentByName(db, input.name);
|
|
2384
|
+
}
|
|
2385
|
+
const id = randomUUID2();
|
|
2386
|
+
db.query("INSERT INTO agents (id, name, description) VALUES (?1, ?2, ?3)").run(id, input.name, input.description ?? null);
|
|
2387
|
+
return getAgentById(db, id);
|
|
2388
|
+
}
|
|
2389
|
+
function getAgentById(db, id) {
|
|
2390
|
+
const row = db.query("SELECT * FROM agents WHERE id = ?1").get(id);
|
|
2391
|
+
return row ? mapAgent(row) : null;
|
|
2392
|
+
}
|
|
2393
|
+
function getAgentByName(db, name) {
|
|
2394
|
+
const row = db.query("SELECT * FROM agents WHERE name = ?1").get(name);
|
|
2395
|
+
return row ? mapAgent(row) : null;
|
|
2396
|
+
}
|
|
2397
|
+
function heartbeatAgent(db, agentId) {
|
|
2398
|
+
db.query("UPDATE agents SET last_seen_at = datetime('now') WHERE id = ?1").run(agentId);
|
|
2399
|
+
return getAgentById(db, agentId);
|
|
2400
|
+
}
|
|
2401
|
+
function setAgentFocus(db, agentId, focus) {
|
|
2402
|
+
db.query("UPDATE agents SET focus = ?2, last_seen_at = datetime('now') WHERE id = ?1").run(agentId, focus ?? null);
|
|
2403
|
+
return getAgentById(db, agentId);
|
|
2404
|
+
}
|
|
2405
|
+
function listAgents(db) {
|
|
2406
|
+
return db.query("SELECT * FROM agents ORDER BY last_seen_at DESC").all().map(mapAgent);
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
// src/lib/version.ts
|
|
2410
|
+
var VERSION = "0.0.1";
|
|
2411
|
+
|
|
2412
|
+
// src/cli/index.ts
|
|
2413
|
+
init_database();
|
|
2414
|
+
init_database();
|
|
2415
|
+
|
|
2416
|
+
// src/db/migrate.ts
|
|
2417
|
+
init_database();
|
|
2418
|
+
|
|
2419
|
+
// src/db/schema.ts
|
|
2420
|
+
var MIGRATIONS = [
|
|
2421
|
+
{
|
|
2422
|
+
id: "0001_init",
|
|
2423
|
+
sql: `
|
|
2424
|
+
CREATE TABLE IF NOT EXISTS migrations (
|
|
2425
|
+
id TEXT PRIMARY KEY,
|
|
2426
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2427
|
+
);
|
|
2428
|
+
|
|
2429
|
+
CREATE TABLE IF NOT EXISTS parties (
|
|
2430
|
+
id TEXT PRIMARY KEY,
|
|
2431
|
+
kind TEXT NOT NULL CHECK(kind IN ('issuer', 'customer')),
|
|
2432
|
+
legal_name TEXT NOT NULL,
|
|
2433
|
+
email TEXT,
|
|
2434
|
+
tax_id TEXT,
|
|
2435
|
+
country TEXT,
|
|
2436
|
+
address TEXT,
|
|
2437
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2438
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2439
|
+
);
|
|
2440
|
+
|
|
2441
|
+
CREATE TABLE IF NOT EXISTS invoices (
|
|
2442
|
+
id TEXT PRIMARY KEY,
|
|
2443
|
+
number TEXT NOT NULL UNIQUE,
|
|
2444
|
+
issuer_id TEXT NOT NULL REFERENCES parties(id),
|
|
2445
|
+
customer_id TEXT NOT NULL REFERENCES parties(id),
|
|
2446
|
+
status TEXT NOT NULL CHECK(status IN ('draft', 'sent', 'partially_paid', 'paid', 'void', 'overdue')),
|
|
2447
|
+
currency TEXT NOT NULL,
|
|
2448
|
+
notes TEXT,
|
|
2449
|
+
issued_at TEXT NOT NULL,
|
|
2450
|
+
due_at TEXT,
|
|
2451
|
+
subtotal_cents INTEGER NOT NULL DEFAULT 0,
|
|
2452
|
+
tax_cents INTEGER NOT NULL DEFAULT 0,
|
|
2453
|
+
total_cents INTEGER NOT NULL DEFAULT 0,
|
|
2454
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2455
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2456
|
+
);
|
|
2457
|
+
|
|
2458
|
+
CREATE TABLE IF NOT EXISTS invoice_lines (
|
|
2459
|
+
id TEXT PRIMARY KEY,
|
|
2460
|
+
invoice_id TEXT NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
|
|
2461
|
+
position INTEGER NOT NULL,
|
|
2462
|
+
description TEXT NOT NULL,
|
|
2463
|
+
quantity REAL NOT NULL DEFAULT 1,
|
|
2464
|
+
unit_price_cents INTEGER NOT NULL DEFAULT 0,
|
|
2465
|
+
tax_rate_basis_points INTEGER NOT NULL DEFAULT 0,
|
|
2466
|
+
line_total_cents INTEGER NOT NULL DEFAULT 0,
|
|
2467
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2468
|
+
);
|
|
2469
|
+
|
|
2470
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_invoice_lines_invoice_position
|
|
2471
|
+
ON invoice_lines(invoice_id, position);
|
|
2472
|
+
|
|
2473
|
+
CREATE INDEX IF NOT EXISTS idx_invoices_number ON invoices(number);
|
|
2474
|
+
CREATE INDEX IF NOT EXISTS idx_invoices_status ON invoices(status);
|
|
2475
|
+
CREATE INDEX IF NOT EXISTS idx_invoices_issued_at ON invoices(issued_at);
|
|
2476
|
+
CREATE INDEX IF NOT EXISTS idx_invoices_due_at ON invoices(due_at);
|
|
2477
|
+
|
|
2478
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS invoices_fts USING fts5(
|
|
2479
|
+
invoice_id UNINDEXED,
|
|
2480
|
+
number,
|
|
2481
|
+
issuer_name,
|
|
2482
|
+
customer_name,
|
|
2483
|
+
notes
|
|
2484
|
+
);
|
|
2485
|
+
|
|
2486
|
+
CREATE TRIGGER IF NOT EXISTS invoices_ai_fts
|
|
2487
|
+
AFTER INSERT ON invoices
|
|
2488
|
+
BEGIN
|
|
2489
|
+
INSERT INTO invoices_fts(invoice_id, number, issuer_name, customer_name, notes)
|
|
2490
|
+
VALUES (
|
|
2491
|
+
NEW.id,
|
|
2492
|
+
NEW.number,
|
|
2493
|
+
COALESCE((SELECT legal_name FROM parties WHERE id = NEW.issuer_id), ''),
|
|
2494
|
+
COALESCE((SELECT legal_name FROM parties WHERE id = NEW.customer_id), ''),
|
|
2495
|
+
COALESCE(NEW.notes, '')
|
|
2496
|
+
);
|
|
2497
|
+
END;
|
|
2498
|
+
|
|
2499
|
+
CREATE TRIGGER IF NOT EXISTS invoices_au_fts
|
|
2500
|
+
AFTER UPDATE ON invoices
|
|
2501
|
+
BEGIN
|
|
2502
|
+
DELETE FROM invoices_fts WHERE invoice_id = OLD.id;
|
|
2503
|
+
INSERT INTO invoices_fts(invoice_id, number, issuer_name, customer_name, notes)
|
|
2504
|
+
VALUES (
|
|
2505
|
+
NEW.id,
|
|
2506
|
+
NEW.number,
|
|
2507
|
+
COALESCE((SELECT legal_name FROM parties WHERE id = NEW.issuer_id), ''),
|
|
2508
|
+
COALESCE((SELECT legal_name FROM parties WHERE id = NEW.customer_id), ''),
|
|
2509
|
+
COALESCE(NEW.notes, '')
|
|
2510
|
+
);
|
|
2511
|
+
END;
|
|
2512
|
+
|
|
2513
|
+
CREATE TRIGGER IF NOT EXISTS invoices_ad_fts
|
|
2514
|
+
AFTER DELETE ON invoices
|
|
2515
|
+
BEGIN
|
|
2516
|
+
DELETE FROM invoices_fts WHERE invoice_id = OLD.id;
|
|
2517
|
+
END;
|
|
2518
|
+
`
|
|
2519
|
+
},
|
|
2520
|
+
{
|
|
2521
|
+
id: "0002_party_fts_sync",
|
|
2522
|
+
sql: `
|
|
2523
|
+
CREATE TRIGGER IF NOT EXISTS parties_au_invoice_fts
|
|
2524
|
+
AFTER UPDATE ON parties
|
|
2525
|
+
BEGIN
|
|
2526
|
+
DELETE FROM invoices_fts
|
|
2527
|
+
WHERE invoice_id IN (
|
|
2528
|
+
SELECT id FROM invoices WHERE issuer_id = NEW.id OR customer_id = NEW.id
|
|
2529
|
+
);
|
|
2530
|
+
|
|
2531
|
+
INSERT INTO invoices_fts(invoice_id, number, issuer_name, customer_name, notes)
|
|
2532
|
+
SELECT
|
|
2533
|
+
i.id,
|
|
2534
|
+
i.number,
|
|
2535
|
+
COALESCE((SELECT legal_name FROM parties p WHERE p.id = i.issuer_id), ''),
|
|
2536
|
+
COALESCE((SELECT legal_name FROM parties p WHERE p.id = i.customer_id), ''),
|
|
2537
|
+
COALESCE(i.notes, '')
|
|
2538
|
+
FROM invoices i
|
|
2539
|
+
WHERE i.issuer_id = NEW.id OR i.customer_id = NEW.id;
|
|
2540
|
+
END;
|
|
2541
|
+
|
|
2542
|
+
INSERT INTO invoices_fts(invoice_id, number, issuer_name, customer_name, notes)
|
|
2543
|
+
SELECT
|
|
2544
|
+
i.id,
|
|
2545
|
+
i.number,
|
|
2546
|
+
COALESCE((SELECT legal_name FROM parties p WHERE p.id = i.issuer_id), ''),
|
|
2547
|
+
COALESCE((SELECT legal_name FROM parties p WHERE p.id = i.customer_id), ''),
|
|
2548
|
+
COALESCE(i.notes, '')
|
|
2549
|
+
FROM invoices i
|
|
2550
|
+
WHERE i.id NOT IN (SELECT invoice_id FROM invoices_fts);
|
|
2551
|
+
`
|
|
2552
|
+
},
|
|
2553
|
+
{
|
|
2554
|
+
id: "0003_agents",
|
|
2555
|
+
sql: `
|
|
2556
|
+
CREATE TABLE IF NOT EXISTS agents (
|
|
2557
|
+
id TEXT PRIMARY KEY,
|
|
2558
|
+
name TEXT NOT NULL UNIQUE,
|
|
2559
|
+
description TEXT,
|
|
2560
|
+
focus TEXT,
|
|
2561
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2562
|
+
last_seen_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2563
|
+
);
|
|
2564
|
+
`
|
|
2565
|
+
}
|
|
2566
|
+
];
|
|
2567
|
+
|
|
2568
|
+
// src/db/migrate.ts
|
|
2569
|
+
function ensureMigrationsTable(db) {
|
|
2570
|
+
db.exec(`
|
|
2571
|
+
CREATE TABLE IF NOT EXISTS migrations (
|
|
2572
|
+
id TEXT PRIMARY KEY,
|
|
2573
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2574
|
+
);
|
|
2575
|
+
`);
|
|
2576
|
+
}
|
|
2577
|
+
function applyMigrations(db) {
|
|
2578
|
+
ensureMigrationsTable(db);
|
|
2579
|
+
const getMigration = db.query("SELECT id FROM migrations WHERE id = ?1");
|
|
2580
|
+
const markMigration = db.query("INSERT INTO migrations (id) VALUES (?1)");
|
|
2581
|
+
const applied = [];
|
|
2582
|
+
for (const migration of MIGRATIONS) {
|
|
2583
|
+
const row = getMigration.get(migration.id);
|
|
2584
|
+
if (row) {
|
|
2585
|
+
continue;
|
|
2586
|
+
}
|
|
2587
|
+
db.transaction(() => {
|
|
2588
|
+
db.exec(migration.sql);
|
|
2589
|
+
markMigration.run(migration.id);
|
|
2590
|
+
})();
|
|
2591
|
+
applied.push(migration.id);
|
|
2592
|
+
}
|
|
2593
|
+
return applied;
|
|
2594
|
+
}
|
|
2595
|
+
function migrateDatabase(options = {}) {
|
|
2596
|
+
const db = openInvoiceDatabase(options);
|
|
2597
|
+
try {
|
|
2598
|
+
return applyMigrations(db);
|
|
2599
|
+
} finally {
|
|
2600
|
+
db.close();
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
// src/cli/index.ts
|
|
2605
|
+
var program2 = new Command;
|
|
2606
|
+
program2.name("invoices").description("CLI for invoices").version(VERSION).command("ping").description("Health check").action(() => {
|
|
2607
|
+
console.log("invoices CLI is ready");
|
|
2608
|
+
});
|
|
2609
|
+
program2.command("db:migrate").description("Run local SQLite migrations").action(() => {
|
|
2610
|
+
const applied = migrateDatabase();
|
|
2611
|
+
const dbPath = defaultDatabasePath();
|
|
2612
|
+
if (applied.length === 0) {
|
|
2613
|
+
console.log(`No pending migrations (${dbPath})`);
|
|
2614
|
+
return;
|
|
2615
|
+
}
|
|
2616
|
+
console.log(`Applied migrations (${dbPath}): ${applied.join(", ")}`);
|
|
2617
|
+
});
|
|
2618
|
+
program2.command("party:create").description("Create issuer/customer party").requiredOption("--kind <kind>", "issuer|customer").requiredOption("--name <legalName>", "legal name").option("--email <email>", "email").option("--tax-id <taxId>", "tax id").option("--country <country>", "country").option("--address <address>", "address").action((opts) => {
|
|
2619
|
+
const db = openInvoiceDatabase();
|
|
2620
|
+
try {
|
|
2621
|
+
const party = createParty(db, {
|
|
2622
|
+
kind: opts.kind === "issuer" ? "issuer" : "customer",
|
|
2623
|
+
legalName: opts.name,
|
|
2624
|
+
email: opts.email,
|
|
2625
|
+
taxId: opts.taxId,
|
|
2626
|
+
country: opts.country,
|
|
2627
|
+
address: opts.address
|
|
2628
|
+
});
|
|
2629
|
+
console.log(JSON.stringify(party, null, 2));
|
|
2630
|
+
} finally {
|
|
2631
|
+
db.close();
|
|
2632
|
+
}
|
|
2633
|
+
});
|
|
2634
|
+
program2.command("party:list").description("List parties").option("--kind <kind>", "issuer|customer").action((opts) => {
|
|
2635
|
+
const db = openInvoiceDatabase();
|
|
2636
|
+
try {
|
|
2637
|
+
const parties = listParties(db, opts.kind === "issuer" || opts.kind === "customer" ? opts.kind : undefined);
|
|
2638
|
+
console.log(JSON.stringify(parties, null, 2));
|
|
2639
|
+
} finally {
|
|
2640
|
+
db.close();
|
|
2641
|
+
}
|
|
2642
|
+
});
|
|
2643
|
+
program2.command("invoice:create").description("Create invoice. Use --line 'Description:qty:unit_price_cents:tax_rate_basis_points'").requiredOption("--number <number>", "invoice number").requiredOption("--issuer-id <issuerId>", "issuer party id").requiredOption("--customer-id <customerId>", "customer party id").requiredOption("--currency <currency>", "currency code").requiredOption("--line <line...>", "line item in desc:qty:unit:tax format").option("--notes <notes>", "invoice notes").option("--issued-at <issuedAt>", "ISO datetime", new Date().toISOString()).option("--due-at <dueAt>", "ISO datetime").action((opts) => {
|
|
2644
|
+
const linesRaw = Array.isArray(opts.line) ? opts.line : [String(opts.line)];
|
|
2645
|
+
const lines = linesRaw.map((line, idx) => {
|
|
2646
|
+
const [description, qtyRaw, unitRaw, taxRaw] = String(line).split(":");
|
|
2647
|
+
const quantity = Number(qtyRaw);
|
|
2648
|
+
const unitPriceCents = Number(unitRaw);
|
|
2649
|
+
const taxRateBasisPoints = taxRaw ? Number(taxRaw) : 0;
|
|
2650
|
+
if (!description || Number.isNaN(quantity) || Number.isNaN(unitPriceCents) || Number.isNaN(taxRateBasisPoints)) {
|
|
2651
|
+
throw new Error(`Invalid --line at index ${idx}. Expected desc:qty:unit_price_cents:tax_rate_basis_points`);
|
|
2652
|
+
}
|
|
2653
|
+
return { description, quantity, unitPriceCents, taxRateBasisPoints };
|
|
2654
|
+
});
|
|
2655
|
+
const db = openInvoiceDatabase();
|
|
2656
|
+
try {
|
|
2657
|
+
const invoice = createInvoice(db, {
|
|
2658
|
+
number: String(opts.number),
|
|
2659
|
+
issuerId: String(opts.issuerId),
|
|
2660
|
+
customerId: String(opts.customerId),
|
|
2661
|
+
currency: String(opts.currency),
|
|
2662
|
+
notes: opts.notes ? String(opts.notes) : undefined,
|
|
2663
|
+
issuedAt: String(opts.issuedAt),
|
|
2664
|
+
dueAt: opts.dueAt ? String(opts.dueAt) : undefined,
|
|
2665
|
+
lines
|
|
2666
|
+
});
|
|
2667
|
+
console.log(JSON.stringify(invoice, null, 2));
|
|
2668
|
+
} finally {
|
|
2669
|
+
db.close();
|
|
2670
|
+
}
|
|
2671
|
+
});
|
|
2672
|
+
program2.command("invoice:get").description("Get invoice by id").argument("<id>", "invoice id").action((id) => {
|
|
2673
|
+
const db = openInvoiceDatabase();
|
|
2674
|
+
try {
|
|
2675
|
+
const invoice = getInvoiceById(db, id);
|
|
2676
|
+
console.log(JSON.stringify(invoice, null, 2));
|
|
2677
|
+
} finally {
|
|
2678
|
+
db.close();
|
|
2679
|
+
}
|
|
2680
|
+
});
|
|
2681
|
+
program2.command("invoice:list").description("List invoices").option("--status <status>", "invoice status").option("--limit <limit>", "limit", "50").option("--offset <offset>", "offset", "0").action((opts) => {
|
|
2682
|
+
const db = openInvoiceDatabase();
|
|
2683
|
+
try {
|
|
2684
|
+
const invoices = listInvoices(db, {
|
|
2685
|
+
status: opts.status,
|
|
2686
|
+
limit: Number(opts.limit),
|
|
2687
|
+
offset: Number(opts.offset)
|
|
2688
|
+
});
|
|
2689
|
+
console.log(JSON.stringify(invoices, null, 2));
|
|
2690
|
+
} finally {
|
|
2691
|
+
db.close();
|
|
2692
|
+
}
|
|
2693
|
+
});
|
|
2694
|
+
program2.command("invoice:search").description("Full-text search invoices").argument("<query>", "query").option("--status <status>", "invoice status").option("--limit <limit>", "limit", "20").option("--offset <offset>", "offset", "0").action((query, opts) => {
|
|
2695
|
+
const db = openInvoiceDatabase();
|
|
2696
|
+
try {
|
|
2697
|
+
const invoices = searchInvoices(db, query, {
|
|
2698
|
+
status: opts.status,
|
|
2699
|
+
limit: Number(opts.limit),
|
|
2700
|
+
offset: Number(opts.offset)
|
|
2701
|
+
});
|
|
2702
|
+
console.log(JSON.stringify(invoices, null, 2));
|
|
2703
|
+
} finally {
|
|
2704
|
+
db.close();
|
|
2705
|
+
}
|
|
2706
|
+
});
|
|
2707
|
+
program2.command("invoice:status").description("Update invoice status").argument("<id>", "invoice id").argument("<status>", "draft|sent|partially_paid|paid|void|overdue").action((id, status) => {
|
|
2708
|
+
const db = openInvoiceDatabase();
|
|
2709
|
+
try {
|
|
2710
|
+
const updated = updateInvoiceStatus(db, id, status);
|
|
2711
|
+
console.log(JSON.stringify(updated, null, 2));
|
|
2712
|
+
} finally {
|
|
2713
|
+
db.close();
|
|
2714
|
+
}
|
|
2715
|
+
});
|
|
2716
|
+
program2.command("tui").description("Open interactive Ink dashboard").action(async () => {
|
|
2717
|
+
console.log(chalk.gray("Launching invoices TUI. Press q to quit."));
|
|
2718
|
+
const { runInvoicesTui: runInvoicesTui2 } = await Promise.resolve().then(() => (init_tui(), exports_tui));
|
|
2719
|
+
await runInvoicesTui2();
|
|
2720
|
+
});
|
|
2721
|
+
program2.command("agent:register").description("Register or refresh an agent").requiredOption("--name <name>", "agent name").option("--description <description>", "agent description").action((opts) => {
|
|
2722
|
+
const db = openInvoiceDatabase();
|
|
2723
|
+
try {
|
|
2724
|
+
const agent = registerAgent(db, { name: opts.name, description: opts.description });
|
|
2725
|
+
console.log(JSON.stringify(agent, null, 2));
|
|
2726
|
+
} finally {
|
|
2727
|
+
db.close();
|
|
2728
|
+
}
|
|
2729
|
+
});
|
|
2730
|
+
program2.command("agent:heartbeat").description("Update agent heartbeat").argument("<agentId>", "agent id").action((agentId) => {
|
|
2731
|
+
const db = openInvoiceDatabase();
|
|
2732
|
+
try {
|
|
2733
|
+
const agent = heartbeatAgent(db, agentId);
|
|
2734
|
+
console.log(JSON.stringify(agent, null, 2));
|
|
2735
|
+
} finally {
|
|
2736
|
+
db.close();
|
|
2737
|
+
}
|
|
2738
|
+
});
|
|
2739
|
+
program2.command("agent:set-focus").description("Set or clear focus for an agent").argument("<agentId>", "agent id").option("--focus <focus>", "focus string").action((agentId, opts) => {
|
|
2740
|
+
const db = openInvoiceDatabase();
|
|
2741
|
+
try {
|
|
2742
|
+
const agent = setAgentFocus(db, agentId, opts.focus);
|
|
2743
|
+
console.log(JSON.stringify(agent, null, 2));
|
|
2744
|
+
} finally {
|
|
2745
|
+
db.close();
|
|
2746
|
+
}
|
|
2747
|
+
});
|
|
2748
|
+
program2.command("agent:list").description("List agents").action(() => {
|
|
2749
|
+
const db = openInvoiceDatabase();
|
|
2750
|
+
try {
|
|
2751
|
+
const agents = listAgents(db);
|
|
2752
|
+
console.log(JSON.stringify(agents, null, 2));
|
|
2753
|
+
} finally {
|
|
2754
|
+
db.close();
|
|
2755
|
+
}
|
|
2756
|
+
});
|
|
2757
|
+
program2.parse();
|