@sanity/workflows-cli 0.0.0-main-039-20261113092657
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 +1936 -0
- package/LICENSE +21 -0
- package/README.md +814 -0
- package/bin/run.js +8 -0
- package/dist/command-ids.d.ts +10 -0
- package/dist/command-ids.js +10 -0
- package/dist/commands/workflows/abort.d.ts +29 -0
- package/dist/commands/workflows/abort.js +50 -0
- package/dist/commands/workflows/blueprints/generate.d.ts +12 -0
- package/dist/commands/workflows/blueprints/generate.js +46 -0
- package/dist/commands/workflows/codemod/packages.d.ts +13 -0
- package/dist/commands/workflows/codemod/packages.js +41 -0
- package/dist/commands/workflows/definitions/delete.d.ts +44 -0
- package/dist/commands/workflows/definitions/delete.js +74 -0
- package/dist/commands/workflows/definitions/diff.d.ts +23 -0
- package/dist/commands/workflows/definitions/diff.js +55 -0
- package/dist/commands/workflows/definitions/list.d.ts +51 -0
- package/dist/commands/workflows/definitions/list.js +139 -0
- package/dist/commands/workflows/definitions/show.d.ts +65 -0
- package/dist/commands/workflows/definitions/show.js +129 -0
- package/dist/commands/workflows/definitions/validate.d.ts +14 -0
- package/dist/commands/workflows/definitions/validate.js +50 -0
- package/dist/commands/workflows/deploy.d.ts +117 -0
- package/dist/commands/workflows/deploy.js +266 -0
- package/dist/commands/workflows/diagnose.d.ts +52 -0
- package/dist/commands/workflows/diagnose.js +195 -0
- package/dist/commands/workflows/fire-action.d.ts +53 -0
- package/dist/commands/workflows/fire-action.js +182 -0
- package/dist/commands/workflows/list.d.ts +78 -0
- package/dist/commands/workflows/list.js +248 -0
- package/dist/commands/workflows/nuke.d.ts +73 -0
- package/dist/commands/workflows/nuke.js +176 -0
- package/dist/commands/workflows/reset-activity.d.ts +32 -0
- package/dist/commands/workflows/reset-activity.js +60 -0
- package/dist/commands/workflows/show.d.ts +29 -0
- package/dist/commands/workflows/show.js +156 -0
- package/dist/commands/workflows/start.d.ts +57 -0
- package/dist/commands/workflows/start.js +196 -0
- package/dist/commands/workflows/tail.d.ts +23 -0
- package/dist/commands/workflows/tail.js +83 -0
- package/dist/commands/workflows/whoami.d.ts +15 -0
- package/dist/commands/workflows/whoami.js +53 -0
- package/dist/help.d.ts +13 -0
- package/dist/help.js +50 -0
- package/dist/hooks/finally/telemetry.d.ts +3 -0
- package/dist/hooks/finally/telemetry.js +10 -0
- package/dist/hooks/prerun/telemetry.d.ts +28 -0
- package/dist/hooks/prerun/telemetry.js +37 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/base-command.d.ts +14 -0
- package/dist/lib/base-command.js +41 -0
- package/dist/lib/blueprint-emit.d.ts +56 -0
- package/dist/lib/blueprint-emit.js +233 -0
- package/dist/lib/blueprint-needs.d.ts +5 -0
- package/dist/lib/blueprint-needs.js +231 -0
- package/dist/lib/blueprint-stacks.d.ts +6 -0
- package/dist/lib/blueprint-stacks.js +111 -0
- package/dist/lib/cause-detail.d.ts +9 -0
- package/dist/lib/cause-detail.js +112 -0
- package/dist/lib/client.d.ts +27 -0
- package/dist/lib/client.js +45 -0
- package/dist/lib/codemod/ast.d.ts +17 -0
- package/dist/lib/codemod/ast.js +14 -0
- package/dist/lib/codemod/export-rename.d.ts +17 -0
- package/dist/lib/codemod/export-rename.js +253 -0
- package/dist/lib/codemod/manifest-rewrite.d.ts +14 -0
- package/dist/lib/codemod/manifest-rewrite.js +97 -0
- package/dist/lib/codemod/package-map.d.ts +26 -0
- package/dist/lib/codemod/package-map.js +90 -0
- package/dist/lib/codemod/report.d.ts +4 -0
- package/dist/lib/codemod/report.js +46 -0
- package/dist/lib/codemod/run.d.ts +23 -0
- package/dist/lib/codemod/run.js +106 -0
- package/dist/lib/codemod/source-rewrite.d.ts +20 -0
- package/dist/lib/codemod/source-rewrite.js +149 -0
- package/dist/lib/command-context.d.ts +20 -0
- package/dist/lib/command-context.js +24 -0
- package/dist/lib/command-telemetry.d.ts +54 -0
- package/dist/lib/command-telemetry.js +82 -0
- package/dist/lib/context.d.ts +155 -0
- package/dist/lib/context.js +272 -0
- package/dist/lib/default-partition.d.ts +23 -0
- package/dist/lib/default-partition.js +48 -0
- package/dist/lib/definition-files.d.ts +14 -0
- package/dist/lib/definition-files.js +60 -0
- package/dist/lib/definitions.d.ts +63 -0
- package/dist/lib/definitions.js +84 -0
- package/dist/lib/diff.d.ts +12 -0
- package/dist/lib/diff.js +93 -0
- package/dist/lib/env.d.ts +7 -0
- package/dist/lib/env.js +8 -0
- package/dist/lib/fail.d.ts +36 -0
- package/dist/lib/fail.js +55 -0
- package/dist/lib/failure-kind.d.ts +7 -0
- package/dist/lib/failure-kind.js +66 -0
- package/dist/lib/flags.d.ts +49 -0
- package/dist/lib/flags.js +117 -0
- package/dist/lib/follow-up-command.d.ts +18 -0
- package/dist/lib/follow-up-command.js +12 -0
- package/dist/lib/history-rows.d.ts +3 -0
- package/dist/lib/history-rows.js +44 -0
- package/dist/lib/load-config.d.ts +51 -0
- package/dist/lib/load-config.js +108 -0
- package/dist/lib/nuke.d.ts +124 -0
- package/dist/lib/nuke.js +206 -0
- package/dist/lib/operation-args.d.ts +42 -0
- package/dist/lib/operation-args.js +21 -0
- package/dist/lib/ops-report.d.ts +45 -0
- package/dist/lib/ops-report.js +31 -0
- package/dist/lib/params.d.ts +3 -0
- package/dist/lib/params.js +21 -0
- package/dist/lib/pick-dataset.d.ts +10 -0
- package/dist/lib/pick-dataset.js +30 -0
- package/dist/lib/prompt.d.ts +35 -0
- package/dist/lib/prompt.js +11 -0
- package/dist/lib/read-fanout.d.ts +45 -0
- package/dist/lib/read-fanout.js +44 -0
- package/dist/lib/select-deployment.d.ts +31 -0
- package/dist/lib/select-deployment.js +91 -0
- package/dist/lib/share-definitions.d.ts +101 -0
- package/dist/lib/share-definitions.js +188 -0
- package/dist/lib/telemetry-setup.d.ts +81 -0
- package/dist/lib/telemetry-setup.js +111 -0
- package/dist/lib/telemetry.d.ts +81 -0
- package/dist/lib/telemetry.js +90 -0
- package/dist/lib/ui.d.ts +91 -0
- package/dist/lib/ui.js +116 -0
- package/dist/standalone-argv.d.ts +14 -0
- package/dist/standalone-argv.js +57 -0
- package/oclif.manifest.json +1731 -0
- package/package.json +121 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { SanityClient } from '@sanity/client';
|
|
2
|
+
import { type DeployedDefinition, type WorkflowConfig, type WorkflowDefinition } from '@sanity/workflows';
|
|
3
|
+
interface DefinitionShowQueryArgs {
|
|
4
|
+
name: string;
|
|
5
|
+
partition?: string | undefined;
|
|
6
|
+
version?: number | undefined;
|
|
7
|
+
}
|
|
8
|
+
export declare function buildDefinitionShowQuery(flags: DefinitionShowQueryArgs): {
|
|
9
|
+
groq: string;
|
|
10
|
+
params: Record<string, unknown>;
|
|
11
|
+
};
|
|
12
|
+
export declare function buildDefinitionPartitionsQuery(name: string): {
|
|
13
|
+
groq: string;
|
|
14
|
+
params: Record<string, unknown>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Fetch the deployed definition a command targets — latest unless pinned via
|
|
18
|
+
* `version`. An absent pinned version always fails (the caller asked for
|
|
19
|
+
* something specific); an absent latest returns `undefined` so the caller
|
|
20
|
+
* decides whether that's fatal.
|
|
21
|
+
*/
|
|
22
|
+
export declare function fetchDeployedDefinition({ client, name, partition, version, }: {
|
|
23
|
+
client: SanityClient;
|
|
24
|
+
name: string;
|
|
25
|
+
partition: string;
|
|
26
|
+
version: number | undefined;
|
|
27
|
+
}): Promise<DeployedDefinition | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Gate a definition filter against the whole config before a command resolves
|
|
30
|
+
* which deployment it targets — a name that matches no definition anywhere can
|
|
31
|
+
* never deploy, whichever deployment is chosen, so it fails here rather than
|
|
32
|
+
* behind the interactive deployment picker. Membership in the targeted
|
|
33
|
+
* deployment stays with {@link selectDefinitions}, which fails on a name only
|
|
34
|
+
* some other deployment carries.
|
|
35
|
+
*
|
|
36
|
+
* The filter and the deployment selector both take a bare name, so a value
|
|
37
|
+
* naming a deployment is pointed at the flag it belongs to instead of getting a
|
|
38
|
+
* dead-end list of definitions it was never going to be in — in which case the
|
|
39
|
+
* available-definitions hint is replaced, not appended to.
|
|
40
|
+
*/
|
|
41
|
+
export declare function assertKnownDefinition(config: WorkflowConfig, only: string | undefined): void;
|
|
42
|
+
/**
|
|
43
|
+
* Narrow a batch to one definition name (`only`), failing with the available
|
|
44
|
+
* names when there's no match. With `only` unset the whole batch passes through
|
|
45
|
+
* (a fresh array, so callers can mutate without aliasing). `context` prefixes
|
|
46
|
+
* the failure so callers whose run spans several deployments can attribute it.
|
|
47
|
+
*/
|
|
48
|
+
export declare function selectDefinitions(allDefinitions: WorkflowDefinition[], { only, context }: {
|
|
49
|
+
only: string | undefined;
|
|
50
|
+
context?: string;
|
|
51
|
+
}): WorkflowDefinition[];
|
|
52
|
+
/**
|
|
53
|
+
* Per-definition structural/GROQ checks plus one batch-level check the engine
|
|
54
|
+
* doesn't do for us: duplicate `name` within the set. Returns the
|
|
55
|
+
* human-readable error lines; empty means the batch is valid.
|
|
56
|
+
*/
|
|
57
|
+
export declare function collectValidationErrors(defs: WorkflowDefinition[]): string[];
|
|
58
|
+
/** Validate a set of definitions, exiting with the collected errors on any
|
|
59
|
+
* failure. The single validate-then-fail gate `deploy` and `definitions diff`
|
|
60
|
+
* share, so the two can't drift into divergent failure messages. `context`
|
|
61
|
+
* prefixes the failure with the deployment it belongs to in multi-target runs. */
|
|
62
|
+
export declare function validateOrFail(defs: WorkflowDefinition[], context?: string): void;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { WORKFLOW_DEFINITION_TYPE, assertReadableModel, definitionPartitionsGroq, errorMessage, partitionScopeFilter, validateDefinition, } from '@sanity/workflows';
|
|
2
|
+
import { fail } from './fail.js';
|
|
3
|
+
export function buildDefinitionShowQuery(flags) {
|
|
4
|
+
const params = { name: flags.name };
|
|
5
|
+
const filters = [`_type == "${WORKFLOW_DEFINITION_TYPE}"`, 'name == $name'];
|
|
6
|
+
if (flags.version !== undefined) {
|
|
7
|
+
params.version = flags.version;
|
|
8
|
+
filters.push('version == $version');
|
|
9
|
+
}
|
|
10
|
+
if (flags.partition) {
|
|
11
|
+
params.tag = flags.partition;
|
|
12
|
+
filters.push(partitionScopeFilter());
|
|
13
|
+
}
|
|
14
|
+
const groq = `*[${filters.join(' && ')}] | order(version desc) [0]`;
|
|
15
|
+
return { groq, params };
|
|
16
|
+
}
|
|
17
|
+
export function buildDefinitionPartitionsQuery(name) {
|
|
18
|
+
return {
|
|
19
|
+
groq: definitionPartitionsGroq(),
|
|
20
|
+
params: { definition: name },
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export async function fetchDeployedDefinition({ client, name, partition, version, }) {
|
|
24
|
+
const { groq, params } = buildDefinitionShowQuery({ name, partition, version });
|
|
25
|
+
const deployed = await client.fetch(groq, params, {
|
|
26
|
+
tag: 'definition.load',
|
|
27
|
+
});
|
|
28
|
+
if (deployed === null && version !== undefined) {
|
|
29
|
+
fail(`No deployed definition "${name}" v${version}.`);
|
|
30
|
+
}
|
|
31
|
+
return deployed === null ? undefined : assertReadableModel(deployed);
|
|
32
|
+
}
|
|
33
|
+
function availableDefinitions(names) {
|
|
34
|
+
return `Available: ${names.join(', ')}`;
|
|
35
|
+
}
|
|
36
|
+
export function assertKnownDefinition(config, only) {
|
|
37
|
+
if (!only) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const names = new Set(config.deployments.flatMap((d) => d.definitions.map((def) => def.name)));
|
|
41
|
+
if (names.has(only)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const namesADeployment = config.deployments.some((d) => d.name === only);
|
|
45
|
+
fail(`No definition named "${only}".`, namesADeployment
|
|
46
|
+
? `"${only}" is a deployment — did you mean --deployment ${only}?`
|
|
47
|
+
: availableDefinitions([...names]));
|
|
48
|
+
}
|
|
49
|
+
export function selectDefinitions(allDefinitions, { only, context = '' }) {
|
|
50
|
+
if (!only) {
|
|
51
|
+
return [...allDefinitions];
|
|
52
|
+
}
|
|
53
|
+
const filtered = allDefinitions.filter((d) => d.name === only);
|
|
54
|
+
if (filtered.length === 0) {
|
|
55
|
+
fail(`${context}No definition named "${only}".`, availableDefinitions(allDefinitions.map((d) => d.name)));
|
|
56
|
+
}
|
|
57
|
+
return filtered;
|
|
58
|
+
}
|
|
59
|
+
export function collectValidationErrors(defs) {
|
|
60
|
+
const errors = [];
|
|
61
|
+
const seen = new Map();
|
|
62
|
+
for (const d of defs) {
|
|
63
|
+
const count = (seen.get(d.name) ?? 0) + 1;
|
|
64
|
+
seen.set(d.name, count);
|
|
65
|
+
if (count === 2) {
|
|
66
|
+
errors.push(` · duplicate name "${d.name}" in local batch`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (const d of defs) {
|
|
70
|
+
try {
|
|
71
|
+
validateDefinition(d);
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
errors.push(` · ${d.name}: ${errorMessage(err)}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return errors;
|
|
78
|
+
}
|
|
79
|
+
export function validateOrFail(defs, context = '') {
|
|
80
|
+
const errors = collectValidationErrors(defs);
|
|
81
|
+
if (errors.length > 0) {
|
|
82
|
+
fail(`${context}Validation failed:`, errors.join('\n'));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DiffEntry } from '@sanity/workflows';
|
|
2
|
+
/** One summary line per diff entry, tagged create / unchanged, followed by the
|
|
3
|
+
* warnings a deploy would print for it. Definitions are immutable, so a
|
|
4
|
+
* content change is a new version (`create`), never an update. */
|
|
5
|
+
export declare function summaryLines(entries: DiffEntry[]): string[];
|
|
6
|
+
/** Render a JSON diff of `oldObj` → `newObj` as `+`/`-`/context lines, long
|
|
7
|
+
* unchanged runs collapsed to a dim `… N unchanged lines` marker. */
|
|
8
|
+
export declare function diffLines(oldObj: object, newObj: object): string[];
|
|
9
|
+
/** The full diff report: the summary block plus a per-change diff of the
|
|
10
|
+
* authored content — the deploy envelope is stripped from both sides (the
|
|
11
|
+
* version and docId already render on the summary line). */
|
|
12
|
+
export declare function diffReport(entries: DiffEntry[]): string[];
|
package/dist/lib/diff.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
import { diffJson } from 'diff';
|
|
3
|
+
import logSymbols from 'log-symbols';
|
|
4
|
+
import { sectionHeader, warningLines } from './ui.js';
|
|
5
|
+
export function summaryLines(entries) {
|
|
6
|
+
const tags = {
|
|
7
|
+
create: styleText('green', '+ create '),
|
|
8
|
+
unchanged: styleText('dim', ' unchanged'),
|
|
9
|
+
};
|
|
10
|
+
return entries.flatMap((e) => {
|
|
11
|
+
const head = ` ${tags[e.status]} ${e.name} v${e.version} ${styleText('dim', `(${e.docId})`)}`;
|
|
12
|
+
return [head, ...warningLines(e.warnings)];
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
const CONTEXT_LINES = 3;
|
|
16
|
+
const DEPLOY_STAMPED_KEYS = new Set(['tag', 'version', 'contentHash']);
|
|
17
|
+
function authoredContent(doc) {
|
|
18
|
+
return Object.fromEntries(Object.entries(doc).filter(([key]) => !key.startsWith('_') && !DEPLOY_STAMPED_KEYS.has(key)));
|
|
19
|
+
}
|
|
20
|
+
function chunkKind(chunk) {
|
|
21
|
+
if (chunk.added) {
|
|
22
|
+
return 'added';
|
|
23
|
+
}
|
|
24
|
+
if (chunk.removed) {
|
|
25
|
+
return 'removed';
|
|
26
|
+
}
|
|
27
|
+
return 'context';
|
|
28
|
+
}
|
|
29
|
+
function taggedLines(oldObj, newObj) {
|
|
30
|
+
return diffJson(oldObj, newObj).flatMap((chunk) => {
|
|
31
|
+
const chunkLines = chunk.value.split('\n');
|
|
32
|
+
if (chunkLines.at(-1) === '') {
|
|
33
|
+
chunkLines.pop();
|
|
34
|
+
}
|
|
35
|
+
return chunkLines.map((text) => ({ kind: chunkKind(chunk), text }));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function collapseUnchanged(lines, context) {
|
|
39
|
+
const changed = lines.flatMap((line, i) => (line.kind === 'context' ? [] : [i]));
|
|
40
|
+
if (changed.length === 0) {
|
|
41
|
+
return lines;
|
|
42
|
+
}
|
|
43
|
+
const keep = lines.map((_, i) => changed.some((c) => Math.abs(c - i) <= context));
|
|
44
|
+
const out = [];
|
|
45
|
+
let run = [];
|
|
46
|
+
const flushRun = () => {
|
|
47
|
+
if (run.length === 1) {
|
|
48
|
+
out.push(...run);
|
|
49
|
+
}
|
|
50
|
+
else if (run.length > 1) {
|
|
51
|
+
out.push({ kind: 'elision', count: run.length });
|
|
52
|
+
}
|
|
53
|
+
run = [];
|
|
54
|
+
};
|
|
55
|
+
lines.forEach((line, i) => {
|
|
56
|
+
if (keep[i]) {
|
|
57
|
+
flushRun();
|
|
58
|
+
out.push(line);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
run.push(line);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
flushRun();
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
function renderLine(line) {
|
|
68
|
+
if (line.kind === 'elision') {
|
|
69
|
+
return styleText('dim', ` … ${line.count} unchanged lines`);
|
|
70
|
+
}
|
|
71
|
+
if (line.kind === 'added') {
|
|
72
|
+
return styleText('green', `+ ${line.text}`);
|
|
73
|
+
}
|
|
74
|
+
if (line.kind === 'removed') {
|
|
75
|
+
return styleText('red', `- ${line.text}`);
|
|
76
|
+
}
|
|
77
|
+
return styleText('dim', ` ${line.text}`);
|
|
78
|
+
}
|
|
79
|
+
export function diffLines(oldObj, newObj) {
|
|
80
|
+
return collapseUnchanged(taggedLines(oldObj, newObj), CONTEXT_LINES).map(renderLine);
|
|
81
|
+
}
|
|
82
|
+
export function diffReport(entries) {
|
|
83
|
+
const lines = ['', sectionHeader('Summary'), ...summaryLines(entries)];
|
|
84
|
+
const changes = entries.filter((e) => e.status !== 'unchanged');
|
|
85
|
+
if (changes.length === 0) {
|
|
86
|
+
lines.push('', `${logSymbols.success} no changes — running deploy would be a no-op`);
|
|
87
|
+
return lines;
|
|
88
|
+
}
|
|
89
|
+
for (const e of changes) {
|
|
90
|
+
lines.push('', styleText('bold', `── ${e.name} v${e.version} ──`), ...diffLines(authoredContent(e.existing ?? {}), authoredContent(e.expected)));
|
|
91
|
+
}
|
|
92
|
+
return lines;
|
|
93
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const ENV: {
|
|
2
|
+
readonly SANITY_AUTH_TOKEN: 'SANITY_AUTH_TOKEN';
|
|
3
|
+
readonly SANITY_API_HOST: 'SANITY_API_HOST';
|
|
4
|
+
};
|
|
5
|
+
/** The explicit env token, or undefined when unset/blank. Token resolution
|
|
6
|
+
* and the auth recovery hint must agree on this. */
|
|
7
|
+
export declare function envAuthToken(): string | undefined;
|
package/dist/lib/env.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render a user-facing error to stderr with no oclif framing or Node stack,
|
|
3
|
+
* and exit non-zero.
|
|
4
|
+
*
|
|
5
|
+
* Exits via oclif's `Errors.exit`, never `process.exit`: a raw `process.exit`
|
|
6
|
+
* would skip the command's `finally` hook, dropping the trace and any
|
|
7
|
+
* batched telemetry that hasn't flushed yet.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fail(headline: string, detail?: string): never;
|
|
10
|
+
/**
|
|
11
|
+
* {@link fail} with {@link failureDetail} of `cause` as the detail. The exit
|
|
12
|
+
* error keeps `cause`, so the telemetry hook can classify the failure.
|
|
13
|
+
*/
|
|
14
|
+
export declare function failOn(cause: unknown, headline: string): never;
|
|
15
|
+
export declare function warnOnStderr(message: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Run `fn`, funnelling a thrown env/validation error through {@link failOn}
|
|
18
|
+
* so an expected, user-fixable failure (missing input, bad value) reads
|
|
19
|
+
* clean instead of as an oclif stack trace. Keeps the throwing `read*`
|
|
20
|
+
* helpers pure and unit-testable while giving callers one clean boundary.
|
|
21
|
+
*/
|
|
22
|
+
export declare function failOnThrow<T>(headline: string, fn: () => T): T;
|
|
23
|
+
/** An API rejection of the auth token itself — as opposed to a 403, where
|
|
24
|
+
* the identity is valid but lacks the role for the operation. Walks the
|
|
25
|
+
* `cause` chain because the 401 often arrives wrapped: the engine's
|
|
26
|
+
* `/users/me` actor resolution rethrows it inside its own `Error`. */
|
|
27
|
+
export declare function isAuthRejection(err: unknown): boolean;
|
|
28
|
+
/** `err` followed by its `cause` links, depth-bounded against self-referencing chains. */
|
|
29
|
+
export declare function causeChain(err: unknown): unknown[];
|
|
30
|
+
/**
|
|
31
|
+
* The detail block for a command failure: the error's message, plus the
|
|
32
|
+
* auth recovery hint when the API rejected the token. Every site that
|
|
33
|
+
* renders a caught command error should build its detail here, so the
|
|
34
|
+
* hint reaches the user no matter which call the token died at.
|
|
35
|
+
*/
|
|
36
|
+
export declare function failureDetail(err: unknown): string;
|
package/dist/lib/fail.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
import { Errors } from '@oclif/core';
|
|
3
|
+
import { ClientError } from '@sanity/client';
|
|
4
|
+
import { errorMessage } from '@sanity/workflows';
|
|
5
|
+
import logSymbols from 'log-symbols';
|
|
6
|
+
import { envAuthToken } from './env.js';
|
|
7
|
+
export function fail(headline, detail) {
|
|
8
|
+
writeFailure(headline, detail);
|
|
9
|
+
return Errors.exit(1);
|
|
10
|
+
}
|
|
11
|
+
export function failOn(cause, headline) {
|
|
12
|
+
writeFailure(headline, failureDetail(cause));
|
|
13
|
+
throw Object.defineProperty(new Errors.ExitError(1), 'cause', { value: cause });
|
|
14
|
+
}
|
|
15
|
+
function writeFailure(headline, detail) {
|
|
16
|
+
process.stderr.write(`${styleText('red', `${logSymbols.error} ${headline}`)}\n`);
|
|
17
|
+
if (detail !== undefined && detail !== '') {
|
|
18
|
+
for (const line of detail.split('\n')) {
|
|
19
|
+
process.stderr.write(` ${line}\n`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function warnOnStderr(message) {
|
|
24
|
+
process.stderr.write(`${styleText('yellow', `${logSymbols.warning} ${message}`)}\n`);
|
|
25
|
+
}
|
|
26
|
+
export function failOnThrow(headline, fn) {
|
|
27
|
+
try {
|
|
28
|
+
return fn();
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
failOn(err, headline);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function isAuthRejection(err) {
|
|
35
|
+
return causeChain(err).some((link) => link instanceof ClientError && link.statusCode === 401);
|
|
36
|
+
}
|
|
37
|
+
export function causeChain(err) {
|
|
38
|
+
const chain = [];
|
|
39
|
+
let current = err;
|
|
40
|
+
while (current !== undefined && chain.length < 5) {
|
|
41
|
+
chain.push(current);
|
|
42
|
+
current = current instanceof Error ? current.cause : undefined;
|
|
43
|
+
}
|
|
44
|
+
return chain;
|
|
45
|
+
}
|
|
46
|
+
function authRecoveryHint() {
|
|
47
|
+
if (envAuthToken() !== undefined) {
|
|
48
|
+
return 'The SANITY_AUTH_TOKEN token was rejected — check it is valid and has access to this project.';
|
|
49
|
+
}
|
|
50
|
+
return 'Your `sanity login` session may have expired — run `sanity login` and retry.';
|
|
51
|
+
}
|
|
52
|
+
export function failureDetail(err) {
|
|
53
|
+
const message = errorMessage(err);
|
|
54
|
+
return isAuthRejection(err) ? `${message}\n${authRecoveryHint()}` : message;
|
|
55
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type WorkflowErrorKind } from '@sanity/workflows';
|
|
2
|
+
import type { WorkflowCliCommandResult } from './telemetry.ts';
|
|
3
|
+
/** Why a command failed, as a closed label. It never carries text from the error. */
|
|
4
|
+
export type CliFailureKind = WorkflowErrorKind | 'usage' | 'auth-missing' | 'permission-denied' | 'resource-not-found' | 'request-failed' | 'config-invalid' | 'definition-file-invalid' | 'other';
|
|
5
|
+
/** Record `err` as a flag or argument parsing failure. */
|
|
6
|
+
export declare function markUsageError(err: unknown): void;
|
|
7
|
+
export declare function commandResult(error: unknown): WorkflowCliCommandResult;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ClientError, ConnectionFailedError, isTimeoutError, ServerError } from '@sanity/client';
|
|
2
|
+
import { WorkflowError } from '@sanity/workflows';
|
|
3
|
+
import { AuthMissingError } from './client.js';
|
|
4
|
+
import { DefinitionFileError } from './definition-files.js';
|
|
5
|
+
import { causeChain } from './fail.js';
|
|
6
|
+
import { ConfigLoadError } from './load-config.js';
|
|
7
|
+
const usageErrors = new WeakSet();
|
|
8
|
+
export function markUsageError(err) {
|
|
9
|
+
if (err instanceof Error) {
|
|
10
|
+
usageErrors.add(err);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const STATUS_KINDS = {
|
|
14
|
+
401: 'permission-denied',
|
|
15
|
+
403: 'permission-denied',
|
|
16
|
+
404: 'resource-not-found',
|
|
17
|
+
};
|
|
18
|
+
const NETWORK_CODES = new Set([
|
|
19
|
+
'ECONNREFUSED',
|
|
20
|
+
'ECONNRESET',
|
|
21
|
+
'ENOTFOUND',
|
|
22
|
+
'EAI_AGAIN',
|
|
23
|
+
'ETIMEDOUT',
|
|
24
|
+
'EHOSTUNREACH',
|
|
25
|
+
'ENETUNREACH',
|
|
26
|
+
'ENETDOWN',
|
|
27
|
+
'EPIPE',
|
|
28
|
+
'UND_ERR_CONNECT_TIMEOUT',
|
|
29
|
+
'UND_ERR_SOCKET',
|
|
30
|
+
]);
|
|
31
|
+
function errorCode(err) {
|
|
32
|
+
return err instanceof Error && 'code' in err && typeof err.code === 'string' ? err.code : '';
|
|
33
|
+
}
|
|
34
|
+
function statusKind(status) {
|
|
35
|
+
return (status === undefined ? undefined : STATUS_KINDS[status]) ?? 'request-failed';
|
|
36
|
+
}
|
|
37
|
+
function requestKind(err) {
|
|
38
|
+
if (err instanceof ClientError || err instanceof ServerError) {
|
|
39
|
+
return statusKind(err.statusCode);
|
|
40
|
+
}
|
|
41
|
+
if (err instanceof ConnectionFailedError) {
|
|
42
|
+
return statusKind(err.status);
|
|
43
|
+
}
|
|
44
|
+
return isTimeoutError(err) || NETWORK_CODES.has(errorCode(err)) ? 'request-failed' : undefined;
|
|
45
|
+
}
|
|
46
|
+
const RULES = [
|
|
47
|
+
(err) => (err instanceof WorkflowError ? err.kind : undefined),
|
|
48
|
+
(err) => (err instanceof Error && usageErrors.has(err) ? 'usage' : undefined),
|
|
49
|
+
(err) => (err instanceof AuthMissingError ? 'auth-missing' : undefined),
|
|
50
|
+
requestKind,
|
|
51
|
+
(err) => (err instanceof ConfigLoadError ? 'config-invalid' : undefined),
|
|
52
|
+
(err) => (err instanceof DefinitionFileError ? 'definition-file-invalid' : undefined),
|
|
53
|
+
];
|
|
54
|
+
function failureKind(error) {
|
|
55
|
+
const chain = causeChain(error);
|
|
56
|
+
for (const rule of RULES) {
|
|
57
|
+
const kind = chain.map(rule).find((match) => match !== undefined);
|
|
58
|
+
if (kind !== undefined) {
|
|
59
|
+
return kind;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return 'other';
|
|
63
|
+
}
|
|
64
|
+
export function commandResult(error) {
|
|
65
|
+
return error === undefined ? { success: true } : { success: false, failureKind: failureKind(error) };
|
|
66
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export declare function flagTokens(argv: string[]): string[];
|
|
2
|
+
export declare function isLongFlag(token: string, name: string): boolean;
|
|
3
|
+
/** The last value of a flag read straight off argv, by its long name or its
|
|
4
|
+
* `char` short form, for code that runs before oclif parses flags. */
|
|
5
|
+
export declare function rawFlagValue(argv: string[], flag: {
|
|
6
|
+
name: string;
|
|
7
|
+
char?: string | undefined;
|
|
8
|
+
}): string | undefined;
|
|
9
|
+
export declare function partitionFlag(options: {
|
|
10
|
+
description: string;
|
|
11
|
+
exclusive?: string[];
|
|
12
|
+
}): import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
/** For every command that reads `sanity.workflow.ts`. */
|
|
14
|
+
export declare const configFlags: {
|
|
15
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
16
|
+
};
|
|
17
|
+
/** Name one resource for a read command to inspect, the way the Sanity CLI's
|
|
18
|
+
* `--project-id` / `--dataset` and `--media-library-id` name one. Write commands
|
|
19
|
+
* never spread these: they act through a deployment from `sanity.workflow.ts`. */
|
|
20
|
+
export declare const resourceFlags: {
|
|
21
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
22
|
+
dataset: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
23
|
+
'media-library-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
24
|
+
};
|
|
25
|
+
/** Selectors for commands that list or show what is deployed. */
|
|
26
|
+
export declare const readFlags: {
|
|
27
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
28
|
+
deployment: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
29
|
+
partition: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
30
|
+
};
|
|
31
|
+
export declare const instanceFlags: {
|
|
32
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
33
|
+
deployment: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
34
|
+
partition: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
35
|
+
};
|
|
36
|
+
/** Deploy overrides the partition flag to select every matching deployment. */
|
|
37
|
+
export declare const deploymentFlags: {
|
|
38
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
39
|
+
deployment: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
40
|
+
partition: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
41
|
+
};
|
|
42
|
+
/** Structured stdout instead of the rendered view. One payload convention:
|
|
43
|
+
* verb commands emit their operation envelope (`instanceId` carries the doc
|
|
44
|
+
* `_id`); read commands emit document-shaped data — `show` commands print
|
|
45
|
+
* the fetched document as stored, list commands print the table's row view
|
|
46
|
+
* (plus each row's `resource`) under a `truncated` flag. */
|
|
47
|
+
export declare const jsonFlags: {
|
|
48
|
+
json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
49
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
const instancePartitionDescription = "Workflow environment partition (e.g. prod, test) — narrows which resources are searched for the instance; the instance's stored partition is what the command uses.";
|
|
3
|
+
const partitionAsFilterDescription = 'Workflow environment partition (e.g. prod, test) — an optional query filter that also narrows which resources are searched; omit to span them all.';
|
|
4
|
+
export function flagTokens(argv) {
|
|
5
|
+
const terminator = argv.indexOf('--');
|
|
6
|
+
return terminator === -1 ? argv : argv.slice(0, terminator);
|
|
7
|
+
}
|
|
8
|
+
export function isLongFlag(token, name) {
|
|
9
|
+
return token === `--${name}` || token.startsWith(`--${name}=`);
|
|
10
|
+
}
|
|
11
|
+
function isShortFlag(token, char) {
|
|
12
|
+
return char !== undefined && token.startsWith(`-${char}`) && !token.startsWith('--');
|
|
13
|
+
}
|
|
14
|
+
function inlineValue(token, name) {
|
|
15
|
+
if (token.startsWith(`--${name}=`)) {
|
|
16
|
+
return token.slice(`--${name}=`.length);
|
|
17
|
+
}
|
|
18
|
+
if (token.startsWith('--') || token.length < 3) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
return token.slice(token[2] === '=' ? 3 : 2);
|
|
22
|
+
}
|
|
23
|
+
export function rawFlagValue(argv, flag) {
|
|
24
|
+
const tokens = flagTokens(argv);
|
|
25
|
+
const index = tokens.findLastIndex((token) => isLongFlag(token, flag.name) || isShortFlag(token, flag.char));
|
|
26
|
+
const token = tokens[index];
|
|
27
|
+
if (token === undefined) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return inlineValue(token, flag.name) ?? tokens[index + 1];
|
|
31
|
+
}
|
|
32
|
+
export function partitionFlag(options) {
|
|
33
|
+
return Flags.string({ aliases: ['tag'], ...options });
|
|
34
|
+
}
|
|
35
|
+
export const configFlags = {
|
|
36
|
+
path: Flags.string({
|
|
37
|
+
description: 'Path to a sanity.workflow file, or a directory to search from upward through its parents. Defaults to the current directory.',
|
|
38
|
+
helpValue: '<file|dir>',
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
const OVERRIDES_CONFIG = ' (overrides sanity.workflow.ts)';
|
|
42
|
+
const CONFIG_SELECTORS = ['path', 'deployment'];
|
|
43
|
+
function nonEmpty(name) {
|
|
44
|
+
return async (input) => {
|
|
45
|
+
const trimmed = input.trim();
|
|
46
|
+
if (trimmed === '') {
|
|
47
|
+
throw new Error(`\`--${name}\` cannot be empty if provided`);
|
|
48
|
+
}
|
|
49
|
+
return trimmed;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export const resourceFlags = {
|
|
53
|
+
'project-id': Flags.string({
|
|
54
|
+
char: 'p',
|
|
55
|
+
description: `Project ID of the dataset to read${OVERRIDES_CONFIG}`,
|
|
56
|
+
helpGroup: 'OVERRIDE',
|
|
57
|
+
helpValue: '<id>',
|
|
58
|
+
dependsOn: ['dataset'],
|
|
59
|
+
exclusive: ['media-library-id', ...CONFIG_SELECTORS],
|
|
60
|
+
parse: nonEmpty('project-id'),
|
|
61
|
+
}),
|
|
62
|
+
dataset: Flags.string({
|
|
63
|
+
char: 'd',
|
|
64
|
+
description: `Dataset to read${OVERRIDES_CONFIG}`,
|
|
65
|
+
helpGroup: 'OVERRIDE',
|
|
66
|
+
helpValue: '<name>',
|
|
67
|
+
dependsOn: ['project-id'],
|
|
68
|
+
exclusive: ['media-library-id', ...CONFIG_SELECTORS],
|
|
69
|
+
parse: nonEmpty('dataset'),
|
|
70
|
+
}),
|
|
71
|
+
'media-library-id': Flags.string({
|
|
72
|
+
description: `Media library to read${OVERRIDES_CONFIG}`,
|
|
73
|
+
helpGroup: 'OVERRIDE',
|
|
74
|
+
helpValue: '<id>',
|
|
75
|
+
exclusive: ['project-id', 'dataset', ...CONFIG_SELECTORS],
|
|
76
|
+
parse: nonEmpty('media-library-id'),
|
|
77
|
+
}),
|
|
78
|
+
};
|
|
79
|
+
export const readFlags = {
|
|
80
|
+
...configFlags,
|
|
81
|
+
deployment: Flags.string({
|
|
82
|
+
description: 'Deployment name — read only the resource and partition that deployment targets.',
|
|
83
|
+
exclusive: ['partition'],
|
|
84
|
+
}),
|
|
85
|
+
partition: partitionFlag({
|
|
86
|
+
description: partitionAsFilterDescription,
|
|
87
|
+
exclusive: ['deployment'],
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
export const instanceFlags = {
|
|
91
|
+
...configFlags,
|
|
92
|
+
deployment: Flags.string({
|
|
93
|
+
description: 'Deployment name — narrow the instance search to the resource that deployment targets; the partition still comes from the loaded instance.',
|
|
94
|
+
exclusive: ['partition'],
|
|
95
|
+
}),
|
|
96
|
+
partition: partitionFlag({
|
|
97
|
+
description: instancePartitionDescription,
|
|
98
|
+
exclusive: ['deployment'],
|
|
99
|
+
}),
|
|
100
|
+
};
|
|
101
|
+
export const deploymentFlags = {
|
|
102
|
+
...configFlags,
|
|
103
|
+
deployment: Flags.string({
|
|
104
|
+
description: 'Deployment name — the unique identity of one deployment in the config.',
|
|
105
|
+
exclusive: ['partition'],
|
|
106
|
+
}),
|
|
107
|
+
partition: partitionFlag({
|
|
108
|
+
description: 'Workflow environment partition (e.g. prod, test) — selects the deployment to act on while the partition names exactly one; pass --deployment when it spans several.',
|
|
109
|
+
exclusive: ['deployment'],
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
export const jsonFlags = {
|
|
113
|
+
json: Flags.boolean({
|
|
114
|
+
description: 'Emit structured JSON instead of rendered output.',
|
|
115
|
+
default: false,
|
|
116
|
+
}),
|
|
117
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The deployment selection a run was given, repeated on every command it suggests. */
|
|
2
|
+
export interface CommandSelection {
|
|
3
|
+
deployment?: string | undefined;
|
|
4
|
+
partition?: string | undefined;
|
|
5
|
+
allPartitions?: boolean | undefined;
|
|
6
|
+
}
|
|
7
|
+
/** What a suggested command needs from the current run. */
|
|
8
|
+
export interface FollowUpContext {
|
|
9
|
+
/** `this.config.bin`: the standalone binary, or the host CLI mounting it. */
|
|
10
|
+
bin: string;
|
|
11
|
+
selection?: CommandSelection;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A complete command line to suggest to the user: the binary that is running,
|
|
15
|
+
* the `workflows` topic when mounted in a host CLI, `words`, then the selection
|
|
16
|
+
* flags of the current run.
|
|
17
|
+
*/
|
|
18
|
+
export declare function followUpCommand({ bin, selection }: FollowUpContext, ...words: string[]): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { STANDALONE_BIN, WORKFLOWS_TOPIC } from '../command-ids.js';
|
|
2
|
+
export function followUpCommand({ bin, selection = {} }, ...words) {
|
|
3
|
+
const prefix = bin === STANDALONE_BIN ? [bin] : [bin, WORKFLOWS_TOPIC];
|
|
4
|
+
return [...prefix, ...words, ...selectionFlags(selection)].join(' ');
|
|
5
|
+
}
|
|
6
|
+
function selectionFlags({ deployment, partition, allPartitions }) {
|
|
7
|
+
return [
|
|
8
|
+
...(deployment === undefined ? [] : ['--deployment', deployment]),
|
|
9
|
+
...(partition === undefined ? [] : ['--partition', partition]),
|
|
10
|
+
...(allPartitions === true ? ['--all-partitions'] : []),
|
|
11
|
+
];
|
|
12
|
+
}
|