@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,15 +1,16 @@
|
|
|
1
|
-
import { classifyPrincipalId, lakePrincipalId, resolveAccess, } from '@sanity/workflows';
|
|
1
|
+
import { classifyPrincipalId, describeResource, lakePrincipalId, resolveAccess, resourceGdr, } from '@sanity/workflows';
|
|
2
2
|
import { WorkflowCommand } from '../../lib/base-command.js';
|
|
3
3
|
import { resolveReadTargets } from '../../lib/context.js';
|
|
4
|
-
import { fail,
|
|
4
|
+
import { fail, failOn } from '../../lib/fail.js';
|
|
5
5
|
import { jsonFlags, readFlags, resourceFlags } from '../../lib/flags.js';
|
|
6
|
-
import { formatKeyValue
|
|
6
|
+
import { formatKeyValue } from '../../lib/ui.js';
|
|
7
7
|
export default class WhoamiCommand extends WorkflowCommand {
|
|
8
8
|
static description = 'Show the user ID Workflows records for your login. Put the Sanity user ID in user assignee fields.';
|
|
9
9
|
static examples = [
|
|
10
10
|
'<%= config.bin %> workflows whoami',
|
|
11
11
|
'<%= config.bin %> workflows whoami --json',
|
|
12
|
-
'<%= config.bin %> workflows whoami --
|
|
12
|
+
'<%= config.bin %> workflows whoami --workflow-resource dataset:abc123.production',
|
|
13
|
+
'<%= config.bin %> workflows whoami --workflow-resource media-library:mlAbc123',
|
|
13
14
|
];
|
|
14
15
|
static flags = {
|
|
15
16
|
...readFlags,
|
|
@@ -21,24 +22,23 @@ export default class WhoamiCommand extends WorkflowCommand {
|
|
|
21
22
|
const { targets } = await resolveReadTargets(flags);
|
|
22
23
|
const [target, ...rest] = targets;
|
|
23
24
|
if (target === undefined || rest.length > 0) {
|
|
24
|
-
fail('The config names several resources — pass --deployment or --
|
|
25
|
+
fail('The config names several resources — pass --deployment or --workflow-resource to choose one.', targets.map(({ resource }) => describeResource(resource)).join('\n'));
|
|
25
26
|
}
|
|
26
|
-
const access = await resolveAccess(target.client).catch((err) =>
|
|
27
|
-
const whoami = whoamiFrom(access
|
|
27
|
+
const access = await resolveAccess(target.client).catch((err) => failOn(err, 'Could not resolve your user:'));
|
|
28
|
+
const whoami = whoamiFrom(access);
|
|
28
29
|
if (flags.json) {
|
|
29
|
-
this.log(JSON.stringify(whoami, null, 2));
|
|
30
|
+
this.log(JSON.stringify({ ...whoami, resource: resourceGdr(target.resource) }, null, 2));
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
|
-
whoamiLines(whoami).forEach((line) => this.log(line));
|
|
33
|
+
whoamiLines({ ...whoami, resource: describeResource(target.resource) }).forEach((line) => this.log(line));
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
|
-
function whoamiFrom(access
|
|
36
|
+
function whoamiFrom(access) {
|
|
36
37
|
const { id } = access.actor;
|
|
37
38
|
return {
|
|
38
39
|
kind: classifyPrincipalId(id).namespace === 'robot' ? 'robot' : 'user',
|
|
39
40
|
sanityUserId: id,
|
|
40
41
|
projectUserId: lakePrincipalId(access),
|
|
41
|
-
resource,
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
function whoamiLines(whoami) {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { commandResult } from '../../lib/failure-kind.js';
|
|
1
2
|
import { finishCliTelemetry } from '../../lib/telemetry.js';
|
|
2
3
|
const hook = async function (options) {
|
|
3
4
|
try {
|
|
4
|
-
await finishCliTelemetry(
|
|
5
|
+
await finishCliTelemetry(commandResult(options.error));
|
|
5
6
|
}
|
|
6
7
|
catch {
|
|
7
8
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Hook } from '@oclif/core';
|
|
2
|
+
import { type WorkflowResource } from '@sanity/workflows';
|
|
2
3
|
import type { CommandTelemetry, FlagShape } from '../../lib/command-telemetry.ts';
|
|
3
4
|
export declare const CLI_PACKAGE_NAME = "@sanity/workflows-cli";
|
|
4
5
|
/** Under a host CLI, oclif passes this plugin's hooks the host's config, so
|
|
@@ -24,5 +25,9 @@ export declare function installPrerunTelemetry(args: {
|
|
|
24
25
|
commandTelemetry?: CommandTelemetry | undefined;
|
|
25
26
|
env?: Record<string, string | undefined>;
|
|
26
27
|
}): Promise<void>;
|
|
28
|
+
/** The resource the run's resource flags name, read before oclif parses
|
|
29
|
+
* them; a malformed value attributes the run to no resource, and the
|
|
30
|
+
* command itself reports the error. */
|
|
31
|
+
export declare function telemetryTarget(argv: string[]): WorkflowResource | undefined;
|
|
27
32
|
declare const hook: Hook<'prerun'>;
|
|
28
33
|
export default hook;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { parseResourceGdr } from '@sanity/workflows';
|
|
1
2
|
import { shouldRunWorkflowCliTelemetry } from '../../command-ids.js';
|
|
2
3
|
import { commandTraceContext } from '../../lib/command-context.js';
|
|
3
4
|
import { rawFlagValue, resourceFlags } from '../../lib/flags.js';
|
|
5
|
+
import { explicitResource } from '../../lib/resource-target.js';
|
|
4
6
|
import { shouldForceShareTelemetry } from '../../lib/share-definitions.js';
|
|
5
7
|
import { setupCliTelemetry } from '../../lib/telemetry-setup.js';
|
|
6
8
|
export const CLI_PACKAGE_NAME = '@sanity/workflows-cli';
|
|
@@ -15,15 +17,28 @@ export async function installPrerunTelemetry(args) {
|
|
|
15
17
|
await setupCliTelemetry({
|
|
16
18
|
cliVersion: version,
|
|
17
19
|
configPath: rawFlagValue(argv, { name: 'path' }),
|
|
18
|
-
target:
|
|
19
|
-
|
|
20
|
-
dataset: rawFlagValue(argv, { name: 'dataset', char: resourceFlags.dataset.char }),
|
|
21
|
-
},
|
|
20
|
+
target: telemetryTarget(argv),
|
|
21
|
+
deployment: rawFlagValue(argv, { name: 'deployment' }),
|
|
22
22
|
forceSend: shouldForceShareTelemetry({ commandId, argv }),
|
|
23
23
|
context: commandTraceContext({ commandId, argv, flagDefs, commandTelemetry, env }),
|
|
24
24
|
env,
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
+
export function telemetryTarget(argv) {
|
|
28
|
+
const flag = (name) => rawFlagValue(argv, { name, char: resourceFlags[name].char });
|
|
29
|
+
try {
|
|
30
|
+
const workflowResource = flag('workflow-resource');
|
|
31
|
+
return explicitResource({
|
|
32
|
+
'workflow-resource': workflowResource === undefined ? undefined : parseResourceGdr(workflowResource),
|
|
33
|
+
'project-id': flag('project-id'),
|
|
34
|
+
dataset: flag('dataset'),
|
|
35
|
+
'media-library-id': flag('media-library-id'),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
27
42
|
const hook = async function ({ config, Command, argv }) {
|
|
28
43
|
await installPrerunTelemetry({
|
|
29
44
|
bin: config.bin,
|
|
@@ -2,9 +2,9 @@ import { Command } from '@oclif/core';
|
|
|
2
2
|
/**
|
|
3
3
|
* Base for workflow commands: prompt interrupts exit with the conventional
|
|
4
4
|
* SIGINT code and no stack; an auth rejection, a reader-floor acknowledgement
|
|
5
|
-
* failure, and an unknown-project-roles or spawn-contract rejection
|
|
6
|
-
* expected
|
|
7
|
-
* other error propagates to oclif unchanged.
|
|
5
|
+
* failure, and an unknown-project-roles or spawn-contract rejection (all
|
|
6
|
+
* expected and user-fixable) render through the clean {@link failOn} path.
|
|
7
|
+
* Every other error propagates to oclif unchanged.
|
|
8
8
|
*/
|
|
9
9
|
export declare abstract class WorkflowCommand extends Command {
|
|
10
10
|
protected init(): Promise<void>;
|
package/dist/lib/base-command.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { styleText } from 'node:util';
|
|
2
|
-
import { Command } from '@oclif/core';
|
|
2
|
+
import { Command, Errors } from '@oclif/core';
|
|
3
3
|
import { exitCodes } from '@sanity/cli-core';
|
|
4
4
|
import { ReaderModelAcknowledgementError, SpawnContractsInvalidError, UnknownProjectRolesError, } from '@sanity/workflows';
|
|
5
|
-
import {
|
|
5
|
+
import { failOn, isAuthRejection } from './fail.js';
|
|
6
|
+
import { markUsageError } from './failure-kind.js';
|
|
6
7
|
import { flagTokens, isLongFlag } from './flags.js';
|
|
7
8
|
export class WorkflowCommand extends Command {
|
|
8
9
|
async init() {
|
|
@@ -16,18 +17,24 @@ export class WorkflowCommand extends Command {
|
|
|
16
17
|
await super.init();
|
|
17
18
|
}
|
|
18
19
|
async catch(err) {
|
|
20
|
+
if (!this.parsed) {
|
|
21
|
+
markUsageError(err);
|
|
22
|
+
}
|
|
23
|
+
if (err instanceof Errors.ExitError) {
|
|
24
|
+
return super.catch(err);
|
|
25
|
+
}
|
|
19
26
|
if (err.name === 'ExitPromptError' || err.message === 'SIGINT') {
|
|
20
27
|
this.logToStderr(`${styleText('yellow', '›')} Aborted by user`);
|
|
21
28
|
return this.exit(exitCodes.SIGINT);
|
|
22
29
|
}
|
|
23
30
|
if (isAuthRejection(err)) {
|
|
24
|
-
|
|
31
|
+
failOn(err, 'Authentication failed:');
|
|
25
32
|
}
|
|
26
33
|
if (err instanceof ReaderModelAcknowledgementError) {
|
|
27
|
-
|
|
34
|
+
failOn(err, 'Reader-floor acknowledgement:');
|
|
28
35
|
}
|
|
29
36
|
if (err instanceof UnknownProjectRolesError || err instanceof SpawnContractsInvalidError) {
|
|
30
|
-
|
|
37
|
+
failOn(err, 'Definitions rejected:');
|
|
31
38
|
}
|
|
32
39
|
return super.catch(err);
|
|
33
40
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type RuntimeEmissionPlan } from '@sanity/workflows/blueprints/generate';
|
|
2
2
|
export declare function needsFunctions(plan: RuntimeEmissionPlan): boolean;
|
|
3
3
|
export declare function needsReportLines(plan: RuntimeEmissionPlan): string[];
|
|
4
4
|
export declare function needsSummaryLines(plan: RuntimeEmissionPlan): string[];
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { styleText } from 'node:util';
|
|
2
|
+
import { everyUpdateCause, } from '@sanity/workflows/blueprints/generate';
|
|
3
|
+
import { directoryProjectId, resourceLabel } from '@sanity/workflows/generate';
|
|
2
4
|
import logSymbols from 'log-symbols';
|
|
3
5
|
import { deploymentLabel } from './select-deployment.js';
|
|
4
6
|
import { pluralize, sectionHeader } from './ui.js';
|
|
@@ -99,9 +101,13 @@ function drainLines(plan, emission) {
|
|
|
99
101
|
return [];
|
|
100
102
|
return [
|
|
101
103
|
` effects: ${declared.length} declared — ${declared.join(', ')}`,
|
|
102
|
-
...emission.drains.map((drain) => `${DETAIL}drain
|
|
104
|
+
...emission.drains.map((drain) => `${DETAIL}${DRAIN_KIND[drain.trigger]} ${drain.name} runs ${drain.effectNames.join(', ')}`),
|
|
103
105
|
];
|
|
104
106
|
}
|
|
107
|
+
const DRAIN_KIND = {
|
|
108
|
+
document: 'drain function',
|
|
109
|
+
scheduled: 'scheduled drain function (heartbeat stack, every minute)',
|
|
110
|
+
};
|
|
105
111
|
function clockLines(plan, emission) {
|
|
106
112
|
const { heartbeat } = emission;
|
|
107
113
|
if (heartbeat === undefined)
|
|
@@ -115,10 +121,34 @@ function clockLines(plan, emission) {
|
|
|
115
121
|
...(heartbeat.definitions.length === 0
|
|
116
122
|
? []
|
|
117
123
|
: [`${REASON}ticks instances of ${heartbeat.definitions.join(', ')}`]),
|
|
124
|
+
...clockStartLines(heartbeat),
|
|
118
125
|
`${REASON}plan cadence: every minute on Enterprise, hourly on Growth, daily on Free` +
|
|
119
126
|
` (https://www.sanity.io/docs/functions/functions-introduction)`,
|
|
120
127
|
];
|
|
121
128
|
}
|
|
129
|
+
function clockStartLines(heartbeat) {
|
|
130
|
+
const { clockStarts } = heartbeat;
|
|
131
|
+
if (clockStarts.length === 0)
|
|
132
|
+
return [];
|
|
133
|
+
return [
|
|
134
|
+
...clockStarts.map((start) => `${REASON}starts ${start.definition} on documents in ${watchedLabel(start.resource)}` +
|
|
135
|
+
' whose start filter has come true, with no write'),
|
|
136
|
+
`${REASON}after the deploy: starts every document the filter already admits that no` +
|
|
137
|
+
' instance holds, not only those that become eligible later, a share per run',
|
|
138
|
+
...windowLines(heartbeat.clockStartWindow),
|
|
139
|
+
`${REASON}cost: up to ${heartbeat.clockStartQueries.total} queries on every run,` +
|
|
140
|
+
` ${heartbeat.clockStartQueries.workflowResource} of them to the workflow resource`,
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
function windowLines(window) {
|
|
144
|
+
if (window === undefined)
|
|
145
|
+
return [];
|
|
146
|
+
return [
|
|
147
|
+
`${REASON}outside the workflow resource each run checks ${window} matching documents,` +
|
|
148
|
+
` held ones included: with n matching, a run every minute checks every window within` +
|
|
149
|
+
` 2 * ceil(n / ${window}) - 1 runs`,
|
|
150
|
+
];
|
|
151
|
+
}
|
|
122
152
|
function heartbeatSentence(heartbeat) {
|
|
123
153
|
const [first, ...rest] = heartbeat.reasons;
|
|
124
154
|
const more = rest.map((reason) => `, and ${reason}`).join('');
|
|
@@ -145,23 +175,70 @@ function watcherLines(emission) {
|
|
|
145
175
|
` — ${watcher.definitions.join(', ')}`,
|
|
146
176
|
...watcherReasonLines(watcher.updates),
|
|
147
177
|
]),
|
|
148
|
-
...mediaLibraryAccessLines(emission),
|
|
149
178
|
];
|
|
150
179
|
}
|
|
151
180
|
function watchedLabel(resource) {
|
|
152
|
-
return resource
|
|
181
|
+
return directoryProjectId(resource) === undefined
|
|
182
|
+
? `${resourceLabel(resource.type)} ${resource.id}`
|
|
183
|
+
: resource.id;
|
|
153
184
|
}
|
|
154
|
-
function
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
185
|
+
function organizationAccessLines(emission) {
|
|
186
|
+
if (emission === undefined)
|
|
187
|
+
return [];
|
|
188
|
+
const watched = emission.startWatchers.map((watcher) => watcher.resource);
|
|
189
|
+
const { ids, labels } = outsideProjects([emission.workflowResource, ...watched]);
|
|
190
|
+
if (ids.length === 0)
|
|
159
191
|
return [];
|
|
192
|
+
const store = emission.workflowResource.id;
|
|
193
|
+
const read = ids.filter((id) => id !== store);
|
|
194
|
+
const access = [
|
|
195
|
+
...(ids.includes(store) ? [`read and write ${store}`] : []),
|
|
196
|
+
...(read.length === 0 ? [] : [`read ${read.join(', ')}`]),
|
|
197
|
+
].join(', and ');
|
|
160
198
|
const robots = [emission.robotName, ...(emission.heartbeat ? [emission.heartbeat.robotName] : [])];
|
|
161
199
|
return [
|
|
162
|
-
` ${logSymbols.warning} ${robots.join(' and ')} must
|
|
163
|
-
` must the identity that deploys the project stack: confirm their
|
|
164
|
-
` in the organization that owns the library, or the deploy or every start there fails`,
|
|
200
|
+
` ${logSymbols.warning} ${robots.join(' and ')} must ${access}, and so` +
|
|
201
|
+
` must the identity that deploys the project stack: confirm their ${labels.join(' and ')}` +
|
|
202
|
+
` access in the organization that owns the library, or the deploy or every start there fails`,
|
|
203
|
+
];
|
|
204
|
+
}
|
|
205
|
+
function clockStartAccessLines(emission) {
|
|
206
|
+
const heartbeat = emission?.heartbeat;
|
|
207
|
+
if (emission === undefined || heartbeat === undefined)
|
|
208
|
+
return [];
|
|
209
|
+
const named = new Set([emission.workflowResource, ...emission.startWatchers.map((watcher) => watcher.resource)].map((resource) => resource.id));
|
|
210
|
+
const { ids, labels } = outsideProjects(heartbeat.clockStarts
|
|
211
|
+
.map((start) => start.resource)
|
|
212
|
+
.filter((resource) => !named.has(resource.id)));
|
|
213
|
+
if (ids.length === 0)
|
|
214
|
+
return [];
|
|
215
|
+
return [
|
|
216
|
+
` ${logSymbols.warning} ${heartbeat.robotName} must read ${ids.join(', ')} for its` +
|
|
217
|
+
` clock starts: confirm its ${labels.join(' and ')} access in the organization that owns` +
|
|
218
|
+
` the library, or every clock start there fails`,
|
|
219
|
+
];
|
|
220
|
+
}
|
|
221
|
+
function outsideProjects(resources) {
|
|
222
|
+
const outside = resources.filter((resource) => directoryProjectId(resource) === undefined);
|
|
223
|
+
return {
|
|
224
|
+
ids: [...new Set(outside.map((resource) => resource.id))],
|
|
225
|
+
labels: [...new Set(outside.map((resource) => resourceLabel(resource.type)))],
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function heartbeatProjectReadLines(heartbeat) {
|
|
229
|
+
if (heartbeat === undefined || directoryProjectId(heartbeat.workflowResource) !== undefined) {
|
|
230
|
+
return [];
|
|
231
|
+
}
|
|
232
|
+
const datasets = heartbeat.aliasResources
|
|
233
|
+
.filter((resource) => directoryProjectId(resource) !== undefined)
|
|
234
|
+
.map((resource) => resource.id);
|
|
235
|
+
if (datasets.length === 0)
|
|
236
|
+
return [];
|
|
237
|
+
const clocked = heartbeat.clockStarts.some((start) => datasets.includes(start.resource.id));
|
|
238
|
+
return [
|
|
239
|
+
` ${logSymbols.warning} ${heartbeat.robotName} holds no project role, so it cannot read` +
|
|
240
|
+
` ${datasets.join(', ')}: a scheduled drain or tick whose effect or condition reads` +
|
|
241
|
+
` that alias fails${clocked ? ', and so does every clock start there' : ''}`,
|
|
165
242
|
];
|
|
166
243
|
}
|
|
167
244
|
function watcherReasonLines(updates) {
|
|
@@ -179,12 +256,6 @@ function watcherReasonLines(updates) {
|
|
|
179
256
|
}
|
|
180
257
|
return [];
|
|
181
258
|
}
|
|
182
|
-
const EVERY_WRITE_CAUSE = {
|
|
183
|
-
readsOtherDocuments: 'reads another document, which a trigger cannot evaluate',
|
|
184
|
-
readsNow: 'reads $now, which a trigger cannot bind',
|
|
185
|
-
readsVariable: 'reads $tag or $definition, which a trigger cannot bind',
|
|
186
|
-
notBoolean: 'is not recognized as a boolean expression',
|
|
187
|
-
};
|
|
188
259
|
const EVERY_WRITE_REMEDY = {
|
|
189
260
|
readsOtherDocuments: 'move the cross-document condition into a stage',
|
|
190
261
|
readsNow: 'move the time condition into a stage',
|
|
@@ -196,9 +267,17 @@ function warningLines(plan) {
|
|
|
196
267
|
return [
|
|
197
268
|
...plan.anyTypeSubjects.map((name) => ` ${logSymbols.warning} ${name} declares a subject with no types, so a write to any` +
|
|
198
269
|
` document type invokes its watcher — declare types unless that is intended`),
|
|
199
|
-
...plan.
|
|
270
|
+
...organizationAccessLines(plan.emission),
|
|
271
|
+
...clockStartAccessLines(plan.emission),
|
|
272
|
+
...heartbeatProjectReadLines(plan.emission?.heartbeat),
|
|
273
|
+
...plan.everyWriteStartFilters.map(({ definition, reason }) => ` ${logSymbols.warning} ${definition} has a start filter that ${everyUpdateCause[reason]},` +
|
|
200
274
|
` so every matching write starts it and a later matching write can start another` +
|
|
201
|
-
` instance for the same document — ${EVERY_WRITE_REMEDY[reason]}
|
|
275
|
+
` instance for the same document — ${EVERY_WRITE_REMEDY[reason]}`),
|
|
276
|
+
...plan.unclockedStartFilters.map((name) => ` ${logSymbols.warning} ${name} has a start filter that reads $now, but the heartbeat` +
|
|
277
|
+
` does not start it on the clock: the filter also reads another document or is not a` +
|
|
278
|
+
` boolean expression, its subject is not taken from the started document, or its` +
|
|
279
|
+
` start.roles admit no role the heartbeat robot holds, so a document the filter admits` +
|
|
280
|
+
` by time alone starts only on a later write`),
|
|
202
281
|
...plan.subjectlessDefinitions.map((name) => ` ${logSymbols.warning} ${name} starts autonomously but declares no subject, so no` +
|
|
203
282
|
` document write can start it and it gets no watcher`),
|
|
204
283
|
];
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { FUNCTIONS_DIR, hasDocumentFunctions, HEARTBEAT_BLUEPRINT_PATH, HEARTBEAT_STACK_DIR, } from '@sanity/workflows/blueprints/generate';
|
|
1
|
+
import { FUNCTIONS_DIR, hasDocumentFunctions, HEARTBEAT_BLUEPRINT_PATH, HEARTBEAT_STACK_DIR, scheduledDrains, } from '@sanity/workflows/blueprints/generate';
|
|
2
2
|
import logSymbols from 'log-symbols';
|
|
3
3
|
import { sectionHeader } from './ui.js';
|
|
4
4
|
const DEPLOY_COMMAND = 'npx sanity@latest blueprints deploy';
|
|
5
5
|
function heartbeatStackWrites(args) {
|
|
6
|
-
const functionDirs = args.plan.deployments.flatMap(({ emission }) => emission?.heartbeat === undefined
|
|
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}/`));
|
|
7
9
|
return args.applied.filter((write) => write.path.startsWith(`${HEARTBEAT_STACK_DIR}/`) ||
|
|
8
10
|
functionDirs.some((dir) => write.path.startsWith(dir)));
|
|
9
11
|
}
|
|
@@ -30,15 +32,31 @@ function heartbeatStackLines(args) {
|
|
|
30
32
|
return [];
|
|
31
33
|
const changed = changedWrites(heartbeat);
|
|
32
34
|
const created = changed.some((write) => write.path === HEARTBEAT_BLUEPRINT_PATH && write.status === 'created');
|
|
33
|
-
|
|
35
|
+
const drains = hasScheduledDrains(args.plan);
|
|
36
|
+
if (!verbose && !drains && changed.length === 0)
|
|
34
37
|
return [];
|
|
38
|
+
const redeploy = created ? [] : redeployLines({ changed, drains });
|
|
35
39
|
return [
|
|
36
40
|
` heartbeat stack: \`${DEPLOY_COMMAND}\` in ${HEARTBEAT_STACK_DIR}/, with an` +
|
|
37
|
-
` organization deploy token. It holds
|
|
38
|
-
...
|
|
41
|
+
` organization deploy token. It holds ${heartbeatStackHolds(drains)}.`,
|
|
42
|
+
...redeploy,
|
|
39
43
|
...(verbose || created ? missingHeartbeatLines(args.plan) : []),
|
|
40
44
|
];
|
|
41
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
|
+
}
|
|
42
60
|
function heartbeatRedeployLine(changed) {
|
|
43
61
|
if (changed.length === 0) {
|
|
44
62
|
return (` ${logSymbols.info} the heartbeat stack's files are unchanged: these definitions need no` +
|
|
@@ -57,8 +75,9 @@ function missingHeartbeatLines(plan) {
|
|
|
57
75
|
];
|
|
58
76
|
}
|
|
59
77
|
function waitingWithoutHeartbeat(plan) {
|
|
60
|
-
const
|
|
61
|
-
|
|
78
|
+
const { emission } = plan;
|
|
79
|
+
const heartbeat = emission?.heartbeat;
|
|
80
|
+
if (emission === undefined || heartbeat === undefined)
|
|
62
81
|
return [];
|
|
63
82
|
const ticked = new Set(heartbeat.definitions);
|
|
64
83
|
const within = (sites) => [
|
|
@@ -66,8 +85,13 @@ function waitingWithoutHeartbeat(plan) {
|
|
|
66
85
|
];
|
|
67
86
|
const deadlines = within(plan.needs.heartbeat.timeSites);
|
|
68
87
|
const spawners = within(plan.needs.heartbeat.spawnSites);
|
|
69
|
-
const
|
|
88
|
+
const scheduled = scheduledDrains(emission).flatMap((drain) => drain.effectNames);
|
|
89
|
+
const effects = plan.hosting.effects.function.filter((name) => !scheduled.includes(name));
|
|
90
|
+
const clocked = [...new Set(heartbeat.clockStarts.map((start) => start.definition))];
|
|
70
91
|
return [
|
|
92
|
+
...(scheduled.length === 0
|
|
93
|
+
? []
|
|
94
|
+
: [`no effect runs at all, because its drain runs from this stack: ${scheduled.join(', ')}`]),
|
|
71
95
|
...(deadlines.length === 0
|
|
72
96
|
? []
|
|
73
97
|
: [
|
|
@@ -84,5 +108,11 @@ function waitingWithoutHeartbeat(plan) {
|
|
|
84
108
|
`an effect queued before its drain existed, or whose drain timed out, waits until` +
|
|
85
109
|
` a later drain picks it up: ${effects.join(', ')}`,
|
|
86
110
|
]),
|
|
111
|
+
...(clocked.length === 0
|
|
112
|
+
? []
|
|
113
|
+
: [
|
|
114
|
+
`a document whose start filter comes true by time alone starts only on a later write,` +
|
|
115
|
+
` or never in a Media Library: ${clocked.join(', ')}`,
|
|
116
|
+
]),
|
|
87
117
|
];
|
|
88
118
|
}
|
package/dist/lib/client.d.ts
CHANGED
|
@@ -15,8 +15,11 @@ export declare function resolveApiHost(): string | undefined;
|
|
|
15
15
|
* that only 401s once it hits the API.
|
|
16
16
|
*/
|
|
17
17
|
export declare function resolveToken(): Promise<string>;
|
|
18
|
+
export declare class AuthMissingError extends Error {
|
|
19
|
+
constructor();
|
|
20
|
+
}
|
|
18
21
|
/** {@link resolveToken}, funnelling the no-token case through the clean
|
|
19
|
-
* {@link
|
|
22
|
+
* {@link failOn} path rather than letting it surface as an oclif stack. */
|
|
20
23
|
export declare function resolveTokenOrFail(): Promise<string>;
|
|
21
24
|
/** Build a client for a workflow resource, authenticated with `token`. */
|
|
22
25
|
export declare function clientFor(resource: WorkflowResource, token: string): SanityClient;
|
package/dist/lib/client.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { getCliToken, isStaging } from '@sanity/cli-core';
|
|
2
2
|
import { createClient } from '@sanity/client';
|
|
3
|
-
import { clientConfigFromResource, ENGINE_API_VERSION,
|
|
3
|
+
import { clientConfigFromResource, ENGINE_API_VERSION, } from '@sanity/workflows';
|
|
4
4
|
import { ENV, envAuthToken } from './env.js';
|
|
5
|
-
import {
|
|
5
|
+
import { failOn } from './fail.js';
|
|
6
6
|
const API_VERSION = ENGINE_API_VERSION;
|
|
7
7
|
const STAGING_API_HOST = 'https://api.sanity.work';
|
|
8
8
|
export function resolveApiHost() {
|
|
@@ -17,10 +17,15 @@ export async function resolveToken() {
|
|
|
17
17
|
if (fromSession) {
|
|
18
18
|
return fromSession;
|
|
19
19
|
}
|
|
20
|
-
throw new
|
|
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
|
+
}
|
|
21
26
|
}
|
|
22
27
|
export async function resolveTokenOrFail() {
|
|
23
|
-
return resolveToken().catch((err) =>
|
|
28
|
+
return resolveToken().catch((err) => failOn(err, 'Authentication required:'));
|
|
24
29
|
}
|
|
25
30
|
function baseClientConfig(token) {
|
|
26
31
|
const apiHost = resolveApiHost();
|
|
@@ -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,19 @@
|
|
|
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. An import
|
|
14
|
+
* whose new name the file already imports from the same entry merges into that
|
|
15
|
+
* import. If the file binds the old or new name otherwise, the import is
|
|
16
|
+
* aliased instead; if it reuses a namespace name, that namespace's members are
|
|
17
|
+
* reported for a hand rename.
|
|
18
|
+
*/
|
|
19
|
+
export declare function renameExports(program: AstNode, source: string): ExportRenameResult;
|