@moxo/cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.md +61 -2
- package/dist/args.d.ts +25 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +71 -0
- package/dist/args.js.map +1 -0
- package/dist/catalog.d.ts +30 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +33 -0
- package/dist/catalog.js.map +1 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +15 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/api.d.ts +18 -0
- package/dist/commands/api.d.ts.map +1 -0
- package/dist/commands/api.js +84 -0
- package/dist/commands/api.js.map +1 -0
- package/dist/commands/auth.d.ts +16 -0
- package/dist/commands/auth.d.ts.map +1 -0
- package/dist/commands/auth.js +173 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/generated.d.ts +31 -0
- package/dist/commands/generated.d.ts.map +1 -0
- package/dist/commands/generated.js +147 -0
- package/dist/commands/generated.js.map +1 -0
- package/dist/commands/help.d.ts +34 -0
- package/dist/commands/help.d.ts.map +1 -0
- package/dist/commands/help.js +117 -0
- package/dist/commands/help.js.map +1 -0
- package/dist/commands/templates.d.ts +59 -0
- package/dist/commands/templates.d.ts.map +1 -0
- package/dist/commands/templates.js +388 -0
- package/dist/commands/templates.js.map +1 -0
- package/dist/config.d.ts +63 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +104 -0
- package/dist/config.js.map +1 -0
- package/dist/confirm.d.ts +11 -0
- package/dist/confirm.d.ts.map +1 -0
- package/dist/confirm.js +21 -0
- package/dist/confirm.js.map +1 -0
- package/dist/context.d.ts +53 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +129 -0
- package/dist/context.js.map +1 -0
- package/dist/credentials.d.ts +50 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +148 -0
- package/dist/credentials.js.map +1 -0
- package/dist/errors.d.ts +43 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/fields.d.ts +49 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/fields.js +280 -0
- package/dist/fields.js.map +1 -0
- package/dist/main.d.ts +20 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +189 -0
- package/dist/main.js.map +1 -0
- package/dist/oauth.d.ts +62 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +156 -0
- package/dist/oauth.js.map +1 -0
- package/dist/render.d.ts +10 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +46 -0
- package/dist/render.js.map +1 -0
- package/dist/session.d.ts +91 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +235 -0
- package/dist/session.js.map +1 -0
- package/dist/spec.d.ts +72 -0
- package/dist/spec.d.ts.map +1 -0
- package/dist/spec.js +171 -0
- package/dist/spec.js.map +1 -0
- package/dist/version.d.ts +7 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +7 -0
- package/dist/version.js.map +1 -0
- package/npm-shrinkwrap.json +3868 -0
- package/package.json +46 -4
package/dist/config.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profiles: one sign-in each, to one organization. The file holds no token
|
|
3
|
+
* (credentials.ts keeps those), only where each profile signs in and who it
|
|
4
|
+
* is, so `whoami` and the line before a write can name the organization.
|
|
5
|
+
*
|
|
6
|
+
* ~/.config/moxo/config.json (MOXO_CONFIG_DIR; %APPDATA%\moxo on Windows)
|
|
7
|
+
* ~/.cache/moxo/ (MOXO_CACHE_DIR; the API description)
|
|
8
|
+
*/
|
|
9
|
+
import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { homedir } from 'node:os';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { usageError } from './errors.js';
|
|
13
|
+
export const DEFAULT_HOST = 'https://api.moxo.com';
|
|
14
|
+
/**
|
|
15
|
+
* Each region's API host, which is also its sign-in issuer. The same list as
|
|
16
|
+
* infra/cells.json (`key` → `https://<healthHost>`); a test holds them equal.
|
|
17
|
+
*/
|
|
18
|
+
export const REGION_HOSTS = {
|
|
19
|
+
us: 'https://api.moxo.com',
|
|
20
|
+
eu: 'https://api.eu.moxo.com',
|
|
21
|
+
au: 'https://api.au.moxo.com',
|
|
22
|
+
sg: 'https://api.sg.moxo.com',
|
|
23
|
+
};
|
|
24
|
+
/** The host a run names (--host, --region, then MOXO_HOST), or undefined when it names none. */
|
|
25
|
+
export function requestedHost(flags, env) {
|
|
26
|
+
if (flags.host && flags.region)
|
|
27
|
+
throw usageError('Give --host or --region, not both.');
|
|
28
|
+
if (flags.region) {
|
|
29
|
+
const host = REGION_HOSTS[flags.region.toLowerCase()];
|
|
30
|
+
if (!host)
|
|
31
|
+
throw usageError(`"${flags.region}" is not a region: use ${Object.keys(REGION_HOSTS).join(', ').replace(/, (\w+)$/, ' or $1')}.`);
|
|
32
|
+
return host;
|
|
33
|
+
}
|
|
34
|
+
const raw = flags.host ?? env.MOXO_HOST;
|
|
35
|
+
return raw ? normalizeHost(raw) : undefined;
|
|
36
|
+
}
|
|
37
|
+
/** The host a run names, or the default. */
|
|
38
|
+
export function hostFor(flags, env) {
|
|
39
|
+
return requestedHost(flags, env) ?? DEFAULT_HOST;
|
|
40
|
+
}
|
|
41
|
+
export const DEFAULT_PROFILE = 'default';
|
|
42
|
+
export function configDir(ctx) {
|
|
43
|
+
if (ctx.env.MOXO_CONFIG_DIR)
|
|
44
|
+
return ctx.env.MOXO_CONFIG_DIR;
|
|
45
|
+
if (ctx.platform === 'win32' && ctx.env.APPDATA)
|
|
46
|
+
return join(ctx.env.APPDATA, 'moxo');
|
|
47
|
+
return join(ctx.env.XDG_CONFIG_HOME || join(ctx.env.HOME || homedir(), '.config'), 'moxo');
|
|
48
|
+
}
|
|
49
|
+
export function cacheDir(ctx) {
|
|
50
|
+
if (ctx.env.MOXO_CACHE_DIR)
|
|
51
|
+
return ctx.env.MOXO_CACHE_DIR;
|
|
52
|
+
if (ctx.platform === 'win32' && ctx.env.LOCALAPPDATA)
|
|
53
|
+
return join(ctx.env.LOCALAPPDATA, 'moxo', 'cache');
|
|
54
|
+
return join(ctx.env.XDG_CACHE_HOME || join(ctx.env.HOME || homedir(), '.cache'), 'moxo');
|
|
55
|
+
}
|
|
56
|
+
/** Makes the folder, readable only by its owner (and makes it so if it was not). */
|
|
57
|
+
export function privateDir(dir) {
|
|
58
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
59
|
+
chmodSync(dir, 0o700);
|
|
60
|
+
}
|
|
61
|
+
/** Writes a file whole or not at all, readable only by its owner. */
|
|
62
|
+
export function writePrivateFile(path, text) {
|
|
63
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
64
|
+
writeFileSync(tmp, text, { mode: 0o600 });
|
|
65
|
+
renameSync(tmp, path);
|
|
66
|
+
}
|
|
67
|
+
export function loadConfig(ctx) {
|
|
68
|
+
try {
|
|
69
|
+
const parsed = JSON.parse(readFileSync(join(configDir(ctx), 'config.json'), 'utf8'));
|
|
70
|
+
return { defaultProfile: parsed.defaultProfile, profiles: parsed.profiles ?? {} };
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return { profiles: {} };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export function saveConfig(ctx, config) {
|
|
77
|
+
const dir = configDir(ctx);
|
|
78
|
+
privateDir(dir);
|
|
79
|
+
writePrivateFile(join(dir, 'config.json'), `${JSON.stringify(config, null, 2)}\n`);
|
|
80
|
+
}
|
|
81
|
+
const PROFILE_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
82
|
+
/** The profile this run uses: --profile, then MOXO_PROFILE, then the default one. */
|
|
83
|
+
export function profileName(ctx, flag, config) {
|
|
84
|
+
const name = flag ?? ctx.env.MOXO_PROFILE ?? config.defaultProfile ?? DEFAULT_PROFILE;
|
|
85
|
+
if (!PROFILE_NAME.test(name))
|
|
86
|
+
throw usageError(`"${name}" can't be a profile name: use letters, digits, "-" and "_".`);
|
|
87
|
+
return name;
|
|
88
|
+
}
|
|
89
|
+
/** A host as the CLI keeps it: https (http only for this machine), no path, no trailing slash. */
|
|
90
|
+
export function normalizeHost(raw) {
|
|
91
|
+
let url;
|
|
92
|
+
try {
|
|
93
|
+
url = new URL(raw.includes('://') ? raw : `https://${raw}`);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
throw usageError(`"${raw}" is not a Moxo address. Give one like https://api.moxo.com.`);
|
|
97
|
+
}
|
|
98
|
+
const local = url.hostname === 'localhost' || url.hostname === '127.0.0.1' || url.hostname === '[::1]';
|
|
99
|
+
if (url.protocol !== 'https:' && !(url.protocol === 'http:' && local)) {
|
|
100
|
+
throw usageError(`${url.origin} is not https. The CLI sends your token only over https (http only to this machine).`);
|
|
101
|
+
}
|
|
102
|
+
return url.origin;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,MAAM,YAAY,GAAG,sBAAsB,CAAC;AAEnD;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAqC;IAC5D,EAAE,EAAE,sBAAsB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AAC,CAAC,CAAC,WAAW,GAAG,EAAE,CAAC,CAAC;IAC9D,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,UAAU,CAAC,IAAI,GAAG,8DAA8D,CAAC,CAAC;IAC1F,CAAC;IACD,MAAM,KAAK,GAAG,GAAG,CAAC,QAAQ,KAAK,WAAW,IAAI,GAAG,CAAC,QAAQ,KAAK,WAAW,IAAI,GAAG,CAAC,QAAQ,KAAK,OAAO,CAAC;IACvG,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,CAAC,GAAG,CAAC,QAAQ,KAAK,OAAO,IAAI,KAAK,CAAC,EAAE,CAAC;QACtE,MAAM,UAAU,CAAC,GAAG,GAAG,CAAC,MAAM,sFAAsF,CAAC,CAAC;IACxH,CAAC;IACD,OAAO,GAAG,CAAC,MAAM,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The second generic rule: a call that sends messages to people asks first.
|
|
3
|
+
*
|
|
4
|
+
* With a terminal, the CLI says what the call does and asks; anything but
|
|
5
|
+
* "y" or "yes" stops it. Without one (CI, an agent, a script), it is refused
|
|
6
|
+
* (exit 1, CONFIRMATION_REQUIRED) unless --yes was given: nothing is sent by
|
|
7
|
+
* default.
|
|
8
|
+
*/
|
|
9
|
+
import type { Context, Output } from './context.js';
|
|
10
|
+
export declare function confirmSending(ctx: Context, out: Output, what: string, yes: boolean): Promise<void>;
|
|
11
|
+
//# sourceMappingURL=confirm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"confirm.d.ts","sourceRoot":"","sources":["../src/confirm.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGpD,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAQzG"}
|
package/dist/confirm.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The second generic rule: a call that sends messages to people asks first.
|
|
3
|
+
*
|
|
4
|
+
* With a terminal, the CLI says what the call does and asks; anything but
|
|
5
|
+
* "y" or "yes" stops it. Without one (CI, an agent, a script), it is refused
|
|
6
|
+
* (exit 1, CONFIRMATION_REQUIRED) unless --yes was given: nothing is sent by
|
|
7
|
+
* default.
|
|
8
|
+
*/
|
|
9
|
+
import { CliError, EXIT } from './errors.js';
|
|
10
|
+
export async function confirmSending(ctx, out, what, yes) {
|
|
11
|
+
if (yes)
|
|
12
|
+
return;
|
|
13
|
+
if (!ctx.terminal.interactive) {
|
|
14
|
+
throw new CliError(`${what} This sends messages to people, so it needs a yes. Nothing was sent: to go ahead, pass --yes.`, EXIT.FAILED, 'CONFIRMATION_REQUIRED');
|
|
15
|
+
}
|
|
16
|
+
out.err(`${what} This sends messages to people.`);
|
|
17
|
+
const answer = (await ctx.terminal.ask('Go ahead? [y/N] ')).trim().toLowerCase();
|
|
18
|
+
if (answer !== 'y' && answer !== 'yes')
|
|
19
|
+
throw new CliError('Stopped. Nothing was sent.', EXIT.FAILED, 'NOT_CONFIRMED');
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=confirm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"confirm.js","sourceRoot":"","sources":["../src/confirm.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,GAAY,EAAE,GAAW,EAAE,IAAY,EAAE,GAAY;IACxF,IAAI,GAAG;QAAE,OAAO;IAChB,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC9B,MAAM,IAAI,QAAQ,CAAC,GAAG,IAAI,+FAA+F,EAAE,IAAI,CAAC,MAAM,EAAE,uBAAuB,CAAC,CAAC;IACnK,CAAC;IACD,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,iCAAiC,CAAC,CAAC;IAClD,MAAM,MAAM,GAAG,CAAC,MAAM,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACjF,IAAI,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,KAAK;QAAE,MAAM,IAAI,QAAQ,CAAC,4BAA4B,EAAE,IAAI,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;AACzH,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything a command reads from or writes to the world, in one object, so
|
|
3
|
+
* tests run the real commands with a fake terminal, a temporary home and a
|
|
4
|
+
* stub server.
|
|
5
|
+
*
|
|
6
|
+
* Output goes through `out` and `err`, which take the token out of anything
|
|
7
|
+
* they print: the CLI never prints a token, even one a server echoes back.
|
|
8
|
+
*/
|
|
9
|
+
export interface Terminal {
|
|
10
|
+
/** Standard input is a terminal: the CLI may ask a question. */
|
|
11
|
+
readonly interactive: boolean;
|
|
12
|
+
/** All of standard input, for `login --token` and `--input -`. */
|
|
13
|
+
readAll(): Promise<string>;
|
|
14
|
+
/** Asks one question on standard error and reads the answer; `hidden` does not echo it. */
|
|
15
|
+
ask(question: string, opts?: {
|
|
16
|
+
hidden?: boolean;
|
|
17
|
+
}): Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
export interface Context {
|
|
20
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
21
|
+
readonly cwd: string;
|
|
22
|
+
readonly terminal: Terminal;
|
|
23
|
+
readonly stdout: (data: string | Uint8Array) => void;
|
|
24
|
+
readonly stderr: (text: string) => void;
|
|
25
|
+
readonly fetch: typeof fetch;
|
|
26
|
+
readonly now: () => number;
|
|
27
|
+
readonly sleep: (ms: number) => Promise<void>;
|
|
28
|
+
readonly openBrowser: (url: string) => void;
|
|
29
|
+
readonly platform: NodeJS.Platform;
|
|
30
|
+
}
|
|
31
|
+
/** Writes through the context, with every secret the run knows replaced. */
|
|
32
|
+
export declare class Output {
|
|
33
|
+
private readonly ctx;
|
|
34
|
+
private readonly secrets;
|
|
35
|
+
constructor(ctx: Pick<Context, 'stdout' | 'stderr'>);
|
|
36
|
+
/** Never print this value. */
|
|
37
|
+
hide(secret: string | undefined): void;
|
|
38
|
+
private clean;
|
|
39
|
+
out(text: string): void;
|
|
40
|
+
err(text: string): void;
|
|
41
|
+
json(value: unknown): void;
|
|
42
|
+
}
|
|
43
|
+
/** The program that opens an address in the browser, and its arguments: run with no shell, so the address is one argument and nothing more. */
|
|
44
|
+
export declare function browserCommand(platform: NodeJS.Platform, url: string): [string, string[]];
|
|
45
|
+
/**
|
|
46
|
+
* Whether the CLI may open this sign-in address: https (http only to this
|
|
47
|
+
* machine), on the same site as the server it signs in to (`app.moxo.com`
|
|
48
|
+
* for `api.moxo.com`). Anything else is not opened.
|
|
49
|
+
*/
|
|
50
|
+
export declare function safeToOpen(url: string, issuer: string): boolean;
|
|
51
|
+
/** The real world, for `cli.ts`. */
|
|
52
|
+
export declare function processContext(): Context;
|
|
53
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,MAAM,WAAW,QAAQ;IACvB,gEAAgE;IAChE,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,kEAAkE;IAClE,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC3B,2FAA2F;IAC3F,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA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,cAAc,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAIzF;AASD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAW/D;AAYD,oCAAoC;AACpC,wBAAgB,cAAc,IAAI,OAAO,CAiBxC"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything a command reads from or writes to the world, in one object, so
|
|
3
|
+
* tests run the real commands with a fake terminal, a temporary home and a
|
|
4
|
+
* stub server.
|
|
5
|
+
*
|
|
6
|
+
* Output goes through `out` and `err`, which take the token out of anything
|
|
7
|
+
* they print: the CLI never prints a token, even one a server echoes back.
|
|
8
|
+
*/
|
|
9
|
+
import { execFile } from 'node:child_process';
|
|
10
|
+
import { createInterface } from 'node:readline';
|
|
11
|
+
import { Writable } from 'node:stream';
|
|
12
|
+
/** Writes through the context, with every secret the run knows replaced. */
|
|
13
|
+
export class Output {
|
|
14
|
+
ctx;
|
|
15
|
+
secrets = new Set();
|
|
16
|
+
constructor(ctx) {
|
|
17
|
+
this.ctx = ctx;
|
|
18
|
+
}
|
|
19
|
+
/** Never print this value. */
|
|
20
|
+
hide(secret) {
|
|
21
|
+
if (secret && secret.length >= 8)
|
|
22
|
+
this.secrets.add(secret);
|
|
23
|
+
}
|
|
24
|
+
clean(text) {
|
|
25
|
+
let out = text;
|
|
26
|
+
for (const s of this.secrets)
|
|
27
|
+
out = out.split(s).join('[token]');
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
out(text) {
|
|
31
|
+
this.ctx.stdout(this.clean(text.endsWith('\n') ? text : `${text}\n`));
|
|
32
|
+
}
|
|
33
|
+
err(text) {
|
|
34
|
+
this.ctx.stderr(this.clean(text.endsWith('\n') ? text : `${text}\n`));
|
|
35
|
+
}
|
|
36
|
+
json(value) {
|
|
37
|
+
this.out(JSON.stringify(value, null, 2));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function readAllStdin() {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const chunks = [];
|
|
43
|
+
process.stdin.on('data', (c) => chunks.push(c));
|
|
44
|
+
process.stdin.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
45
|
+
process.stdin.on('error', reject);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function askOnTerminal(question, hidden) {
|
|
49
|
+
let muted = false;
|
|
50
|
+
const output = new Writable({
|
|
51
|
+
write(chunk, _enc, cb) {
|
|
52
|
+
if (!muted)
|
|
53
|
+
process.stderr.write(chunk);
|
|
54
|
+
cb();
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const rl = createInterface({ input: process.stdin, output, terminal: true });
|
|
58
|
+
return new Promise((resolve) => {
|
|
59
|
+
rl.question(question, (answer) => {
|
|
60
|
+
rl.close();
|
|
61
|
+
if (hidden)
|
|
62
|
+
process.stderr.write('\n');
|
|
63
|
+
resolve(answer);
|
|
64
|
+
});
|
|
65
|
+
muted = hidden;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** The program that opens an address in the browser, and its arguments: run with no shell, so the address is one argument and nothing more. */
|
|
69
|
+
export function browserCommand(platform, url) {
|
|
70
|
+
if (platform === 'darwin')
|
|
71
|
+
return ['open', [url]];
|
|
72
|
+
if (platform === 'win32')
|
|
73
|
+
return ['rundll32', ['url.dll,FileProtocolHandler', url]];
|
|
74
|
+
return ['xdg-open', [url]];
|
|
75
|
+
}
|
|
76
|
+
const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
|
|
77
|
+
/** The last two labels of a host name (`app.eu.moxo.com` → `moxo.com`): the site it belongs to. */
|
|
78
|
+
function siteOf(hostname) {
|
|
79
|
+
return hostname.split('.').slice(-2).join('.');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether the CLI may open this sign-in address: https (http only to this
|
|
83
|
+
* machine), on the same site as the server it signs in to (`app.moxo.com`
|
|
84
|
+
* for `api.moxo.com`). Anything else is not opened.
|
|
85
|
+
*/
|
|
86
|
+
export function safeToOpen(url, issuer) {
|
|
87
|
+
let u;
|
|
88
|
+
let i;
|
|
89
|
+
try {
|
|
90
|
+
u = new URL(url);
|
|
91
|
+
i = new URL(issuer);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (LOOPBACK.has(u.hostname) || LOOPBACK.has(i.hostname))
|
|
97
|
+
return u.origin === i.origin || (u.protocol === 'http:' && u.hostname === i.hostname);
|
|
98
|
+
return u.protocol === 'https:' && siteOf(u.hostname) === siteOf(i.hostname);
|
|
99
|
+
}
|
|
100
|
+
function openInBrowser(url) {
|
|
101
|
+
const [cmd, args] = browserCommand(process.platform, url);
|
|
102
|
+
try {
|
|
103
|
+
const child = execFile(cmd, args, { windowsHide: true }, () => undefined);
|
|
104
|
+
child.unref();
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
// No browser: the code and the address are printed anyway.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** The real world, for `cli.ts`. */
|
|
111
|
+
export function processContext() {
|
|
112
|
+
return {
|
|
113
|
+
env: process.env,
|
|
114
|
+
cwd: process.cwd(),
|
|
115
|
+
terminal: {
|
|
116
|
+
interactive: Boolean(process.stdin.isTTY),
|
|
117
|
+
readAll: readAllStdin,
|
|
118
|
+
ask: (q, opts) => askOnTerminal(q, Boolean(opts?.hidden)),
|
|
119
|
+
},
|
|
120
|
+
stdout: (t) => process.stdout.write(t),
|
|
121
|
+
stderr: (t) => process.stderr.write(t),
|
|
122
|
+
fetch: globalThis.fetch,
|
|
123
|
+
now: () => Date.now(),
|
|
124
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
125
|
+
openBrowser: openInBrowser,
|
|
126
|
+
platform: process.platform,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAwBvC,4EAA4E;AAC5E,MAAM,OAAO,MAAM;IAGY;IAFZ,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAE7C,YAA6B,GAAuC;QAAvC,QAAG,GAAH,GAAG,CAAoC;IAAG,CAAC;IAExE,8BAA8B;IAC9B,IAAI,CAAC,MAA0B;QAC7B,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,IAAI,CAAC;YAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC7D,CAAC;IAEO,KAAK,CAAC,IAAY;QACxB,IAAI,GAAG,GAAG,IAAI,CAAC;QACf,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,OAAO;YAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACjE,OAAO,GAAG,CAAC;IACb,CAAC;IAED,GAAG,CAAC,IAAY;QACd,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC;IACxE,CAAC;IAED,GAAG,CAAC,IAAY;QACd,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,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AAC,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACtC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACtC,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE;QACrB,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACpD,WAAW,EAAE,aAAa;QAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ;KAC3B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a profile's token is kept, and how a run finds the token it uses.
|
|
3
|
+
*
|
|
4
|
+
* The token goes to the system keychain where there is one: macOS Keychain
|
|
5
|
+
* (`security`), or the Secret Service on Linux (`secret-tool`). Both get the
|
|
6
|
+
* token on standard input, never as an argument, so it is never in a process
|
|
7
|
+
* list. Elsewhere, or with MOXO_CREDENTIAL_STORE=file, it goes to
|
|
8
|
+
* credentials.json beside the config, readable only by its owner (as gh and
|
|
9
|
+
* the AWS CLI keep theirs).
|
|
10
|
+
*
|
|
11
|
+
* MOXO_TOKEN, when set, is used instead of any stored token: CI keeps a
|
|
12
|
+
* person's personal access token in a secret and sets it.
|
|
13
|
+
*/
|
|
14
|
+
import type { Context } from './context.js';
|
|
15
|
+
import { type StoreKind } from './config.js';
|
|
16
|
+
export interface StoredCredential {
|
|
17
|
+
/** The bearer token: a personal access token, or an OAuth access token. */
|
|
18
|
+
token: string;
|
|
19
|
+
refreshToken?: string;
|
|
20
|
+
/** When the access token stops working, in ms since the epoch. */
|
|
21
|
+
expiresAt?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface CredentialStore {
|
|
24
|
+
readonly kind: StoreKind;
|
|
25
|
+
get(profile: string): Promise<StoredCredential | undefined>;
|
|
26
|
+
set(profile: string, credential: StoredCredential): Promise<void>;
|
|
27
|
+
delete(profile: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The one line `security -i` reads to save a credential. Only a profile name
|
|
31
|
+
* (letters, digits, `-`, `_`) and base64 go into it, checked here, so nothing
|
|
32
|
+
* in a token can end the line or start another command.
|
|
33
|
+
*/
|
|
34
|
+
export declare function keychainInput(profile: string, credential: StoredCredential): string;
|
|
35
|
+
/** The store a profile's token is kept in. */
|
|
36
|
+
export declare function credentialStore(ctx: Pick<Context, 'env' | 'platform'>, kind: StoreKind): CredentialStore;
|
|
37
|
+
export interface Saved {
|
|
38
|
+
store: CredentialStore;
|
|
39
|
+
/** The file, though a keychain was tried: there was none to use. */
|
|
40
|
+
fellBack: boolean;
|
|
41
|
+
/** Where the file is, when the token went to it. */
|
|
42
|
+
path?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Saves a new token in the system keychain, or in the file when there is no
|
|
46
|
+
* keychain (or MOXO_CREDENTIAL_STORE=file). Answers the store that kept it,
|
|
47
|
+
* and whether that was a fallback, so the CLI can say where the token went.
|
|
48
|
+
*/
|
|
49
|
+
export declare function saveCredential(ctx: Pick<Context, 'env' | 'platform'>, profile: string, credential: StoredCredential, keychainFor?: () => CredentialStore): Promise<Saved>;
|
|
50
|
+
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../src/credentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAKH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAA2C,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAEtF,MAAM,WAAW,gBAAgB;IAC/B,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kEAAkE;IAClE,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;IAC5D,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAClE,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxC;AAWD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,gBAAgB,GAAG,MAAM,CAInF;AAwGD,8CAA8C;AAC9C,wBAAgB,eAAe,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,GAAG,UAAU,CAAC,EAAE,IAAI,EAAE,SAAS,GAAG,eAAe,CAGxG;AAED,MAAM,WAAW,KAAK;IACpB,KAAK,EAAE,eAAe,CAAC;IACvB,oEAAoE;IACpE,QAAQ,EAAE,OAAO,CAAC;IAClB,oDAAoD;IACpD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,GAAG,UAAU,CAAC,EACtC,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,gBAAgB,EAC5B,WAAW,GAAE,MAAM,eAAwD,GAC1E,OAAO,CAAC,KAAK,CAAC,CAchB"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a profile's token is kept, and how a run finds the token it uses.
|
|
3
|
+
*
|
|
4
|
+
* The token goes to the system keychain where there is one: macOS Keychain
|
|
5
|
+
* (`security`), or the Secret Service on Linux (`secret-tool`). Both get the
|
|
6
|
+
* token on standard input, never as an argument, so it is never in a process
|
|
7
|
+
* list. Elsewhere, or with MOXO_CREDENTIAL_STORE=file, it goes to
|
|
8
|
+
* credentials.json beside the config, readable only by its owner (as gh and
|
|
9
|
+
* the AWS CLI keep theirs).
|
|
10
|
+
*
|
|
11
|
+
* MOXO_TOKEN, when set, is used instead of any stored token: CI keeps a
|
|
12
|
+
* person's personal access token in a secret and sets it.
|
|
13
|
+
*/
|
|
14
|
+
import { spawn } from 'node:child_process';
|
|
15
|
+
import { readFileSync } from 'node:fs';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
import { configDir, privateDir, writePrivateFile } from './config.js';
|
|
18
|
+
const SERVICE = 'moxo-cli';
|
|
19
|
+
function encode(c) {
|
|
20
|
+
return Buffer.from(JSON.stringify(c), 'utf8').toString('base64');
|
|
21
|
+
}
|
|
22
|
+
const SAFE_PROFILE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
23
|
+
const BASE64 = /^[A-Za-z0-9+/]+=*$/;
|
|
24
|
+
/**
|
|
25
|
+
* The one line `security -i` reads to save a credential. Only a profile name
|
|
26
|
+
* (letters, digits, `-`, `_`) and base64 go into it, checked here, so nothing
|
|
27
|
+
* in a token can end the line or start another command.
|
|
28
|
+
*/
|
|
29
|
+
export function keychainInput(profile, credential) {
|
|
30
|
+
const value = encode(credential);
|
|
31
|
+
if (!SAFE_PROFILE.test(profile) || !BASE64.test(value))
|
|
32
|
+
throw new Error('A profile name or a token the keychain command can\'t take safely.');
|
|
33
|
+
return `add-generic-password -U -a ${profile} -s ${SERVICE} -l "Moxo CLI" -w ${value}\n`;
|
|
34
|
+
}
|
|
35
|
+
function decode(text) {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(Buffer.from(text.trim(), 'base64').toString('utf8'));
|
|
38
|
+
return typeof parsed.token === 'string' && parsed.token.length > 0 ? parsed : undefined;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Runs a program with `input` on standard input; never passes a secret as an argument. */
|
|
45
|
+
function run(cmd, args, input) {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
const child = spawn(cmd, args, { stdio: ['pipe', 'pipe', 'ignore'] });
|
|
48
|
+
let stdout = '';
|
|
49
|
+
child.stdout.on('data', (d) => (stdout += d.toString('utf8')));
|
|
50
|
+
child.on('error', reject);
|
|
51
|
+
child.on('close', (code) => resolve({ code, stdout }));
|
|
52
|
+
child.stdin.end(input ?? '');
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
class MacKeychain {
|
|
56
|
+
kind = 'keychain';
|
|
57
|
+
async get(profile) {
|
|
58
|
+
const r = await run('security', ['find-generic-password', '-a', profile, '-s', SERVICE, '-w']);
|
|
59
|
+
return r.code === 0 ? decode(r.stdout) : undefined;
|
|
60
|
+
}
|
|
61
|
+
async set(profile, credential) {
|
|
62
|
+
// `security -i` reads its commands from standard input: the token never
|
|
63
|
+
// appears in an argument list. Base64 keeps the line free of quotes.
|
|
64
|
+
const r = await run('security', ['-i'], keychainInput(profile, credential));
|
|
65
|
+
if (r.code !== 0)
|
|
66
|
+
throw new Error('The keychain refused to save the token.');
|
|
67
|
+
}
|
|
68
|
+
async delete(profile) {
|
|
69
|
+
await run('security', ['delete-generic-password', '-a', profile, '-s', SERVICE]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
class SecretService {
|
|
73
|
+
kind = 'keychain';
|
|
74
|
+
async get(profile) {
|
|
75
|
+
const r = await run('secret-tool', ['lookup', 'service', SERVICE, 'account', profile]);
|
|
76
|
+
return r.code === 0 ? decode(r.stdout) : undefined;
|
|
77
|
+
}
|
|
78
|
+
async set(profile, credential) {
|
|
79
|
+
const r = await run('secret-tool', ['store', '--label=Moxo CLI', 'service', SERVICE, 'account', profile], encode(credential));
|
|
80
|
+
if (r.code !== 0)
|
|
81
|
+
throw new Error('The Secret Service refused to save the token.');
|
|
82
|
+
}
|
|
83
|
+
async delete(profile) {
|
|
84
|
+
await run('secret-tool', ['clear', 'service', SERVICE, 'account', profile]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
class FileStore {
|
|
88
|
+
dir;
|
|
89
|
+
kind = 'file';
|
|
90
|
+
constructor(dir) {
|
|
91
|
+
this.dir = dir;
|
|
92
|
+
}
|
|
93
|
+
read() {
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(readFileSync(join(this.dir, 'credentials.json'), 'utf8'));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
get path() {
|
|
102
|
+
return join(this.dir, 'credentials.json');
|
|
103
|
+
}
|
|
104
|
+
write(all) {
|
|
105
|
+
privateDir(this.dir);
|
|
106
|
+
writePrivateFile(join(this.dir, 'credentials.json'), `${JSON.stringify(all, null, 2)}\n`);
|
|
107
|
+
}
|
|
108
|
+
async get(profile) {
|
|
109
|
+
const v = this.read()[profile];
|
|
110
|
+
return v ? decode(v) : undefined;
|
|
111
|
+
}
|
|
112
|
+
async set(profile, credential) {
|
|
113
|
+
this.write({ ...this.read(), [profile]: encode(credential) });
|
|
114
|
+
}
|
|
115
|
+
async delete(profile) {
|
|
116
|
+
const all = this.read();
|
|
117
|
+
delete all[profile];
|
|
118
|
+
this.write(all);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** The store a profile's token is kept in. */
|
|
122
|
+
export function credentialStore(ctx, kind) {
|
|
123
|
+
if (kind === 'file')
|
|
124
|
+
return new FileStore(configDir(ctx));
|
|
125
|
+
return ctx.platform === 'darwin' ? new MacKeychain() : new SecretService();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Saves a new token in the system keychain, or in the file when there is no
|
|
129
|
+
* keychain (or MOXO_CREDENTIAL_STORE=file). Answers the store that kept it,
|
|
130
|
+
* and whether that was a fallback, so the CLI can say where the token went.
|
|
131
|
+
*/
|
|
132
|
+
export async function saveCredential(ctx, profile, credential, keychainFor = () => credentialStore(ctx, 'keychain')) {
|
|
133
|
+
const tryKeychain = ctx.env.MOXO_CREDENTIAL_STORE !== 'file' && (ctx.platform === 'darwin' || ctx.platform === 'linux');
|
|
134
|
+
if (tryKeychain) {
|
|
135
|
+
const keychain = keychainFor();
|
|
136
|
+
try {
|
|
137
|
+
await keychain.set(profile, credential);
|
|
138
|
+
return { store: keychain, fellBack: false };
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// No keychain on this machine (a server, a container): keep it in the file.
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const file = new FileStore(configDir(ctx));
|
|
145
|
+
await file.set(profile, credential);
|
|
146
|
+
return { store: file, fellBack: tryKeychain, path: file.path };
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=credentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../src/credentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,gBAAgB,EAAkB,MAAM,aAAa,CAAC;AAiBtF,MAAM,OAAO,GAAG,UAAU,CAAC;AAE3B,SAAS,MAAM,CAAC,CAAmB;IACjC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACnE,CAAC;AAED,MAAM,YAAY,GAAG,kCAAkC,CAAC;AACxD,MAAM,MAAM,GAAG,oBAAoB,CAAC;AAEpC;;;;GAI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e,EACf,UAA4B,EAC5B,cAAqC,GAAG,EAAE,CAAC,eAAe,CAAC,GAAG,EAAE,UAAU,CAAC;IAE3E,MAAM,WAAW,GAAG,GAAG,CAAC,GAAG,CAAC,qBAAqB,KAAK,MAAM,IAAI,CAAC,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,GAAG,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;IACxH,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAC;QAC/B,IAAI,CAAC;YACH,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YACxC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;QAC9C,CAAC;QAAC,MAAM,CAAC;YACP,4EAA4E;QAC9E,CAAC;IACH,CAAC;IACD,MAAM,IAAI,GAAG,IAAI,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3C,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;IACpC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;AACjE,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exit codes and the one error every command throws.
|
|
3
|
+
*
|
|
4
|
+
* The codes are a contract with scripts and CI (docs: the CLI guide, "Exit
|
|
5
|
+
* codes"), so they never change meaning:
|
|
6
|
+
* 0 done
|
|
7
|
+
* 1 refused: Moxo refused the call, a check failed, the command was
|
|
8
|
+
* wrong, or a call that sends messages to people had no yes (no terminal
|
|
9
|
+
* to ask in and no --yes: the message says to pass --yes). The message
|
|
10
|
+
* says which; with --json, `error.code` does too (CONFIRMATION_REQUIRED).
|
|
11
|
+
* 2 changed since you pulled: the template changed after the file was
|
|
12
|
+
* pulled (`VERSION_CONFLICT`, `REVISION_REQUIRED`). Pull again, make the
|
|
13
|
+
* change in that file, push again.
|
|
14
|
+
* 3 sign in again: no token, a token Moxo refused, or a profile signed in
|
|
15
|
+
* to another host than the one named.
|
|
16
|
+
*/
|
|
17
|
+
export declare const EXIT: {
|
|
18
|
+
readonly OK: 0;
|
|
19
|
+
readonly FAILED: 1;
|
|
20
|
+
readonly CHANGED_SINCE_PULL: 2;
|
|
21
|
+
readonly SIGN_IN: 3;
|
|
22
|
+
};
|
|
23
|
+
export type ExitCode = (typeof EXIT)[keyof typeof EXIT];
|
|
24
|
+
export declare function worstExit(codes: readonly ExitCode[]): ExitCode;
|
|
25
|
+
export declare class CliError extends Error {
|
|
26
|
+
readonly exitCode: ExitCode;
|
|
27
|
+
/** Moxo's error code, when Moxo refused the call. */
|
|
28
|
+
readonly code?: string | undefined;
|
|
29
|
+
readonly details?: unknown | undefined;
|
|
30
|
+
/** A usage line to print after the message. */
|
|
31
|
+
readonly usage?: string | undefined;
|
|
32
|
+
constructor(message: string, exitCode?: ExitCode,
|
|
33
|
+
/** Moxo's error code, when Moxo refused the call. */
|
|
34
|
+
code?: string | undefined, details?: unknown | undefined,
|
|
35
|
+
/** A usage line to print after the message. */
|
|
36
|
+
usage?: string | undefined);
|
|
37
|
+
}
|
|
38
|
+
export declare function usageError(message: string, usage?: string): CliError;
|
|
39
|
+
/** The codes that mean "the template changed since this file was pulled". */
|
|
40
|
+
export declare const CONFLICT_CODES: ReadonlySet<string>;
|
|
41
|
+
/** The exit code for a refusal Moxo answered with this status and code. */
|
|
42
|
+
export declare function exitForRefusal(status: number, code: string | undefined): ExitCode;
|
|
43
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,eAAO,MAAM,IAAI;;;;;CAKP,CAAC;AAEX,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,IAAI,CAAC,CAAC,MAAM,OAAO,IAAI,CAAC,CAAC;AAKxD,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,QAAQ,EAAE,GAAG,QAAQ,CAG9D;AAED,qBAAa,QAAS,SAAQ,KAAK;IAG/B,QAAQ,CAAC,QAAQ,EAAE,QAAQ;IAC3B,qDAAqD;IACrD,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM;IACtB,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO;IAC1B,+CAA+C;IAC/C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM;gBANvB,OAAO,EAAE,MAAM,EACN,QAAQ,GAAE,QAAsB;IACzC,qDAAqD;IAC5C,IAAI,CAAC,EAAE,MAAM,YAAA,EACb,OAAO,CAAC,EAAE,OAAO,YAAA;IAC1B,+CAA+C;IACtC,KAAK,CAAC,EAAE,MAAM,YAAA;CAI1B;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,QAAQ,CAEpE;AAED,6EAA6E;AAC7E,eAAO,MAAM,cAAc,EAAE,WAAW,CAAC,MAAM,CAAsD,CAAC;AAEtG,2EAA2E;AAC3E,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,QAAQ,CAIjF"}
|