@wirecat/cli-core 0.0.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +344 -0
- package/config/biome.base.json +56 -0
- package/config/cspell.json +56 -0
- package/config/lefthook.yml +34 -0
- package/config/rumdl.toml +9 -0
- package/config/tsconfig.base.json +21 -0
- package/config/tsconfig.scripts.json +13 -0
- package/config/tsconfig.test.json +11 -0
- package/config/vale/CliDocs/Filler.yml +9 -0
- package/config/vale/CliDocs/SentenceLength.yml +6 -0
- package/config/vale/Tg/Tone.yml +7 -0
- package/dist/codegen/index.d.ts +12 -0
- package/dist/codegen/index.d.ts.map +1 -0
- package/dist/codegen/index.js +11 -0
- package/dist/codegen/index.js.map +1 -0
- package/dist/codegen/manifest.d.ts +47 -0
- package/dist/codegen/manifest.d.ts.map +1 -0
- package/dist/codegen/manifest.js +73 -0
- package/dist/codegen/manifest.js.map +1 -0
- package/dist/codegen/model.d.ts +146 -0
- package/dist/codegen/model.d.ts.map +1 -0
- package/dist/codegen/model.js +7 -0
- package/dist/codegen/model.js.map +1 -0
- package/dist/codegen/names.d.ts +4 -0
- package/dist/codegen/names.d.ts.map +1 -0
- package/dist/codegen/names.js +46 -0
- package/dist/codegen/names.js.map +1 -0
- package/dist/codegen/pipeline.d.ts +24 -0
- package/dist/codegen/pipeline.d.ts.map +1 -0
- package/dist/codegen/pipeline.js +193 -0
- package/dist/codegen/pipeline.js.map +1 -0
- package/dist/codegen/runtime.d.ts +24 -0
- package/dist/codegen/runtime.d.ts.map +1 -0
- package/dist/codegen/runtime.js +38 -0
- package/dist/codegen/runtime.js.map +1 -0
- package/dist/codegen/tree.d.ts +29 -0
- package/dist/codegen/tree.d.ts.map +1 -0
- package/dist/codegen/tree.js +87 -0
- package/dist/codegen/tree.js.map +1 -0
- package/dist/codegen/typescript.d.ts +6 -0
- package/dist/codegen/typescript.d.ts.map +1 -0
- package/dist/codegen/typescript.js +78 -0
- package/dist/codegen/typescript.js.map +1 -0
- package/dist/codegen/valibot.d.ts +10 -0
- package/dist/codegen/valibot.d.ts.map +1 -0
- package/dist/codegen/valibot.js +132 -0
- package/dist/codegen/valibot.js.map +1 -0
- package/dist/commands/index.d.ts +76 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +62 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/page.d.ts +80 -0
- package/dist/commands/page.d.ts.map +1 -0
- package/dist/commands/page.js +100 -0
- package/dist/commands/page.js.map +1 -0
- package/dist/completion/index.d.ts +37 -0
- package/dist/completion/index.d.ts.map +1 -0
- package/dist/completion/index.js +87 -0
- package/dist/completion/index.js.map +1 -0
- package/dist/config.d.ts +20 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +61 -0
- package/dist/config.js.map +1 -0
- package/dist/credentials.d.ts +48 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +120 -0
- package/dist/credentials.js.map +1 -0
- package/dist/dev/bin.d.ts +3 -0
- package/dist/dev/bin.d.ts.map +1 -0
- package/dist/dev/bin.js +8 -0
- package/dist/dev/bin.js.map +1 -0
- package/dist/dev/main.d.ts +7 -0
- package/dist/dev/main.d.ts.map +1 -0
- package/dist/dev/main.js +149 -0
- package/dist/dev/main.js.map +1 -0
- package/dist/dev/next-version.d.ts +11 -0
- package/dist/dev/next-version.d.ts.map +1 -0
- package/dist/dev/next-version.js +33 -0
- package/dist/dev/next-version.js.map +1 -0
- package/dist/dev/slow-tests.d.ts +14 -0
- package/dist/dev/slow-tests.d.ts.map +1 -0
- package/dist/dev/slow-tests.js +17 -0
- package/dist/dev/slow-tests.js.map +1 -0
- package/dist/dev/test-matrix.d.ts +35 -0
- package/dist/dev/test-matrix.d.ts.map +1 -0
- package/dist/dev/test-matrix.js +93 -0
- package/dist/dev/test-matrix.js.map +1 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +36 -0
- package/dist/errors.js.map +1 -0
- package/dist/exit-codes.d.ts +11 -0
- package/dist/exit-codes.d.ts.map +1 -0
- package/dist/exit-codes.js +25 -0
- package/dist/exit-codes.js.map +1 -0
- package/dist/http/index.d.ts +20 -0
- package/dist/http/index.d.ts.map +1 -0
- package/dist/http/index.js +46 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/keyring.d.ts +27 -0
- package/dist/keyring.d.ts.map +1 -0
- package/dist/keyring.js +45 -0
- package/dist/keyring.js.map +1 -0
- package/dist/logger.d.ts +12 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +7 -0
- package/dist/logger.js.map +1 -0
- package/dist/logging.d.ts +31 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +58 -0
- package/dist/logging.js.map +1 -0
- package/dist/mcp/index.d.ts +32 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +105 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/paths.d.ts +28 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +23 -0
- package/dist/paths.js.map +1 -0
- package/dist/pretty.d.ts +12 -0
- package/dist/pretty.d.ts.map +1 -0
- package/dist/pretty.js +83 -0
- package/dist/pretty.js.map +1 -0
- package/dist/release/index.d.ts +38 -0
- package/dist/release/index.d.ts.map +1 -0
- package/dist/release/index.js +91 -0
- package/dist/release/index.js.map +1 -0
- package/dist/release/markdown.d.ts +36 -0
- package/dist/release/markdown.d.ts.map +1 -0
- package/dist/release/markdown.js +153 -0
- package/dist/release/markdown.js.map +1 -0
- package/dist/release/structure.d.ts +16 -0
- package/dist/release/structure.d.ts.map +1 -0
- package/dist/release/structure.js +74 -0
- package/dist/release/structure.js.map +1 -0
- package/dist/renderer.d.ts +34 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +55 -0
- package/dist/renderer.js.map +1 -0
- package/dist/retry.d.ts +27 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +26 -0
- package/dist/retry.js.map +1 -0
- package/dist/sanitize.d.ts +25 -0
- package/dist/sanitize.d.ts.map +1 -0
- package/dist/sanitize.js +52 -0
- package/dist/sanitize.js.map +1 -0
- package/dist/skill/index.d.ts +78 -0
- package/dist/skill/index.d.ts.map +1 -0
- package/dist/skill/index.js +190 -0
- package/dist/skill/index.js.map +1 -0
- package/dist/streams.d.ts +24 -0
- package/dist/streams.d.ts.map +1 -0
- package/dist/streams.js +26 -0
- package/dist/streams.js.map +1 -0
- package/dist/testing/index.d.ts +24 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +29 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/time.d.ts +21 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +19 -0
- package/dist/time.js.map +1 -0
- package/dist/update/index.d.ts +80 -0
- package/dist/update/index.d.ts.map +1 -0
- package/dist/update/index.js +159 -0
- package/dist/update/index.js.map +1 -0
- package/package.json +116 -2
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { CommandInfo, OptionInfo } from "./index.js";
|
|
2
|
+
/** The labels of the tables; the same for every CLI writing in one language. */
|
|
3
|
+
export type CommandsPageLabels = {
|
|
4
|
+
option: string;
|
|
5
|
+
does: string;
|
|
6
|
+
argument: string;
|
|
7
|
+
isWhat: string;
|
|
8
|
+
required: string;
|
|
9
|
+
optional: string;
|
|
10
|
+
/** Precedes a default: `Default` → "… Default: `20`." */
|
|
11
|
+
defaultIs: string;
|
|
12
|
+
/** Precedes the allowed values: `One of` → "… One of: `new`, `old`." */
|
|
13
|
+
oneOf: string;
|
|
14
|
+
code: string;
|
|
15
|
+
when: string;
|
|
16
|
+
success: string;
|
|
17
|
+
otherwise: string;
|
|
18
|
+
};
|
|
19
|
+
export declare const COMMANDS_PAGE_LABELS: {
|
|
20
|
+
readonly en: {
|
|
21
|
+
readonly option: "Option";
|
|
22
|
+
readonly does: "What it does";
|
|
23
|
+
readonly argument: "Argument";
|
|
24
|
+
readonly isWhat: "What it is";
|
|
25
|
+
readonly required: "required";
|
|
26
|
+
readonly optional: "optional";
|
|
27
|
+
readonly defaultIs: "Default";
|
|
28
|
+
readonly oneOf: "One of";
|
|
29
|
+
readonly code: "Code";
|
|
30
|
+
readonly when: "When";
|
|
31
|
+
readonly success: "it worked";
|
|
32
|
+
readonly otherwise: "anything else";
|
|
33
|
+
};
|
|
34
|
+
readonly ru: {
|
|
35
|
+
readonly option: "Опция";
|
|
36
|
+
readonly does: "Что делает";
|
|
37
|
+
readonly argument: "Аргумент";
|
|
38
|
+
readonly isWhat: "Что это";
|
|
39
|
+
readonly required: "обязательный";
|
|
40
|
+
readonly optional: "необязательный";
|
|
41
|
+
readonly defaultIs: "По умолчанию";
|
|
42
|
+
readonly oneOf: "Одно из";
|
|
43
|
+
readonly code: "Код";
|
|
44
|
+
readonly when: "Когда";
|
|
45
|
+
readonly success: "получилось";
|
|
46
|
+
readonly otherwise: "всё остальное";
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
/** The CLI's own words: everything on the page that is not read out of the program. */
|
|
50
|
+
export type CommandsPageText = {
|
|
51
|
+
/** The first line, an HTML comment saying the page is generated and by what. */
|
|
52
|
+
banner: string;
|
|
53
|
+
title: string;
|
|
54
|
+
/** Markdown between the title and the global options. */
|
|
55
|
+
intro: string;
|
|
56
|
+
globalHeading: string;
|
|
57
|
+
globalIntro: string;
|
|
58
|
+
/** Shown under a command that changes something outside this machine. */
|
|
59
|
+
mutates: string;
|
|
60
|
+
/** Shown instead under a command marked `local`, which changes only this machine; without it, nothing is. */
|
|
61
|
+
mutatesLocal?: string;
|
|
62
|
+
exitHeading: string;
|
|
63
|
+
exitIntro: string;
|
|
64
|
+
/** Markdown after the exit codes; may be empty. */
|
|
65
|
+
outro: string;
|
|
66
|
+
};
|
|
67
|
+
export type CommandsPage = {
|
|
68
|
+
cli: string;
|
|
69
|
+
commands: readonly CommandInfo[];
|
|
70
|
+
options: readonly OptionInfo[];
|
|
71
|
+
labels: CommandsPageLabels;
|
|
72
|
+
text: CommandsPageText;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The whole `docs/commands.md`: every command at any depth, every option and argument, the exit
|
|
76
|
+
* codes. Descriptions are quoted as `--help` prints them, untranslated — a translation here would
|
|
77
|
+
* give the sentence a second home, and the one on screen is the one that gets corrected.
|
|
78
|
+
*/
|
|
79
|
+
export declare const commandsPage: ({ cli, commands, options, labels, text }: CommandsPage) => string;
|
|
80
|
+
//# sourceMappingURL=page.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../src/commands/page.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAgB,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvE,gFAAgF;AAChF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,yDAAyD;IACzD,SAAS,EAAE,MAAM,CAAA;IACjB,wEAAwE;IACxE,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,oBAAoB;;iBAE7B,MAAM,EAAE,QAAQ;iBAChB,IAAI,EAAE,cAAc;iBACpB,QAAQ,EAAE,UAAU;iBACpB,MAAM,EAAE,YAAY;iBACpB,QAAQ,EAAE,UAAU;iBACpB,QAAQ,EAAE,UAAU;iBACpB,SAAS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
+
import { EXIT_CODES } from "../exit-codes.js";
|
|
2
|
+
export const COMMANDS_PAGE_LABELS = {
|
|
3
|
+
en: {
|
|
4
|
+
option: "Option",
|
|
5
|
+
does: "What it does",
|
|
6
|
+
argument: "Argument",
|
|
7
|
+
isWhat: "What it is",
|
|
8
|
+
required: "required",
|
|
9
|
+
optional: "optional",
|
|
10
|
+
defaultIs: "Default",
|
|
11
|
+
oneOf: "One of",
|
|
12
|
+
code: "Code",
|
|
13
|
+
when: "When",
|
|
14
|
+
success: "it worked",
|
|
15
|
+
otherwise: "anything else",
|
|
16
|
+
},
|
|
17
|
+
ru: {
|
|
18
|
+
option: "Опция",
|
|
19
|
+
does: "Что делает",
|
|
20
|
+
argument: "Аргумент",
|
|
21
|
+
isWhat: "Что это",
|
|
22
|
+
required: "обязательный",
|
|
23
|
+
optional: "необязательный",
|
|
24
|
+
defaultIs: "По умолчанию",
|
|
25
|
+
oneOf: "Одно из",
|
|
26
|
+
code: "Код",
|
|
27
|
+
when: "Когда",
|
|
28
|
+
success: "получилось",
|
|
29
|
+
otherwise: "всё остальное",
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
/** A cell that will not break the table it sits in; `~~struck~~` in a description would strike the row. */
|
|
33
|
+
const cell = (text) => (text ?? "").replace(/\|/g, "\\|").replace(/~/g, "\\~").replace(/\n+/g, " ").trim();
|
|
34
|
+
const sentence = (text) => {
|
|
35
|
+
const said = cell(text);
|
|
36
|
+
return said === "" || /[.!?]$/.test(said) ? said : `${said}.`;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The whole `docs/commands.md`: every command at any depth, every option and argument, the exit
|
|
40
|
+
* codes. Descriptions are quoted as `--help` prints them, untranslated — a translation here would
|
|
41
|
+
* give the sentence a second home, and the one on screen is the one that gets corrected.
|
|
42
|
+
*/
|
|
43
|
+
export const commandsPage = ({ cli, commands, options, labels, text }) => {
|
|
44
|
+
// What a value may be and what it is when left out: Commander keeps both out of the description.
|
|
45
|
+
const extras = ({ choices, default: fallback }) => [
|
|
46
|
+
choices ? ` ${labels.oneOf}: ${choices.map((choice) => `\`${choice}\``).join(", ")}.` : "",
|
|
47
|
+
fallback === undefined || fallback === false ? "" : ` ${labels.defaultIs}: \`${String(fallback)}\`.`,
|
|
48
|
+
].join("");
|
|
49
|
+
// The flags go through `cell` too: `--order <recent|name>` would otherwise split the row.
|
|
50
|
+
const optionRows = (list) => list.map((option) => `| \`${cell(option.flags)}\` | ${sentence(option.description)}${extras(option)} |`).join("\n");
|
|
51
|
+
const argumentRows = (list) => list
|
|
52
|
+
.map((argument) => `| \`${cell(argument.name)}\` | ${argument.required ? labels.required : labels.optional} | ${sentence(argument.description)}${extras(argument)} |`)
|
|
53
|
+
.join("\n");
|
|
54
|
+
const body = (command) => {
|
|
55
|
+
const parts = [cell(command.description), ""];
|
|
56
|
+
const label = command.mutates ? (command.local ? text.mutatesLocal : text.mutates) : undefined;
|
|
57
|
+
if (label)
|
|
58
|
+
parts.push(label, "");
|
|
59
|
+
parts.push("```sh", command.usage, "```");
|
|
60
|
+
if (command.arguments.length > 0)
|
|
61
|
+
parts.push("", `| ${labels.argument} | | ${labels.isWhat} |`, "|---|---|---|", argumentRows(command.arguments));
|
|
62
|
+
if (command.options.length > 0)
|
|
63
|
+
parts.push("", `| ${labels.option} | ${labels.does} |`, "|---|---|", optionRows(command.options));
|
|
64
|
+
return parts;
|
|
65
|
+
};
|
|
66
|
+
// A group shows its own usage only when it takes options of its own, as `doctor --online` does.
|
|
67
|
+
const section = (command, depth) => {
|
|
68
|
+
const heading = `${"#".repeat(Math.min(depth, 4))} \`${cli} ${command.path.join(" ")}\``;
|
|
69
|
+
if (command.commands.length === 0)
|
|
70
|
+
return [heading, "", ...body(command)].join("\n");
|
|
71
|
+
const own = command.options.length > 0 ? body(command) : [cell(command.description)];
|
|
72
|
+
return [heading, "", ...own, "", command.commands.map((child) => section(child, depth + 1)).join("\n\n")]
|
|
73
|
+
.join("\n")
|
|
74
|
+
.trimEnd();
|
|
75
|
+
};
|
|
76
|
+
const exitCodes = [
|
|
77
|
+
`| ${labels.code} | ${labels.when} |`,
|
|
78
|
+
"|---|---|",
|
|
79
|
+
`| \`0\` | ${labels.success} |`,
|
|
80
|
+
...Object.entries(EXIT_CODES).map(([name, code]) => `| \`${code}\` | \`${name}\` |`),
|
|
81
|
+
`| \`1\` | ${labels.otherwise} |`,
|
|
82
|
+
].join("\n");
|
|
83
|
+
return [
|
|
84
|
+
text.banner,
|
|
85
|
+
`# ${text.title}`,
|
|
86
|
+
text.intro,
|
|
87
|
+
`## ${text.globalHeading}`,
|
|
88
|
+
text.globalIntro,
|
|
89
|
+
[`| ${labels.option} | ${labels.does} |`, "|---|---|", optionRows(options)].join("\n"),
|
|
90
|
+
commands.map((command) => section(command, 2)).join("\n\n"),
|
|
91
|
+
`## ${text.exitHeading}`,
|
|
92
|
+
text.exitIntro,
|
|
93
|
+
exitCodes,
|
|
94
|
+
text.outro,
|
|
95
|
+
]
|
|
96
|
+
.filter((part) => part !== "")
|
|
97
|
+
.join("\n\n")
|
|
98
|
+
.concat("\n");
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../../src/commands/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAqB7C,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,EAAE,EAAE;QACF,MAAM,EAAE,QAAQ;QAChB,IAAI,EAAE,cAAc;QACpB,QAAQ,EAAE,UAAU;QACpB,MAAM,EAAE,YAAY;QACpB,QAAQ,EAAE,UAAU;QACpB,QAAQ,EAAE,UAAU;QACpB,SAAS,EAAE,SAAS;QACpB,KAAK,EAAE,QAAQ;QACf,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,MAAM;QACZ,OAAO,EAAE,WAAW;QACpB,SAAS,EAAE,eAAe;KAC3B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,KAAK;KACX;SACE,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC;SAC7B,IAAI,CAAC,MAAM,CAAC;SACZ,MAAM,CAAC,IAAI,CAAC,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell completion over the command registry: which word may come next, as data.
|
|
3
|
+
*
|
|
4
|
+
* It speaks the protocol of the shell scripts `@bomb.sh/tab` generates — the shell runs
|
|
5
|
+
* `<cli> complete -- <words>` and reads `value<TAB>description` lines ending in `:<directive>` —
|
|
6
|
+
* so a CLI prints those scripts with tab and answers the requests with this. Nothing here reads a
|
|
7
|
+
* terminal, a file or the network: the words come in, the suggestions go out, and where values come
|
|
8
|
+
* from (a local cache, a config file) is the CLI's business, handed in as `sources`.
|
|
9
|
+
*/
|
|
10
|
+
import type { CommandInfo, OptionInfo } from "../commands/index.js";
|
|
11
|
+
export interface Suggestion {
|
|
12
|
+
value: string;
|
|
13
|
+
description: string;
|
|
14
|
+
}
|
|
15
|
+
/** Values for one argument or option. Called on every Tab, so it must be cheap and must not connect anywhere. */
|
|
16
|
+
export type Values = () => readonly (string | Suggestion)[];
|
|
17
|
+
export interface CompletionSources {
|
|
18
|
+
/** By argument name, as registered: `chat`, `person`. */
|
|
19
|
+
arguments?: Readonly<Record<string, Values>>;
|
|
20
|
+
/** By long option name without dashes: `chat` for `--chat <id>`. */
|
|
21
|
+
options?: Readonly<Record<string, Values>>;
|
|
22
|
+
/** Offered alongside the top-level commands — a profile name that may come first, say. */
|
|
23
|
+
firstWord?: Values;
|
|
24
|
+
}
|
|
25
|
+
export interface CompletionRequest {
|
|
26
|
+
commands: readonly CommandInfo[];
|
|
27
|
+
globalOptions?: readonly OptionInfo[];
|
|
28
|
+
/** Everything after `complete --`; the last word is the one being typed, `""` for a fresh one. */
|
|
29
|
+
words: readonly string[];
|
|
30
|
+
sources?: CompletionSources;
|
|
31
|
+
}
|
|
32
|
+
/** Tells the shell not to fall back to file names when nothing matched. */
|
|
33
|
+
export declare const NO_FILE_COMPLETION = 4;
|
|
34
|
+
export declare const suggest: ({ commands, globalOptions, words, sources }: CompletionRequest) => Suggestion[];
|
|
35
|
+
/** The answer in the form the shell script reads. */
|
|
36
|
+
export declare const formatSuggestions: (suggestions: readonly Suggestion[]) => string;
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/completion/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAgB,WAAW,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEjF,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,iHAAiH;AACjH,MAAM,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,GAAG,UAAU,CAAC,EAAE,CAAA;AAE3D,MAAM,WAAW,iBAAiB;IAChC,yDAAyD;IACzD,SAAS,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IAC5C,oEAAoE;IACpE,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IAC1C,0FAA0F;IAC1F,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,WAAW,EAAE,CAAA;IAChC,aAAa,CAAC,EAAE,SAAS,UAAU,EAAE,CAAA;IACrC,kGAAkG;IAClG,KAAK,EAAE,SAAS,MAAM,EAAE,CAAA;IACxB,OAAO,CAAC,EAAE,iBAAiB,CAAA;CAC5B;AAED,2EAA2E;AAC3E,eAAO,MAAM,kBAAkB,IAAI,CAAA;AAEnC,eAAO,MAAM,OAAO,gDAA2D,iBAAiB,KAAG,UAAU,EA2C5G,CAAA;AAED,qDAAqD;AACrD,eAAO,MAAM,iBAAiB,gBAAiB,SAAS,UAAU,EAAE,KAAG,MAC4C,CAAA"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Tells the shell not to fall back to file names when nothing matched. */
|
|
2
|
+
export const NO_FILE_COMPLETION = 4;
|
|
3
|
+
export const suggest = ({ commands, globalOptions = [], words, sources = {} }) => {
|
|
4
|
+
const typed = words.at(-1) ?? "";
|
|
5
|
+
const before = words.slice(0, -1);
|
|
6
|
+
let level = commands;
|
|
7
|
+
let current;
|
|
8
|
+
let positional = 0;
|
|
9
|
+
let pending;
|
|
10
|
+
for (const word of before) {
|
|
11
|
+
if (pending) {
|
|
12
|
+
pending = undefined;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (word.startsWith("-")) {
|
|
16
|
+
const option = findOption(word, current, globalOptions);
|
|
17
|
+
if (option?.takesValue && !word.includes("="))
|
|
18
|
+
pending = option;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const child = level.find((command) => command.name === word);
|
|
22
|
+
if (child) {
|
|
23
|
+
current = child;
|
|
24
|
+
level = child.commands;
|
|
25
|
+
positional = 0;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
positional += 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const candidates = pending
|
|
32
|
+
? optionValues(pending, sources)
|
|
33
|
+
: typed.startsWith("-")
|
|
34
|
+
? typed.includes("=")
|
|
35
|
+
? valuesAfterEquals(typed, current, globalOptions, sources)
|
|
36
|
+
: optionNames(current, globalOptions)
|
|
37
|
+
: [
|
|
38
|
+
...level.filter((command) => command.state !== "deprecated").map(asSuggestion),
|
|
39
|
+
...(current ? argumentValues(current.arguments, positional, sources) : []),
|
|
40
|
+
...(current ? [] : resolve(sources.firstWord)),
|
|
41
|
+
];
|
|
42
|
+
const unique = new Map(candidates.map((candidate) => [candidate.value, candidate]));
|
|
43
|
+
return [...unique.values()].filter(({ value }) => value.startsWith(typed));
|
|
44
|
+
};
|
|
45
|
+
/** The answer in the form the shell script reads. */
|
|
46
|
+
export const formatSuggestions = (suggestions) => [...suggestions.map(({ value, description }) => `${value}\t${description}`), `:${NO_FILE_COMPLETION}`].join("\n");
|
|
47
|
+
const asSuggestion = (command) => ({
|
|
48
|
+
value: command.name,
|
|
49
|
+
description: command.summary ?? command.description,
|
|
50
|
+
});
|
|
51
|
+
const longName = (option) => option.flags.match(/--([\w-]+)/)?.[1];
|
|
52
|
+
const findOption = (word, current, globals) => {
|
|
53
|
+
const name = word.replace(/^-+/, "").split("=")[0];
|
|
54
|
+
const matches = (option) => longName(option) === name || option.flags.split(/[ ,]+/).includes(`-${name}`);
|
|
55
|
+
return [...(current?.options ?? []), ...globals].find(matches);
|
|
56
|
+
};
|
|
57
|
+
const optionNames = (current, globals) => [...(current?.options ?? []), ...globals].flatMap((option) => {
|
|
58
|
+
const name = longName(option);
|
|
59
|
+
return name ? [{ value: `--${name}`, description: option.description }] : [];
|
|
60
|
+
});
|
|
61
|
+
/** `--kind <dialog|group|channel>`: Commander does not know these as choices, but the flags say them. */
|
|
62
|
+
const inlineChoices = (flags) => flags.match(/<([^>]*\|[^>]*)>/)?.[1]?.split("|") ?? [];
|
|
63
|
+
const optionValues = (option, sources) => {
|
|
64
|
+
const name = longName(option);
|
|
65
|
+
return [
|
|
66
|
+
...[...(option.choices ?? []), ...inlineChoices(option.flags)].map((value) => ({ value, description: "" })),
|
|
67
|
+
...resolve(name ? sources.options?.[name] : undefined),
|
|
68
|
+
];
|
|
69
|
+
};
|
|
70
|
+
const valuesAfterEquals = (typed, current, globals, sources) => {
|
|
71
|
+
const [flag] = typed.split("=");
|
|
72
|
+
const option = findOption(flag ?? "", current, globals);
|
|
73
|
+
if (!option?.takesValue)
|
|
74
|
+
return [];
|
|
75
|
+
return optionValues(option, sources).map(({ value, description }) => ({ value: `${flag}=${value}`, description }));
|
|
76
|
+
};
|
|
77
|
+
const argumentValues = (args, position, sources) => {
|
|
78
|
+
const argument = args[position] ?? (args.at(-1)?.variadic ? args.at(-1) : undefined);
|
|
79
|
+
if (!argument)
|
|
80
|
+
return [];
|
|
81
|
+
return [
|
|
82
|
+
...(argument.choices ?? []).map((value) => ({ value, description: "" })),
|
|
83
|
+
...resolve(sources.arguments?.[argument.name]),
|
|
84
|
+
];
|
|
85
|
+
};
|
|
86
|
+
const resolve = (values) => (values?.() ?? []).map((value) => (typeof value === "string" ? { value, description: "" } : value));
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/completion/index.ts"],"names":[],"mappings":"AAoCA,2EAA2E;AAC3E,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAA;AAEnC,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,EAAE,QAAQ,EAAE,aAAa,GAAG,EAAE,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,EAAqB,EAAgB,EAAE;IAChH,MAAM,KAAK,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;IAChC,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAEjC,IAAI,KAAK,GAA2B,QAAQ,CAAA;IAC5C,IAAI,OAAgC,CAAA;IACpC,IAAI,UAAU,GAAG,CAAC,CAAA;IAClB,IAAI,OAA+B,CAAA;IAEnC,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;QAC1B,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,GAAG,SAAS,CAAA;YACnB,SAAQ;QACV,CAAC;QACD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,EAAE,OAAO,EAAE,aAAa,CAA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AAC,IAAI,CAAC,CAAC;KAC/C,CAAA;AACH,CAAC,CAAA;AAED,MAAM,OAAO,GAAG,CAAC,MAA0B,EAAgB,EAAE,CAC3D,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAA"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
/**
|
|
3
|
+
* Atomic, and the directory is locked down before the first write.
|
|
4
|
+
*
|
|
5
|
+
* Atomic because a partial credential file loses **every** profile's secret, not just the one
|
|
6
|
+
* being written, and the window for that is exactly a Ctrl+C mid-write. The directory mode matters
|
|
7
|
+
* as much as the file's: `0o600` on a file inside a world-readable directory is worth little.
|
|
8
|
+
* ⚠ Both modes are ignored on Windows.
|
|
9
|
+
*/
|
|
10
|
+
export declare const writeSecurely: (path: string, contents: string, mode: number) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Reads and validates a JSON config. A missing file is not an error — it is a program that has not
|
|
13
|
+
* been configured yet — but a malformed one is, and it says which field and why.
|
|
14
|
+
*
|
|
15
|
+
* The schema belongs to the host: what a profile holds is the one thing no shared package can know.
|
|
16
|
+
*/
|
|
17
|
+
export declare const loadConfigFile: <TSchema extends v.GenericSchema>(path: string, schema: TSchema, fallback: () => v.InferOutput<TSchema>) => v.InferOutput<TSchema>;
|
|
18
|
+
/** Config is not a secret, so it is `0644`; the credential file next to it is not. */
|
|
19
|
+
export declare const saveConfigFile: (path: string, config: unknown) => void;
|
|
20
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAA;AAE5B;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,SAAU,MAAM,YAAY,MAAM,QAAQ,MAAM,KAAG,IAe5E,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,SAAS,CAAC,CAAC,aAAa,QACtD,MAAM,UACJ,OAAO,YACL,MAAM,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,KACrC,CAAC,CAAC,WAAW,CAAC,OAAO,CAqBvB,CAAA;AAED,sFAAsF;AACtF,eAAO,MAAM,cAAc,SAAU,MAAM,UAAU,OAAO,KAAG,IAE9D,CAAA"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import * as v from "valibot";
|
|
5
|
+
/**
|
|
6
|
+
* Atomic, and the directory is locked down before the first write.
|
|
7
|
+
*
|
|
8
|
+
* Atomic because a partial credential file loses **every** profile's secret, not just the one
|
|
9
|
+
* being written, and the window for that is exactly a Ctrl+C mid-write. The directory mode matters
|
|
10
|
+
* as much as the file's: `0o600` on a file inside a world-readable directory is worth little.
|
|
11
|
+
* ⚠ Both modes are ignored on Windows.
|
|
12
|
+
*/
|
|
13
|
+
export const writeSecurely = (path, contents, mode) => {
|
|
14
|
+
const dir = dirname(path);
|
|
15
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
16
|
+
// An unguessable name opened with `wx` fails on anything already there, a planted symlink
|
|
17
|
+
// included, instead of writing through it.
|
|
18
|
+
const temp = join(dir, `.${randomBytes(8).toString("hex")}.tmp`);
|
|
19
|
+
try {
|
|
20
|
+
writeFileSync(temp, contents, { mode, flag: "wx" });
|
|
21
|
+
// A rename within one directory is atomic on every platform we target.
|
|
22
|
+
renameSync(temp, path);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
rmSync(temp, { force: true });
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Reads and validates a JSON config. A missing file is not an error — it is a program that has not
|
|
31
|
+
* been configured yet — but a malformed one is, and it says which field and why.
|
|
32
|
+
*
|
|
33
|
+
* The schema belongs to the host: what a profile holds is the one thing no shared package can know.
|
|
34
|
+
*/
|
|
35
|
+
export const loadConfigFile = (path, schema, fallback) => {
|
|
36
|
+
let text;
|
|
37
|
+
try {
|
|
38
|
+
text = readFileSync(path, "utf8");
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return fallback();
|
|
42
|
+
}
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
parsed = JSON.parse(text);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new Error(`${path} is not valid JSON`);
|
|
49
|
+
}
|
|
50
|
+
const result = v.safeParse(schema, parsed);
|
|
51
|
+
if (!result.success) {
|
|
52
|
+
const problems = result.issues.map((issue) => `${v.getDotPath(issue) ?? "(root)"}: ${issue.message}`);
|
|
53
|
+
throw new Error(`${path} is not a valid config:\n ${problems.join("\n ")}`);
|
|
54
|
+
}
|
|
55
|
+
return result.output;
|
|
56
|
+
};
|
|
57
|
+
/** Config is not a secret, so it is `0644`; the credential file next to it is not. */
|
|
58
|
+
export const saveConfigFile = (path, config) => {
|
|
59
|
+
writeSecurely(path, `${JSON.stringify(config, null, 2)}\n`, 0o644);
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,aA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KAAK,CAAC,IAAI,CAAC,CAAA;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,oBAAoB,CAAC,CAAA;IAC9C,CAAC;IAED,MAAM,MAAM,GAAG,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC1C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,QAAQ,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;QACrG,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,8BAA8B,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;IAC/E,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAA;AACtB,CAAC,CAAA;AAED,sFAAsF;AACtF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,IAAY,EAAE,MAAe,EAAQ,EAAE;IACpE,aAAa,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;AACpE,CAAC,CAAA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type KeyringStore } from "./keyring.js";
|
|
2
|
+
export declare const CREDENTIAL_STORAGE: readonly ["auto", "keyring", "file"];
|
|
3
|
+
export type CredentialStorage = (typeof CREDENTIAL_STORAGE)[number];
|
|
4
|
+
export type CredentialSource = "environment" | "keyring" | "file";
|
|
5
|
+
export interface StoredCredential {
|
|
6
|
+
secret: string;
|
|
7
|
+
source: CredentialSource;
|
|
8
|
+
}
|
|
9
|
+
export interface CredentialsOptions {
|
|
10
|
+
/** Where the fallback file lives when the keyring cannot be used. */
|
|
11
|
+
configDir: string;
|
|
12
|
+
/** The keyring service name — the command's name, e.g. `max-cli`. */
|
|
13
|
+
service: string;
|
|
14
|
+
/**
|
|
15
|
+
* **True when the config directory came from the environment rather than the OS convention.**
|
|
16
|
+
*
|
|
17
|
+
* The OS keyring is global: an entry is addressed by service and account and knows nothing about
|
|
18
|
+
* which config directory asked for it. So a throwaway config directory looks isolated and is
|
|
19
|
+
* not — it overwrites the real secret for that account. In `brazecli` that destroyed two working
|
|
20
|
+
* keys on 2026-09-14, and a keyring entry cannot be read back out. Passing `true` here scopes the
|
|
21
|
+
* service name to the directory, making the isolation people already assume they have real.
|
|
22
|
+
*/
|
|
23
|
+
isolated?: boolean;
|
|
24
|
+
/** Read before the keyring, for CI. */
|
|
25
|
+
envVar?: string;
|
|
26
|
+
storage?: CredentialStorage;
|
|
27
|
+
keyring?: KeyringStore;
|
|
28
|
+
env?: NodeJS.ProcessEnv;
|
|
29
|
+
fileName?: string;
|
|
30
|
+
/** Where the one-line warning goes when the keyring is unavailable. Never stdout. */
|
|
31
|
+
warn?: (message: string) => void;
|
|
32
|
+
}
|
|
33
|
+
export declare const keyringService: (service: string, configDir: string, isolated?: boolean) => string;
|
|
34
|
+
/**
|
|
35
|
+
* Environment first, then the OS keyring, then a file.
|
|
36
|
+
*
|
|
37
|
+
* `auto` does not probe whether a keyring exists: it attempts the operation, and on failure warns
|
|
38
|
+
* once and falls through. A probe would touch the user's keychain for nothing and could still
|
|
39
|
+
* succeed where the real operation fails.
|
|
40
|
+
*/
|
|
41
|
+
export declare class Credentials {
|
|
42
|
+
#private;
|
|
43
|
+
constructor(options: CredentialsOptions);
|
|
44
|
+
read(account: string): StoredCredential | undefined;
|
|
45
|
+
write(account: string, secret: string): CredentialSource;
|
|
46
|
+
remove(account: string): CredentialSource[];
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../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A4CF"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { writeSecurely } from "./config.js";
|
|
4
|
+
import { CliError } from "./errors.js";
|
|
5
|
+
import { systemKeyring } from "./keyring.js";
|
|
6
|
+
export const CREDENTIAL_STORAGE = ["auto", "keyring", "file"];
|
|
7
|
+
export const keyringService = (service, configDir, isolated = false) => isolated ? `${service}:${configDir}` : service;
|
|
8
|
+
/**
|
|
9
|
+
* Environment first, then the OS keyring, then a file.
|
|
10
|
+
*
|
|
11
|
+
* `auto` does not probe whether a keyring exists: it attempts the operation, and on failure warns
|
|
12
|
+
* once and falls through. A probe would touch the user's keychain for nothing and could still
|
|
13
|
+
* succeed where the real operation fails.
|
|
14
|
+
*/
|
|
15
|
+
export class Credentials {
|
|
16
|
+
#configDir;
|
|
17
|
+
#storage;
|
|
18
|
+
#keyring;
|
|
19
|
+
#env;
|
|
20
|
+
#service;
|
|
21
|
+
#envVar;
|
|
22
|
+
#fileName;
|
|
23
|
+
#warn;
|
|
24
|
+
#warned = false;
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.#configDir = options.configDir;
|
|
27
|
+
this.#storage = options.storage ?? "auto";
|
|
28
|
+
this.#keyring = options.keyring ?? systemKeyring;
|
|
29
|
+
this.#env = options.env ?? process.env;
|
|
30
|
+
this.#envVar = options.envVar;
|
|
31
|
+
this.#fileName = options.fileName ?? "credentials.json";
|
|
32
|
+
this.#warn = options.warn ?? ((message) => process.stderr.write(`${message}\n`));
|
|
33
|
+
this.#service = keyringService(options.service, options.configDir, options.isolated);
|
|
34
|
+
}
|
|
35
|
+
read(account) {
|
|
36
|
+
const fromEnv = this.#envVar ? this.#env[this.#envVar]?.trim() : undefined;
|
|
37
|
+
if (fromEnv)
|
|
38
|
+
return { secret: fromEnv, source: "environment" };
|
|
39
|
+
if (this.#storage !== "file") {
|
|
40
|
+
const fromKeyring = this.#tryKeyring(() => this.#keyring.get(this.#service, account));
|
|
41
|
+
if (fromKeyring)
|
|
42
|
+
return { secret: fromKeyring, source: "keyring" };
|
|
43
|
+
}
|
|
44
|
+
const fromFile = this.#readFile()?.[account]?.secret;
|
|
45
|
+
return fromFile ? { secret: fromFile, source: "file" } : undefined;
|
|
46
|
+
}
|
|
47
|
+
write(account, secret) {
|
|
48
|
+
if (this.#storage !== "file") {
|
|
49
|
+
const stored = this.#tryKeyring(() => {
|
|
50
|
+
this.#keyring.set(this.#service, account, secret);
|
|
51
|
+
return true;
|
|
52
|
+
});
|
|
53
|
+
if (stored)
|
|
54
|
+
return "keyring";
|
|
55
|
+
}
|
|
56
|
+
const store = this.#readFile();
|
|
57
|
+
if (!store) {
|
|
58
|
+
throw new CliError("configuration_error", `${this.#path()} cannot be read as a credentials file; storing now would replace every secret in it. ` +
|
|
59
|
+
"Fix the file or move it aside, then try again.");
|
|
60
|
+
}
|
|
61
|
+
store[account] = { secret };
|
|
62
|
+
this.#writeFile(store);
|
|
63
|
+
return "file";
|
|
64
|
+
}
|
|
65
|
+
remove(account) {
|
|
66
|
+
const removed = [];
|
|
67
|
+
if (this.#storage !== "file" && this.#tryKeyring(() => this.#keyring.delete(this.#service, account))) {
|
|
68
|
+
removed.push("keyring");
|
|
69
|
+
}
|
|
70
|
+
const store = this.#readFile();
|
|
71
|
+
if (store?.[account] !== undefined) {
|
|
72
|
+
delete store[account];
|
|
73
|
+
this.#writeFile(store);
|
|
74
|
+
removed.push("file");
|
|
75
|
+
}
|
|
76
|
+
return removed;
|
|
77
|
+
}
|
|
78
|
+
#path() {
|
|
79
|
+
return join(this.#configDir, this.#fileName);
|
|
80
|
+
}
|
|
81
|
+
/** `undefined` when the file exists but is not a store: writing would replace what it holds. */
|
|
82
|
+
#readFile() {
|
|
83
|
+
let text;
|
|
84
|
+
try {
|
|
85
|
+
text = readFileSync(this.#path(), "utf8");
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (error.code === "ENOENT")
|
|
89
|
+
return {};
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const parsed = JSON.parse(text);
|
|
94
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : undefined;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// The parser's message quotes the file, and the file holds secrets.
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
#writeFile(store) {
|
|
102
|
+
writeSecurely(this.#path(), `${JSON.stringify(store, null, 2)}\n`, 0o600);
|
|
103
|
+
}
|
|
104
|
+
#tryKeyring(operation) {
|
|
105
|
+
if (this.#storage === "keyring")
|
|
106
|
+
return operation();
|
|
107
|
+
try {
|
|
108
|
+
return operation();
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (!this.#warned) {
|
|
112
|
+
this.#warned = true;
|
|
113
|
+
this.#warn(`the OS keyring is unavailable (${error instanceof Error ? error.message : String(error)}); ` +
|
|
114
|
+
`storing in ${this.#path()} instead, readable only by you`);
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=credentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../src/credentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAqB,aAAa,EAAE,MAAM,cAAc,CAAA;AAE/D,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,CAAU,CAAA;AAmCtE,MAAM,CAA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AAC,IAAI,CAAC,CAAA;YACxC,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAE,MAAoB,CAAC,CAAC,CAAC,SAAS,CAAA;QACpH,CAAC;QAAC,MAAM,CAAC;YACP,oEAAoE;YACpE,OAAO,SAAS,CAAA;QAClB,CAAC;IACH,CAAC;IAED,UAAU,CAAC,KAAgB;QACzB,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;IAC3E,CAAC;IAED,WAAW,CAAI,SAAkB;QAC/B,IAAI,IAAI,CAAC,QAAQ,KAAK,SAAS;YAAE,OAAO,SAAS,EAAE,CAAA;QAEnD,IAAI,CAAC;YACH,OAAO,SAAS,EAAE,CAAA;QACpB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBAClB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;gBACnB,IAAI,CAAC,KAAK,CACR,kCAAkC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK;oBAC3F,cAAc,IAAI,CAAC,KAAK,EAAE,gCAAgC,CAC7D,CAAA;YACH,CAAC;YACD,OAAO,SAAS,CAAA;QAClB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../../src/dev/bin.ts"],"names":[],"mappings":""}
|
package/dist/dev/bin.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { main } from "./main.js";
|
|
3
|
+
process.exitCode = await main(process.argv.slice(2), {
|
|
4
|
+
cwd: process.cwd(),
|
|
5
|
+
out: (text) => process.stdout.write(`${text}\n`),
|
|
6
|
+
err: (text) => process.stderr.write(`${text}\n`),
|
|
7
|
+
});
|
|
8
|
+
//# sourceMappingURL=bin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../../src/dev/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,OAAO,CAAC,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;IACnD,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;IAClB,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,IAAI,CAAC;IAChD,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,IAAI,CAAC;CACjD,CAAC,CAAA"}
|