@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,111 @@
|
|
|
1
|
+
import { FUNCTIONS_DIR, hasDocumentFunctions, HEARTBEAT_BLUEPRINT_PATH, HEARTBEAT_STACK_DIR, scheduledDrains, } from '@sanity/workflows/blueprints/generate';
|
|
2
|
+
import logSymbols from 'log-symbols';
|
|
3
|
+
import { sectionHeader } from './ui.js';
|
|
4
|
+
const DEPLOY_COMMAND = 'npx sanity@latest blueprints deploy';
|
|
5
|
+
function heartbeatStackWrites(args) {
|
|
6
|
+
const functionDirs = args.plan.deployments.flatMap(({ emission }) => emission?.heartbeat === undefined
|
|
7
|
+
? []
|
|
8
|
+
: [emission.heartbeat.name, ...scheduledDrains(emission).map((drain) => drain.name)].map((name) => `${FUNCTIONS_DIR}/${name}/`));
|
|
9
|
+
return args.applied.filter((write) => write.path.startsWith(`${HEARTBEAT_STACK_DIR}/`) ||
|
|
10
|
+
functionDirs.some((dir) => write.path.startsWith(dir)));
|
|
11
|
+
}
|
|
12
|
+
export function stackDeployLines(args) {
|
|
13
|
+
const lines = [
|
|
14
|
+
...projectStackLines(args.plan),
|
|
15
|
+
...heartbeatStackLines({ ...args, heartbeat: heartbeatStackWrites(args) }),
|
|
16
|
+
];
|
|
17
|
+
return lines.length === 0 ? [] : [sectionHeader('Deploy'), ...lines];
|
|
18
|
+
}
|
|
19
|
+
const changedWrites = (writes) => writes.filter((write) => write.status !== 'unchanged');
|
|
20
|
+
function projectStackLines(plan) {
|
|
21
|
+
const projectStack = plan.deployments.some(({ emission }) => emission !== undefined && hasDocumentFunctions(emission));
|
|
22
|
+
if (!projectStack)
|
|
23
|
+
return [];
|
|
24
|
+
return [
|
|
25
|
+
` project stack: \`${DEPLOY_COMMAND}\` here, with a token that can deploy to the` +
|
|
26
|
+
` project. It holds every document function; redeploy it after any definition change.`,
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
function heartbeatStackLines(args) {
|
|
30
|
+
const { heartbeat, verbose } = args;
|
|
31
|
+
if (heartbeat.length === 0)
|
|
32
|
+
return [];
|
|
33
|
+
const changed = changedWrites(heartbeat);
|
|
34
|
+
const created = changed.some((write) => write.path === HEARTBEAT_BLUEPRINT_PATH && write.status === 'created');
|
|
35
|
+
const drains = hasScheduledDrains(args.plan);
|
|
36
|
+
if (!verbose && !drains && changed.length === 0)
|
|
37
|
+
return [];
|
|
38
|
+
const redeploy = created ? [] : redeployLines({ changed, drains });
|
|
39
|
+
return [
|
|
40
|
+
` heartbeat stack: \`${DEPLOY_COMMAND}\` in ${HEARTBEAT_STACK_DIR}/, with an` +
|
|
41
|
+
` organization deploy token. It holds ${heartbeatStackHolds(drains)}.`,
|
|
42
|
+
...redeploy,
|
|
43
|
+
...(verbose || created ? missingHeartbeatLines(args.plan) : []),
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
function redeployLines(args) {
|
|
47
|
+
if (args.drains && args.changed.length === 0)
|
|
48
|
+
return [];
|
|
49
|
+
return [heartbeatRedeployLine(args.changed)];
|
|
50
|
+
}
|
|
51
|
+
function hasScheduledDrains(plan) {
|
|
52
|
+
return plan.deployments.some(({ emission }) => emission !== undefined && scheduledDrains(emission).length > 0);
|
|
53
|
+
}
|
|
54
|
+
function heartbeatStackHolds(drains) {
|
|
55
|
+
if (!drains)
|
|
56
|
+
return 'only the scheduled heartbeat';
|
|
57
|
+
return ('the scheduled heartbeat and the scheduled drains, which bundle your effect handlers;' +
|
|
58
|
+
' redeploy it after any handler or definition change and after upgrading the Workflows packages');
|
|
59
|
+
}
|
|
60
|
+
function heartbeatRedeployLine(changed) {
|
|
61
|
+
if (changed.length === 0) {
|
|
62
|
+
return (` ${logSymbols.info} the heartbeat stack's files are unchanged: these definitions need no` +
|
|
63
|
+
` heartbeat redeploy. Redeploy both stacks after upgrading the Workflows packages.`);
|
|
64
|
+
}
|
|
65
|
+
return (` ${logSymbols.warning} ${changed.map((write) => write.path).join(', ')} changed: if you` +
|
|
66
|
+
` have deployed the heartbeat stack, redeploy it with the organization deploy token.`);
|
|
67
|
+
}
|
|
68
|
+
function missingHeartbeatLines(plan) {
|
|
69
|
+
const waiting = plan.deployments.flatMap(waitingWithoutHeartbeat);
|
|
70
|
+
if (waiting.length === 0)
|
|
71
|
+
return [];
|
|
72
|
+
return [
|
|
73
|
+
` ${logSymbols.warning} Without the heartbeat stack nothing checks these on a schedule, and nothing reports it:`,
|
|
74
|
+
...waiting.map((line) => ` ${line}`),
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
function waitingWithoutHeartbeat(plan) {
|
|
78
|
+
const { emission } = plan;
|
|
79
|
+
const heartbeat = emission?.heartbeat;
|
|
80
|
+
if (emission === undefined || heartbeat === undefined)
|
|
81
|
+
return [];
|
|
82
|
+
const ticked = new Set(heartbeat.definitions);
|
|
83
|
+
const within = (sites) => [
|
|
84
|
+
...new Set(sites.map((site) => site.definition).filter((name) => ticked.has(name))),
|
|
85
|
+
];
|
|
86
|
+
const deadlines = within(plan.needs.heartbeat.timeSites);
|
|
87
|
+
const spawners = within(plan.needs.heartbeat.spawnSites);
|
|
88
|
+
const scheduled = scheduledDrains(emission).flatMap((drain) => drain.effectNames);
|
|
89
|
+
const effects = plan.hosting.effects.function.filter((name) => !scheduled.includes(name));
|
|
90
|
+
return [
|
|
91
|
+
...(scheduled.length === 0
|
|
92
|
+
? []
|
|
93
|
+
: [`no effect runs at all, because its drain runs from this stack: ${scheduled.join(', ')}`]),
|
|
94
|
+
...(deadlines.length === 0
|
|
95
|
+
? []
|
|
96
|
+
: [
|
|
97
|
+
`$now deadlines take effect only when another engine call evaluates them in ${deadlines.join(', ')}`,
|
|
98
|
+
]),
|
|
99
|
+
...(spawners.length === 0
|
|
100
|
+
? []
|
|
101
|
+
: [
|
|
102
|
+
`a parent that missed a child settling waits for another engine call in ${spawners.join(', ')}`,
|
|
103
|
+
]),
|
|
104
|
+
...(effects.length === 0
|
|
105
|
+
? []
|
|
106
|
+
: [
|
|
107
|
+
`an effect queued before its drain existed, or whose drain timed out, waits until` +
|
|
108
|
+
` a later drain picks it up: ${effects.join(', ')}`,
|
|
109
|
+
]),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Diagnosis, type MissingDocument, type StuckCause, type SuggestedRemediation } from '@sanity/workflows';
|
|
2
|
+
export declare function stuckHeadline(cause: StuckCause): string;
|
|
3
|
+
export interface InstanceDiagnostic {
|
|
4
|
+
diagnosis: Diagnosis;
|
|
5
|
+
/** Full evaluation evidence, including references outside the blocking subset. */
|
|
6
|
+
allMissingDocuments?: MissingDocument[] | undefined;
|
|
7
|
+
remediations: SuggestedRemediation[];
|
|
8
|
+
}
|
|
9
|
+
export declare function stuckDiagnosisLines({ diagnosis, allMissingDocuments, remediations, }: InstanceDiagnostic): string[];
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
import { formatDateTime } from '@sanity/cli-core/dates';
|
|
3
|
+
import { _additionalMissingDocuments, _fieldTargetLabel, _missingDocumentsSummary, } from '@sanity/workflows';
|
|
4
|
+
import logSymbols from 'log-symbols';
|
|
5
|
+
import { sectionHeader } from './ui.js';
|
|
6
|
+
function missingDocumentsDetail(documents) {
|
|
7
|
+
return {
|
|
8
|
+
headline: 'referenced document missing',
|
|
9
|
+
why: [
|
|
10
|
+
...documents.map(({ target, reference }) => `${_fieldTargetLabel(target)}: ${reference.id}`),
|
|
11
|
+
_missingDocumentsSummary(documents),
|
|
12
|
+
],
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function additionalDocumentLines(diagnosis, allMissingDocuments) {
|
|
16
|
+
const documents = _additionalMissingDocuments(diagnosis, allMissingDocuments);
|
|
17
|
+
if (documents.length === 0)
|
|
18
|
+
return [];
|
|
19
|
+
return ['', sectionHeader('Missing references'), ...missingDocumentsDetail(documents).why];
|
|
20
|
+
}
|
|
21
|
+
function failedEffectDetail(effect) {
|
|
22
|
+
const ran = effect.durationMs !== undefined ? ` (after ${effect.durationMs}ms)` : '';
|
|
23
|
+
return {
|
|
24
|
+
headline: `a failed effect from action '${effect.origin.name}' is blocking its activity`,
|
|
25
|
+
why: [
|
|
26
|
+
styleText('red', `${logSymbols.error} failed effect: ${effect.name}`),
|
|
27
|
+
` queued by action '${effect.origin.name}', failed ${formatDateTime(effect.ranAt)}${ran}`,
|
|
28
|
+
...(effect.error !== undefined ? [` error: ${effect.error.message}`] : []),
|
|
29
|
+
'',
|
|
30
|
+
`The activity that fired '${effect.origin.name}' is waiting on this effect. It failed`,
|
|
31
|
+
`against an external system, so the activity never resolves and the stage can't advance.`,
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function hungEffectDetail(effect) {
|
|
36
|
+
return {
|
|
37
|
+
headline: `effect '${effect.name}' was claimed but never completed`,
|
|
38
|
+
why: [
|
|
39
|
+
styleText('yellow', `${logSymbols.warning} hung effect: ${effect.name}`),
|
|
40
|
+
` claimed ${formatDateTime(effect.claim?.claimedAt ?? '?')} but never reported back — the`,
|
|
41
|
+
` drainer likely died mid-dispatch, so it won't drain on its own.`,
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function failedActivityDetail(activity) {
|
|
46
|
+
return {
|
|
47
|
+
headline: `activity '${activity}' failed`,
|
|
48
|
+
why: [
|
|
49
|
+
styleText('red', `${logSymbols.error} activity '${activity}' is in a terminal failed state.`),
|
|
50
|
+
`Any exit transition gated on '${activity}' being done can never fire.`,
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function noTransitionDetail() {
|
|
55
|
+
return {
|
|
56
|
+
headline: `no exit transition's trigger is satisfied`,
|
|
57
|
+
why: [
|
|
58
|
+
`${logSymbols.info} every activity is resolved, but no exit transition's \`when\` is true.`,
|
|
59
|
+
`Likely a routing state value a trigger reads never got written.`,
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function transitionUnevaluableDetail(transitions) {
|
|
64
|
+
return {
|
|
65
|
+
headline: `an exit transition's trigger could not be evaluated`,
|
|
66
|
+
why: [
|
|
67
|
+
`${logSymbols.info} every activity is resolved, but ${transitions.join(', ')} reads an operand`,
|
|
68
|
+
`that is missing or unreadable (GROQ null), so routing is held rather than`,
|
|
69
|
+
`falling through. Make the data the trigger reads readable — publish the`,
|
|
70
|
+
`subject (or fill the field) — and the next tick or commit re-checks the`,
|
|
71
|
+
`transitions; the instance advances only if one is then satisfied. No`,
|
|
72
|
+
`remediation verb fixes this.`,
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function remediationLines(remediations) {
|
|
77
|
+
return remediations.map((r) => ` • ${r.verb} — ${r.rationale}`);
|
|
78
|
+
}
|
|
79
|
+
function causeDetail(cause) {
|
|
80
|
+
switch (cause.kind) {
|
|
81
|
+
case 'document-missing':
|
|
82
|
+
return missingDocumentsDetail(cause.documents);
|
|
83
|
+
case 'failed-effect':
|
|
84
|
+
return failedEffectDetail(cause.effect);
|
|
85
|
+
case 'hung-effect':
|
|
86
|
+
return hungEffectDetail(cause.effect);
|
|
87
|
+
case 'failed-activity':
|
|
88
|
+
return failedActivityDetail(cause.activity);
|
|
89
|
+
case 'no-transition-fires':
|
|
90
|
+
return noTransitionDetail();
|
|
91
|
+
case 'transition-unevaluable':
|
|
92
|
+
return transitionUnevaluableDetail(cause.transitions);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export function stuckHeadline(cause) {
|
|
96
|
+
return `${logSymbols.warning} ${styleText('yellow', 'STUCK')} — ${causeDetail(cause).headline}`;
|
|
97
|
+
}
|
|
98
|
+
export function stuckDiagnosisLines({ diagnosis, allMissingDocuments, remediations, }) {
|
|
99
|
+
if (diagnosis.state !== 'stuck')
|
|
100
|
+
return [];
|
|
101
|
+
const detail = causeDetail(diagnosis.cause);
|
|
102
|
+
const runnable = remediations.filter((remediation) => remediation.available);
|
|
103
|
+
return [
|
|
104
|
+
'',
|
|
105
|
+
sectionHeader("Why it's stuck"),
|
|
106
|
+
...detail.why,
|
|
107
|
+
...additionalDocumentLines(diagnosis, allMissingDocuments),
|
|
108
|
+
...(runnable.length > 0
|
|
109
|
+
? ['', sectionHeader('Suggested fix'), ...remediationLines(runnable)]
|
|
110
|
+
: []),
|
|
111
|
+
];
|
|
112
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type SanityClient } from '@sanity/client';
|
|
2
|
+
import { type WorkflowResource } from '@sanity/workflows';
|
|
3
|
+
/**
|
|
4
|
+
* The API host to hit: an explicit `SANITY_API_HOST` wins; otherwise mirror
|
|
5
|
+
* the Sanity CLI — staging routes to the staging API, prod falls through to
|
|
6
|
+
* `@sanity/client`'s default (`undefined` here). Pairs with
|
|
7
|
+
* {@link resolveToken}, which reads the matching session token. Exported so a
|
|
8
|
+
* destructive command can display the environment its clients will hit.
|
|
9
|
+
*/
|
|
10
|
+
export declare function resolveApiHost(): string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Resolve the Sanity auth token. An explicit `SANITY_AUTH_TOKEN` wins (CI /
|
|
13
|
+
* scripted use); otherwise fall back to the `sanity login` session token.
|
|
14
|
+
* Throws when neither exists rather than building an unauthenticated client
|
|
15
|
+
* that only 401s once it hits the API.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveToken(): Promise<string>;
|
|
18
|
+
export declare class AuthMissingError extends Error {
|
|
19
|
+
constructor();
|
|
20
|
+
}
|
|
21
|
+
/** {@link resolveToken}, funnelling the no-token case through the clean
|
|
22
|
+
* {@link failOn} path rather than letting it surface as an oclif stack. */
|
|
23
|
+
export declare function resolveTokenOrFail(): Promise<string>;
|
|
24
|
+
/** Build a client for a workflow resource, authenticated with `token`. */
|
|
25
|
+
export declare function clientFor(resource: WorkflowResource, token: string): SanityClient;
|
|
26
|
+
/** Build a client for the global API host, not bound to any project or resource. */
|
|
27
|
+
export declare function globalClient(token: string): SanityClient;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { getCliToken, isStaging } from '@sanity/cli-core';
|
|
2
|
+
import { createClient } from '@sanity/client';
|
|
3
|
+
import { clientConfigFromResource, ENGINE_API_VERSION, } from '@sanity/workflows';
|
|
4
|
+
import { ENV, envAuthToken } from './env.js';
|
|
5
|
+
import { failOn } from './fail.js';
|
|
6
|
+
const API_VERSION = ENGINE_API_VERSION;
|
|
7
|
+
const STAGING_API_HOST = 'https://api.sanity.work';
|
|
8
|
+
export function resolveApiHost() {
|
|
9
|
+
return process.env[ENV.SANITY_API_HOST] ?? (isStaging() ? STAGING_API_HOST : undefined);
|
|
10
|
+
}
|
|
11
|
+
export async function resolveToken() {
|
|
12
|
+
const fromEnv = envAuthToken();
|
|
13
|
+
if (fromEnv !== undefined) {
|
|
14
|
+
return fromEnv;
|
|
15
|
+
}
|
|
16
|
+
const fromSession = await getCliToken();
|
|
17
|
+
if (fromSession) {
|
|
18
|
+
return fromSession;
|
|
19
|
+
}
|
|
20
|
+
throw new AuthMissingError();
|
|
21
|
+
}
|
|
22
|
+
export class AuthMissingError extends Error {
|
|
23
|
+
constructor() {
|
|
24
|
+
super('No Sanity token found — run `sanity login`, or set SANITY_AUTH_TOKEN.');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function resolveTokenOrFail() {
|
|
28
|
+
return resolveToken().catch((err) => failOn(err, 'Authentication required:'));
|
|
29
|
+
}
|
|
30
|
+
function baseClientConfig(token) {
|
|
31
|
+
const apiHost = resolveApiHost();
|
|
32
|
+
return {
|
|
33
|
+
token,
|
|
34
|
+
apiVersion: API_VERSION,
|
|
35
|
+
useCdn: false,
|
|
36
|
+
requestTagPrefix: 'sanity.workflows.cli',
|
|
37
|
+
...(apiHost ? { apiHost } : {}),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function clientFor(resource, token) {
|
|
41
|
+
return createClient({ ...baseClientConfig(token), ...clientConfigFromResource(resource) });
|
|
42
|
+
}
|
|
43
|
+
export function globalClient(token) {
|
|
44
|
+
return createClient({ ...baseClientConfig(token), useProjectHostname: false });
|
|
45
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface Span {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
}
|
|
5
|
+
export interface AstNode extends Span {
|
|
6
|
+
type: string;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
/** Replaces `start`..`end` (UTF-16 offsets) of the original source. */
|
|
10
|
+
export interface Edit extends Span {
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function isNode(value: unknown): value is AstNode;
|
|
14
|
+
/** Child nodes, each with the key that holds it. */
|
|
15
|
+
export declare function childEntries(node: AstNode): Array<[string, AstNode]>;
|
|
16
|
+
/** `edits` must not overlap. */
|
|
17
|
+
export declare function applyEdits(source: string, edits: readonly Edit[]): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function isNode(value) {
|
|
2
|
+
return (typeof value === 'object' && value !== null && typeof Reflect.get(value, 'type') === 'string');
|
|
3
|
+
}
|
|
4
|
+
export function childEntries(node) {
|
|
5
|
+
return Object.entries(node)
|
|
6
|
+
.filter(([key]) => key !== 'parent')
|
|
7
|
+
.flatMap(([key, value]) => (Array.isArray(value) ? value : [value]).map((child) => [key, child]))
|
|
8
|
+
.filter((entry) => isNode(entry[1]));
|
|
9
|
+
}
|
|
10
|
+
export function applyEdits(source, edits) {
|
|
11
|
+
return edits
|
|
12
|
+
.toSorted((a, b) => b.start - a.start)
|
|
13
|
+
.reduce((text, edit) => text.slice(0, edit.start) + edit.text + text.slice(edit.end), source);
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type AstNode, type Edit, type Span } from './ast.ts';
|
|
2
|
+
export type RenameReportKind = 'shadowed-rename' | 'shadowed-namespace';
|
|
3
|
+
/** A rename aliased or skipped because the file reuses a name. */
|
|
4
|
+
export interface RenameReport extends Span {
|
|
5
|
+
kind: RenameReportKind;
|
|
6
|
+
text: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ExportRenameResult {
|
|
9
|
+
edits: Edit[];
|
|
10
|
+
reports: RenameReport[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Renames imports of renamed exports and every reference to them. If the file
|
|
14
|
+
* reuses the old or new name, the import is aliased instead; if it reuses a
|
|
15
|
+
* namespace name, that namespace's members are reported for a hand rename.
|
|
16
|
+
*/
|
|
17
|
+
export declare function renameExports(program: AstNode): ExportRenameResult;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { childEntries, isNode } from './ast.js';
|
|
2
|
+
import { exportRenamesFor } from './package-map.js';
|
|
3
|
+
const BINDING_PATTERNS = new Set([
|
|
4
|
+
'VariableDeclarator.id',
|
|
5
|
+
'FunctionDeclaration.params',
|
|
6
|
+
'FunctionExpression.params',
|
|
7
|
+
'ArrowFunctionExpression.params',
|
|
8
|
+
'TSDeclareFunction.params',
|
|
9
|
+
'TSEmptyBodyFunctionExpression.params',
|
|
10
|
+
'TSFunctionType.params',
|
|
11
|
+
'TSConstructorType.params',
|
|
12
|
+
'TSCallSignatureDeclaration.params',
|
|
13
|
+
'TSConstructSignatureDeclaration.params',
|
|
14
|
+
'TSMethodSignature.params',
|
|
15
|
+
'CatchClause.param',
|
|
16
|
+
]);
|
|
17
|
+
const PATTERN_EXITS = new Set(['right', 'key', 'typeAnnotation', 'decorators']);
|
|
18
|
+
const DECLARATIONS = new Set([
|
|
19
|
+
'FunctionDeclaration.id',
|
|
20
|
+
'FunctionExpression.id',
|
|
21
|
+
'TSDeclareFunction.id',
|
|
22
|
+
'ClassDeclaration.id',
|
|
23
|
+
'ClassExpression.id',
|
|
24
|
+
'TSTypeAliasDeclaration.id',
|
|
25
|
+
'TSInterfaceDeclaration.id',
|
|
26
|
+
'TSEnumDeclaration.id',
|
|
27
|
+
'TSModuleDeclaration.id',
|
|
28
|
+
'TSImportEqualsDeclaration.id',
|
|
29
|
+
'TSTypeParameter.name',
|
|
30
|
+
]);
|
|
31
|
+
const NAME_SLOTS = new Set([
|
|
32
|
+
'MemberExpression.property',
|
|
33
|
+
'JSXMemberExpression.property',
|
|
34
|
+
'TSQualifiedName.right',
|
|
35
|
+
'Property.key',
|
|
36
|
+
'MethodDefinition.key',
|
|
37
|
+
'PropertyDefinition.key',
|
|
38
|
+
'AccessorProperty.key',
|
|
39
|
+
'TSAbstractMethodDefinition.key',
|
|
40
|
+
'TSAbstractPropertyDefinition.key',
|
|
41
|
+
'TSAbstractAccessorProperty.key',
|
|
42
|
+
'TSPropertySignature.key',
|
|
43
|
+
'TSMethodSignature.key',
|
|
44
|
+
'TSEnumMember.id',
|
|
45
|
+
'JSXAttribute.name',
|
|
46
|
+
'JSXNamespacedName.namespace',
|
|
47
|
+
'JSXNamespacedName.name',
|
|
48
|
+
'LabeledStatement.label',
|
|
49
|
+
'BreakStatement.label',
|
|
50
|
+
'ContinueStatement.label',
|
|
51
|
+
'ExportSpecifier.exported',
|
|
52
|
+
]);
|
|
53
|
+
const MEMBER_PARTS = {
|
|
54
|
+
MemberExpression: ['object', 'property'],
|
|
55
|
+
JSXMemberExpression: ['object', 'property'],
|
|
56
|
+
TSQualifiedName: ['left', 'right'],
|
|
57
|
+
};
|
|
58
|
+
const MODULE_DECLARATIONS = new Set([
|
|
59
|
+
'ImportDeclaration',
|
|
60
|
+
'ExportNamedDeclaration',
|
|
61
|
+
'ExportAllDeclaration',
|
|
62
|
+
]);
|
|
63
|
+
function isIdentifier(node) {
|
|
64
|
+
return (isNode(node) &&
|
|
65
|
+
(node.type === 'Identifier' || node.type === 'JSXIdentifier') &&
|
|
66
|
+
typeof node['name'] === 'string');
|
|
67
|
+
}
|
|
68
|
+
function isUnaliased(other, name) {
|
|
69
|
+
return isNode(other) && other.start === name.start;
|
|
70
|
+
}
|
|
71
|
+
function renamed(renames, name) {
|
|
72
|
+
return renames !== undefined && Object.hasOwn(renames, name) ? renames[name] : undefined;
|
|
73
|
+
}
|
|
74
|
+
function moduleSource(node) {
|
|
75
|
+
const source = node['source'];
|
|
76
|
+
return isNode(source) && typeof source['value'] === 'string' ? source['value'] : undefined;
|
|
77
|
+
}
|
|
78
|
+
function replace(node, text) {
|
|
79
|
+
return { start: node.start, end: node.end, text };
|
|
80
|
+
}
|
|
81
|
+
function exportSpecifierEdit(specifier, renames) {
|
|
82
|
+
const local = specifier['local'];
|
|
83
|
+
if (!isIdentifier(local))
|
|
84
|
+
return undefined;
|
|
85
|
+
const to = renamed(renames, local.name);
|
|
86
|
+
if (to === undefined)
|
|
87
|
+
return undefined;
|
|
88
|
+
return replace(local, isUnaliased(specifier['exported'], local) ? `${to} as ${local.name}` : to);
|
|
89
|
+
}
|
|
90
|
+
function importsFromModule(node) {
|
|
91
|
+
return MODULE_DECLARATIONS.has(node.type) && moduleSource(node) !== undefined;
|
|
92
|
+
}
|
|
93
|
+
function importPlan(specifier, renames) {
|
|
94
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
95
|
+
const local = specifier['local'];
|
|
96
|
+
return isIdentifier(local) ? { namespace: local.name } : undefined;
|
|
97
|
+
}
|
|
98
|
+
const imported = specifier['imported'];
|
|
99
|
+
if (specifier.type !== 'ImportSpecifier' || !isIdentifier(imported))
|
|
100
|
+
return undefined;
|
|
101
|
+
const to = renamed(renames, imported.name);
|
|
102
|
+
if (to === undefined)
|
|
103
|
+
return undefined;
|
|
104
|
+
return isUnaliased(specifier['local'], imported)
|
|
105
|
+
? { local: { imported, from: imported.name, to } }
|
|
106
|
+
: { edit: replace(imported, to) };
|
|
107
|
+
}
|
|
108
|
+
function applyImportPlan(plan, { scan, renames }) {
|
|
109
|
+
if ('namespace' in plan)
|
|
110
|
+
scan.namespaces.set(plan.namespace, renames);
|
|
111
|
+
else if ('local' in plan)
|
|
112
|
+
scan.locals.push(plan.local);
|
|
113
|
+
else
|
|
114
|
+
scan.edits.push(plan.edit);
|
|
115
|
+
}
|
|
116
|
+
function scanModuleDeclaration(node, scan) {
|
|
117
|
+
const source = moduleSource(node);
|
|
118
|
+
const renames = source === undefined ? undefined : exportRenamesFor(source);
|
|
119
|
+
if (renames === undefined)
|
|
120
|
+
return;
|
|
121
|
+
const specifiers = Array.isArray(node['specifiers']) ? node['specifiers'].filter(isNode) : [];
|
|
122
|
+
if (node.type === 'ImportDeclaration') {
|
|
123
|
+
specifiers
|
|
124
|
+
.map((specifier) => importPlan(specifier, renames))
|
|
125
|
+
.forEach((plan) => plan && applyImportPlan(plan, { scan, renames }));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
specifiers
|
|
129
|
+
.map((specifier) => exportSpecifierEdit(specifier, renames))
|
|
130
|
+
.forEach((edit) => edit && scan.edits.push(edit));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function namespaceMember(node, namespaces) {
|
|
134
|
+
const parts = MEMBER_PARTS[node.type];
|
|
135
|
+
if (parts === undefined || node['computed'] === true)
|
|
136
|
+
return undefined;
|
|
137
|
+
const [objectKey, propertyKey] = parts;
|
|
138
|
+
const object = node[objectKey];
|
|
139
|
+
const property = node[propertyKey];
|
|
140
|
+
if (!isIdentifier(object) || !isIdentifier(property))
|
|
141
|
+
return undefined;
|
|
142
|
+
const to = renamed(namespaces.get(object.name), property.name);
|
|
143
|
+
if (to === undefined)
|
|
144
|
+
return undefined;
|
|
145
|
+
return {
|
|
146
|
+
namespace: object.name,
|
|
147
|
+
edit: replace(property, to),
|
|
148
|
+
text: `${object.name}.${property.name} → ${to}`,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function leftmostName(qualifier) {
|
|
152
|
+
if (isIdentifier(qualifier))
|
|
153
|
+
return qualifier;
|
|
154
|
+
return isNode(qualifier) && qualifier.type === 'TSQualifiedName'
|
|
155
|
+
? leftmostName(qualifier['left'])
|
|
156
|
+
: undefined;
|
|
157
|
+
}
|
|
158
|
+
function importTypeEdit(node) {
|
|
159
|
+
if (node.type !== 'TSImportType')
|
|
160
|
+
return undefined;
|
|
161
|
+
const source = moduleSource(node);
|
|
162
|
+
const first = leftmostName(node['qualifier']);
|
|
163
|
+
if (source === undefined || first === undefined)
|
|
164
|
+
return undefined;
|
|
165
|
+
const to = renamed(exportRenamesFor(source), first.name);
|
|
166
|
+
return to === undefined ? undefined : replace(first, to);
|
|
167
|
+
}
|
|
168
|
+
function scanIdentifier({ node, parent, key, inPattern }, scan) {
|
|
169
|
+
if (!isIdentifier(node))
|
|
170
|
+
return;
|
|
171
|
+
const slot = `${parent.type}.${key}`;
|
|
172
|
+
if (inPattern || DECLARATIONS.has(slot))
|
|
173
|
+
scan.bindings.add(node.name);
|
|
174
|
+
else if (!NAME_SLOTS.has(slot) || parent['computed'] === true) {
|
|
175
|
+
scan.references.push({ node, parent, key });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function childVisits({ node, inPattern }) {
|
|
179
|
+
return childEntries(node)
|
|
180
|
+
.filter(([key]) => node.type !== 'TSImportType' || key !== 'qualifier')
|
|
181
|
+
.map(([key, child]) => ({
|
|
182
|
+
node: child,
|
|
183
|
+
parent: node,
|
|
184
|
+
key,
|
|
185
|
+
inPattern: BINDING_PATTERNS.has(`${node.type}.${key}`) || (inPattern && !PATTERN_EXITS.has(key)),
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
function scanNode(visit, scan) {
|
|
189
|
+
if (importsFromModule(visit.node))
|
|
190
|
+
return;
|
|
191
|
+
scanIdentifier(visit, scan);
|
|
192
|
+
const member = namespaceMember(visit.node, scan.namespaces);
|
|
193
|
+
if (member !== undefined)
|
|
194
|
+
scan.members.push(member);
|
|
195
|
+
const importType = importTypeEdit(visit.node);
|
|
196
|
+
if (importType !== undefined)
|
|
197
|
+
scan.edits.push(importType);
|
|
198
|
+
childVisits(visit).forEach((child) => scanNode(child, scan));
|
|
199
|
+
}
|
|
200
|
+
function referenceEdit({ node, parent, key }, { from, to }) {
|
|
201
|
+
if (parent.type === 'Property' && parent['shorthand'] === true && key === 'value') {
|
|
202
|
+
return replace(node, `${from}: ${to}`);
|
|
203
|
+
}
|
|
204
|
+
if (parent.type === 'ExportSpecifier')
|
|
205
|
+
return exportSpecifierEdit(parent, { [from]: to });
|
|
206
|
+
return replace(node, to);
|
|
207
|
+
}
|
|
208
|
+
function renameLocalImport({ imported, from, to }, { bindings, references }) {
|
|
209
|
+
if (bindings.has(from) || bindings.has(to)) {
|
|
210
|
+
return {
|
|
211
|
+
edits: [replace(imported, `${to} as ${from}`)],
|
|
212
|
+
reports: [
|
|
213
|
+
{
|
|
214
|
+
start: imported.start,
|
|
215
|
+
end: imported.end,
|
|
216
|
+
kind: 'shadowed-rename',
|
|
217
|
+
text: `${from} → ${to}`,
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const edits = references
|
|
223
|
+
.filter((ref) => ref.node.name === from)
|
|
224
|
+
.map((ref) => referenceEdit(ref, { from, to }))
|
|
225
|
+
.filter((edit) => edit !== undefined);
|
|
226
|
+
return { edits: [replace(imported, to), ...edits], reports: [] };
|
|
227
|
+
}
|
|
228
|
+
function renameMember({ namespace, edit, text }, { bindings }) {
|
|
229
|
+
return bindings.has(namespace)
|
|
230
|
+
? { edits: [], reports: [{ start: edit.start, end: edit.end, kind: 'shadowed-namespace', text }] }
|
|
231
|
+
: { edits: [edit], reports: [] };
|
|
232
|
+
}
|
|
233
|
+
export function renameExports(program) {
|
|
234
|
+
const scan = {
|
|
235
|
+
edits: [],
|
|
236
|
+
locals: [],
|
|
237
|
+
namespaces: new Map(),
|
|
238
|
+
members: [],
|
|
239
|
+
bindings: new Set(),
|
|
240
|
+
references: [],
|
|
241
|
+
};
|
|
242
|
+
const body = Array.isArray(program['body']) ? program['body'].filter(isNode) : [];
|
|
243
|
+
body.filter(importsFromModule).forEach((node) => scanModuleDeclaration(node, scan));
|
|
244
|
+
body.forEach((node) => scanNode({ node, parent: program, key: 'body', inPattern: false }, scan));
|
|
245
|
+
const results = [
|
|
246
|
+
...scan.locals.map((local) => renameLocalImport(local, scan)),
|
|
247
|
+
...scan.members.map((member) => renameMember(member, scan)),
|
|
248
|
+
];
|
|
249
|
+
return {
|
|
250
|
+
edits: [...scan.edits, ...results.flatMap((result) => result.edits)],
|
|
251
|
+
reports: results.flatMap((result) => result.reports),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface ManifestRewriteResult {
|
|
2
|
+
source: string;
|
|
3
|
+
changes: string[];
|
|
4
|
+
reports: string[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Replaces every retired package in a `package.json`'s dependency fields with
|
|
8
|
+
* the package that replaces it, at `range`. A replacement lands once per field,
|
|
9
|
+
* in place of the first retired entry it replaces. A dependency the retired
|
|
10
|
+
* package installed and its replacement only peers on is added to the same
|
|
11
|
+
* field when absent. Mentions outside the
|
|
12
|
+
* dependency fields (scripts, overrides) are reported, not rewritten.
|
|
13
|
+
*/
|
|
14
|
+
export declare function rewriteManifest(source: string, range: string): ManifestRewriteResult;
|