@astryxdesign/cli 0.6.3-canary.7210f0d → 0.6.3-canary.7ada42e

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 (40) hide show
  1. package/README.md +2 -0
  2. package/api/upgrade/_adapter.d.mts +22 -4
  3. package/api/upgrade/_adapter.mjs +19 -6
  4. package/api/upgrade/run/run.mjs +355 -59
  5. package/api/upgrade/upgrade.doc.mjs +5 -1
  6. package/api/upgrade/upgrade.type.d.mts +34 -0
  7. package/api/upgrade/upgrade.type.mjs +15 -0
  8. package/assets/codemods/__tests__/runner.test.mjs +330 -8
  9. package/assets/codemods/integration-runner.mjs +56 -4
  10. package/assets/codemods/integration-runner.protection.test.mjs +153 -0
  11. package/assets/codemods/run-codemod.mjs +177 -34
  12. package/assets/codemods/runner.mjs +248 -110
  13. package/assets/codemods/transforms/next/__tests__/migrate-native-picker-to-presentation.test.mjs +63 -0
  14. package/assets/codemods/transforms/next/__tests__/migrate-theme-catalog-to-descriptors.test.mjs +4 -0
  15. package/assets/codemods/transforms/next/index.mjs +8 -0
  16. package/assets/codemods/transforms/next/migrate-native-picker-to-presentation.mjs +148 -0
  17. package/assets/docs/cli-integrations.doc.mjs +13 -0
  18. package/assets/templates/blocks/components/DateInput/DateInputDateRange.tsx +1 -1
  19. package/assets/templates/blocks/components/Item/ItemDocumentTabs.doc.mjs +14 -0
  20. package/assets/templates/blocks/components/Item/ItemDocumentTabs.tsx +100 -0
  21. package/assets/templates/blocks/components/TimeInput/TimeInputConstrained.tsx +1 -0
  22. package/authoring/codemod/type.ts +12 -0
  23. package/clients/cli/commands/build-theme.mjs +10 -44
  24. package/clients/cli/commands/docs.mjs +1 -1
  25. package/clients/cli/commands/text-json-parity.test.mjs +719 -0
  26. package/clients/cli/commands/theme-targets.behavior.test.mjs +4 -3
  27. package/clients/cli/commands/upgrade.doc.mjs +2 -1
  28. package/clients/cli/commands/upgrade.file-protection.test.mjs +228 -0
  29. package/clients/cli/commands/upgrade.mjs +29 -7
  30. package/clients/cli/formatters/index.mjs +2 -0
  31. package/clients/cli/formatters/index.test.mjs +6 -0
  32. package/foundation/fs/file-protection.d.mts +33 -0
  33. package/foundation/fs/file-protection.mjs +825 -0
  34. package/foundation/fs/file-protection.test.mjs +250 -0
  35. package/foundation/response/error-codes.d.mts +3 -1
  36. package/foundation/response/error-codes.d.ts +2 -0
  37. package/foundation/response/error-codes.doc.mjs +10 -0
  38. package/foundation/response/error-codes.mjs +6 -0
  39. package/foundation/response/error-codes.test.mjs +55 -11
  40. package/package.json +10 -9
@@ -17,8 +17,9 @@ describe('astryx theme targets', () => {
17
17
  const {status, stdout} = await runCli(['theme', 'targets', 'Switch']);
18
18
 
19
19
  expect(status).toBe(0);
20
- expect(stdout).toMatch(/^switch\s+Switch\s+size\s+checked, disabled$/m);
21
- expect(stdout).toMatch(/^switch-thumb\s+Switch\s+size\s+checked$/m);
20
+ // Inline layout: key component - props - states - className
21
+ expect(stdout).toMatch(/^switch\s+Switch - size - checked, disabled - astryx-switch$/m);
22
+ expect(stdout).toMatch(/^switch-thumb\s+Switch - size - checked - astryx-switch-thumb$/m);
22
23
  expect(stdout).toMatch(/4 across 1 component/);
23
24
  });
24
25
 
@@ -28,7 +29,7 @@ describe('astryx theme targets', () => {
28
29
  expect(status).toBe(0);
29
30
  expect(stdout).toMatch(/^popover\s{2,}Popover/m);
30
31
  expect(stdout).toMatch(
31
- /^popover-surface \[deprecated; use popover\]\s{2,}Popover/m,
32
+ /^popover-surface\s{2,}Popover - astryx-popover-surface - deprecated; use popover$/m,
32
33
  );
33
34
  });
34
35
 
@@ -101,13 +101,14 @@ export const doc = {
101
101
  {label: 'Apply a migration', cli: 'astryx upgrade --from 0.1.0 --apply'},
102
102
  ],
103
103
  exitCodes: [
104
- {code: 0, when: 'success (including dry-run previews)'},
104
+ {code: 0, when: 'success, including complete dry-run previews'},
105
105
  {
106
106
  code: 1,
107
107
  when:
108
108
  'missing or invalid --from, --registry with --list or a migration flag, a --path escape, ' +
109
109
  'no installed @astryxdesign/core (or legacy @xds/core), jscodeshift missing and not installed by --install-deps, ' +
110
110
  'an astryx.config that fails validation and that no pending config codemod repairs, ' +
111
+ 'an unreadable or invalid protection declaration, a protected file that still requires a codemod change, ' +
111
112
  'an unknown codemod, a codemod or post-codemod hook failure, or unresolved registry items',
112
113
  },
113
114
  ],
@@ -0,0 +1,228 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file End-to-end protection and regeneration behavior for `astryx upgrade`.
5
+ */
6
+
7
+ import {afterEach, beforeEach, describe, expect, it} from 'vitest';
8
+ import {spawnSync} from 'node:child_process';
9
+ import * as fs from 'node:fs';
10
+ import * as os from 'node:os';
11
+ import * as path from 'node:path';
12
+ import {fileURLToPath} from 'node:url';
13
+
14
+ const CLI = path.resolve(
15
+ path.dirname(fileURLToPath(import.meta.url)),
16
+ '..',
17
+ 'bin',
18
+ 'astryx.mjs',
19
+ );
20
+ let root;
21
+
22
+ function write(relative, contents) {
23
+ const file = path.join(root, relative);
24
+ fs.mkdirSync(path.dirname(file), {recursive: true});
25
+ fs.writeFileSync(file, contents);
26
+ return file;
27
+ }
28
+
29
+ function run(args) {
30
+ const result = spawnSync(
31
+ process.execPath,
32
+ [CLI, '--json', 'upgrade', ...args],
33
+ {
34
+ cwd: root,
35
+ encoding: 'utf8',
36
+ timeout: 60_000,
37
+ env: {...process.env, FORCE_COLOR: '0', CI: ''},
38
+ },
39
+ );
40
+ return {...result, envelope: JSON.parse(result.stdout)};
41
+ }
42
+
43
+ function seed({hook = false} = {}) {
44
+ write('package.json', JSON.stringify({name: 'consumer', private: true}));
45
+ write(
46
+ 'node_modules/@astryxdesign/core/package.json',
47
+ JSON.stringify({name: '@astryxdesign/core', version: '0.6.0'}),
48
+ );
49
+ write('.gitattributes', 'generated/** linguist-generated=true\n');
50
+ const source =
51
+ "import {useResizable} from '@astryxdesign/core';\n" +
52
+ 'export const usePanel = () => useResizable({minSizePx: 200});\n';
53
+ write('src/owned.tsx', source);
54
+ write('generated/panel.tsx', `// @generated\n${source}`);
55
+ write(
56
+ 'astryx.config.mjs',
57
+ hook
58
+ ? `export default {hooks: {postCodemod: [{name: 'regenerate', buildCommand: () => ({command: process.execPath, args: ['-e', ${JSON.stringify("const fs=require('node:fs');const p='generated/panel.tsx';const s=fs.readFileSync(p,'utf8');fs.writeFileSync(p,s.replaceAll('minSizePx','minSize')); ")} ]})}]}};\n`
59
+ : 'export default {};\n',
60
+ );
61
+ }
62
+
63
+ beforeEach(() => {
64
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-protected-upgrade-'));
65
+ });
66
+
67
+ afterEach(() => {
68
+ fs.rmSync(root, {recursive: true, force: true});
69
+ });
70
+
71
+ const ARGS = [
72
+ '--from',
73
+ '0.5.0',
74
+ '--codemod',
75
+ 'rename-resizable-pixel-bounds',
76
+ '--path',
77
+ '.',
78
+ ];
79
+
80
+ describe('upgrade protected-file contract', () => {
81
+ it('reports the same protected candidate in dry-run and apply while retaining owned edits', () => {
82
+ seed();
83
+
84
+ const dry = run(ARGS);
85
+ expect(dry.status).toBe(1);
86
+ expect(dry.envelope).toMatchObject({
87
+ type: 'upgrade.run',
88
+ data: {
89
+ complete: false,
90
+ errorCode: 'ERR_CODEMOD_PROTECTED',
91
+ modifiedFiles: ['src/owned.tsx'],
92
+ protectedFiles: [
93
+ {
94
+ file: 'generated/panel.tsx',
95
+ reasons: ['generated'],
96
+ declarations: expect.arrayContaining([
97
+ 'linguist-generated in .gitattributes:1',
98
+ '@generated in the leading comment block',
99
+ ]),
100
+ },
101
+ ],
102
+ },
103
+ });
104
+ expect(fs.readFileSync(path.join(root, 'src/owned.tsx'), 'utf8')).toContain(
105
+ 'minSizePx',
106
+ );
107
+
108
+ const applied = run([...ARGS, '--apply']);
109
+ expect(applied.status).toBe(1);
110
+ expect(applied.envelope.data.protectedFiles).toEqual(
111
+ dry.envelope.data.protectedFiles,
112
+ );
113
+ expect(fs.readFileSync(path.join(root, 'src/owned.tsx'), 'utf8')).toContain(
114
+ 'minSize: 200',
115
+ );
116
+ expect(
117
+ fs.readFileSync(path.join(root, 'generated/panel.tsx'), 'utf8'),
118
+ ).toContain('minSizePx');
119
+ });
120
+
121
+ it('preserves a protected config migration when strict config loading fails', () => {
122
+ write('package.json', JSON.stringify({name: 'consumer', private: true}));
123
+ write(
124
+ 'node_modules/@astryxdesign/core/package.json',
125
+ JSON.stringify({name: '@astryxdesign/core', version: '0.1.3'}),
126
+ );
127
+ write(
128
+ 'astryx.config.mjs',
129
+ `// @generated\nexport default {layout: {components: {KpiCard: '@/KpiCard'}}};\n`,
130
+ );
131
+ write('src/index.ts', 'export {};\n');
132
+
133
+ const result = run([
134
+ '--from',
135
+ '0.1.2',
136
+ '--codemod',
137
+ 'migrate-layout-components-to-experimental',
138
+ '--path',
139
+ 'src',
140
+ '--apply',
141
+ ]);
142
+
143
+ expect(result.status).toBe(1);
144
+ expect(result.envelope).toMatchObject({
145
+ type: 'upgrade.run',
146
+ data: {
147
+ complete: false,
148
+ errorCode: 'ERR_CODEMOD_PROTECTED',
149
+ protectedFiles: [expect.objectContaining({file: 'astryx.config.mjs'})],
150
+ },
151
+ });
152
+ expect(
153
+ fs.readFileSync(path.join(root, 'astryx.config.mjs'), 'utf8'),
154
+ ).toContain('layout');
155
+ });
156
+
157
+ it('fails before writes when a protection declaration cannot be parsed', () => {
158
+ seed();
159
+ write('.hgignore', 'syntax: regexp\n[bad\n');
160
+
161
+ const result = run([...ARGS, '--apply']);
162
+
163
+ expect(result.status).toBe(1);
164
+ expect(result.envelope).toMatchObject({
165
+ code: 'ERR_CODEMOD_PROTECTION_SOURCE',
166
+ });
167
+ expect(result.envelope.error).toContain('.hgignore');
168
+ expect(fs.readFileSync(path.join(root, 'src/owned.tsx'), 'utf8')).toContain(
169
+ 'minSizePx',
170
+ );
171
+ });
172
+
173
+ it('preserves protected-file facts when regeneration fails', () => {
174
+ seed();
175
+ write(
176
+ 'astryx.config.mjs',
177
+ `export default {hooks: {postCodemod: [{name: 'regenerate', buildCommand: () => ({command: process.execPath, args: ['-e', 'process.exit(7)']})}]}};\n`,
178
+ );
179
+
180
+ const applied = run([...ARGS, '--apply']);
181
+
182
+ expect(applied.status).toBe(1);
183
+ expect(applied.envelope).toMatchObject({
184
+ type: 'upgrade.run',
185
+ data: {
186
+ complete: false,
187
+ errorCode: 'ERR_CODEMOD_PROTECTED',
188
+ protectedFiles: [
189
+ expect.objectContaining({file: 'generated/panel.tsx'}),
190
+ ],
191
+ errors: [
192
+ expect.objectContaining({
193
+ codemod: 'post-codemod-hook',
194
+ error: expect.stringContaining('Post-codemod hook failed'),
195
+ }),
196
+ ],
197
+ },
198
+ });
199
+ });
200
+
201
+ it('runs declared regeneration and clears a generated-file block after reevaluation', () => {
202
+ seed({hook: true});
203
+
204
+ const applied = run([...ARGS, '--apply']);
205
+
206
+ expect(applied.status, applied.stderr).toBe(0);
207
+ expect(applied.envelope).toMatchObject({
208
+ type: 'upgrade.run',
209
+ data: {
210
+ complete: true,
211
+ modifiedFiles: ['src/owned.tsx', 'generated/panel.tsx'],
212
+ protectedFiles: [],
213
+ declinedCandidates: [],
214
+ },
215
+ });
216
+ expect(fs.readFileSync(path.join(root, 'src/owned.tsx'), 'utf8')).toContain(
217
+ 'minSize: 200',
218
+ );
219
+ expect(
220
+ fs.readFileSync(path.join(root, 'generated/panel.tsx'), 'utf8'),
221
+ ).toContain('minSize: 200');
222
+
223
+ const second = run([...ARGS, '--apply']);
224
+ expect(second.status, second.stderr).toBe(0);
225
+ expect(second.envelope.data.complete).toBe(true);
226
+ expect(second.envelope.data.filesChanged).toBe(0);
227
+ });
228
+ });
@@ -13,6 +13,7 @@
13
13
  */
14
14
 
15
15
  import {jsonOut, jsonError} from '../../../foundation/response/json.mjs';
16
+ import {emit, section, records} from '../formatters/index.mjs';
16
17
  import {logger} from '../../../api/logger.mjs';
17
18
  import {AstryxError} from '../../../api/error.mjs';
18
19
  import {defineCommand} from '../lib/define-command.mjs';
@@ -33,27 +34,45 @@ export function registerUpgrade(program) {
33
34
  */
34
35
  async options => {
35
36
  const json = program.opts().json || false;
36
- logger.setSilent(json);
37
+ const isList = Boolean(options.list);
38
+ // Silence the logger for --list so the handler renders from the
39
+ // result (parity with --json), and for --json as before.
40
+ // Re-enable on error so handled failures still print.
41
+ logger.setSilent(json || isList);
37
42
 
38
43
  /** @type {import('../../../api/upgrade/upgrade.type.mjs').UpgradeListResponse | import('../../../api/upgrade/upgrade.type.mjs').UpgradeRegistryResponse | import('../../../api/upgrade/upgrade.type.mjs').UpgradeStatusResponse | import('../../../api/upgrade/upgrade.type.mjs').UpgradeRunResponse} */
39
44
  let result;
40
45
  try {
41
46
  result = await upgradeApi(options, {cwd: process.cwd()});
42
47
  } catch (e) {
43
- // Handled failures throw AstryxError: --json emits the structured
44
- // envelope (byte-identical to the old inline jsonError) + exits 1;
45
- // human mode already saw the error line + outro via the logger, so we
46
- // just set the exit code. Anything else is unexpected — let the
47
- // top-level boundary in bin/astryx.mjs format it as before.
48
+ // Re-enable the logger so the error is visible in text mode.
49
+ // For --list the logger was silent; for other modes the API
50
+ // already printed the error before throwing.
51
+ logger.setSilent(json);
48
52
  if (e instanceof AstryxError) {
49
53
  if (json) jsonError(e.message, undefined, e.code);
50
- else process.exitCode = 1;
54
+ else {
55
+ // The logger was silent only for --list; replay the error there.
56
+ // Non-list failures were already printed by the API.
57
+ if (isList) logger.error(e.message);
58
+ process.exitCode = 1;
59
+ }
51
60
  return NO_RESULT_SET;
52
61
  }
53
62
  throw e;
54
63
  }
55
64
 
56
65
  if (json) jsonOut(result);
66
+ else if (result.type === 'upgrade.list') {
67
+ // Render the list from the result, not the API logger, so the text
68
+ // carries the same fields the JSON does (name, title, version, optional).
69
+ emit(
70
+ section('Available codemods'),
71
+ records(result.data, {
72
+ fields: ['name', 'title', 'version', 'optional'],
73
+ }),
74
+ );
75
+ }
57
76
 
58
77
  const registrySummary =
59
78
  result.type === 'upgrade.registry'
@@ -65,6 +84,9 @@ export function registerUpgrade(program) {
65
84
  ? result.data.registryCompositions
66
85
  : undefined;
67
86
  if (registrySummary && !registrySummary.ok) process.exitCode = 1;
87
+ if (result.type === 'upgrade.run' && result.data.complete === false) {
88
+ process.exitCode = 1;
89
+ }
68
90
 
69
91
  // `--list` is the one lookup here: which migrations exist for the
70
92
  // range. Everything else migrates the project — an effect, reported by
@@ -152,6 +152,8 @@ function toAscii(s) {
152
152
  .replace(/[\u2018\u2019]/g, "'")
153
153
  .replace(/[\u201C\u201D]/g, '"')
154
154
  .replace(/\u2026/g, '...')
155
+ .replace(/\u2192/g, '->')
156
+ .replace(/\u2190/g, '<-')
155
157
  .replace(/\u00a0/g, ' ');
156
158
  }
157
159
 
@@ -117,6 +117,12 @@ describe('ASCII normalization', () => {
117
117
  expect(list(['Avatar \u2014 Group']).toString()).toBe('- Avatar - Group');
118
118
  });
119
119
 
120
+ it('converts Unicode arrows to ASCII arrows', () => {
121
+ expect(text('Rename items \u2192 options').toString()).toBe('Rename items -> options');
122
+ expect(text('options \u2190 items').toString()).toBe('options <- items');
123
+ expect(record({title: 'a \u2192 b'}).toString()).toBe('title: a -> b');
124
+ });
125
+
120
126
  it('leaves code verbatim (no normalization)', () => {
121
127
  expect(code('a \u2014 b').toString()).toBe('a \u2014 b');
122
128
  });
@@ -0,0 +1,33 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * @typedef {{file: string, reason: 'generated'|'vendored'|'ignored'|'dependency'|'vcs'|'symlink'|'outside-root', declaration: string, source?: string, line?: number, command?: string}} FileProtection
6
+ */
7
+ /**
8
+ * Create an eager, fail-closed resolver. Every declaration under root is parsed
9
+ * before the caller can begin writing consumer files.
10
+ * @param {string} root
11
+ * @param {{overrides?: Map<string, string|null>}} [options]
12
+ */
13
+ export function createFileProtectionResolver(root: string, { overrides }?: {
14
+ overrides?: Map<string, string | null>;
15
+ }): Readonly<{
16
+ root: string;
17
+ classify: (filePath: string) => FileProtection[];
18
+ }>;
19
+ /** A stable failure raised before any consumer write. */
20
+ export class ProtectionSourceError extends Error {
21
+ /** @param {string} source @param {string} reason */
22
+ constructor(source: string, reason: string);
23
+ code: string;
24
+ source: string;
25
+ }
26
+ export type FileProtection = {
27
+ file: string;
28
+ reason: "generated" | "vendored" | "ignored" | "dependency" | "vcs" | "symlink" | "outside-root";
29
+ declaration: string;
30
+ source?: string;
31
+ line?: number;
32
+ command?: string;
33
+ };