@tulcn/lxz 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.
- package/README.md +63 -2
- package/dist/application.d.ts +2 -0
- package/dist/application.js +17 -0
- package/dist/auth/client.d.ts +5 -0
- package/dist/auth/client.js +14 -0
- package/dist/auth/gateway.d.ts +1 -0
- package/dist/auth/gateway.js +10 -0
- package/dist/auth/host.d.ts +2 -0
- package/dist/auth/host.js +16 -0
- package/dist/auth/identity.d.ts +45 -0
- package/dist/auth/identity.js +106 -0
- package/dist/auth/index.d.ts +10 -0
- package/dist/auth/index.js +13 -0
- package/dist/auth/inspection.d.ts +14 -0
- package/dist/auth/inspection.js +22 -0
- package/dist/auth/modules.d.ts +12 -0
- package/dist/auth/modules.js +25 -0
- package/dist/auth/platform.d.ts +26 -0
- package/dist/auth/platform.js +102 -0
- package/dist/auth/protocol.d.ts +17 -0
- package/dist/auth/protocol.js +1 -0
- package/dist/auth/provider.d.ts +40 -0
- package/dist/auth/provider.js +174 -0
- package/dist/auth/response.d.ts +2 -0
- package/dist/auth/response.js +17 -0
- package/dist/auth/service.d.ts +5 -0
- package/dist/auth/service.js +72 -0
- package/dist/auth/store.d.ts +26 -0
- package/dist/auth/store.js +171 -0
- package/dist/cli/browser.d.ts +1 -0
- package/dist/cli/browser.js +11 -0
- package/dist/cli/contracts.d.ts +85 -0
- package/dist/cli/contracts.js +51 -0
- package/dist/cli/deployment.d.ts +13 -0
- package/dist/cli/deployment.js +35 -0
- package/dist/cli/device-login.d.ts +29 -0
- package/dist/cli/device-login.js +87 -0
- package/dist/cli/environment.d.ts +9 -0
- package/dist/cli/environment.js +19 -0
- package/dist/cli/errors.d.ts +30 -0
- package/dist/cli/errors.js +22 -0
- package/dist/cli/files.d.ts +6 -0
- package/dist/cli/files.js +71 -0
- package/dist/cli/http.d.ts +6 -0
- package/dist/cli/http.js +38 -0
- package/dist/cli/identity-commands.d.ts +3 -0
- package/dist/cli/identity-commands.js +56 -0
- package/dist/cli/index.d.ts +13 -0
- package/dist/cli/index.js +13 -0
- package/dist/cli/init.d.ts +205 -0
- package/dist/cli/init.js +77 -0
- package/dist/cli/modules.d.ts +6 -0
- package/dist/cli/modules.js +18 -0
- package/dist/cli/output.d.ts +2 -0
- package/dist/cli/output.js +35 -0
- package/dist/cli/prompts.d.ts +34 -0
- package/dist/cli/prompts.js +58 -0
- package/dist/cli/runtime.d.ts +2 -0
- package/dist/cli/runtime.js +137 -0
- package/dist/cli/schemas.d.ts +187 -0
- package/dist/cli/schemas.js +46 -0
- package/dist/doctor.d.ts +2 -0
- package/dist/doctor.js +72 -0
- package/dist/internal/client.d.ts +6 -0
- package/dist/internal/client.js +51 -0
- package/dist/internal/host.d.ts +1 -0
- package/dist/internal/host.js +50 -0
- package/dist/internal/protocol.d.ts +27 -0
- package/dist/internal/protocol.js +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +12 -0
- package/dist/skills/agents.d.ts +7 -0
- package/dist/skills/agents.js +11 -0
- package/dist/skills/artifacts.d.ts +9 -0
- package/dist/skills/artifacts.js +12 -0
- package/dist/skills/bundle.d.ts +22 -0
- package/dist/skills/bundle.js +77 -0
- package/dist/skills/client.d.ts +5 -0
- package/dist/skills/client.js +9 -0
- package/dist/skills/commands.d.ts +5 -0
- package/dist/skills/commands.js +20 -0
- package/dist/skills/delivery.d.ts +60 -0
- package/dist/skills/delivery.js +89 -0
- package/dist/skills/index.d.ts +7 -0
- package/dist/skills/index.js +7 -0
- package/dist/skills/installer.d.ts +23 -0
- package/dist/skills/installer.js +130 -0
- package/dist/skills/paths.d.ts +2 -0
- package/dist/skills/paths.js +28 -0
- package/package.json +36 -3
- package/scripts/install.mjs +17 -0
- package/skills/SKILL.md +38 -0
- package/skills/bundle.json +7 -0
- package/skills/commands.md +440 -0
- package/skills/features.md +12 -0
package/dist/cli/init.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { CliError, failure } from './errors.js';
|
|
2
|
+
import { errorSchema } from './schemas.js';
|
|
3
|
+
import { approvalNext, deviceLoginInBackground, pendingProperties, waitParameter, waitsInBackground } from './device-login.js';
|
|
4
|
+
const stageSchema = { type: 'object', required: ['status'], properties: { status: { enum: ['ready', 'failed', 'blocked', 'pending'] }, account: { type: 'string' }, target: { type: 'string' }, reason: { type: 'string' }, error: errorSchema } };
|
|
5
|
+
export const initializationSchema = {
|
|
6
|
+
type: 'object', required: ['identity', 'service', 'skill', 'ready'],
|
|
7
|
+
properties: {
|
|
8
|
+
identity: { ...stageSchema, properties: { ...stageSchema.properties, ...pendingProperties } }, service: stageSchema, ready: { type: 'boolean' },
|
|
9
|
+
next: { type: 'string', description: 'What to do next while authorization is pending' },
|
|
10
|
+
skill: { ...stageSchema, properties: {
|
|
11
|
+
...stageSchema.properties, directory: { type: 'string' }, version: { type: 'string' }, action: { enum: ['installed', 'updated', 'unchanged'] }, incompatible: { type: 'boolean' },
|
|
12
|
+
changes: { type: 'array', items: { type: 'object', properties: { file: { type: 'string' }, change: { enum: ['add', 'update', 'remove'] } }, required: ['file', 'change'] } },
|
|
13
|
+
installed: { type: 'array', items: { type: 'object', properties: { directory: { type: 'string' }, version: { type: 'string' }, action: { enum: ['installed', 'updated', 'unchanged'] } }, required: ['directory', 'version', 'action'] } },
|
|
14
|
+
} },
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
export function initCommand(identity, skill, clientId, example) {
|
|
18
|
+
return {
|
|
19
|
+
description: 'Authorize and install the Agent Skill for first use', effects: 'local', timeoutMs: 660000, interactiveTimeout: 'total',
|
|
20
|
+
parameters: {
|
|
21
|
+
'device-login': { type: 'boolean', description: 'Allow device authorization in non-interactive mode; returns the link at once and waits for approval in the background', default: false },
|
|
22
|
+
wait: waitParameter,
|
|
23
|
+
browser: { type: 'boolean', description: 'Open the authorization page in the default browser; use --no-browser to open the URL on another device', default: true },
|
|
24
|
+
'skills-dir': { type: 'string', description: 'Skills root; overrides LXZ_SKILLS_DIR. Default: registered roots, or ~/.agents/skills and ~/.linkwise/skills (Linkwise)' },
|
|
25
|
+
'skill-conflict': { type: 'string', description: 'Local Skill changes: error keeps them; replace overwrites managed files', choices: ['error', 'replace'], default: 'error' },
|
|
26
|
+
},
|
|
27
|
+
result: initializationSchema, examples: [example],
|
|
28
|
+
async run(input, context) {
|
|
29
|
+
// A stored session, or a failure that the stages below report, keeps the original flow.
|
|
30
|
+
if (waitsInBackground(input, context) && !await identity().status(context.signal).then(Boolean, () => true)) {
|
|
31
|
+
return deviceLoginInBackground(input, context, link => ({
|
|
32
|
+
identity: { status: 'pending', ...link, reason: 'Waiting in the background for the user to approve in a browser; the Skill is installed after approval' },
|
|
33
|
+
service: { status: 'blocked', target: clientId, reason: 'identity' }, skill: { status: 'pending' }, ready: false,
|
|
34
|
+
next: approvalNext(link, example.replace(/ init$/, '') + ' status'),
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
const state = { identity: { status: 'pending' }, service: { status: 'blocked', target: clientId, reason: 'identity' }, skill: { status: 'pending' }, ready: false };
|
|
38
|
+
let failed = false;
|
|
39
|
+
try {
|
|
40
|
+
const auth = identity();
|
|
41
|
+
const current = await auth.status(context.signal) ?? await auth.login({ ...context, allowDeviceLogin: Boolean(input['device-login']), browser: input.browser !== false });
|
|
42
|
+
state.identity = { status: 'ready', account: current.account };
|
|
43
|
+
try {
|
|
44
|
+
await auth.credential(context.signal);
|
|
45
|
+
state.service = { status: 'ready', target: clientId };
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
context.signal.throwIfAborted();
|
|
49
|
+
const detail = failure(error);
|
|
50
|
+
if (detail.code === 'AUTH_REQUIRED' || detail.code === 'AUTH_EXPIRED')
|
|
51
|
+
state.identity = { status: 'failed', error: detail };
|
|
52
|
+
state.service = { status: 'failed', target: clientId, error: detail };
|
|
53
|
+
failed = true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
context.signal.throwIfAborted();
|
|
58
|
+
state.identity = { status: 'failed', error: failure(error) };
|
|
59
|
+
failed = true;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const directory = typeof input['skills-dir'] === 'string' ? input['skills-dir'] : undefined;
|
|
63
|
+
context.signal.throwIfAborted();
|
|
64
|
+
state.skill = { status: 'ready', ...Object(await skill.install(directory, input['skill-conflict'], context.signal)) };
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
context.signal.throwIfAborted();
|
|
68
|
+
state.skill = { status: 'failed', error: failure(error) };
|
|
69
|
+
failed = true;
|
|
70
|
+
}
|
|
71
|
+
state.ready = !failed;
|
|
72
|
+
if (failed)
|
|
73
|
+
throw new CliError('INIT_INCOMPLETE', 'Initialization is incomplete; completed stages were kept.', 'Resolve each failed stage and run init again.', false, state);
|
|
74
|
+
return state;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
export function registerCommandModules(modules) {
|
|
3
|
+
const commands = new Map();
|
|
4
|
+
const identities = new Set();
|
|
5
|
+
for (const module of modules) {
|
|
6
|
+
if ('enabled' in module || typeof module.commands === 'function')
|
|
7
|
+
throw new CliError('INCOMPATIBLE', `Module ${module.id} uses legacy registration. Remove enabled and omit disabled modules from the registration array. Pass a command record as commands; replace commands: () => createCommands() with commands: createCommands().`, 'Update this CLI module registration and rebuild.');
|
|
8
|
+
if (identities.has(module.id))
|
|
9
|
+
throw new CliError('CONFIGURATION', 'Duplicate feature module registration.', 'Ask the CLI maintainer to check module IDs.');
|
|
10
|
+
identities.add(module.id);
|
|
11
|
+
for (const [name, command] of Object.entries(module.commands)) {
|
|
12
|
+
if (commands.has(name))
|
|
13
|
+
throw new CliError('CONFIGURATION', `Command ${name} is registered more than once.`, 'Ask the CLI maintainer to rename commands or disable duplicate modules.');
|
|
14
|
+
commands.set(name, { ...command, module: module.id });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return Object.fromEntries(commands);
|
|
18
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Console decoders such as PowerShell on Chinese Windows read stdout with the local code page; ASCII JSON parses the same everywhere.
|
|
2
|
+
export function jsonText(value, space) {
|
|
3
|
+
return JSON.stringify(value, null, space).replace(/[^\x00-\x7e]/g, character => '\\u' + character.charCodeAt(0).toString(16).padStart(4, '0'));
|
|
4
|
+
}
|
|
5
|
+
function cell(value) {
|
|
6
|
+
return typeof value === 'string' ? JSON.stringify(value).slice(1, -1) : value === undefined ? '' : String(value);
|
|
7
|
+
}
|
|
8
|
+
function scalar(value) {
|
|
9
|
+
return value === null || typeof value !== 'object';
|
|
10
|
+
}
|
|
11
|
+
function rows(value) {
|
|
12
|
+
if (scalar(value))
|
|
13
|
+
return [cell(value)];
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
if (!value.length)
|
|
16
|
+
return ['[]'];
|
|
17
|
+
if (value.every(scalar))
|
|
18
|
+
return value.map(cell);
|
|
19
|
+
if (value.every(item => item !== null && !Array.isArray(item) && typeof item === 'object' && Object.values(item).every(scalar))) {
|
|
20
|
+
const columns = [...new Set(value.flatMap(item => Object.keys(item).filter(key => item[key] !== undefined)))];
|
|
21
|
+
if (columns.length)
|
|
22
|
+
return [columns.map(key => cell(key)).join('\t'), ...value.map(item => columns.map(key => Object.hasOwn(item, key) ? cell(item[key]) : '').join('\t'))];
|
|
23
|
+
}
|
|
24
|
+
return value.flatMap((item, index) => [`[${index}]:`, ...rows(item).map(line => ' ' + line)]);
|
|
25
|
+
}
|
|
26
|
+
const entries = Object.entries(value).filter(([, item]) => item !== undefined);
|
|
27
|
+
if (!entries.length)
|
|
28
|
+
return ['{}'];
|
|
29
|
+
return entries.flatMap(([key, item]) => scalar(item)
|
|
30
|
+
? [`${cell(key)}\t${cell(item)}`]
|
|
31
|
+
: [`${cell(key)}:`, ...rows(item).map(line => ' ' + line)]);
|
|
32
|
+
}
|
|
33
|
+
export function rowText(value) {
|
|
34
|
+
return typeof value === 'string' ? value : rows(value).join('\n');
|
|
35
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Readable, Writable } from 'node:stream';
|
|
2
|
+
interface PromptOptions {
|
|
3
|
+
message: string;
|
|
4
|
+
input?: Readable;
|
|
5
|
+
output?: Writable;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
interface Choice<T> {
|
|
9
|
+
value: T;
|
|
10
|
+
label: string;
|
|
11
|
+
hint?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const prompts: {
|
|
14
|
+
isCancel: (value: unknown) => boolean;
|
|
15
|
+
text(options: PromptOptions & {
|
|
16
|
+
initialValue?: string;
|
|
17
|
+
validate?: (value: string | undefined) => string | undefined;
|
|
18
|
+
}): Promise<string | symbol>;
|
|
19
|
+
select<T>(options: PromptOptions & {
|
|
20
|
+
options: Choice<T>[];
|
|
21
|
+
initialValue?: T;
|
|
22
|
+
}): Promise<T | symbol>;
|
|
23
|
+
multiselect<T>(options: PromptOptions & {
|
|
24
|
+
options: Choice<T>[];
|
|
25
|
+
initialValues?: T[];
|
|
26
|
+
required?: boolean;
|
|
27
|
+
}): Promise<T[] | symbol>;
|
|
28
|
+
confirm(options: PromptOptions & {
|
|
29
|
+
initialValue?: boolean;
|
|
30
|
+
}): Promise<boolean | symbol>;
|
|
31
|
+
};
|
|
32
|
+
export declare function answer<T>(value: T | symbol): T;
|
|
33
|
+
export declare function ask(message: string, initialValue?: string, signal?: AbortSignal, validate?: (value: string) => string | undefined): Promise<string>;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ConfirmPrompt, MultiSelectPrompt, SelectPrompt, TextPrompt, isCancel } from '@clack/core';
|
|
2
|
+
import { CliError } from './errors.js';
|
|
3
|
+
function frame(message, state, error, body) {
|
|
4
|
+
const marker = state === 'cancel' ? 'x' : state === 'submit' ? '+' : '?';
|
|
5
|
+
return `${marker} ${message}\n${state === 'error' ? `! ${error}\n` : ''}${body}`;
|
|
6
|
+
}
|
|
7
|
+
async function runPrompt(options, create) {
|
|
8
|
+
const controller = new AbortController();
|
|
9
|
+
const abort = () => controller.abort(options.signal?.reason);
|
|
10
|
+
if (options.signal?.aborted)
|
|
11
|
+
abort();
|
|
12
|
+
else
|
|
13
|
+
options.signal?.addEventListener('abort', abort, { once: true });
|
|
14
|
+
try {
|
|
15
|
+
return await create(controller.signal).prompt();
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
options.signal?.removeEventListener('abort', abort);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export const prompts = {
|
|
22
|
+
isCancel: (value) => isCancel(value),
|
|
23
|
+
text(options) {
|
|
24
|
+
return runPrompt(options, signal => new TextPrompt({ ...options, signal, output: options.output ?? process.stderr, initialUserInput: options.initialValue,
|
|
25
|
+
render() { return frame(options.message, this.state, this.error, this.userInputWithCursor); },
|
|
26
|
+
}));
|
|
27
|
+
},
|
|
28
|
+
select(options) {
|
|
29
|
+
return runPrompt(options, signal => new SelectPrompt({ ...options, signal, output: options.output ?? process.stderr,
|
|
30
|
+
render() {
|
|
31
|
+
const choices = this.state === 'submit' ? options.options.filter(item => item.value === this.value) : options.options;
|
|
32
|
+
return frame(options.message, this.state, this.error, choices.map(item => `${options.options[this.cursor] === item ? '>' : ' '} ${item.label}${item.hint ? ` (${item.hint})` : ''}`).join('\n'));
|
|
33
|
+
},
|
|
34
|
+
}));
|
|
35
|
+
},
|
|
36
|
+
multiselect(options) {
|
|
37
|
+
return runPrompt(options, signal => new MultiSelectPrompt({ ...options, signal, output: options.output ?? process.stderr,
|
|
38
|
+
render() {
|
|
39
|
+
const selected = this.value ?? [];
|
|
40
|
+
const choices = this.state === 'submit' ? options.options.filter(item => selected.includes(item.value)) : options.options;
|
|
41
|
+
return frame(options.message, this.state, this.error, choices.map(item => `${options.options[this.cursor] === item ? '>' : ' '} [${selected.includes(item.value) ? 'x' : ' '}] ${item.label}${item.hint ? ` (${item.hint})` : ''}`).join('\n') || '(none)');
|
|
42
|
+
},
|
|
43
|
+
}));
|
|
44
|
+
},
|
|
45
|
+
confirm(options) {
|
|
46
|
+
return runPrompt(options, signal => new ConfirmPrompt({ ...options, signal, output: options.output ?? process.stderr, active: 'Yes', inactive: 'No',
|
|
47
|
+
render() { return frame(options.message, this.state, this.error, this.value ? '> Yes / No' : 'Yes / > No'); },
|
|
48
|
+
}));
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
export function answer(value) {
|
|
52
|
+
if (isCancel(value))
|
|
53
|
+
throw new CliError('CANCELLED', 'Operation cancelled.', 'Run the command again when needed.');
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
export async function ask(message, initialValue, signal, validate) {
|
|
57
|
+
return answer(await prompts.text({ message, initialValue, signal, validate: value => value?.trim() ? validate?.(value.trim()) : 'Enter a value' }));
|
|
58
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { defineCommand, renderUsage } from 'citty';
|
|
2
|
+
import { parseArgs } from 'node:util';
|
|
3
|
+
import { argumentDefinitions, commonParameters, commandParameters, contractVersion, describe } from './contracts.js';
|
|
4
|
+
import { CliError, exitCodes, failure } from './errors.js';
|
|
5
|
+
import { jsonText, rowText } from './output.js';
|
|
6
|
+
function jsonLine(value) {
|
|
7
|
+
return jsonText(value) + '\n';
|
|
8
|
+
}
|
|
9
|
+
function validate(input, parameters, required) {
|
|
10
|
+
for (const [name, p] of Object.entries(parameters)) {
|
|
11
|
+
const value = input[name];
|
|
12
|
+
if (required && p.required && (value === undefined || value === ''))
|
|
13
|
+
throw new CliError('INVALID_INPUT', `Provide --${name}.`, `See --help and provide ${p.description}.`);
|
|
14
|
+
if (value !== undefined && ((p.choices && !p.choices.includes(String(value))) || (p.pattern && !new RegExp(p.pattern).test(String(value))))) {
|
|
15
|
+
throw new CliError('INVALID_INPUT', `--${name} has an invalid value.`, `${p.description}${p.choices ? `; choices: ${p.choices.join(', ')}` : ''}. See --help for usage.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function jsonMode(raw, parameters) {
|
|
20
|
+
let enabled = false;
|
|
21
|
+
for (let index = 0; index < raw.length; index++) {
|
|
22
|
+
const flag = raw[index];
|
|
23
|
+
if (flag === '--')
|
|
24
|
+
break;
|
|
25
|
+
if (flag === '--json' || flag === '--json=true')
|
|
26
|
+
enabled = true;
|
|
27
|
+
else if (flag === '--no-json' || flag === '--json=false')
|
|
28
|
+
enabled = false;
|
|
29
|
+
else if (!flag.includes('=') && parameters[flag.slice(2)]?.type === 'string' && !raw[index + 1]?.startsWith('-'))
|
|
30
|
+
index++;
|
|
31
|
+
}
|
|
32
|
+
return enabled;
|
|
33
|
+
}
|
|
34
|
+
export async function runApplication(app, argv = process.argv.slice(2)) {
|
|
35
|
+
argv = argv.map((value) => value === '-h' ? '--help' : value === '-v' ? '--version' : value);
|
|
36
|
+
let json = false;
|
|
37
|
+
const meta = { contractVersion, cli: app.packageName, version: app.version };
|
|
38
|
+
const write = (data) => process.stdout.write(json ? jsonLine({ ok: true, data, meta }) : rowText(data) + '\n');
|
|
39
|
+
let dispose = () => { };
|
|
40
|
+
try {
|
|
41
|
+
const names = [...new Set(Object.keys(app.commands).flatMap((name) => {
|
|
42
|
+
const words = name.split(' ');
|
|
43
|
+
return words.map((_, index) => words.slice(0, index + 1).join(' '));
|
|
44
|
+
}))].sort((a, b) => b.length - a.length);
|
|
45
|
+
const leading = [];
|
|
46
|
+
let offset = 0;
|
|
47
|
+
while (argv[offset]?.startsWith('--')) {
|
|
48
|
+
const name = argv[offset].slice(2).split('=')[0].replace(/^no-/, '');
|
|
49
|
+
if (!commonParameters[name])
|
|
50
|
+
break;
|
|
51
|
+
const flag = argv[offset++];
|
|
52
|
+
leading.push(flag);
|
|
53
|
+
if (commonParameters[name].type === 'string' && !flag.includes('=') && argv[offset] !== undefined)
|
|
54
|
+
leading.push(argv[offset++]);
|
|
55
|
+
}
|
|
56
|
+
const remaining = argv.slice(offset);
|
|
57
|
+
// Route only positional command words; option values never become commands.
|
|
58
|
+
const explicitRoute = names.find((name) => name.split(' ').every((word, i) => remaining[i] === word));
|
|
59
|
+
const route = explicitRoute ?? app.defaultCommand;
|
|
60
|
+
const command = route ? app.commands[route] : undefined;
|
|
61
|
+
const parameters = commandParameters(command);
|
|
62
|
+
const raw = [...leading, ...(explicitRoute ? remaining.slice(explicitRoute.split(' ').length) : remaining)];
|
|
63
|
+
json = jsonMode(raw, parameters);
|
|
64
|
+
let input;
|
|
65
|
+
try {
|
|
66
|
+
const parsed = parseArgs({ args: raw, options: Object.fromEntries(Object.entries(parameters).map(([key, p]) => [key, { type: p.type, default: p.default }])), allowNegative: true, strict: true, allowPositionals: false });
|
|
67
|
+
input = parsed.values;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new CliError('INVALID_INPUT', 'Unknown command, option or invalid option type.', `Use ${app.name} --help to view commands and options.`);
|
|
71
|
+
}
|
|
72
|
+
if (input.version) {
|
|
73
|
+
write({ version: app.version, contractVersion });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (input.schema) {
|
|
77
|
+
write(describe(app, explicitRoute));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (input.help || (!argv.length && !app.defaultCommand)) {
|
|
81
|
+
const definition = defineCommand({ meta: { name: route ? `${app.name} ${route}` : app.name, version: app.version, description: command?.description ?? app.description }, args: argumentDefinitions(parameters) });
|
|
82
|
+
const usage = await renderUsage(definition);
|
|
83
|
+
const children = Object.entries(app.commands).filter(([name]) => !route || name.startsWith(route + ' '));
|
|
84
|
+
const commands = children.length ? `\nCommands:\n${children.map(([name, c]) => ` ${route ? name.slice(route.length + 1) : name} ${c.description}`).join('\n')}\n\nUse ${route ? `${app.name} ${route}` : app.name} <command> -h for options and examples.` : '';
|
|
85
|
+
write(`${usage}${command ? `\nExamples:\n${command.examples.join('\n')}` : ''}${commands}`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (!command)
|
|
89
|
+
throw new CliError('INVALID_INPUT', 'Choose a command.', `Run ${app.name} --help.`);
|
|
90
|
+
validate(input, parameters, true);
|
|
91
|
+
const interactive = Boolean(process.stdin.isTTY && process.stderr.isTTY && !input['non-interactive']);
|
|
92
|
+
const idle = interactive && command.interactiveTimeout !== 'total';
|
|
93
|
+
const timeout = Number(input['timeout-ms']);
|
|
94
|
+
const controller = new AbortController();
|
|
95
|
+
let timer;
|
|
96
|
+
const resetTimer = () => {
|
|
97
|
+
clearTimeout(timer);
|
|
98
|
+
timer = setTimeout(() => controller.abort(idle
|
|
99
|
+
? new CliError('TIMEOUT', 'No input for 60 seconds; operation ended.', 'Run the command again to continue.', true)
|
|
100
|
+
: new CliError('TIMEOUT', 'Operation timed out.', 'Check network and service status, or increase --timeout-ms and retry.', true)), idle ? 60000 : timeout);
|
|
101
|
+
};
|
|
102
|
+
const stdinPaused = process.stdin.isPaused();
|
|
103
|
+
resetTimer();
|
|
104
|
+
if (idle)
|
|
105
|
+
process.stdin.on('data', resetTimer);
|
|
106
|
+
const cancel = () => controller.abort(new CliError('CANCELLED', 'Operation cancelled.', 'Run again when needed.'));
|
|
107
|
+
process.once('SIGINT', cancel);
|
|
108
|
+
process.once('SIGTERM', cancel);
|
|
109
|
+
dispose = () => {
|
|
110
|
+
clearTimeout(timer);
|
|
111
|
+
if (idle) {
|
|
112
|
+
process.stdin.off('data', resetTimer);
|
|
113
|
+
if (stdinPaused)
|
|
114
|
+
process.stdin.pause();
|
|
115
|
+
}
|
|
116
|
+
process.off('SIGINT', cancel);
|
|
117
|
+
process.off('SIGTERM', cancel);
|
|
118
|
+
};
|
|
119
|
+
const aborted = new Promise((_, reject) => controller.signal.addEventListener('abort', () => reject(controller.signal.reason), { once: true }));
|
|
120
|
+
const result = await Promise.race([command.run(input, {
|
|
121
|
+
interactive, argv,
|
|
122
|
+
signal: controller.signal, log: (message) => process.stderr.write(`${message}\n`),
|
|
123
|
+
}), aborted]);
|
|
124
|
+
write(!json && command.format ? command.format(result) : result);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
const detail = failure(error);
|
|
128
|
+
if (json)
|
|
129
|
+
process.stdout.write(jsonLine({ ok: false, error: detail, meta }));
|
|
130
|
+
else
|
|
131
|
+
process.stderr.write(`${detail.code}: ${detail.message}\nNext: ${detail.next}\n${detail.details ? rowText(detail.details) + '\n' : ''}`);
|
|
132
|
+
process.exitCode = exitCodes[detail.code];
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
dispose();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { Application } from './contracts.js';
|
|
2
|
+
export declare const schemaDialect: "https://json-schema.org/draft/2020-12/schema";
|
|
3
|
+
export declare const errorSchema: {
|
|
4
|
+
type: string;
|
|
5
|
+
required: string[];
|
|
6
|
+
properties: {
|
|
7
|
+
code: {
|
|
8
|
+
enum: string[];
|
|
9
|
+
};
|
|
10
|
+
message: {
|
|
11
|
+
type: string;
|
|
12
|
+
};
|
|
13
|
+
retryable: {
|
|
14
|
+
type: string;
|
|
15
|
+
};
|
|
16
|
+
next: {
|
|
17
|
+
type: string;
|
|
18
|
+
};
|
|
19
|
+
details: {};
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export declare function resultSchema(app?: Application, command?: string): {
|
|
23
|
+
$schema: "https://json-schema.org/draft/2020-12/schema";
|
|
24
|
+
oneOf: ({
|
|
25
|
+
type: string;
|
|
26
|
+
required: string[];
|
|
27
|
+
properties: {
|
|
28
|
+
ok: {
|
|
29
|
+
const: boolean;
|
|
30
|
+
};
|
|
31
|
+
data: Record<string, unknown>;
|
|
32
|
+
meta: {
|
|
33
|
+
type: string;
|
|
34
|
+
required: string[];
|
|
35
|
+
properties: {
|
|
36
|
+
contractVersion: {
|
|
37
|
+
const: string;
|
|
38
|
+
};
|
|
39
|
+
cli: {
|
|
40
|
+
const: string;
|
|
41
|
+
type?: undefined;
|
|
42
|
+
} | {
|
|
43
|
+
type: string;
|
|
44
|
+
const?: undefined;
|
|
45
|
+
};
|
|
46
|
+
version: {
|
|
47
|
+
const: string;
|
|
48
|
+
type?: undefined;
|
|
49
|
+
} | {
|
|
50
|
+
type: string;
|
|
51
|
+
const?: undefined;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
error?: undefined;
|
|
56
|
+
};
|
|
57
|
+
} | {
|
|
58
|
+
type: string;
|
|
59
|
+
required: string[];
|
|
60
|
+
properties: {
|
|
61
|
+
ok: {
|
|
62
|
+
const: boolean;
|
|
63
|
+
};
|
|
64
|
+
error: {
|
|
65
|
+
type: string;
|
|
66
|
+
required: string[];
|
|
67
|
+
properties: {
|
|
68
|
+
code: {
|
|
69
|
+
enum: string[];
|
|
70
|
+
};
|
|
71
|
+
message: {
|
|
72
|
+
type: string;
|
|
73
|
+
};
|
|
74
|
+
retryable: {
|
|
75
|
+
type: string;
|
|
76
|
+
};
|
|
77
|
+
next: {
|
|
78
|
+
type: string;
|
|
79
|
+
};
|
|
80
|
+
details: {};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
meta: {
|
|
84
|
+
type: string;
|
|
85
|
+
required: string[];
|
|
86
|
+
properties: {
|
|
87
|
+
contractVersion: {
|
|
88
|
+
const: string;
|
|
89
|
+
};
|
|
90
|
+
cli: {
|
|
91
|
+
const: string;
|
|
92
|
+
type?: undefined;
|
|
93
|
+
} | {
|
|
94
|
+
type: string;
|
|
95
|
+
const?: undefined;
|
|
96
|
+
};
|
|
97
|
+
version: {
|
|
98
|
+
const: string;
|
|
99
|
+
type?: undefined;
|
|
100
|
+
} | {
|
|
101
|
+
type: string;
|
|
102
|
+
const?: undefined;
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
data?: undefined;
|
|
107
|
+
};
|
|
108
|
+
})[];
|
|
109
|
+
};
|
|
110
|
+
export declare function schemaTemplates(app?: Application): Record<string, string>;
|
|
111
|
+
export declare function schemaArtifacts(app: Application): Record<string, string>;
|
|
112
|
+
export declare const commandDocumentSchema: {
|
|
113
|
+
$schema: "https://json-schema.org/draft/2020-12/schema";
|
|
114
|
+
$id: string;
|
|
115
|
+
type: string;
|
|
116
|
+
required: string[];
|
|
117
|
+
properties: {
|
|
118
|
+
contractVersion: {
|
|
119
|
+
const: string;
|
|
120
|
+
};
|
|
121
|
+
name: {
|
|
122
|
+
type: string;
|
|
123
|
+
};
|
|
124
|
+
packageName: {
|
|
125
|
+
type: string;
|
|
126
|
+
pattern: string;
|
|
127
|
+
};
|
|
128
|
+
version: {
|
|
129
|
+
type: string;
|
|
130
|
+
};
|
|
131
|
+
description: {
|
|
132
|
+
type: string;
|
|
133
|
+
};
|
|
134
|
+
commands: {
|
|
135
|
+
type: string;
|
|
136
|
+
additionalProperties: {
|
|
137
|
+
type: string;
|
|
138
|
+
required: string[];
|
|
139
|
+
properties: {
|
|
140
|
+
module: {
|
|
141
|
+
type: string;
|
|
142
|
+
};
|
|
143
|
+
description: {
|
|
144
|
+
type: string;
|
|
145
|
+
};
|
|
146
|
+
effects: {
|
|
147
|
+
enum: string[];
|
|
148
|
+
};
|
|
149
|
+
preview: {
|
|
150
|
+
type: string;
|
|
151
|
+
};
|
|
152
|
+
examples: {
|
|
153
|
+
type: string;
|
|
154
|
+
items: {
|
|
155
|
+
type: string;
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
input: {
|
|
159
|
+
type: string;
|
|
160
|
+
required: string[];
|
|
161
|
+
properties: {
|
|
162
|
+
type: {
|
|
163
|
+
const: string;
|
|
164
|
+
};
|
|
165
|
+
properties: {
|
|
166
|
+
type: string;
|
|
167
|
+
};
|
|
168
|
+
required: {
|
|
169
|
+
type: string;
|
|
170
|
+
items: {
|
|
171
|
+
type: string;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
additionalProperties: {
|
|
175
|
+
const: boolean;
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
result: {
|
|
180
|
+
type: string;
|
|
181
|
+
required: string[];
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { contractVersion, describe } from './contracts.js';
|
|
2
|
+
import { exitCodes } from './errors.js';
|
|
3
|
+
// Kept as a $schema member: the dialect is a schema identifier, not an address the CLI contacts, and code checks read it so
|
|
4
|
+
const dialect = { $schema: 'https://json-schema.org/draft/2020-12/schema' };
|
|
5
|
+
export const schemaDialect = dialect.$schema;
|
|
6
|
+
export const errorSchema = {
|
|
7
|
+
type: 'object', required: ['code', 'message', 'retryable', 'next'],
|
|
8
|
+
properties: { code: { enum: Object.keys(exitCodes) }, message: { type: 'string' }, retryable: { type: 'boolean' }, next: { type: 'string' }, details: {} },
|
|
9
|
+
};
|
|
10
|
+
export function resultSchema(app, command) {
|
|
11
|
+
const meta = { type: 'object', required: ['contractVersion', 'cli', 'version'], properties: { contractVersion: { const: contractVersion }, cli: app ? { const: app.packageName } : { type: 'string' }, version: app ? { const: app.version } : { type: 'string' } } };
|
|
12
|
+
return {
|
|
13
|
+
$schema: schemaDialect,
|
|
14
|
+
oneOf: [
|
|
15
|
+
{ type: 'object', required: ['ok', 'data', 'meta'], properties: { ok: { const: true }, data: app && command ? app.commands[command].result : {}, meta } },
|
|
16
|
+
{ type: 'object', required: ['ok', 'error', 'meta'], properties: { ok: { const: false }, error: errorSchema, meta } },
|
|
17
|
+
],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function schemaTemplates(app) {
|
|
21
|
+
const json = (value) => JSON.stringify(value, null, 2) + '\n';
|
|
22
|
+
return {
|
|
23
|
+
'commands.schema.json': json(commandDocumentSchema),
|
|
24
|
+
'result.schema.json': json(resultSchema(app)),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function schemaArtifacts(app) {
|
|
28
|
+
const facts = describe(app);
|
|
29
|
+
const json = (value) => JSON.stringify(value, null, 2) + '\n';
|
|
30
|
+
return {
|
|
31
|
+
...schemaTemplates(app),
|
|
32
|
+
'commands.json': json(facts),
|
|
33
|
+
...Object.fromEntries(Object.entries(facts.commands).flatMap(([name, command]) => {
|
|
34
|
+
const path = name.replaceAll(' ', '.');
|
|
35
|
+
return [[path + '.input.schema.json', json({ $schema: schemaDialect, ...command.input })], [path + '.result.schema.json', json(resultSchema(app, name))]];
|
|
36
|
+
})),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export const commandDocumentSchema = {
|
|
40
|
+
$schema: schemaDialect, $id: 'urn:lianxz:commands:1', type: 'object',
|
|
41
|
+
required: ['contractVersion', 'name', 'packageName', 'version', 'description', 'commands'],
|
|
42
|
+
properties: {
|
|
43
|
+
contractVersion: { const: contractVersion }, name: { type: 'string' }, packageName: { type: 'string', pattern: '^@tulcn/' }, version: { type: 'string' }, description: { type: 'string' },
|
|
44
|
+
commands: { type: 'object', additionalProperties: { type: 'object', required: ['description', 'effects', 'preview', 'examples', 'input', 'result'], properties: { module: { type: 'string' }, description: { type: 'string' }, effects: { enum: ['none', 'local', 'remote'] }, preview: { type: 'boolean' }, examples: { type: 'array', items: { type: 'string' } }, input: { type: 'object', required: ['type', 'properties', 'required'], properties: { type: { const: 'object' }, properties: { type: 'object' }, required: { type: 'array', items: { type: 'string' } }, additionalProperties: { const: false } } }, result: { type: 'object', required: ['type'] } } } },
|
|
45
|
+
},
|
|
46
|
+
};
|
package/dist/doctor.d.ts
ADDED