@sanity/workflows-cli 0.0.0-stage → 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.
Files changed (134) hide show
  1. package/CHANGELOG.md +1981 -0
  2. package/LICENSE +21 -0
  3. package/README.md +848 -2
  4. package/bin/run.js +8 -0
  5. package/dist/command-ids.d.ts +10 -0
  6. package/dist/command-ids.js +10 -0
  7. package/dist/commands/workflows/abort.d.ts +29 -0
  8. package/dist/commands/workflows/abort.js +50 -0
  9. package/dist/commands/workflows/blueprints/generate.d.ts +12 -0
  10. package/dist/commands/workflows/blueprints/generate.js +46 -0
  11. package/dist/commands/workflows/codemod/packages.d.ts +13 -0
  12. package/dist/commands/workflows/codemod/packages.js +41 -0
  13. package/dist/commands/workflows/definitions/delete.d.ts +44 -0
  14. package/dist/commands/workflows/definitions/delete.js +74 -0
  15. package/dist/commands/workflows/definitions/diff.d.ts +23 -0
  16. package/dist/commands/workflows/definitions/diff.js +55 -0
  17. package/dist/commands/workflows/definitions/list.d.ts +52 -0
  18. package/dist/commands/workflows/definitions/list.js +139 -0
  19. package/dist/commands/workflows/definitions/show.d.ts +66 -0
  20. package/dist/commands/workflows/definitions/show.js +129 -0
  21. package/dist/commands/workflows/definitions/validate.d.ts +14 -0
  22. package/dist/commands/workflows/definitions/validate.js +50 -0
  23. package/dist/commands/workflows/deploy.d.ts +117 -0
  24. package/dist/commands/workflows/deploy.js +266 -0
  25. package/dist/commands/workflows/diagnose.d.ts +53 -0
  26. package/dist/commands/workflows/diagnose.js +195 -0
  27. package/dist/commands/workflows/fire-action.d.ts +53 -0
  28. package/dist/commands/workflows/fire-action.js +182 -0
  29. package/dist/commands/workflows/list.d.ts +79 -0
  30. package/dist/commands/workflows/list.js +249 -0
  31. package/dist/commands/workflows/nuke.d.ts +73 -0
  32. package/dist/commands/workflows/nuke.js +175 -0
  33. package/dist/commands/workflows/reset-activity.d.ts +32 -0
  34. package/dist/commands/workflows/reset-activity.js +60 -0
  35. package/dist/commands/workflows/show.d.ts +30 -0
  36. package/dist/commands/workflows/show.js +156 -0
  37. package/dist/commands/workflows/start.d.ts +57 -0
  38. package/dist/commands/workflows/start.js +196 -0
  39. package/dist/commands/workflows/tail.d.ts +24 -0
  40. package/dist/commands/workflows/tail.js +83 -0
  41. package/dist/commands/workflows/whoami.d.ts +16 -0
  42. package/dist/commands/workflows/whoami.js +53 -0
  43. package/dist/help.d.ts +13 -0
  44. package/dist/help.js +50 -0
  45. package/dist/hooks/finally/telemetry.d.ts +3 -0
  46. package/dist/hooks/finally/telemetry.js +10 -0
  47. package/dist/hooks/prerun/telemetry.d.ts +33 -0
  48. package/dist/hooks/prerun/telemetry.js +52 -0
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.js +1 -0
  51. package/dist/lib/base-command.d.ts +14 -0
  52. package/dist/lib/base-command.js +41 -0
  53. package/dist/lib/blueprint-emit.d.ts +56 -0
  54. package/dist/lib/blueprint-emit.js +233 -0
  55. package/dist/lib/blueprint-needs.d.ts +5 -0
  56. package/dist/lib/blueprint-needs.js +284 -0
  57. package/dist/lib/blueprint-stacks.d.ts +6 -0
  58. package/dist/lib/blueprint-stacks.js +118 -0
  59. package/dist/lib/cause-detail.d.ts +9 -0
  60. package/dist/lib/cause-detail.js +112 -0
  61. package/dist/lib/client.d.ts +27 -0
  62. package/dist/lib/client.js +45 -0
  63. package/dist/lib/codemod/ast.d.ts +17 -0
  64. package/dist/lib/codemod/ast.js +14 -0
  65. package/dist/lib/codemod/export-rename.d.ts +19 -0
  66. package/dist/lib/codemod/export-rename.js +337 -0
  67. package/dist/lib/codemod/manifest-rewrite.d.ts +17 -0
  68. package/dist/lib/codemod/manifest-rewrite.js +133 -0
  69. package/dist/lib/codemod/package-map.d.ts +27 -0
  70. package/dist/lib/codemod/package-map.js +99 -0
  71. package/dist/lib/codemod/report.d.ts +4 -0
  72. package/dist/lib/codemod/report.js +51 -0
  73. package/dist/lib/codemod/run.d.ts +25 -0
  74. package/dist/lib/codemod/run.js +108 -0
  75. package/dist/lib/codemod/source-rewrite.d.ts +20 -0
  76. package/dist/lib/codemod/source-rewrite.js +150 -0
  77. package/dist/lib/command-context.d.ts +20 -0
  78. package/dist/lib/command-context.js +24 -0
  79. package/dist/lib/command-telemetry.d.ts +54 -0
  80. package/dist/lib/command-telemetry.js +82 -0
  81. package/dist/lib/context.d.ts +151 -0
  82. package/dist/lib/context.js +263 -0
  83. package/dist/lib/default-partition.d.ts +23 -0
  84. package/dist/lib/default-partition.js +47 -0
  85. package/dist/lib/definition-files.d.ts +14 -0
  86. package/dist/lib/definition-files.js +60 -0
  87. package/dist/lib/definitions.d.ts +63 -0
  88. package/dist/lib/definitions.js +84 -0
  89. package/dist/lib/diff.d.ts +12 -0
  90. package/dist/lib/diff.js +93 -0
  91. package/dist/lib/env.d.ts +7 -0
  92. package/dist/lib/env.js +8 -0
  93. package/dist/lib/fail.d.ts +36 -0
  94. package/dist/lib/fail.js +55 -0
  95. package/dist/lib/failure-kind.d.ts +7 -0
  96. package/dist/lib/failure-kind.js +66 -0
  97. package/dist/lib/flags.d.ts +53 -0
  98. package/dist/lib/flags.js +127 -0
  99. package/dist/lib/follow-up-command.d.ts +18 -0
  100. package/dist/lib/follow-up-command.js +12 -0
  101. package/dist/lib/history-rows.d.ts +3 -0
  102. package/dist/lib/history-rows.js +44 -0
  103. package/dist/lib/load-config.d.ts +51 -0
  104. package/dist/lib/load-config.js +108 -0
  105. package/dist/lib/nuke.d.ts +124 -0
  106. package/dist/lib/nuke.js +206 -0
  107. package/dist/lib/operation-args.d.ts +42 -0
  108. package/dist/lib/operation-args.js +21 -0
  109. package/dist/lib/ops-report.d.ts +45 -0
  110. package/dist/lib/ops-report.js +31 -0
  111. package/dist/lib/params.d.ts +3 -0
  112. package/dist/lib/params.js +21 -0
  113. package/dist/lib/pick-resource.d.ts +13 -0
  114. package/dist/lib/pick-resource.js +82 -0
  115. package/dist/lib/prompt.d.ts +35 -0
  116. package/dist/lib/prompt.js +11 -0
  117. package/dist/lib/read-fanout.d.ts +45 -0
  118. package/dist/lib/read-fanout.js +45 -0
  119. package/dist/lib/resource-target.d.ts +10 -0
  120. package/dist/lib/resource-target.js +12 -0
  121. package/dist/lib/select-deployment.d.ts +31 -0
  122. package/dist/lib/select-deployment.js +91 -0
  123. package/dist/lib/share-definitions.d.ts +101 -0
  124. package/dist/lib/share-definitions.js +188 -0
  125. package/dist/lib/telemetry-setup.d.ts +77 -0
  126. package/dist/lib/telemetry-setup.js +117 -0
  127. package/dist/lib/telemetry.d.ts +81 -0
  128. package/dist/lib/telemetry.js +90 -0
  129. package/dist/lib/ui.d.ts +88 -0
  130. package/dist/lib/ui.js +114 -0
  131. package/dist/standalone-argv.d.ts +14 -0
  132. package/dist/standalone-argv.js +57 -0
  133. package/oclif.manifest.json +1894 -0
  134. package/package.json +118 -3
@@ -0,0 +1,337 @@
1
+ import { childEntries, isNode } from './ast.js';
2
+ import { entryFor, 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 specifiersOf(declaration) {
94
+ return Array.isArray(declaration['specifiers']) ? declaration['specifiers'].filter(isNode) : [];
95
+ }
96
+ function isTypeOnly(declaration, specifier) {
97
+ return declaration['importKind'] === 'type' || specifier['importKind'] === 'type';
98
+ }
99
+ function lineEnd(source, offset) {
100
+ return source[offset] === '\n' ? offset + 1 : offset;
101
+ }
102
+ function removalSpans({ declaration, removed }, source) {
103
+ const specifiers = specifiersOf(declaration);
104
+ const lastKept = specifiers.findLastIndex((specifier) => !removed.has(specifier));
105
+ const last = specifiers.at(-1);
106
+ if (lastKept === -1 || last === undefined) {
107
+ return [{ start: declaration.start, end: lineEnd(source, declaration.end) }];
108
+ }
109
+ const inner = specifiers.slice(0, lastKept).flatMap((specifier, index) => {
110
+ const next = specifiers[index + 1];
111
+ return removed.has(specifier) && next ? [{ start: specifier.start, end: next.start }] : [];
112
+ });
113
+ const kept = specifiers[lastKept];
114
+ const tail = specifiers[lastKept + 1];
115
+ if (kept === undefined || tail === undefined)
116
+ return inner;
117
+ const start = kept.type === 'ImportSpecifier' ? kept.end : tail.start;
118
+ return [...inner, { start, end: last.end }];
119
+ }
120
+ function importPlan(specifier, { renames, context }) {
121
+ if (specifier.type === 'ImportNamespaceSpecifier') {
122
+ const local = specifier['local'];
123
+ return isIdentifier(local) ? { namespace: local.name } : undefined;
124
+ }
125
+ const imported = specifier['imported'];
126
+ if (specifier.type !== 'ImportSpecifier' || !isIdentifier(imported))
127
+ return undefined;
128
+ const to = renamed(renames, imported.name);
129
+ if (to === undefined)
130
+ return undefined;
131
+ if (!isUnaliased(specifier['local'], imported))
132
+ return { edit: replace(imported, to) };
133
+ const local = {
134
+ imported,
135
+ from: imported.name,
136
+ to,
137
+ entry: context.entry,
138
+ typeOnly: isTypeOnly(context.declaration, specifier),
139
+ specifier,
140
+ declaration: context.declaration,
141
+ };
142
+ return { local };
143
+ }
144
+ function applyImportPlan(plan, { scan, renames }) {
145
+ if ('namespace' in plan)
146
+ scan.namespaces.set(plan.namespace, renames);
147
+ else if ('local' in plan)
148
+ scan.locals.push(plan.local);
149
+ else
150
+ scan.edits.push(plan.edit);
151
+ }
152
+ function importedName(specifier, { declaration, entry }) {
153
+ const local = specifier['local'];
154
+ const imported = specifier['imported'];
155
+ if (!isIdentifier(local))
156
+ return undefined;
157
+ const name = {
158
+ entry,
159
+ imported: isIdentifier(imported) ? imported.name : undefined,
160
+ typeOnly: isTypeOnly(declaration, specifier),
161
+ };
162
+ return [local.name, name];
163
+ }
164
+ function scanModuleDeclaration(node, scan) {
165
+ const moduleName = moduleSource(node);
166
+ if (moduleName === undefined)
167
+ return;
168
+ const entry = entryFor(moduleName);
169
+ const specifiers = specifiersOf(node);
170
+ const context = { declaration: node, entry };
171
+ if (node.type === 'ImportDeclaration') {
172
+ specifiers
173
+ .map((specifier) => importedName(specifier, context))
174
+ .forEach((name) => name && scan.imports.set(...name));
175
+ }
176
+ const renames = exportRenamesFor(moduleName);
177
+ if (renames === undefined)
178
+ return;
179
+ if (node.type === 'ImportDeclaration') {
180
+ specifiers
181
+ .map((specifier) => importPlan(specifier, { renames, context }))
182
+ .forEach((plan) => plan && applyImportPlan(plan, { scan, renames }));
183
+ }
184
+ else {
185
+ specifiers
186
+ .map((specifier) => exportSpecifierEdit(specifier, renames))
187
+ .forEach((edit) => edit && scan.edits.push(edit));
188
+ }
189
+ }
190
+ function namespaceMember(node, namespaces) {
191
+ const parts = MEMBER_PARTS[node.type];
192
+ if (parts === undefined || node['computed'] === true)
193
+ return undefined;
194
+ const [objectKey, propertyKey] = parts;
195
+ const object = node[objectKey];
196
+ const property = node[propertyKey];
197
+ if (!isIdentifier(object) || !isIdentifier(property))
198
+ return undefined;
199
+ const to = renamed(namespaces.get(object.name), property.name);
200
+ if (to === undefined)
201
+ return undefined;
202
+ return {
203
+ namespace: object.name,
204
+ edit: replace(property, to),
205
+ text: `${object.name}.${property.name} → ${to}`,
206
+ };
207
+ }
208
+ function leftmostName(qualifier) {
209
+ if (isIdentifier(qualifier))
210
+ return qualifier;
211
+ return isNode(qualifier) && qualifier.type === 'TSQualifiedName'
212
+ ? leftmostName(qualifier['left'])
213
+ : undefined;
214
+ }
215
+ function importTypeEdit(node) {
216
+ if (node.type !== 'TSImportType')
217
+ return undefined;
218
+ const source = moduleSource(node);
219
+ const first = leftmostName(node['qualifier']);
220
+ if (source === undefined || first === undefined)
221
+ return undefined;
222
+ const to = renamed(exportRenamesFor(source), first.name);
223
+ return to === undefined ? undefined : replace(first, to);
224
+ }
225
+ function scanIdentifier({ node, parent, key, inPattern }, scan) {
226
+ if (!isIdentifier(node))
227
+ return;
228
+ const slot = `${parent.type}.${key}`;
229
+ if (inPattern || DECLARATIONS.has(slot))
230
+ scan.bindings.add(node.name);
231
+ else if (!NAME_SLOTS.has(slot) || parent['computed'] === true) {
232
+ scan.references.push({ node, parent, key });
233
+ }
234
+ }
235
+ function childVisits({ node, inPattern }) {
236
+ return childEntries(node)
237
+ .filter(([key]) => node.type !== 'TSImportType' || key !== 'qualifier')
238
+ .map(([key, child]) => ({
239
+ node: child,
240
+ parent: node,
241
+ key,
242
+ inPattern: BINDING_PATTERNS.has(`${node.type}.${key}`) || (inPattern && !PATTERN_EXITS.has(key)),
243
+ }));
244
+ }
245
+ function scanNode(visit, scan) {
246
+ if (importsFromModule(visit.node))
247
+ return;
248
+ scanIdentifier(visit, scan);
249
+ const member = namespaceMember(visit.node, scan.namespaces);
250
+ if (member !== undefined)
251
+ scan.members.push(member);
252
+ const importType = importTypeEdit(visit.node);
253
+ if (importType !== undefined)
254
+ scan.edits.push(importType);
255
+ childVisits(visit).forEach((child) => scanNode(child, scan));
256
+ }
257
+ function referenceEdit({ node, parent, key }, { from, to }) {
258
+ if (parent.type === 'Property' && parent['shorthand'] === true && key === 'value') {
259
+ return replace(node, `${from}: ${to}`);
260
+ }
261
+ if (parent.type === 'ExportSpecifier')
262
+ return exportSpecifierEdit(parent, { [from]: to });
263
+ return replace(node, to);
264
+ }
265
+ function mergesInto(local, imports) {
266
+ const existing = imports.get(local.to);
267
+ return (existing !== undefined &&
268
+ existing.entry === local.entry &&
269
+ existing.imported === local.to &&
270
+ (local.typeOnly || !existing.typeOnly));
271
+ }
272
+ function renameLocalImport(local, scan) {
273
+ const { imported, from, to } = local;
274
+ const { bindings, imports, references } = scan;
275
+ if (!bindings.has(from) && !bindings.has(to) && mergesInto(local, imports)) {
276
+ return { ...referenceRenames(local, references), merged: local };
277
+ }
278
+ if (bindings.has(from) || bindings.has(to) || imports.has(to)) {
279
+ return {
280
+ edits: [replace(imported, `${to} as ${from}`)],
281
+ reports: [
282
+ {
283
+ start: imported.start,
284
+ end: imported.end,
285
+ kind: 'shadowed-rename',
286
+ text: `${from} → ${to}`,
287
+ },
288
+ ],
289
+ };
290
+ }
291
+ const renames = referenceRenames(local, references);
292
+ return { edits: [replace(imported, to), ...renames.edits], reports: [] };
293
+ }
294
+ function referenceRenames({ from, to }, references) {
295
+ const edits = references
296
+ .filter((ref) => ref.node.name === from)
297
+ .map((ref) => referenceEdit(ref, { from, to }))
298
+ .filter((edit) => edit !== undefined);
299
+ return { edits, reports: [] };
300
+ }
301
+ function mergeEdits(merged, source) {
302
+ const declarations = new Set(merged.map((local) => local.declaration));
303
+ const removed = new Set(merged.map((local) => local.specifier));
304
+ return [...declarations]
305
+ .flatMap((declaration) => removalSpans({ declaration, removed }, source))
306
+ .map((span) => ({ ...span, text: '' }));
307
+ }
308
+ function renameMember({ namespace, edit, text }, { bindings }) {
309
+ return bindings.has(namespace)
310
+ ? { edits: [], reports: [{ start: edit.start, end: edit.end, kind: 'shadowed-namespace', text }] }
311
+ : { edits: [edit], reports: [] };
312
+ }
313
+ export function renameExports(program, source) {
314
+ const scan = {
315
+ edits: [],
316
+ locals: [],
317
+ namespaces: new Map(),
318
+ members: [],
319
+ bindings: new Set(),
320
+ imports: new Map(),
321
+ references: [],
322
+ };
323
+ const body = Array.isArray(program['body']) ? program['body'].filter(isNode) : [];
324
+ body.filter(importsFromModule).forEach((node) => scanModuleDeclaration(node, scan));
325
+ body.forEach((node) => scanNode({ node, parent: program, key: 'body', inPattern: false }, scan));
326
+ const locals = scan.locals.map((local) => renameLocalImport(local, scan));
327
+ const merged = locals.flatMap((result) => (result.merged ? [result.merged] : []));
328
+ const results = [...locals, ...scan.members.map((member) => renameMember(member, scan))];
329
+ return {
330
+ edits: [
331
+ ...scan.edits,
332
+ ...results.flatMap((result) => result.edits),
333
+ ...mergeEdits(merged, source),
334
+ ],
335
+ reports: results.flatMap((result) => result.reports),
336
+ };
337
+ }
@@ -0,0 +1,17 @@
1
+ export interface ManifestRewriteResult {
2
+ source: string;
3
+ changes: string[];
4
+ reports: string[];
5
+ notes: string[];
6
+ }
7
+ /**
8
+ * Replaces every retired package in a `package.json`'s dependency fields with
9
+ * the package that replaces it, at `range`, or at the workspace link when any
10
+ * entry it replaces was one. A replacement lands once per field, in place of
11
+ * the first retired entry it replaces; a replacement already in the field is
12
+ * kept when it allows that range. A dependency the retired
13
+ * package installed and its replacement only peers on is added to the same
14
+ * field when absent. Mentions outside the
15
+ * dependency fields (scripts, overrides) are reported, not rewritten.
16
+ */
17
+ export declare function rewriteManifest(source: string, range: string): ManifestRewriteResult;
@@ -0,0 +1,133 @@
1
+ import { DEV_ONLY_PACKAGES, isRecord, replacementPackage, retiredNamesIn } from './package-map.js';
2
+ const DEPENDENCY_FIELDS = [
3
+ 'dependencies',
4
+ 'devDependencies',
5
+ 'peerDependencies',
6
+ 'optionalDependencies',
7
+ ];
8
+ function dependenciesIn(manifest, field) {
9
+ const value = manifest[field];
10
+ if (!isRecord(value))
11
+ return undefined;
12
+ return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === 'string'));
13
+ }
14
+ function indentOf(source) {
15
+ return /^[{[]\r?\n([ \t]+)/.exec(source)?.[1] ?? ' ';
16
+ }
17
+ function targetField(field, replacement) {
18
+ return DEV_ONLY_PACKAGES.has(replacement) && field !== 'peerDependencies'
19
+ ? 'devDependencies'
20
+ : field;
21
+ }
22
+ function withReplacements(args) {
23
+ const entries = Object.entries(args.deps).flatMap(([name, range]) => {
24
+ const replacement = replacementPackage(name);
25
+ if (replacement === undefined)
26
+ return [[name, args.added.get(name) ?? range]];
27
+ const addedRange = args.added.get(replacement);
28
+ return addedRange === undefined ? [] : [[replacement, addedRange]];
29
+ });
30
+ const present = new Set(entries.map(([name]) => name));
31
+ const appended = [...args.added].filter(([name]) => !present.has(name));
32
+ return Object.fromEntries([...entries, ...appended]);
33
+ }
34
+ function isWorkspace(range) {
35
+ return range?.startsWith('workspace:') === true;
36
+ }
37
+ function lowerBound(range) {
38
+ const match = /^[\^~]?(\d+)\.(\d+)\.(\d+)$/.exec(range);
39
+ return match?.slice(1).map(Number);
40
+ }
41
+ function satisfies(existing, wanted) {
42
+ if (existing === wanted || isWorkspace(existing))
43
+ return true;
44
+ const have = lowerBound(existing);
45
+ const need = lowerBound(wanted);
46
+ if (have === undefined || need === undefined || isWorkspace(wanted))
47
+ return false;
48
+ const diff = have.map((part, index) => part - (need[index] ?? 0)).find((part) => part !== 0);
49
+ return diff === undefined || diff > 0;
50
+ }
51
+ function mergedRanges(manifest) {
52
+ const merged = new Map(DEPENDENCY_FIELDS.map((field) => [field, new Map()]));
53
+ DEPENDENCY_FIELDS.forEach((field) => {
54
+ Object.entries(dependenciesIn(manifest, field) ?? {}).forEach(([name, declared]) => {
55
+ const replacement = replacementPackage(name);
56
+ if (replacement === undefined)
57
+ return;
58
+ const ranges = merged.get(targetField(field, replacement));
59
+ ranges?.set(replacement, [...(ranges.get(replacement) ?? []), declared]);
60
+ });
61
+ });
62
+ return merged;
63
+ }
64
+ function planFields(manifest, range) {
65
+ const merged = mergedRanges(manifest);
66
+ return new Map(DEPENDENCY_FIELDS.map((field) => {
67
+ const deps = dependenciesIn(manifest, field) ?? {};
68
+ const removed = Object.keys(deps).filter((name) => replacementPackage(name) !== undefined);
69
+ const added = [...(merged.get(field) ?? [])].flatMap(([name, declared]) => {
70
+ const wanted = declared.find(isWorkspace) ?? range;
71
+ const existing = deps[name];
72
+ return existing !== undefined && satisfies(existing, wanted)
73
+ ? []
74
+ : [[name, wanted]];
75
+ });
76
+ return [field, { removed, added: new Map(added) }];
77
+ }));
78
+ }
79
+ function renamedPeerMeta(meta) {
80
+ if (!isRecord(meta))
81
+ return meta;
82
+ return Object.fromEntries(Object.entries(meta).map(([name, value]) => [replacementPackage(name) ?? name, value]));
83
+ }
84
+ function retiredMentions(manifest) {
85
+ return Object.entries(manifest)
86
+ .filter(([key]) => !DEPENDENCY_FIELDS.includes(key))
87
+ .filter(([key]) => key !== 'peerDependenciesMeta')
88
+ .flatMap(([key, value]) => retiredNamesIn(JSON.stringify(value)).map((name) => `"${key}" mentions ${name}`));
89
+ }
90
+ function moveNotes(manifest) {
91
+ return DEPENDENCY_FIELDS.flatMap((field) => Object.keys(dependenciesIn(manifest, field) ?? {}).flatMap((name) => {
92
+ const replacement = replacementPackage(name);
93
+ if (replacement === undefined)
94
+ return [];
95
+ const target = targetField(field, replacement);
96
+ return target === field
97
+ ? []
98
+ : [
99
+ `${replacement} goes to ${target}, not ${field}, because the test bench is for tests. Declare it in ${field} instead if your app runs the bench at runtime.`,
100
+ ];
101
+ }));
102
+ }
103
+ export function rewriteManifest(source, range) {
104
+ const parsed = JSON.parse(source);
105
+ if (!isRecord(parsed))
106
+ return { source, changes: [], reports: [], notes: [] };
107
+ const plans = planFields(parsed, range);
108
+ const changes = [...plans].flatMap(([field, plan]) => [
109
+ ...plan.removed.map((name) => `${field}: removed ${name}`),
110
+ ...[...plan.added].map(([name, added]) => dependenciesIn(parsed, field)?.[name] === undefined
111
+ ? `${field}: added ${name}@${added}`
112
+ : `${field}: updated ${name} to ${added}`),
113
+ ]);
114
+ if (changes.length === 0)
115
+ return { source, changes, reports: retiredMentions(parsed), notes: [] };
116
+ const rewritten = { ...parsed };
117
+ plans.forEach((plan, field) => {
118
+ const deps = dependenciesIn(parsed, field);
119
+ if (deps === undefined && plan.added.size === 0)
120
+ return;
121
+ rewritten[field] = withReplacements({ deps: deps ?? {}, added: plan.added });
122
+ });
123
+ if (parsed['peerDependenciesMeta'] !== undefined) {
124
+ rewritten['peerDependenciesMeta'] = renamedPeerMeta(parsed['peerDependenciesMeta']);
125
+ }
126
+ const trailing = source.endsWith('\n') ? '\n' : '';
127
+ return {
128
+ source: `${JSON.stringify(rewritten, null, indentOf(source))}${trailing}`,
129
+ changes,
130
+ reports: retiredMentions(parsed),
131
+ notes: moveNotes(parsed),
132
+ };
133
+ }
@@ -0,0 +1,27 @@
1
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
2
+ /** The package that replaces `name`, or `undefined` when `name` was not retired. */
3
+ export declare function replacementPackage(name: string): string | undefined;
4
+ /** The test bench installs as a development dependency, except where a package declares it as a peer. */
5
+ export declare const DEV_ONLY_PACKAGES: ReadonlySet<string>;
6
+ export type SpecifierMatch = {
7
+ kind: 'none';
8
+ } | {
9
+ kind: 'rewrite';
10
+ replacement: string;
11
+ } | {
12
+ kind: 'unknown-entry';
13
+ retired: string;
14
+ };
15
+ /**
16
+ * How the codemod treats a module specifier. Only the exact entry points the
17
+ * retired packages exported are rewritten; any other subpath of a retired
18
+ * package resolved nowhere and is reported instead.
19
+ */
20
+ export declare function matchSpecifier(specifier: string): SpecifierMatch;
21
+ /** Old export name to new, for one entry point. */
22
+ export type ExportRenames = Readonly<Record<string, string>>;
23
+ export declare function entryFor(specifier: string): string;
24
+ /** Renames for the entry point `specifier` names; the old and new specifier give the same result. */
25
+ export declare function exportRenamesFor(specifier: string): ExportRenames | undefined;
26
+ /** The retired package names `text` mentions, each as a whole package-name segment. */
27
+ export declare function retiredNamesIn(text: string): string[];
@@ -0,0 +1,99 @@
1
+ const PACKAGE_REPLACEMENTS = {
2
+ '@sanity/workflow-engine': '@sanity/workflows',
3
+ '@sanity/workflow-blueprint': '@sanity/workflows',
4
+ '@sanity/groq-condition-describe': '@sanity/workflows',
5
+ '@sanity/workflow-mcp': '@sanity/workflows',
6
+ '@sanity/workflow-studio-plugin': '@sanity/workflows-ui',
7
+ '@sanity/workflow-studio': '@sanity/workflows-ui',
8
+ '@sanity/workflow-react': '@sanity/workflows-ui',
9
+ '@sanity/workflow-sdk': '@sanity/workflows-ui',
10
+ '@sanity/workflow-components': '@sanity/workflows-ui',
11
+ '@sanity/workflow-diagram': '@sanity/workflows-ui',
12
+ '@sanity/workflow-cli': '@sanity/workflows-cli',
13
+ '@sanity/workflow-engine-test': '@sanity/workflows-test',
14
+ };
15
+ const ENTRY_REPLACEMENTS = {
16
+ '@sanity/workflow-engine': '@sanity/workflows',
17
+ '@sanity/workflow-engine/define': '@sanity/workflows/define',
18
+ '@sanity/workflow-engine/generate': '@sanity/workflows/generate',
19
+ '@sanity/workflow-engine/service': '@sanity/workflows/service',
20
+ '@sanity/workflow-blueprint': '@sanity/workflows/blueprints',
21
+ '@sanity/workflow-blueprint/generate': '@sanity/workflows/blueprints/generate',
22
+ '@sanity/workflow-blueprint/runtime': '@sanity/workflows/blueprints/runtime',
23
+ '@sanity/groq-condition-describe': '@sanity/workflows/describe',
24
+ '@sanity/workflow-mcp': '@sanity/workflows/agents',
25
+ '@sanity/workflow-studio-plugin': '@sanity/workflows-ui/studio-plugin',
26
+ '@sanity/workflow-studio': '@sanity/workflows-ui/studio',
27
+ '@sanity/workflow-react': '@sanity/workflows-ui/react',
28
+ '@sanity/workflow-react/observer': '@sanity/workflows-ui/react/observer',
29
+ '@sanity/workflow-sdk': '@sanity/workflows-ui/sdk',
30
+ '@sanity/workflow-sdk/observer': '@sanity/workflows-ui/sdk/observer',
31
+ '@sanity/workflow-sdk/project-users': '@sanity/workflows-ui/sdk/project-users',
32
+ '@sanity/workflow-components': '@sanity/workflows-ui/components',
33
+ '@sanity/workflow-diagram': '@sanity/workflows-ui/diagram',
34
+ '@sanity/workflow-cli': '@sanity/workflows-cli',
35
+ '@sanity/workflow-engine-test': '@sanity/workflows-test',
36
+ ...Object.fromEntries(Object.entries(PACKAGE_REPLACEMENTS).map(([retired, replacement]) => [
37
+ `${retired}/package.json`,
38
+ `${replacement}/package.json`,
39
+ ])),
40
+ };
41
+ export function isRecord(value) {
42
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
43
+ }
44
+ export function replacementPackage(name) {
45
+ return Object.hasOwn(PACKAGE_REPLACEMENTS, name)
46
+ ? PACKAGE_REPLACEMENTS[name]
47
+ : undefined;
48
+ }
49
+ export const DEV_ONLY_PACKAGES = new Set(['@sanity/workflows-test']);
50
+ function packageNameOf(specifier) {
51
+ const [first = '', second] = specifier.split('/');
52
+ return first.startsWith('@') && second !== undefined ? `${first}/${second}` : first;
53
+ }
54
+ export function matchSpecifier(specifier) {
55
+ const replacement = Object.hasOwn(ENTRY_REPLACEMENTS, specifier)
56
+ ? ENTRY_REPLACEMENTS[specifier]
57
+ : undefined;
58
+ if (replacement !== undefined)
59
+ return { kind: 'rewrite', replacement };
60
+ const name = packageNameOf(specifier);
61
+ return replacementPackage(name) === undefined
62
+ ? { kind: 'none' }
63
+ : { kind: 'unknown-entry', retired: name };
64
+ }
65
+ const EXPORT_RENAMES = {
66
+ '@sanity/workflows-ui/studio-plugin': {
67
+ workflowStudioPlugin: 'workflowsStudioPlugin',
68
+ workflowDefaultDocumentNode: 'workflowsDefaultDocumentNode',
69
+ WorkflowPluginConfig: 'WorkflowsPluginConfig',
70
+ LegacyTagWorkflowPluginConfig: 'LegacyTagWorkflowsPluginConfig',
71
+ },
72
+ '@sanity/workflows-ui/react': { useWorkflowTelemetry: 'useWorkflowsTelemetry' },
73
+ '@sanity/workflows-ui/components': {
74
+ ProjectMember: 'ResourceMember',
75
+ ProjectMembersState: 'ResourceMembersState',
76
+ projectMemberRow: 'resourceMemberRow',
77
+ findProjectMember: 'findResourceMember',
78
+ },
79
+ '@sanity/workflows-ui/sdk': {
80
+ useWorkflowTelemetry: 'useWorkflowsTelemetry',
81
+ WorkflowTelemetryProvider: 'WorkflowsTelemetryProvider',
82
+ },
83
+ };
84
+ export function entryFor(specifier) {
85
+ const match = matchSpecifier(specifier);
86
+ return match.kind === 'rewrite' ? match.replacement : specifier;
87
+ }
88
+ export function exportRenamesFor(specifier) {
89
+ const entry = entryFor(specifier);
90
+ return Object.hasOwn(EXPORT_RENAMES, entry) ? EXPORT_RENAMES[entry] : undefined;
91
+ }
92
+ const RETIRED_NAME_SOURCE = Object.keys(PACKAGE_REPLACEMENTS)
93
+ .sort((a, b) => b.length - a.length)
94
+ .map((name) => name.replaceAll('/', '\\/'))
95
+ .join('|');
96
+ export function retiredNamesIn(text) {
97
+ const pattern = new RegExp(`(?<![\\w@.-])(?:${RETIRED_NAME_SOURCE})(?![\\w-])`, 'g');
98
+ return [...new Set(text.match(pattern) ?? [])];
99
+ }
@@ -0,0 +1,4 @@
1
+ import type { CodemodRun } from './run.ts';
2
+ export declare function codemodLines(args: CodemodRun & {
3
+ dryRun: boolean;
4
+ }): string[];
@@ -0,0 +1,51 @@
1
+ import logSymbols from 'log-symbols';
2
+ import { CLI_PACKAGE_NAME } from '../../hooks/prerun/telemetry.js';
3
+ const REPORT_LABELS = {
4
+ 'unknown-entry': 'not an entry point of the retired package',
5
+ 'computed-specifier': 'built-up module specifier',
6
+ string: 'package name in a string',
7
+ 'jsx-text': 'package name in JSX text',
8
+ comment: 'package name in a comment',
9
+ 'parse-error': 'could not parse the file',
10
+ 'shadowed-rename': 'kept under its old local name because the file reuses it or the new name; works as is',
11
+ 'shadowed-namespace': 'not renamed because the file reuses the namespace name; rename it if it reads the import',
12
+ };
13
+ function rewriteLines(files, verb) {
14
+ const changed = files.filter((file) => file.rewrites.length > 0);
15
+ if (changed.length === 0)
16
+ return [`${logSymbols.info} No imports or dependencies name a retired package.`];
17
+ return [
18
+ `${logSymbols.success} ${verb} ${changed.length} files:`,
19
+ ...changed.map((file) => ` ${file.path}: ${file.rewrites.join(', ')}`),
20
+ ];
21
+ }
22
+ function noteLines(files) {
23
+ const notes = files.flatMap((file) => file.notes.map((note) => `${logSymbols.info} ${file.path}: ${note}`));
24
+ return notes.length === 0 ? [] : ['', ...notes];
25
+ }
26
+ function reportLines(files) {
27
+ const reports = files.flatMap((file) => file.reports.map((report) => ` ${file.path}:${report.line} ${REPORT_LABELS[report.kind]}: ${report.text}`));
28
+ if (reports.length === 0)
29
+ return [];
30
+ return ['', `${logSymbols.warning} Fix these by hand:`, ...reports];
31
+ }
32
+ export function codemodLines(args) {
33
+ const generated = args.generated.length === 0
34
+ ? []
35
+ : ['', `${logSymbols.info} Skipped ${args.generated.length} generated blueprint files.`];
36
+ const next = args.dryRun
37
+ ? []
38
+ : [
39
+ '',
40
+ 'Next: install dependencies, then run',
41
+ ` npx ${CLI_PACKAGE_NAME} blueprints generate (if the project has a generated blueprint)`,
42
+ 'and typecheck.',
43
+ ];
44
+ return [
45
+ ...rewriteLines(args.files, args.dryRun ? 'Would rewrite' : 'Rewrote'),
46
+ ...noteLines(args.files),
47
+ ...reportLines(args.files),
48
+ ...generated,
49
+ ...next,
50
+ ];
51
+ }