@seavideo/cli 0.0.0-stage → 0.3.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.
Files changed (111) hide show
  1. package/LICENSE +12 -0
  2. package/README.md +153 -2
  3. package/README.publish.md +154 -0
  4. package/dist/bin/seavideo.js +11 -0
  5. package/dist/src/atomic-write.js +14 -0
  6. package/dist/src/canvas/api.js +172 -0
  7. package/dist/src/canvas/browser.js +40 -0
  8. package/dist/src/canvas/chrome-binary.js +109 -0
  9. package/dist/src/canvas/daemon.js +264 -0
  10. package/dist/src/canvas/engine-install.js +46 -0
  11. package/dist/src/canvas/engine-log.js +9 -0
  12. package/dist/src/canvas/engine.js +99 -0
  13. package/dist/src/canvas/entry.js +16 -0
  14. package/dist/src/canvas/env-lock.js +164 -0
  15. package/dist/src/canvas/launch.js +97 -0
  16. package/dist/src/canvas/lifecycle.js +65 -0
  17. package/dist/src/canvas/names.js +61 -0
  18. package/dist/src/canvas/params.js +38 -0
  19. package/dist/src/canvas/puppeteer.js +153 -0
  20. package/dist/src/canvas/screenshot.js +23 -0
  21. package/dist/src/canvas/session-record.js +77 -0
  22. package/dist/src/canvas/team.js +97 -0
  23. package/dist/src/canvas/tool-args.js +47 -0
  24. package/dist/src/canvas/tool-session.js +165 -0
  25. package/dist/src/canvas/tool-specs.js +54 -0
  26. package/dist/src/canvas/url.js +38 -0
  27. package/dist/src/cli/args.js +16 -0
  28. package/dist/src/cli/commands/auth.js +51 -0
  29. package/dist/src/cli/commands/canvas/common.js +31 -0
  30. package/dist/src/cli/commands/canvas/engine-choice.js +41 -0
  31. package/dist/src/cli/commands/canvas/index.js +5 -0
  32. package/dist/src/cli/commands/canvas/item.js +233 -0
  33. package/dist/src/cli/commands/canvas/list.js +168 -0
  34. package/dist/src/cli/commands/canvas/register.js +106 -0
  35. package/dist/src/cli/commands/canvas/render.js +229 -0
  36. package/dist/src/cli/commands/canvas/screenshot.js +52 -0
  37. package/dist/src/cli/commands/canvas/session.js +249 -0
  38. package/dist/src/cli/commands/canvas/teams.js +142 -0
  39. package/dist/src/cli/commands/config.js +74 -0
  40. package/dist/src/cli/commands/doctor.js +261 -0
  41. package/dist/src/cli/commands/engine.js +96 -0
  42. package/dist/src/cli/commands/env/create.js +160 -0
  43. package/dist/src/cli/commands/env/fields.js +26 -0
  44. package/dist/src/cli/commands/env/index.js +4 -0
  45. package/dist/src/cli/commands/env/list.js +44 -0
  46. package/dist/src/cli/commands/env/mutate.js +118 -0
  47. package/dist/src/cli/commands/login/flow-token.js +41 -0
  48. package/dist/src/cli/commands/login/index.js +12 -0
  49. package/dist/src/cli/commands/login/oauth.js +69 -0
  50. package/dist/src/cli/commands/login/save.js +26 -0
  51. package/dist/src/cli/commands/logout.js +92 -0
  52. package/dist/src/cli/commands/skill.js +185 -0
  53. package/dist/src/cli/commands/tool.js +279 -0
  54. package/dist/src/cli/commands/update.js +117 -0
  55. package/dist/src/cli/commands/wallet.js +69 -0
  56. package/dist/src/cli/commands/whoami.js +147 -0
  57. package/dist/src/cli/context.js +41 -0
  58. package/dist/src/cli/credential.js +93 -0
  59. package/dist/src/cli/elapsed.js +25 -0
  60. package/dist/src/cli/failure-screenshot.js +24 -0
  61. package/dist/src/cli/flags.js +1 -0
  62. package/dist/src/cli/hints.js +7 -0
  63. package/dist/src/cli/main.js +44 -0
  64. package/dist/src/cli/output-error.js +23 -0
  65. package/dist/src/cli/output.js +47 -0
  66. package/dist/src/cli/program.js +261 -0
  67. package/dist/src/cli/run.js +11 -0
  68. package/dist/src/cli/session.js +63 -0
  69. package/dist/src/config/base-url.js +26 -0
  70. package/dist/src/config/credential-vault.js +115 -0
  71. package/dist/src/config/dotenv.js +59 -0
  72. package/dist/src/config/envname.js +41 -0
  73. package/dist/src/config/paths.js +22 -0
  74. package/dist/src/config/preset-schema.js +67 -0
  75. package/dist/src/config/presets.js +132 -0
  76. package/dist/src/config/resolve.js +41 -0
  77. package/dist/src/config/secret-key.js +63 -0
  78. package/dist/src/config/store-schema.js +143 -0
  79. package/dist/src/config/store.js +273 -0
  80. package/dist/src/config/vault-crypto.js +44 -0
  81. package/dist/src/deps.js +191 -0
  82. package/dist/src/deps.types.js +1 -0
  83. package/dist/src/errors.js +24 -0
  84. package/dist/src/fs-retry.js +34 -0
  85. package/dist/src/i18n.js +1214 -0
  86. package/dist/src/loopback.js +134 -0
  87. package/dist/src/sdk/auth.js +20 -0
  88. package/dist/src/sdk/credential-expiry.js +67 -0
  89. package/dist/src/sdk/discovery.js +90 -0
  90. package/dist/src/sdk/http.js +98 -0
  91. package/dist/src/sdk/identity.js +17 -0
  92. package/dist/src/sdk/jwks.js +119 -0
  93. package/dist/src/sdk/oauth-protocol.js +226 -0
  94. package/dist/src/sdk/oauth.js +179 -0
  95. package/dist/src/sdk/provider/cn.js +34 -0
  96. package/dist/src/sdk/provider/index.js +16 -0
  97. package/dist/src/sdk/provider/oversea.js +41 -0
  98. package/dist/src/sdk/provider/types.js +1 -0
  99. package/dist/src/sdk/refresh.js +66 -0
  100. package/dist/src/semver.js +89 -0
  101. package/dist/src/skill/api.js +176 -0
  102. package/dist/src/text.js +3 -0
  103. package/dist/src/types.js +1 -0
  104. package/dist/src/update/check-cache.js +56 -0
  105. package/dist/src/update/install-layout.js +79 -0
  106. package/dist/src/update/notify.js +15 -0
  107. package/dist/src/update/npm-install.js +64 -0
  108. package/dist/src/update/passive.js +53 -0
  109. package/dist/src/update/registry.js +59 -0
  110. package/dist/src/wallet/api.js +107 -0
  111. package/package.json +59 -4
@@ -0,0 +1,273 @@
1
+ import { CliError } from "../errors.js";
2
+ import { t } from "../i18n.js";
3
+ import { writeFileAtomic } from "../atomic-write.js";
4
+ import { parseEnvConfig, plainConfigOf, credentialSlotsOf } from "./store-schema.js";
5
+ import { hasInlineCredential, mergeStoredCredentials, migrateInlineCredentials, writeSeparated, } from "./credential-vault.js";
6
+ export { parseEnvConfig, serializeEnvConfig } from "./store-schema.js";
7
+ const MODE_DIR = 0o700;
8
+ const MODE_FILE = 0o600;
9
+ function tryParseJson(text) {
10
+ let result;
11
+ try {
12
+ result = JSON.parse(text);
13
+ }
14
+ catch {
15
+ result = undefined;
16
+ }
17
+ return result;
18
+ }
19
+ function asRecord(raw) {
20
+ let result;
21
+ if (typeof raw === 'object' && raw !== null && !Array.isArray(raw)) {
22
+ result = raw;
23
+ }
24
+ else {
25
+ result = null;
26
+ }
27
+ return result;
28
+ }
29
+ function createStoreIo(deps, paths) {
30
+ async function readJson(path) {
31
+ let text;
32
+ try {
33
+ text = await deps.fs.readFile(path, 'utf8');
34
+ }
35
+ catch (err) {
36
+ throw new CliError('config', t('store.readFailed', { path }, deps.env), {
37
+ detail: err.code ?? String(err),
38
+ });
39
+ }
40
+ try {
41
+ return JSON.parse(text);
42
+ }
43
+ catch {
44
+ throw new CliError('config', t('store.jsonParseFailed', { path }, deps.env));
45
+ }
46
+ }
47
+ async function writeJsonAtomic(path, content) {
48
+ try {
49
+ await writeFileAtomic(deps, path, content, MODE_FILE);
50
+ }
51
+ catch (err) {
52
+ throw new CliError('config', t('store.writeFailed', { path }, deps.env), {
53
+ detail: err.code ?? String(err),
54
+ });
55
+ }
56
+ }
57
+ async function ensureEnvDir(env) {
58
+ try {
59
+ await deps.fs.mkdir(paths.envDir(env), { recursive: true, mode: MODE_DIR });
60
+ }
61
+ catch (err) {
62
+ throw new CliError('config', t('store.mkdirFailed', { env }, deps.env), {
63
+ detail: err.code ?? String(err),
64
+ });
65
+ }
66
+ }
67
+ return { deps, paths, readJson, writeJsonAtomic, ensureEnvDir };
68
+ }
69
+ function createReader(io) {
70
+ const { deps, paths } = io;
71
+ async function merged(env, tolerateUnreadable) {
72
+ const raw = await io.readJson(paths.configFile(env));
73
+ const record = asRecord(raw);
74
+ let result;
75
+ if (record === null) {
76
+ result = raw;
77
+ }
78
+ else if (hasInlineCredential(record)) {
79
+ result = await migrateInlineCredentials(deps, paths, env, record);
80
+ }
81
+ else {
82
+ result = await mergeStoredCredentials(deps, paths, env, record, tolerateUnreadable);
83
+ }
84
+ return result;
85
+ }
86
+ async function read(env) {
87
+ return parseEnvConfig(env, await merged(env, false), deps.env);
88
+ }
89
+ async function readForWrite(env) {
90
+ return parseEnvConfig(env, await merged(env, true), deps.env);
91
+ }
92
+ async function readRawFields(env) {
93
+ const record = asRecord(await merged(env, false));
94
+ if (record === null) {
95
+ throw new CliError('config', t('store.notObject', { env }, deps.env));
96
+ }
97
+ return record;
98
+ }
99
+ return { read, readForWrite, readRawFields };
100
+ }
101
+ function createEnvFileOps(io) {
102
+ const { deps, paths } = io;
103
+ const reader = createReader(io);
104
+ async function write(config) {
105
+ await io.ensureEnvDir(config.env);
106
+ await writeSeparated(deps, paths, config.env, plainConfigOf(config), credentialSlotsOf(config));
107
+ }
108
+ async function exists(env) {
109
+ let result;
110
+ try {
111
+ await deps.fs.stat(paths.configFile(env));
112
+ result = true;
113
+ }
114
+ catch {
115
+ result = false;
116
+ }
117
+ return result;
118
+ }
119
+ return {
120
+ read: reader.read,
121
+ readRawFields: reader.readRawFields,
122
+ exists,
123
+ write,
124
+ async writeCredential(env, credential) {
125
+ const config = await reader.readForWrite(env);
126
+ await write({ ...config, credential });
127
+ },
128
+ async writeFlowCredential(env, credential) {
129
+ const config = await reader.readForWrite(env);
130
+ await write({ ...config, flowCredential: credential });
131
+ },
132
+ async remove(env) {
133
+ try {
134
+ await deps.fs.rm(paths.envDir(env), { recursive: true, force: true });
135
+ }
136
+ catch (err) {
137
+ throw new CliError('config', t('store.removeFailed', { env }, deps.env), {
138
+ detail: err.code ?? String(err),
139
+ });
140
+ }
141
+ },
142
+ async list() {
143
+ let result;
144
+ try {
145
+ result = await deps.fs.readdir(paths.root);
146
+ }
147
+ catch (err) {
148
+ if (err.code !== 'ENOENT') {
149
+ throw new CliError('config', t('store.listFailed', {}, deps.env), {
150
+ detail: err.code ?? String(err),
151
+ });
152
+ }
153
+ result = [];
154
+ }
155
+ const envs = [];
156
+ for (const name of result) {
157
+ if (await exists(name))
158
+ envs.push(name);
159
+ else {
160
+ }
161
+ }
162
+ return envs.sort();
163
+ },
164
+ };
165
+ }
166
+ function createPointerOps(io) {
167
+ const { deps, paths } = io;
168
+ return {
169
+ async currentEnv() {
170
+ let text;
171
+ try {
172
+ text = await deps.fs.readFile(paths.currentFile, 'utf8');
173
+ }
174
+ catch (err) {
175
+ if (err.code !== 'ENOENT') {
176
+ throw new CliError('config', t('store.readFailed', { path: paths.currentFile }, deps.env), {
177
+ detail: err.code ?? String(err),
178
+ });
179
+ }
180
+ text = null;
181
+ }
182
+ const o = text === null ? null : asRecord(tryParseJson(text));
183
+ const env = o === null ? undefined : o['env'];
184
+ return typeof env === 'string' && env !== '' ? env : null;
185
+ },
186
+ async clearCurrent() {
187
+ try {
188
+ await deps.fs.unlink(paths.currentFile);
189
+ }
190
+ catch (err) {
191
+ if (err.code !== 'ENOENT') {
192
+ throw new CliError('config', t('store.clearCurrentFailed', {}, deps.env), {
193
+ detail: err.code ?? String(err),
194
+ });
195
+ }
196
+ }
197
+ },
198
+ };
199
+ }
200
+ function createCacheOps(io) {
201
+ const { deps, paths } = io;
202
+ return {
203
+ async readUser(env) {
204
+ const path = paths.userFile(env);
205
+ let text;
206
+ try {
207
+ text = await deps.fs.readFile(path, 'utf8');
208
+ }
209
+ catch (err) {
210
+ if (err.code !== 'ENOENT') {
211
+ throw new CliError('config', t('store.readFailed', { path }, deps.env), {
212
+ detail: err.code ?? String(err),
213
+ });
214
+ }
215
+ text = null;
216
+ }
217
+ const raw = text === null ? null : tryParseJson(text);
218
+ if (raw === undefined) {
219
+ throw new CliError('config', t('store.jsonParseFailed', { path }, deps.env));
220
+ }
221
+ return raw;
222
+ },
223
+ async writeUser(env, profile) {
224
+ await io.ensureEnvDir(env);
225
+ await io.writeJsonAtomic(paths.userFile(env), `${JSON.stringify(profile, null, 2)}\n`);
226
+ },
227
+ async removeUser(env) {
228
+ try {
229
+ await deps.fs.unlink(paths.userFile(env));
230
+ }
231
+ catch (err) {
232
+ if (err.code !== 'ENOENT') {
233
+ throw new CliError('config', t('store.removeUserFailed', { env }, deps.env), {
234
+ detail: err.code ?? String(err),
235
+ });
236
+ }
237
+ }
238
+ },
239
+ async readLastCanvas(env) {
240
+ let result;
241
+ try {
242
+ const o = asRecord(tryParseJson(await deps.fs.readFile(paths.lastCanvasFile(env), 'utf8')));
243
+ const id = o === null ? undefined : o['id'];
244
+ const at = o === null ? undefined : o['at'];
245
+ if (typeof id === 'string' &&
246
+ id !== '' &&
247
+ typeof at === 'string' &&
248
+ !Number.isNaN(Date.parse(at))) {
249
+ result = { id, at };
250
+ }
251
+ else {
252
+ result = null;
253
+ }
254
+ }
255
+ catch {
256
+ result = null;
257
+ }
258
+ return result;
259
+ },
260
+ async writeLastCanvas(env, record) {
261
+ await io.ensureEnvDir(env);
262
+ await io.writeJsonAtomic(paths.lastCanvasFile(env), `${JSON.stringify(record, null, 2)}\n`);
263
+ },
264
+ };
265
+ }
266
+ export function createConfigStore(deps, paths) {
267
+ const io = createStoreIo(deps, paths);
268
+ return {
269
+ ...createEnvFileOps(io),
270
+ ...createPointerOps(io),
271
+ ...createCacheOps(io),
272
+ };
273
+ }
@@ -0,0 +1,44 @@
1
+ import { createCipheriv, createDecipheriv } from 'node:crypto';
2
+ const FORMAT_VERSION = 'v1';
3
+ const ALGORITHM = 'aes-256-gcm';
4
+ export const KEY_BYTES = 32;
5
+ export const IV_BYTES = 12;
6
+ export function sealCredential(input) {
7
+ const cipher = createCipheriv(ALGORITHM, input.key, input.iv);
8
+ cipher.setAAD(Buffer.from(input.aad, 'utf8'));
9
+ const encrypted = Buffer.concat([cipher.update(input.plaintext, 'utf8'), cipher.final()]);
10
+ return [
11
+ FORMAT_VERSION,
12
+ input.iv.toString('base64'),
13
+ cipher.getAuthTag().toString('base64'),
14
+ encrypted.toString('base64'),
15
+ ].join('.');
16
+ }
17
+ export function openCredential(input) {
18
+ const parts = input.blob.trim().split('.');
19
+ let result;
20
+ if (parts.length !== 4 || parts[0] !== FORMAT_VERSION) {
21
+ result = null;
22
+ }
23
+ else {
24
+ result = decrypt(parts, input.key, input.aad);
25
+ }
26
+ return result;
27
+ }
28
+ function decrypt(parts, key, aad) {
29
+ let result;
30
+ try {
31
+ const decipher = createDecipheriv(ALGORITHM, key, Buffer.from(parts[1], 'base64'));
32
+ decipher.setAAD(Buffer.from(aad, 'utf8'));
33
+ decipher.setAuthTag(Buffer.from(parts[2], 'base64'));
34
+ const plain = Buffer.concat([
35
+ decipher.update(Buffer.from(parts[3], 'base64')),
36
+ decipher.final(),
37
+ ]);
38
+ result = plain.toString('utf8');
39
+ }
40
+ catch {
41
+ result = null;
42
+ }
43
+ return result;
44
+ }
@@ -0,0 +1,191 @@
1
+ import { spawn, spawnSync } from 'node:child_process';
2
+ import { randomBytes } from 'node:crypto';
3
+ import { chmod, mkdir, readFile, readdir, realpath, rename, rm, stat, unlink, writeFile, } from 'node:fs/promises';
4
+ import { homedir, platform as osPlatform } from 'node:os';
5
+ import { makeEngineDownloader } from "./canvas/engine-install.js";
6
+ import { makeNpmInstall, npmCommandOf, scriptPathOf } from "./update/npm-install.js";
7
+ import { startLoopbackServer } from "./loopback.js";
8
+ async function readStdin() {
9
+ const chunks = [];
10
+ for await (const chunk of process.stdin) {
11
+ chunks.push(chunk);
12
+ }
13
+ return Buffer.concat(chunks).toString('utf8');
14
+ }
15
+ export function createDeps() {
16
+ return {
17
+ fetch: globalThis.fetch,
18
+ fs: {
19
+ readFile: (path, encoding) => readFile(path, encoding),
20
+ writeFile: (path, data, options) => writeFile(path, data, options),
21
+ rename: (from, to) => rename(from, to),
22
+ chmod: (path, mode) => chmod(path, mode),
23
+ mkdir: async (path, options) => {
24
+ await mkdir(path, options);
25
+ return path;
26
+ },
27
+ rm: (path, options) => rm(path, options),
28
+ readdir: (path) => readdir(path),
29
+ stat: async (path) => {
30
+ const s = await stat(path);
31
+ return { mode: s.mode };
32
+ },
33
+ unlink: (path) => unlink(path),
34
+ },
35
+ clock: () => new Date(),
36
+ sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
37
+ stdout: (text) => {
38
+ process.stdout.write(text);
39
+ },
40
+ stderr: (text) => {
41
+ process.stderr.write(text);
42
+ },
43
+ stderrIsTTY: process.stderr.isTTY === true,
44
+ readStdin,
45
+ homeDir: () => homedir(),
46
+ cwd: () => process.cwd(),
47
+ env: process.env,
48
+ platform: process.platform,
49
+ spawnDetached: (command, args) => {
50
+ const child = spawn(command, [...args], {
51
+ detached: true,
52
+ stdio: 'ignore',
53
+ windowsHide: true,
54
+ });
55
+ child.on('error', () => undefined);
56
+ child.unref();
57
+ if (child.pid === undefined) {
58
+ throw Object.assign(new Error('spawn failed'), { code: 'ESPAWN' });
59
+ }
60
+ return { pid: child.pid };
61
+ },
62
+ terminate: (pid) => {
63
+ terminateWith(process.platform, pid, process.kill, runSync);
64
+ },
65
+ processAlive: (pid) => {
66
+ let alive;
67
+ try {
68
+ process.kill(pid, 0);
69
+ alive = true;
70
+ }
71
+ catch {
72
+ alive = false;
73
+ }
74
+ return alive;
75
+ },
76
+ nodePath: () => process.execPath,
77
+ randomBytes: (size) => randomBytes(size),
78
+ openBrowser: makeOpenBrowser(osPlatform(), spawnLauncher),
79
+ startLoopback: startLoopbackServer,
80
+ arch: process.arch,
81
+ downloadEngine: makeEngineDownloader(),
82
+ runNpmInstall: makeNpmInstall(npmCommandOf(process.platform), undefined, process.platform === 'win32'),
83
+ scriptPath: () => scriptPathOf(process.argv),
84
+ readWindowsRegistry: readRegistryWith(runCapture),
85
+ realpath: (path) => realpath(path),
86
+ };
87
+ }
88
+ export function spawnLauncher(command, args) {
89
+ const child = spawn(command, [...args], { stdio: 'ignore', windowsHide: true });
90
+ child.on('error', () => undefined);
91
+ child.unref();
92
+ return child.pid !== undefined;
93
+ }
94
+ export function makeOpenBrowser(platform, spawnWith) {
95
+ return (url) => {
96
+ const { command, args } = browserCommand(platform, url);
97
+ return spawnWith(command, args);
98
+ };
99
+ }
100
+ export const DEFAULT_REGISTRY_VALUE = '(Default)';
101
+ export function readRegistryWith(run) {
102
+ return async (key, value) => {
103
+ let result;
104
+ try {
105
+ const args = value === DEFAULT_REGISTRY_VALUE ? ['query', key, '/ve'] : ['query', key, '/v', value];
106
+ const { code, stdout } = run('reg', args);
107
+ result = code === 0 ? parseRegQuery(stdout, value) : null;
108
+ }
109
+ catch {
110
+ result = null;
111
+ }
112
+ return result;
113
+ };
114
+ }
115
+ export function parseRegQuery(stdout, value) {
116
+ const lines = stdout.split(/\r?\n/u);
117
+ let result = null;
118
+ if (value === DEFAULT_REGISTRY_VALUE) {
119
+ const line = lines.find((candidate) => /REG_[A-Z_]+/u.test(candidate));
120
+ result = line === undefined ? null : valueAfterType(line);
121
+ }
122
+ else {
123
+ const pattern = new RegExp(`^\\s*${value.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&')}\\s+REG_[A-Z_]+\\s+(.+)$`, 'u');
124
+ for (const line of lines) {
125
+ const match = pattern.exec(line);
126
+ if (match === null || result !== null) {
127
+ }
128
+ else {
129
+ result = match[1].trim();
130
+ }
131
+ }
132
+ }
133
+ return result;
134
+ }
135
+ function valueAfterType(line) {
136
+ const match = /\s+REG_[A-Z_]+\s+(.+)$/u.exec(line);
137
+ return match[1].trim();
138
+ }
139
+ export function runCapture(command, args) {
140
+ const result = spawnSync(command, args, { windowsHide: true });
141
+ return { code: result.status ?? 1, stdout: bufferToText(result.stdout) };
142
+ }
143
+ function bufferToText(value) {
144
+ let result;
145
+ if (value === undefined) {
146
+ result = '';
147
+ }
148
+ else {
149
+ result = value.toString('latin1');
150
+ }
151
+ return result;
152
+ }
153
+ export function runSync(command, args) {
154
+ spawnSync(command, args, { stdio: 'ignore', windowsHide: true });
155
+ }
156
+ export function terminateWith(platform, pid, kill, run) {
157
+ const spec = terminateCommand(platform, pid);
158
+ if (spec.kind === 'signal') {
159
+ try {
160
+ kill(pid, spec.signal);
161
+ }
162
+ catch {
163
+ }
164
+ }
165
+ else {
166
+ run(spec.command, spec.args);
167
+ }
168
+ }
169
+ export function terminateCommand(platform, pid) {
170
+ let result;
171
+ if (platform === 'win32') {
172
+ result = { kind: 'command', command: 'taskkill', args: ['/PID', String(pid), '/T', '/F'] };
173
+ }
174
+ else {
175
+ result = { kind: 'signal', signal: 'SIGKILL' };
176
+ }
177
+ return result;
178
+ }
179
+ export function browserCommand(platform, url) {
180
+ let result;
181
+ if (platform === 'darwin') {
182
+ result = { command: 'open', args: [url] };
183
+ }
184
+ else if (platform === 'win32') {
185
+ result = { command: 'rundll32', args: ['url.dll,FileProtocolHandler', url] };
186
+ }
187
+ else {
188
+ result = { command: 'xdg-open', args: [url] };
189
+ }
190
+ return result;
191
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ const EXIT_CODES = {
2
+ usage: 2,
3
+ config: 1,
4
+ network: 1,
5
+ timeout: 1,
6
+ auth: 1,
7
+ server: 1,
8
+ unsupported: 1,
9
+ };
10
+ export class CliError extends Error {
11
+ kind;
12
+ detail;
13
+ exitCode;
14
+ constructor(kind, message, options = {}) {
15
+ super(message);
16
+ this.name = 'CliError';
17
+ this.kind = kind;
18
+ this.detail = options.detail;
19
+ this.exitCode = EXIT_CODES[kind];
20
+ }
21
+ }
22
+ export function errorMessage(err) {
23
+ return err instanceof Error ? err.message : String(err);
24
+ }
@@ -0,0 +1,34 @@
1
+ const RETRYABLE = new Set(['EBUSY', 'EPERM', 'ENOTEMPTY', 'EACCES']);
2
+ const DEFAULT_ATTEMPTS = 6;
3
+ const DEFAULT_DELAY_MS = 100;
4
+ function codeOf(err) {
5
+ let result;
6
+ if (typeof err === 'object' && err !== null && 'code' in err) {
7
+ const code = err.code;
8
+ result = typeof code === 'string' ? code : undefined;
9
+ }
10
+ else {
11
+ result = undefined;
12
+ }
13
+ return result;
14
+ }
15
+ export async function withRetry(deps, op, options = {}) {
16
+ const attempts = options.attempts ?? DEFAULT_ATTEMPTS;
17
+ const delayMs = options.delayMs ?? DEFAULT_DELAY_MS;
18
+ let attempt = 0;
19
+ for (;;) {
20
+ try {
21
+ return await op();
22
+ }
23
+ catch (err) {
24
+ const code = codeOf(err);
25
+ attempt += 1;
26
+ if (code === undefined || !RETRYABLE.has(code) || attempt >= attempts) {
27
+ throw err;
28
+ }
29
+ else {
30
+ await deps.sleep(delayMs);
31
+ }
32
+ }
33
+ }
34
+ }