@sanity/workflows-cli 0.0.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +407 -0
- package/README.md +95 -44
- package/dist/commands/workflows/codemod/packages.js +2 -2
- package/dist/commands/workflows/definitions/delete.js +2 -2
- package/dist/commands/workflows/definitions/list.d.ts +1 -0
- package/dist/commands/workflows/definitions/show.d.ts +1 -0
- package/dist/commands/workflows/definitions/validate.js +3 -3
- package/dist/commands/workflows/deploy.d.ts +2 -2
- package/dist/commands/workflows/deploy.js +4 -4
- package/dist/commands/workflows/diagnose.d.ts +1 -0
- package/dist/commands/workflows/diagnose.js +2 -2
- package/dist/commands/workflows/fire-action.js +3 -3
- package/dist/commands/workflows/list.d.ts +1 -0
- package/dist/commands/workflows/list.js +2 -1
- package/dist/commands/workflows/nuke.js +2 -3
- package/dist/commands/workflows/show.d.ts +1 -0
- package/dist/commands/workflows/start.js +6 -6
- package/dist/commands/workflows/tail.d.ts +1 -0
- package/dist/commands/workflows/tail.js +3 -3
- package/dist/commands/workflows/whoami.d.ts +1 -0
- package/dist/commands/workflows/whoami.js +11 -11
- package/dist/hooks/finally/telemetry.js +2 -1
- package/dist/hooks/prerun/telemetry.d.ts +5 -0
- package/dist/hooks/prerun/telemetry.js +19 -4
- package/dist/lib/base-command.d.ts +3 -3
- package/dist/lib/base-command.js +12 -5
- package/dist/lib/blueprint-needs.d.ts +1 -1
- package/dist/lib/blueprint-needs.js +98 -19
- package/dist/lib/blueprint-stacks.js +38 -8
- package/dist/lib/client.d.ts +4 -1
- package/dist/lib/client.js +9 -4
- 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 +19 -0
- package/dist/lib/codemod/export-rename.js +337 -0
- package/dist/lib/codemod/manifest-rewrite.d.ts +5 -2
- package/dist/lib/codemod/manifest-rewrite.js +57 -21
- package/dist/lib/codemod/package-map.d.ts +5 -0
- package/dist/lib/codemod/package-map.js +27 -0
- package/dist/lib/codemod/report.d.ts +0 -1
- package/dist/lib/codemod/report.js +9 -1
- package/dist/lib/codemod/run.d.ts +5 -3
- package/dist/lib/codemod/run.js +6 -1
- package/dist/lib/codemod/source-rewrite.d.ts +6 -4
- package/dist/lib/codemod/source-rewrite.js +12 -17
- package/dist/lib/context.d.ts +6 -10
- package/dist/lib/context.js +18 -23
- package/dist/lib/default-partition.js +2 -3
- package/dist/lib/definition-files.d.ts +3 -0
- package/dist/lib/definition-files.js +5 -3
- package/dist/lib/fail.d.ts +8 -1
- package/dist/lib/fail.js +16 -7
- package/dist/lib/failure-kind.d.ts +7 -0
- package/dist/lib/failure-kind.js +66 -0
- package/dist/lib/flags.d.ts +7 -3
- package/dist/lib/flags.js +16 -6
- package/dist/lib/load-config.d.ts +5 -0
- package/dist/lib/load-config.js +4 -6
- package/dist/lib/nuke.d.ts +4 -4
- package/dist/lib/nuke.js +3 -3
- package/dist/lib/operation-args.d.ts +4 -4
- package/dist/lib/operation-args.js +1 -1
- package/dist/lib/ops-report.d.ts +1 -3
- package/dist/lib/ops-report.js +2 -2
- package/dist/lib/pick-resource.d.ts +13 -0
- package/dist/lib/pick-resource.js +82 -0
- package/dist/lib/read-fanout.d.ts +4 -4
- package/dist/lib/read-fanout.js +5 -4
- package/dist/lib/resource-target.d.ts +10 -0
- package/dist/lib/resource-target.js +12 -0
- package/dist/lib/telemetry-setup.d.ts +20 -24
- package/dist/lib/telemetry-setup.js +36 -30
- package/dist/lib/telemetry.d.ts +3 -0
- package/dist/lib/telemetry.js +2 -2
- package/dist/lib/ui.d.ts +1 -4
- package/dist/lib/ui.js +2 -4
- package/oclif.manifest.json +189 -26
- package/package.json +5 -5
- package/dist/lib/pick-dataset.d.ts +0 -10
- package/dist/lib/pick-dataset.js +0 -30
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { parseSync } from 'oxc-parser';
|
|
2
|
+
import { applyEdits, childEntries, isNode } from './ast.js';
|
|
3
|
+
import { renameExports } from './export-rename.js';
|
|
2
4
|
import { matchSpecifier, retiredNamesIn } from './package-map.js';
|
|
3
5
|
const MOCK_CALLEES = new Set([
|
|
4
6
|
'vi.mock',
|
|
@@ -16,9 +18,6 @@ const MOCK_CALLEES = new Set([
|
|
|
16
18
|
'require.resolve',
|
|
17
19
|
'import.meta.resolve',
|
|
18
20
|
]);
|
|
19
|
-
function isNode(value) {
|
|
20
|
-
return (typeof value === 'object' && value !== null && typeof Reflect.get(value, 'type') === 'string');
|
|
21
|
-
}
|
|
22
21
|
function calleeName(callee) {
|
|
23
22
|
if (!isNode(callee))
|
|
24
23
|
return undefined;
|
|
@@ -55,12 +54,6 @@ function specifierOf(node) {
|
|
|
55
54
|
function isStringLiteral(node) {
|
|
56
55
|
return node.type === 'Literal' && typeof node['value'] === 'string';
|
|
57
56
|
}
|
|
58
|
-
function childNodes(node) {
|
|
59
|
-
return Object.entries(node)
|
|
60
|
-
.filter(([key]) => key !== 'parent')
|
|
61
|
-
.flatMap(([, value]) => (Array.isArray(value) ? value : [value]))
|
|
62
|
-
.filter(isNode);
|
|
63
|
-
}
|
|
64
57
|
function lineStarts(source) {
|
|
65
58
|
return [0, ...[...source.matchAll(/\n/g)].map((match) => match.index + 1)];
|
|
66
59
|
}
|
|
@@ -92,7 +85,7 @@ export function rewriteSource(source, filename) {
|
|
|
92
85
|
text: firstLine(parsed.errors[0]?.message ?? ''),
|
|
93
86
|
},
|
|
94
87
|
];
|
|
95
|
-
return { source, rewrites: 0, reports };
|
|
88
|
+
return { source, rewrites: 0, renames: 0, reports };
|
|
96
89
|
}
|
|
97
90
|
const starts = lineStarts(source);
|
|
98
91
|
const specifiers = new Set();
|
|
@@ -138,18 +131,20 @@ export function rewriteSource(source, filename) {
|
|
|
138
131
|
visitOther(node);
|
|
139
132
|
return;
|
|
140
133
|
}
|
|
141
|
-
|
|
134
|
+
childEntries(node).forEach(([, child]) => visit(child));
|
|
142
135
|
};
|
|
143
|
-
|
|
136
|
+
const program = parsed.program;
|
|
137
|
+
visit(program);
|
|
138
|
+
const renamed = renameExports(program, source);
|
|
139
|
+
renamed.reports.forEach(({ kind, text, ...span }) => report(span, { kind, text }));
|
|
140
|
+
const kept = edits.filter((edit) => !renamed.edits.some((other) => other.start <= edit.start && edit.end <= other.end));
|
|
144
141
|
parsed.comments
|
|
145
142
|
.filter((comment) => retiredNamesIn(comment.value).length > 0)
|
|
146
143
|
.forEach((comment) => report(comment, { kind: 'comment', text: firstLine(comment.value) }));
|
|
147
|
-
const rewritten = edits
|
|
148
|
-
.toSorted((a, b) => b.start - a.start)
|
|
149
|
-
.reduce((text, edit) => text.slice(0, edit.start) + edit.text + text.slice(edit.end), source);
|
|
150
144
|
return {
|
|
151
|
-
source:
|
|
152
|
-
rewrites:
|
|
145
|
+
source: applyEdits(source, [...kept, ...renamed.edits]),
|
|
146
|
+
rewrites: kept.length,
|
|
147
|
+
renames: renamed.edits.length,
|
|
153
148
|
reports: reports.toSorted((a, b) => a.line - b.line),
|
|
154
149
|
};
|
|
155
150
|
}
|
package/dist/lib/context.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { SanityClient } from '@sanity/client';
|
|
2
2
|
import { type WorkflowConfig, type WorkflowDeployment, type WorkflowInstance, type WorkflowResource } from '@sanity/workflows';
|
|
3
3
|
import type { EngineScope } from './operation-args.ts';
|
|
4
|
+
import { type ResourceTargetFlags } from './resource-target.ts';
|
|
4
5
|
export interface DeploymentContext {
|
|
5
6
|
deployment: WorkflowDeployment;
|
|
6
7
|
client: SanityClient;
|
|
@@ -30,11 +31,6 @@ export interface ReadTarget {
|
|
|
30
31
|
client: SanityClient;
|
|
31
32
|
deployments: readonly WorkflowDeployment[];
|
|
32
33
|
}
|
|
33
|
-
interface ResourceTargetFlags {
|
|
34
|
-
'project-id'?: string | undefined;
|
|
35
|
-
dataset?: string | undefined;
|
|
36
|
-
'media-library-id'?: string | undefined;
|
|
37
|
-
}
|
|
38
34
|
export interface ReadScope {
|
|
39
35
|
targets: ReadTarget[];
|
|
40
36
|
partition: string | undefined;
|
|
@@ -54,10 +50,10 @@ export declare function ambiguityFor(flags: {
|
|
|
54
50
|
* Without a selector, this is every distinct resource the config mentions.
|
|
55
51
|
* `--partition` narrows to the resources deployed under that partition
|
|
56
52
|
* (partitions may repeat across deployments). `--deployment` narrows to that
|
|
57
|
-
* deployment's resource and partition. `--
|
|
58
|
-
* `--
|
|
59
|
-
* no config loads; `--partition` still filters. With no config found, the run
|
|
60
|
-
* reads the
|
|
53
|
+
* deployment's resource and partition. `--workflow-resource`, or
|
|
54
|
+
* `--project-id` with `--dataset`, replace the config's resources with the one
|
|
55
|
+
* they name, and no config loads; `--partition` still filters. With no config found, the run
|
|
56
|
+
* reads the resource picked on an interactive terminal. The token resolves once for the
|
|
61
57
|
* run, however many targets it spans.
|
|
62
58
|
*/
|
|
63
59
|
export declare function resolveReadTargets(flags: InstanceSelectorFlags): Promise<ReadScope>;
|
|
@@ -88,7 +84,7 @@ export declare function resolveReadResources(config: WorkflowConfig, partition:
|
|
|
88
84
|
*/
|
|
89
85
|
export declare function resolveInstanceContext(flags: InstanceSelectorFlags, instanceId: string): Promise<InstanceContext>;
|
|
90
86
|
/** {@link resolveInstanceContext} for a command that only reads, so with no
|
|
91
|
-
* config found it can locate the instance in a
|
|
87
|
+
* config found it can locate the instance in a resource picked on stderr. */
|
|
92
88
|
export declare function resolveReadInstanceContext(flags: InstanceSelectorFlags, instanceId: string): Promise<InstanceContext>;
|
|
93
89
|
interface InstanceSelectorFlags extends ResourceTargetFlags {
|
|
94
90
|
path?: string | undefined;
|
package/dist/lib/context.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { assertReadableModel, deploymentResourceClients, resourceGdr, } from '@sanity/workflows';
|
|
1
|
+
import { assertReadableModel, deploymentResourceClients, describeResource, resourceGdr, } from '@sanity/workflows';
|
|
2
2
|
import { clientFor, resolveTokenOrFail } from './client.js';
|
|
3
3
|
import { fail } from './fail.js';
|
|
4
4
|
import { loadWorkflowConfig, loadWorkflowConfigIfFound, noConfigFound } from './load-config.js';
|
|
5
|
-
import {
|
|
5
|
+
import { pickResource, RESOURCE_FLAGS_HINT } from './pick-resource.js';
|
|
6
6
|
import { canPromptOnStderr, selectOnStderr } from './prompt.js';
|
|
7
|
+
import { explicitResource } from './resource-target.js';
|
|
7
8
|
import { deploymentsForPartition, selectDeployment } from './select-deployment.js';
|
|
8
|
-
import { resourceLabel } from './ui.js';
|
|
9
9
|
export async function resolveContext(flags) {
|
|
10
10
|
const config = await loadWorkflowConfig(flags.path);
|
|
11
11
|
const deployment = await selectDeployment(config, {
|
|
@@ -13,22 +13,26 @@ export async function resolveContext(flags) {
|
|
|
13
13
|
partition: flags.partition,
|
|
14
14
|
});
|
|
15
15
|
const client = clientFor(deployment.workflowResource, await resolveTokenOrFail());
|
|
16
|
-
return {
|
|
16
|
+
return {
|
|
17
|
+
deployment,
|
|
18
|
+
client,
|
|
19
|
+
scope: deploymentScope({ client, deployment, partition: deployment.partition }),
|
|
20
|
+
};
|
|
17
21
|
}
|
|
18
|
-
function deploymentScope({ client, deployment, }) {
|
|
19
|
-
const resourceClients = deploymentResourceClients({ client, deployment });
|
|
22
|
+
function deploymentScope({ client, deployment, partition, }) {
|
|
20
23
|
return {
|
|
21
|
-
partition
|
|
24
|
+
partition,
|
|
22
25
|
workflowResource: deployment.workflowResource,
|
|
23
|
-
|
|
26
|
+
resourceClients: deploymentResourceClients({ client, deployment }),
|
|
24
27
|
};
|
|
25
28
|
}
|
|
26
29
|
export function instanceScope(hit) {
|
|
27
30
|
const owner = hit.deployments.find((deployment) => deployment.partition === hit.instance.tag);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
return deploymentScope({
|
|
32
|
+
client: hit.client,
|
|
33
|
+
deployment: owner ?? { workflowResource: hit.resource },
|
|
34
|
+
partition: hit.instance.tag,
|
|
35
|
+
});
|
|
32
36
|
}
|
|
33
37
|
function targetsFor(args) {
|
|
34
38
|
return args.resources.map((resource) => ({
|
|
@@ -37,15 +41,6 @@ function targetsFor(args) {
|
|
|
37
41
|
deployments: args.config.deployments.filter((deployment) => resourceGdr(deployment.workflowResource) === resourceGdr(resource)),
|
|
38
42
|
}));
|
|
39
43
|
}
|
|
40
|
-
function explicitResource(flags) {
|
|
41
|
-
if (flags['media-library-id'] !== undefined) {
|
|
42
|
-
return { type: 'media-library', id: flags['media-library-id'] };
|
|
43
|
-
}
|
|
44
|
-
if (flags['project-id'] !== undefined && flags.dataset !== undefined) {
|
|
45
|
-
return { type: 'dataset', id: `${flags['project-id']}.${flags.dataset}` };
|
|
46
|
-
}
|
|
47
|
-
return undefined;
|
|
48
|
-
}
|
|
49
44
|
function unconfiguredTarget(resource, token) {
|
|
50
45
|
return { resource, client: clientFor(resource, token), deployments: [] };
|
|
51
46
|
}
|
|
@@ -91,7 +86,7 @@ async function readSource(flags, { pick }) {
|
|
|
91
86
|
fail(noConfigFound(), RESOURCE_FLAGS_HINT);
|
|
92
87
|
}
|
|
93
88
|
const token = await resolveTokenOrFail();
|
|
94
|
-
return { resource: await
|
|
89
|
+
return { resource: await pickResource(token), token };
|
|
95
90
|
}
|
|
96
91
|
async function configSource(path) {
|
|
97
92
|
return { config: await loadWorkflowConfig(path), token: await resolveTokenOrFail() };
|
|
@@ -206,7 +201,7 @@ export async function soleHitOrChoose(hits, args) {
|
|
|
206
201
|
const candidates = hits.map((hit, index) => ({
|
|
207
202
|
name: candidateName(hit, args.partitionOf(hit)),
|
|
208
203
|
value: String(index),
|
|
209
|
-
description:
|
|
204
|
+
description: describeResource(hit.resource),
|
|
210
205
|
}));
|
|
211
206
|
if (args.ambiguity === 'fail') {
|
|
212
207
|
fail(`${args.subject} exists in several deployments — pass --deployment to choose one.`, candidates.map(({ name, description }) => `${name} (${description})`).join('\n'));
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { DEFAULT_PARTITION } from '@sanity/workflows';
|
|
1
|
+
import { DEFAULT_PARTITION, describeResource } from '@sanity/workflows';
|
|
2
2
|
import { fail, isAuthRejection } from './fail.js';
|
|
3
3
|
import { canPromptOnStderr, confirmOnStderr } from './prompt.js';
|
|
4
|
-
import { resourceLabel } from './ui.js';
|
|
5
4
|
export function defaultPartitionWarning(deployment, deployedPartitions) {
|
|
6
5
|
if (deployment.partition !== DEFAULT_PARTITION ||
|
|
7
6
|
deployedPartitions.length === 0 ||
|
|
@@ -13,7 +12,7 @@ export function defaultPartitionWarning(deployment, deployedPartitions) {
|
|
|
13
12
|
? `This resource's definitions are in partition "${only}". To deploy alongside them instead, set partition: "${only}" on this deployment.`
|
|
14
13
|
: `This resource's definitions are in partitions ${deployedPartitions.join(', ')}. To deploy alongside one of those instead, set partition on this deployment.`;
|
|
15
14
|
return [
|
|
16
|
-
`Partition "${DEFAULT_PARTITION}" of ${
|
|
15
|
+
`Partition "${DEFAULT_PARTITION}" of ${describeResource(deployment.workflowResource)} has no workflow definitions yet, so deployment "${deployment.name}" will be the first there.`,
|
|
17
16
|
existing,
|
|
18
17
|
].join('\n');
|
|
19
18
|
}
|
|
@@ -4,6 +4,9 @@ export interface DefinitionFileEntry {
|
|
|
4
4
|
label: string;
|
|
5
5
|
definition: Record<string, unknown>;
|
|
6
6
|
}
|
|
7
|
+
/** A definition file that is not JSON, or holds something other than workflow definitions. */
|
|
8
|
+
export declare class DefinitionFileError extends Error {
|
|
9
|
+
}
|
|
7
10
|
export declare function readDefinitionFiles(paths: readonly string[]): Promise<DefinitionFileEntry[]>;
|
|
8
11
|
export declare function describeValidation(entries: readonly DefinitionFileEntry[], report: ValidateDefinitionInputsResult): {
|
|
9
12
|
passed: string[];
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { errorMessage } from '@sanity/workflows';
|
|
3
|
+
export class DefinitionFileError extends Error {
|
|
4
|
+
}
|
|
3
5
|
export async function readDefinitionFiles(paths) {
|
|
4
6
|
const files = await Promise.all(paths.map(async (path) => ({ path, value: parseJson(path, await readFile(path, 'utf8')) })));
|
|
5
7
|
return files.flatMap(({ path, value }) => definitionEntries(path, value));
|
|
@@ -26,7 +28,7 @@ function parseJson(path, source) {
|
|
|
26
28
|
return JSON.parse(source);
|
|
27
29
|
}
|
|
28
30
|
catch (err) {
|
|
29
|
-
throw new
|
|
31
|
+
throw new DefinitionFileError(`${path}: not valid JSON (${errorMessage(err)})`, { cause: err });
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
function definitionEntries(path, value) {
|
|
@@ -34,7 +36,7 @@ function definitionEntries(path, value) {
|
|
|
34
36
|
return [{ label: path, definition: definitionObject(value, path) }];
|
|
35
37
|
}
|
|
36
38
|
if (value.length === 0) {
|
|
37
|
-
throw new
|
|
39
|
+
throw new DefinitionFileError(`${path}: the array holds no definitions`);
|
|
38
40
|
}
|
|
39
41
|
return value.map((item, index) => {
|
|
40
42
|
const label = `${path}[${index}]`;
|
|
@@ -43,7 +45,7 @@ function definitionEntries(path, value) {
|
|
|
43
45
|
}
|
|
44
46
|
function definitionObject(value, label) {
|
|
45
47
|
if (!isJsonObject(value)) {
|
|
46
|
-
throw new
|
|
48
|
+
throw new DefinitionFileError(`${label}: expected a workflow definition object, got ${jsonKind(value)}`);
|
|
47
49
|
}
|
|
48
50
|
return value;
|
|
49
51
|
}
|
package/dist/lib/fail.d.ts
CHANGED
|
@@ -7,9 +7,14 @@
|
|
|
7
7
|
* batched telemetry that hasn't flushed yet.
|
|
8
8
|
*/
|
|
9
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;
|
|
10
15
|
export declare function warnOnStderr(message: string): void;
|
|
11
16
|
/**
|
|
12
|
-
* Run `fn`, funnelling a thrown env/validation error through {@link
|
|
17
|
+
* Run `fn`, funnelling a thrown env/validation error through {@link failOn}
|
|
13
18
|
* so an expected, user-fixable failure (missing input, bad value) reads
|
|
14
19
|
* clean instead of as an oclif stack trace. Keeps the throwing `read*`
|
|
15
20
|
* helpers pure and unit-testable while giving callers one clean boundary.
|
|
@@ -20,6 +25,8 @@ export declare function failOnThrow<T>(headline: string, fn: () => T): T;
|
|
|
20
25
|
* `cause` chain because the 401 often arrives wrapped: the engine's
|
|
21
26
|
* `/users/me` actor resolution rethrows it inside its own `Error`. */
|
|
22
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[];
|
|
23
30
|
/**
|
|
24
31
|
* The detail block for a command failure: the error's message, plus the
|
|
25
32
|
* auth recovery hint when the API rejected the token. Every site that
|
package/dist/lib/fail.js
CHANGED
|
@@ -5,13 +5,20 @@ import { errorMessage } from '@sanity/workflows';
|
|
|
5
5
|
import logSymbols from 'log-symbols';
|
|
6
6
|
import { envAuthToken } from './env.js';
|
|
7
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) {
|
|
8
16
|
process.stderr.write(`${styleText('red', `${logSymbols.error} ${headline}`)}\n`);
|
|
9
17
|
if (detail !== undefined && detail !== '') {
|
|
10
18
|
for (const line of detail.split('\n')) {
|
|
11
19
|
process.stderr.write(` ${line}\n`);
|
|
12
20
|
}
|
|
13
21
|
}
|
|
14
|
-
return Errors.exit(1);
|
|
15
22
|
}
|
|
16
23
|
export function warnOnStderr(message) {
|
|
17
24
|
process.stderr.write(`${styleText('yellow', `${logSymbols.warning} ${message}`)}\n`);
|
|
@@ -21,18 +28,20 @@ export function failOnThrow(headline, fn) {
|
|
|
21
28
|
return fn();
|
|
22
29
|
}
|
|
23
30
|
catch (err) {
|
|
24
|
-
|
|
31
|
+
failOn(err, headline);
|
|
25
32
|
}
|
|
26
33
|
}
|
|
27
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 = [];
|
|
28
39
|
let current = err;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
40
|
+
while (current !== undefined && chain.length < 5) {
|
|
41
|
+
chain.push(current);
|
|
33
42
|
current = current instanceof Error ? current.cause : undefined;
|
|
34
43
|
}
|
|
35
|
-
return
|
|
44
|
+
return chain;
|
|
36
45
|
}
|
|
37
46
|
function authRecoveryHint() {
|
|
38
47
|
if (envAuthToken() !== undefined) {
|
|
@@ -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
|
+
}
|
package/dist/lib/flags.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type WorkflowResource } from '@sanity/workflows';
|
|
1
2
|
export declare function flagTokens(argv: string[]): string[];
|
|
2
3
|
export declare function isLongFlag(token: string, name: string): boolean;
|
|
3
4
|
/** The last value of a flag read straight off argv, by its long name or its
|
|
@@ -14,10 +15,13 @@ export declare function partitionFlag(options: {
|
|
|
14
15
|
export declare const configFlags: {
|
|
15
16
|
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
16
17
|
};
|
|
17
|
-
/** Name one resource for a read command to inspect
|
|
18
|
-
*
|
|
19
|
-
*
|
|
18
|
+
/** Name one resource for a read command to inspect. `--workflow-resource`
|
|
19
|
+
* takes any resource type in the `<type>:<id>` form `parseResourceGdr`
|
|
20
|
+
* reads; `--project-id` / `--dataset` name a dataset the way the Sanity CLI
|
|
21
|
+
* does. Write commands never spread these: they act through a deployment
|
|
22
|
+
* from `sanity.workflow.ts`. */
|
|
20
23
|
export declare const resourceFlags: {
|
|
24
|
+
'workflow-resource': import("@oclif/core/interfaces").OptionFlag<WorkflowResource | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
21
25
|
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
22
26
|
dataset: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
23
27
|
'media-library-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
package/dist/lib/flags.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Flags } from '@oclif/core';
|
|
2
|
+
import { parseResourceGdr } from '@sanity/workflows';
|
|
2
3
|
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
4
|
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
5
|
export function flagTokens(argv) {
|
|
@@ -50,29 +51,38 @@ function nonEmpty(name) {
|
|
|
50
51
|
};
|
|
51
52
|
}
|
|
52
53
|
export const resourceFlags = {
|
|
54
|
+
'workflow-resource': Flags.custom({
|
|
55
|
+
description: `Resource to read, as <type>:<id>: dataset:<projectId>.<dataset>, media-library:<id>, canvas:<id> or dashboard:<id>${OVERRIDES_CONFIG}`,
|
|
56
|
+
helpGroup: 'OVERRIDE',
|
|
57
|
+
helpValue: '<type>:<id>',
|
|
58
|
+
exclusive: ['project-id', 'dataset', 'media-library-id', ...CONFIG_SELECTORS],
|
|
59
|
+
parse: async (input) => parseResourceGdr(await nonEmpty('workflow-resource')(input)),
|
|
60
|
+
})(),
|
|
53
61
|
'project-id': Flags.string({
|
|
54
62
|
char: 'p',
|
|
55
|
-
description: `Project ID of the dataset to read${OVERRIDES_CONFIG}`,
|
|
63
|
+
description: `Project ID of the dataset to read, with --dataset${OVERRIDES_CONFIG}`,
|
|
56
64
|
helpGroup: 'OVERRIDE',
|
|
57
65
|
helpValue: '<id>',
|
|
58
66
|
dependsOn: ['dataset'],
|
|
59
|
-
exclusive: ['media-library-id', ...CONFIG_SELECTORS],
|
|
67
|
+
exclusive: ['workflow-resource', 'media-library-id', ...CONFIG_SELECTORS],
|
|
60
68
|
parse: nonEmpty('project-id'),
|
|
61
69
|
}),
|
|
62
70
|
dataset: Flags.string({
|
|
63
71
|
char: 'd',
|
|
64
|
-
description: `Dataset to read${OVERRIDES_CONFIG}`,
|
|
72
|
+
description: `Dataset to read, with --project-id${OVERRIDES_CONFIG}`,
|
|
65
73
|
helpGroup: 'OVERRIDE',
|
|
66
74
|
helpValue: '<name>',
|
|
67
75
|
dependsOn: ['project-id'],
|
|
68
|
-
exclusive: ['media-library-id', ...CONFIG_SELECTORS],
|
|
76
|
+
exclusive: ['workflow-resource', 'media-library-id', ...CONFIG_SELECTORS],
|
|
69
77
|
parse: nonEmpty('dataset'),
|
|
70
78
|
}),
|
|
71
79
|
'media-library-id': Flags.string({
|
|
72
|
-
description: `Media
|
|
80
|
+
description: `Media Library to read${OVERRIDES_CONFIG}`,
|
|
81
|
+
deprecated: { to: 'workflow-resource media-library:<id>' },
|
|
73
82
|
helpGroup: 'OVERRIDE',
|
|
74
83
|
helpValue: '<id>',
|
|
75
|
-
|
|
84
|
+
hidden: true,
|
|
85
|
+
exclusive: ['workflow-resource', 'project-id', 'dataset', ...CONFIG_SELECTORS],
|
|
76
86
|
parse: nonEmpty('media-library-id'),
|
|
77
87
|
}),
|
|
78
88
|
};
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { type WorkflowConfig } from '@sanity/workflows';
|
|
2
|
+
/** A config file that could not be found, loaded, or validated. The message is the detail line under `headline`. */
|
|
3
|
+
export declare class ConfigLoadError extends Error {
|
|
4
|
+
readonly headline: string;
|
|
5
|
+
constructor(headline: string, detail: string);
|
|
6
|
+
}
|
|
2
7
|
/**
|
|
3
8
|
* The validated config a `sanity.workflow` module exports, and the names that
|
|
4
9
|
* module exports it alongside.
|
package/dist/lib/load-config.js
CHANGED
|
@@ -14,14 +14,12 @@ import { CONFIG_FILE_NAMES } from '@sanity/workflows/blueprints';
|
|
|
14
14
|
import { defineWorkflowConfig } from '@sanity/workflows/define';
|
|
15
15
|
import * as find from 'empathic/find';
|
|
16
16
|
import { createJiti } from 'jiti';
|
|
17
|
-
import { fail, warnOnStderr } from './fail.js';
|
|
18
|
-
class ConfigLoadError extends Error {
|
|
17
|
+
import { fail, failOn, warnOnStderr } from './fail.js';
|
|
18
|
+
export class ConfigLoadError extends Error {
|
|
19
19
|
headline;
|
|
20
|
-
detail;
|
|
21
20
|
constructor(headline, detail) {
|
|
22
|
-
super(
|
|
21
|
+
super(detail);
|
|
23
22
|
this.headline = headline;
|
|
24
|
-
this.detail = detail;
|
|
25
23
|
}
|
|
26
24
|
}
|
|
27
25
|
function findConfigFile(path) {
|
|
@@ -91,7 +89,7 @@ async function loadIfFound(path) {
|
|
|
91
89
|
}
|
|
92
90
|
catch (err) {
|
|
93
91
|
if (err instanceof ConfigLoadError) {
|
|
94
|
-
|
|
92
|
+
failOn(err, err.headline);
|
|
95
93
|
}
|
|
96
94
|
throw err;
|
|
97
95
|
}
|
package/dist/lib/nuke.d.ts
CHANGED
|
@@ -10,8 +10,8 @@ export interface NukeTarget {
|
|
|
10
10
|
/** One resolved resource of a {@link NukePlan}: the ids to delete, by doc type. */
|
|
11
11
|
export interface NukeResourcePlan {
|
|
12
12
|
resource: WorkflowResource;
|
|
13
|
-
/** The
|
|
14
|
-
*
|
|
13
|
+
/** The `<type>:<id>` resource address the plan shows this resource as, and,
|
|
14
|
+
* in tag mode, the one the operator confirms by: it holds no whitespace. */
|
|
15
15
|
label: string;
|
|
16
16
|
client: WorkflowClient;
|
|
17
17
|
holdsEngineDocs: boolean;
|
|
@@ -84,7 +84,7 @@ export declare function resolveInstanceNukePlan(args: {
|
|
|
84
84
|
partition: string;
|
|
85
85
|
targets: NukeTarget[];
|
|
86
86
|
}): Promise<NukePlan>;
|
|
87
|
-
/** The
|
|
87
|
+
/** The `<type>:<id>` labels of resources the plan actually deletes from. A
|
|
88
88
|
* resource with nothing to delete is not "involved" and is omitted. Tag mode
|
|
89
89
|
* makes the operator type this set back; instance mode confirms with a plain
|
|
90
90
|
* y/N and consumes it only for the dataset count in its summary. */
|
|
@@ -95,7 +95,7 @@ export declare function planCounts(plan: NukePlan): PlanCounts;
|
|
|
95
95
|
/**
|
|
96
96
|
* Does the operator's typed confirmation name every involved target, and only
|
|
97
97
|
* those? Order-independent, whitespace-tolerant set equality: the operator
|
|
98
|
-
* pastes back all
|
|
98
|
+
* pastes back all `<type>:<id>` targets the plan listed.
|
|
99
99
|
*/
|
|
100
100
|
export declare function confirmationMatches(input: string, targets: string[]): boolean;
|
|
101
101
|
/** The dry-run plan as printable lines: a heading, the content-safety
|
package/dist/lib/nuke.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { styleText } from 'node:util';
|
|
2
|
-
import { GUARD_DOC_TYPE,
|
|
2
|
+
import { GUARD_DOC_TYPE, partitionScopeFilter, resourceGdr, WORKFLOW_DEFINITION_TYPE, WORKFLOW_INSTANCE_TYPE, } from '@sanity/workflows';
|
|
3
3
|
import { fail } from './fail.js';
|
|
4
4
|
import { deploymentLabel } from './select-deployment.js';
|
|
5
|
-
import { formatTable, pluralize,
|
|
5
|
+
import { formatTable, pluralize, sectionHeader } from './ui.js';
|
|
6
6
|
const CHUNK = 200;
|
|
7
7
|
const DEFINITION_ID_TAIL = /([^.]+)\.v(\d+)$/;
|
|
8
8
|
const REQUEST_TAG = 'nuke';
|
|
@@ -44,7 +44,7 @@ async function resolveResourcePlan(args) {
|
|
|
44
44
|
function resourcePlan(target, ids) {
|
|
45
45
|
return {
|
|
46
46
|
resource: target.resource,
|
|
47
|
-
label:
|
|
47
|
+
label: resourceGdr(target.resource),
|
|
48
48
|
client: target.client,
|
|
49
49
|
holdsEngineDocs: target.holdsEngineDocs,
|
|
50
50
|
...ids,
|
|
@@ -2,12 +2,12 @@ import { type Engine, type EngineScopeArgs, type WorkflowClient, type OperationA
|
|
|
2
2
|
/**
|
|
3
3
|
* The engine-scope a CLI verb needs: which partition and which resource
|
|
4
4
|
* the engine's own data lives in, plus the clients for the resources the
|
|
5
|
-
* deployment binds in `resourceAliases
|
|
5
|
+
* deployment binds in `resourceAliases`; {@link EngineScopeArgs} minus the
|
|
6
6
|
* client. Runtime-supplied refs may target the workflow resource or an
|
|
7
|
-
* alias-bound resource
|
|
8
|
-
*
|
|
7
|
+
* alias-bound resource. `resourceClients` is required because an engine
|
|
8
|
+
* without it accepts refs into any addressable resource.
|
|
9
9
|
*/
|
|
10
|
-
export type EngineScope = Pick<EngineScopeArgs, 'partition' | 'workflowResource'
|
|
10
|
+
export type EngineScope = Pick<EngineScopeArgs, 'partition' | 'workflowResource'> & Required<Pick<EngineScopeArgs, 'resourceClients'>>;
|
|
11
11
|
/**
|
|
12
12
|
* The engine args every CLI verb shares: partition scope, workflow resource,
|
|
13
13
|
* the CLI's declared execution context — the advisory "via what" stamped
|
|
@@ -4,7 +4,7 @@ export function baseEngineArgs(scope) {
|
|
|
4
4
|
return {
|
|
5
5
|
partition: scope.partition,
|
|
6
6
|
workflowResource: scope.workflowResource,
|
|
7
|
-
|
|
7
|
+
resourceClients: scope.resourceClients,
|
|
8
8
|
executionContext: { kind: EXECUTION_KINDS.cli, id: 'workflow-cli' },
|
|
9
9
|
telemetry: cliTelemetry().logger,
|
|
10
10
|
};
|
package/dist/lib/ops-report.d.ts
CHANGED
|
@@ -33,9 +33,7 @@ export declare function cascadeTail(cascaded: number): string;
|
|
|
33
33
|
* Run a write verb behind its spinner: start it, run the engine call, and
|
|
34
34
|
* emit the report — warn and bail on a no-op (`changed: false`), otherwise
|
|
35
35
|
* succeed and print the ops block. A throw fails the spinner and exits
|
|
36
|
-
* through {@link
|
|
37
|
-
* command (abort, fire-action, start, definitions delete, nuke)
|
|
38
|
-
* shares.
|
|
36
|
+
* through {@link failOn}.
|
|
39
37
|
*/
|
|
40
38
|
export declare function runWriteVerb<T>({ startLabel, failLabel, failHeadline, run, report, log, }: {
|
|
41
39
|
startLabel: string;
|
package/dist/lib/ops-report.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { styleText } from 'node:util';
|
|
2
2
|
import logSymbols from 'log-symbols';
|
|
3
3
|
import ora from 'ora';
|
|
4
|
-
import {
|
|
4
|
+
import { failOn } from './fail.js';
|
|
5
5
|
export function opsAppliedLines(ranOps) {
|
|
6
6
|
const details = (ranOps ?? []).map((op) => {
|
|
7
7
|
const target = op.target ? styleText('dim', ` → ${op.target.scope}.${op.target.field}`) : '';
|
|
@@ -26,6 +26,6 @@ export async function runWriteVerb({ startLabel, failLabel, failHeadline, run, r
|
|
|
26
26
|
}
|
|
27
27
|
catch (error) {
|
|
28
28
|
spinner.fail(failLabel);
|
|
29
|
-
|
|
29
|
+
failOn(error, failHeadline);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SanityClient } from '@sanity/client';
|
|
2
|
+
import { type WorkflowResource } from '@sanity/workflows';
|
|
3
|
+
import { selectOnStderr } from './prompt.ts';
|
|
4
|
+
export declare const RESOURCE_FLAGS_HINT = "Pass --workflow-resource <type>:<id> (or --project-id with --dataset) to read one resource without a config.";
|
|
5
|
+
type Select = typeof selectOnStderr;
|
|
6
|
+
/** Ask for a resource type, then one resource of it that this login can access. */
|
|
7
|
+
export declare function pickResource(token: string, { client, select, }?: {
|
|
8
|
+
client?: SanityClient;
|
|
9
|
+
select?: Select;
|
|
10
|
+
}): Promise<WorkflowResource>;
|
|
11
|
+
/** The resource ids a `{data: [{id}]}` list answer names. Throws for any other answer. */
|
|
12
|
+
export declare function listedIds(answer: unknown, listPath: string): string[];
|
|
13
|
+
export {};
|