@astryxdesign/cli 0.7.5-canary.ae842c8 → 0.7.5-canary.b58ca63

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 (74) hide show
  1. package/README.md +2 -2
  2. package/api/component/_adapter.mjs +16 -1
  3. package/api/component/component.test.mjs +31 -0
  4. package/api/docs/_adapter.d.mts +5 -2
  5. package/api/docs/_adapter.mjs +24 -16
  6. package/api/docs/docs.test.mjs +18 -0
  7. package/api/docs/integrationDocs.test.mjs +2 -2
  8. package/api/integration/pack-check.lifecycle-output.test.mjs +24 -1
  9. package/api/integration/pack-check.mjs +10 -4
  10. package/api/integration/pack-check.test.mjs +5 -1
  11. package/api/search/_adapter.test.mjs +21 -2
  12. package/api/template/template.mjs +3 -1
  13. package/api/template/unknown-name.test.mjs +63 -0
  14. package/api/upgrade/_adapter.d.mts +47 -2
  15. package/api/upgrade/_adapter.mjs +58 -2
  16. package/api/upgrade/run/failure-receipt.test.mjs +212 -0
  17. package/api/upgrade/run/outcomes.test.mjs +646 -0
  18. package/api/upgrade/run/run.d.mts +36 -0
  19. package/api/upgrade/run/run.mjs +670 -44
  20. package/api/upgrade/upgrade.doc.mjs +4 -4
  21. package/api/upgrade/upgrade.mjs +19 -4
  22. package/api/upgrade/upgrade.type.d.mts +36 -1
  23. package/api/upgrade/upgrade.type.mjs +29 -4
  24. package/assets/codemods/__tests__/runner.symlinks.test.mjs +205 -0
  25. package/assets/codemods/__tests__/runner.unparseable.test.mjs +457 -0
  26. package/assets/codemods/integration-runner.mjs +8 -2
  27. package/assets/codemods/integration-runner.protection.test.mjs +53 -0
  28. package/assets/codemods/outcome.mjs +79 -0
  29. package/assets/codemods/outcome.test.mjs +40 -0
  30. package/assets/codemods/parse.mjs +94 -0
  31. package/assets/codemods/run-codemod.mjs +109 -5
  32. package/assets/codemods/runner.mjs +119 -13
  33. package/assets/codemods/transform-prop.mjs +99 -49
  34. package/assets/codemods/transform-prop.test.mjs +36 -1
  35. package/assets/codemods/transforms/v0.1.3/__tests__/migrate-layout-components-to-experimental.test.mjs +216 -2
  36. package/assets/codemods/transforms/v0.1.3/migrate-layout-components-to-experimental.mjs +88 -6
  37. package/assets/codemods/transforms/v0.6.0/migrate-astryx-theme-selectors-to-data-attrs.mjs +4 -3
  38. package/assets/codemods/transforms/v0.7.0/__tests__/migrate-deprecated-theme-surface.test.mjs +207 -6
  39. package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +143 -70
  40. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface-v0.6-data.mjs +6 -1
  41. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +244 -42
  42. package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +23 -78
  43. package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
  44. package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.doc.mjs +1 -1
  45. package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.tsx +10 -2
  46. package/assets/templates/pages/library/page.tsx +1 -2
  47. package/authoring/codemod/type.ts +10 -0
  48. package/clients/cli/commands/upgrade.doc.mjs +47 -2
  49. package/clients/cli/commands/upgrade.file-protection.test.mjs +175 -0
  50. package/clients/cli/commands/upgrade.flag-help.test.mjs +37 -0
  51. package/clients/cli/commands/upgrade.integration-policy.test.mjs +103 -0
  52. package/clients/cli/commands/upgrade.mjs +9 -2
  53. package/clients/cli/commands/upgrade.outcomes.test.mjs +350 -0
  54. package/foundation/doc-compiler/doc-loads.test.mjs +5 -5
  55. package/foundation/doc-compiler/import.mjs +81 -8
  56. package/foundation/doc-compiler/read.d.mts +4 -3
  57. package/foundation/doc-compiler/read.mjs +15 -10
  58. package/foundation/doc-compiler/read.test.mjs +59 -1
  59. package/foundation/fs/module-loader.d.mts +3 -2
  60. package/foundation/fs/module-loader.mjs +3 -2
  61. package/foundation/fs/publish-file-eperm-mutation.test.mjs +2 -0
  62. package/foundation/fs/read-record.d.mts +27 -0
  63. package/foundation/fs/read-record.mjs +83 -15
  64. package/foundation/fs/read-record.test.mjs +88 -1
  65. package/foundation/response/base.d.ts +7 -0
  66. package/foundation/response/error-codes.doc.mjs +3 -2
  67. package/foundation/response/error-codes.mjs +1 -1
  68. package/foundation/response/json.d.mts +10 -3
  69. package/foundation/response/json.mjs +16 -4
  70. package/foundation/response/response-types.doc.mjs +1 -1
  71. package/foundation/text/suggest-order.d.mts +17 -3
  72. package/foundation/text/suggest-order.mjs +58 -13
  73. package/foundation/text/suggest-order.test.mjs +53 -0
  74. package/package.json +10 -9
@@ -24,6 +24,7 @@ import {
24
24
  latestVersion,
25
25
  } from '../../assets/codemods/registry.mjs';
26
26
  import {runCodemods} from '../../assets/codemods/runner.mjs';
27
+ import {codemodOutcome} from '../../assets/codemods/outcome.mjs';
27
28
  import {
28
29
  discoverIntegrationCodemods,
29
30
  selectIntegrationCodemods,
@@ -141,7 +142,12 @@ export function relativeToOrigin(result, {cwd: root, origin}) {
141
142
  };
142
143
  }
143
144
  if (data.modifiedFiles) data.modifiedFiles = data.modifiedFiles.map(rebase);
144
- for (const key of ['protectedFiles', 'declinedCandidates', 'errors']) {
145
+ for (const key of [
146
+ 'protectedFiles',
147
+ 'declinedCandidates',
148
+ 'errors',
149
+ 'regenerate',
150
+ ]) {
145
151
  if (data[key]) {
146
152
  data[key] = data[key].map(
147
153
  (/** @type {{file: string, declarations?: string[]}} */ entry) => ({
@@ -599,11 +605,59 @@ export async function ensureCodemodDeps({installDeps} = {}) {
599
605
  return ensureJscodeshift({installDeps, silent: logger.silent});
600
606
  }
601
607
 
608
+ /**
609
+ * @typedef {{kind: 'manual', file: string, codemod: string, message: string, line?: number} | {kind: 'regenerate', file: string, codemod: string, message: string, commands: string[]}} CodemodOutcomeRecord
610
+ */
611
+
612
+ /**
613
+ * Wrap each core code and config transform so the non-failure outcomes it
614
+ * throws (`assets/codemods/outcome.mjs`) are recorded instead of reaching the
615
+ * runner as errors. The wrapped transform leaves that file unchanged, exactly
616
+ * as the throw did. Project codemods keep their own contract. One wrapper per
617
+ * pass: the regeneration re-check takes a fresh one, so an outcome a hook
618
+ * resolved does not linger.
619
+ *
620
+ * @template {{version: string, transforms: Array<{name: string, transform: any, meta?: {codemodType?: string}}>}} M
621
+ * @param {M[]} versionManifests
622
+ * @param {string} root project root; recorded files are relative to it
623
+ * @returns {{versionManifests: M[], outcomes: CodemodOutcomeRecord[]}}
624
+ */
625
+ export function withCodemodOutcomes(versionManifests, root) {
626
+ /** @type {CodemodOutcomeRecord[]} */
627
+ const outcomes = [];
628
+ const wrapped = versionManifests.map(manifest => ({
629
+ ...manifest,
630
+ transforms: manifest.transforms.map(entry => {
631
+ if (entry.meta?.codemodType === 'project') return entry;
632
+ const {transform, name} = entry;
633
+ return {
634
+ ...entry,
635
+ /** @param {{path: string, source: string}} file @param {unknown} api */
636
+ transform(file, api) {
637
+ try {
638
+ return transform(file, api);
639
+ } catch (err) {
640
+ const outcome = codemodOutcome(err);
641
+ if (!outcome) throw err;
642
+ outcomes.push({
643
+ ...outcome,
644
+ file: path.relative(root, file.path).split(path.sep).join('/'),
645
+ codemod: `core:${name}`,
646
+ });
647
+ return undefined;
648
+ }
649
+ },
650
+ };
651
+ }),
652
+ }));
653
+ return {versionManifests: /** @type {M[]} */ (wrapped), outcomes};
654
+ }
655
+
602
656
  /**
603
657
  * Run the CORE registry codemods. Runs BEFORE the config is loaded so a core
604
658
  * CONFIG codemod can repair a config the strict loader would otherwise reject.
605
659
  * @param {CoreVersionManifest[]} versionManifests
606
- * @param {{apply: boolean, path: string, codemod?: string, skipCodemods: Set<string>, root?: string, protection?: {root: string, classify: (file: string) => import('../../foundation/fs/file-protection.mjs').FileProtection[]}, silent?: boolean, coversTarget?: boolean}} options
660
+ * @param {{apply: boolean, path: string, codemod?: string, skipCodemods: Set<string>, root?: string, protection?: {root: string, classify: (file: string) => import('../../foundation/fs/file-protection.mjs').FileProtection[]}, silent?: boolean, coversTarget?: boolean, outcomes?: CodemodOutcomeRecord[]}} options
607
661
  */
608
662
  export async function runCoreCodemods(
609
663
  versionManifests,
@@ -616,6 +670,7 @@ export async function runCoreCodemods(
616
670
  protection,
617
671
  silent,
618
672
  coversTarget,
673
+ outcomes,
619
674
  },
620
675
  ) {
621
676
  return runCodemods(versionManifests, {
@@ -627,6 +682,7 @@ export async function runCoreCodemods(
627
682
  root,
628
683
  protection,
629
684
  coversTarget,
685
+ outcomes,
630
686
  });
631
687
  }
632
688
 
@@ -0,0 +1,212 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file A failed upgrade keeps its error contract and adds the receipt: the
5
+ * thrown AstryxError still carries ERR_CODEMOD_FAILED, and its `receipt`
6
+ * names the files the run already changed, from the folder the command ran
7
+ * in. Core codemods come from a stubbed registry so the contract is tested
8
+ * apart from any one shipped codemod.
9
+ */
10
+
11
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
12
+ import * as fs from 'node:fs';
13
+ import * as os from 'node:os';
14
+ import * as path from 'node:path';
15
+
16
+ vi.mock('../../../assets/codemods/registry.mjs', async importOriginal => {
17
+ const actual = /** @type {Record<string, unknown>} */ (
18
+ await importOriginal()
19
+ );
20
+ return {
21
+ ...actual,
22
+ getTransformsBetween: async () => [
23
+ {
24
+ version: '0.2.0',
25
+ transforms: [
26
+ {
27
+ name: 'rename-foo',
28
+ meta: {title: 'Rename foo', fileExtensions: ['.ts']},
29
+ /** @param {{source: string}} file */
30
+ transform: file => file.source.replace(/foo/g, 'bar'),
31
+ },
32
+ {
33
+ name: 'parse-js',
34
+ meta: {title: 'Parse JavaScript', fileExtensions: ['.js']},
35
+ /** @param {{source: string}} file @param {{jscodeshift: (source: string) => unknown}} api */
36
+ transform(file, api) {
37
+ api.jscodeshift(file.source);
38
+ return undefined;
39
+ },
40
+ },
41
+ {
42
+ name: 'fail-on-boom',
43
+ meta: {title: 'Fail on boom', fileExtensions: ['.ts']},
44
+ /** @param {{source: string}} file */
45
+ transform(file) {
46
+ if (file.source.includes('boom')) throw new Error('codemod boom');
47
+ return undefined;
48
+ },
49
+ },
50
+ ],
51
+ },
52
+ ],
53
+ };
54
+ });
55
+
56
+ const {upgrade} = await import('../upgrade.mjs');
57
+ const {AstryxError} = await import('../../error.mjs');
58
+ const {toErrorEnvelope} = await import('../../../foundation/response/json.mjs');
59
+ const {isError} = await import('../../json/parse.mjs');
60
+
61
+ let dir;
62
+
63
+ /** @param {string} rel @param {string} body */
64
+ function write(rel, body) {
65
+ fs.mkdirSync(path.dirname(path.join(dir, rel)), {recursive: true});
66
+ fs.writeFileSync(path.join(dir, rel), body);
67
+ }
68
+
69
+ beforeEach(() => {
70
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upg-failure-receipt-'));
71
+ write(
72
+ 'package.json',
73
+ JSON.stringify({name: 'x', dependencies: {'@astryxdesign/core': '0.2.0'}}),
74
+ );
75
+ write(
76
+ 'node_modules/@astryxdesign/core/package.json',
77
+ JSON.stringify({name: '@astryxdesign/core', version: '0.2.0'}),
78
+ );
79
+ write('src/index.ts', 'const foo = 1;\n');
80
+ write('src/broken.ts', 'const boom = 1;\n');
81
+ });
82
+
83
+ afterEach(() => fs.rmSync(dir, {recursive: true, force: true}));
84
+
85
+ /** @param {Promise<unknown>} promise */
86
+ async function rejection(promise) {
87
+ try {
88
+ await promise;
89
+ } catch (err) {
90
+ return /** @type {any} */ (err);
91
+ }
92
+ throw new Error('expected upgrade() to reject');
93
+ }
94
+
95
+ describe('a failed upgrade', () => {
96
+ it('still rejects with ERR_CODEMOD_FAILED, and the error carries the receipt', async () => {
97
+ const err = await rejection(
98
+ upgrade({from: '0.1.0', apply: true, path: 'src'}, {cwd: dir}),
99
+ );
100
+
101
+ expect(err).toBeInstanceOf(AstryxError);
102
+ expect(err.code).toBe('ERR_CODEMOD_FAILED');
103
+ expect(err.message).toBe('Upgrade completed with 1 codemod error.');
104
+ expect(err.receipt).toMatchObject({
105
+ complete: false,
106
+ errorCode: 'ERR_CODEMOD_FAILED',
107
+ filesChanged: 1,
108
+ modifiedFiles: ['src/index.ts'],
109
+ errors: [
110
+ {
111
+ file: 'src/broken.ts',
112
+ codemod: 'core:fail-on-boom',
113
+ error: 'codemod boom',
114
+ },
115
+ ],
116
+ });
117
+ expect(fs.readFileSync(path.join(dir, 'src/index.ts'), 'utf8')).toBe(
118
+ 'const bar = 1;\n',
119
+ );
120
+ });
121
+
122
+ it('lists a protected file a codemod could not parse in the receipt', async () => {
123
+ write('.gitattributes', 'src/vendor/** linguist-vendored\n');
124
+ write('src/vendor/bundle.js', 'export const x = {;\n');
125
+
126
+ const err = await rejection(
127
+ upgrade({from: '0.1.0', apply: true, path: 'src'}, {cwd: dir}),
128
+ );
129
+
130
+ expect(err.code).toBe('ERR_CODEMOD_FAILED');
131
+ expect(err.receipt.errors).toEqual([
132
+ expect.objectContaining({file: 'src/broken.ts'}),
133
+ ]);
134
+ expect(err.receipt.declinedCandidates).toEqual([
135
+ {
136
+ file: 'src/vendor/bundle.js',
137
+ location: '1:19',
138
+ reason: expect.stringMatching(
139
+ /^unparseable protected file \(vendored\): /,
140
+ ),
141
+ },
142
+ ]);
143
+ });
144
+
145
+ it('names a declined file from the folder the command ran in, with no root-relative path in its reason', async () => {
146
+ write('.gitattributes', 'src/vendor/** linguist-vendored\n');
147
+ write('src/vendor/bundle.js', 'export const x = {;\n');
148
+
149
+ const err = await rejection(
150
+ upgrade(
151
+ {from: '0.1.0', apply: true, path: '.'},
152
+ {cwd: path.join(dir, 'src')},
153
+ ),
154
+ );
155
+
156
+ expect(err.receipt.declinedCandidates).toEqual([
157
+ {
158
+ file: 'vendor/bundle.js',
159
+ location: '1:19',
160
+ reason: expect.stringMatching(
161
+ /^unparseable protected file \(vendored\): /,
162
+ ),
163
+ },
164
+ ]);
165
+ expect(err.receipt.declinedCandidates[0].reason).not.toContain(
166
+ '.gitattributes',
167
+ );
168
+ });
169
+
170
+ it('names receipt files from the folder the command ran in', async () => {
171
+ const err = await rejection(
172
+ upgrade(
173
+ {from: '0.1.0', apply: true, path: '.'},
174
+ {cwd: path.join(dir, 'src')},
175
+ ),
176
+ );
177
+
178
+ expect(err.code).toBe('ERR_CODEMOD_FAILED');
179
+ expect(err.receipt.modifiedFiles).toEqual(['index.ts']);
180
+ expect(err.receipt.errors).toEqual([
181
+ expect.objectContaining({file: 'broken.ts'}),
182
+ ]);
183
+ });
184
+
185
+ it('becomes an error envelope that keeps its code and adds the receipt', async () => {
186
+ const err = await rejection(
187
+ upgrade({from: '0.1.0', path: 'src'}, {cwd: dir}),
188
+ );
189
+
190
+ const envelope = toErrorEnvelope(err);
191
+ expect(isError(envelope)).toBe(true);
192
+ expect(envelope).toMatchObject({
193
+ apiVersion: 1,
194
+ code: 'ERR_CODEMOD_FAILED',
195
+ error: 'Upgrade completed with 1 codemod error.',
196
+ receipt: {modifiedFiles: ['src/index.ts'], complete: false},
197
+ });
198
+ expect(fs.readFileSync(path.join(dir, 'src/index.ts'), 'utf8')).toBe(
199
+ 'const foo = 1;\n',
200
+ );
201
+ });
202
+ });
203
+
204
+ describe('the error envelope', () => {
205
+ it('has no receipt for an error that carries none', () => {
206
+ expect(
207
+ toErrorEnvelope(
208
+ new AstryxError('nope', undefined, 'ERR_INVALID_ARGUMENT'),
209
+ ),
210
+ ).toEqual({apiVersion: 1, error: 'nope', code: 'ERR_INVALID_ARGUMENT'});
211
+ });
212
+ });