@astryxdesign/cli 0.6.4-canary.505e3d8 → 0.6.4-canary.9143f03
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/api/doctor/doctor.d.mts +8 -3
- package/api/doctor/doctor.mjs +90 -9
- package/api/doctor/doctor.test.mjs +122 -10
- package/api/integration/pack-check.lifecycle-output.test.mjs +105 -0
- package/api/integration/pack-check.mjs +28 -3
- package/api/upgrade/run/files-changed.test.mjs +111 -0
- package/api/upgrade/run/run.mjs +5 -3
- package/api/upgrade/upgrade.type.mjs +2 -2
- package/assets/codemods/__tests__/runner.test.mjs +3 -1
- package/assets/codemods/file-count.test.mjs +163 -0
- package/assets/codemods/integration-runner.mjs +3 -3
- package/assets/codemods/runner.mjs +5 -4
- package/clients/cli/lib/json-shim.mjs +24 -14
- package/clients/cli/lib/parse-error-format.test.mjs +81 -0
- package/package.json +9 -9
package/api/doctor/doctor.d.mts
CHANGED
|
@@ -257,15 +257,20 @@ export type DoctorContext = {
|
|
|
257
257
|
* could not be built, when it could not.
|
|
258
258
|
*/
|
|
259
259
|
docsCatalogError?: string | null | undefined;
|
|
260
|
-
/**
|
|
261
|
-
* Combined project-level integration issues, including cross-package template replacement warnings.
|
|
262
|
-
*/
|
|
263
260
|
integrationIssues?: {
|
|
264
261
|
package: string;
|
|
265
262
|
code: string;
|
|
266
263
|
severity: "warning" | "error";
|
|
267
264
|
message: string;
|
|
268
265
|
}[] | null | undefined;
|
|
266
|
+
/**
|
|
267
|
+
* installed dependencies whose integration manifest could not be loaded
|
|
268
|
+
* Combined project-level integration issues, including cross-package template replacement warnings.
|
|
269
|
+
*/
|
|
270
|
+
autolinkFailures?: {
|
|
271
|
+
spec: string;
|
|
272
|
+
error: string;
|
|
273
|
+
}[] | null | undefined;
|
|
269
274
|
/**
|
|
270
275
|
* - Error thrown while resolving the config
|
|
271
276
|
* path (e.g. multiple config files present), surfaced by checkConfig as a FAIL.
|
package/api/doctor/doctor.mjs
CHANGED
|
@@ -26,7 +26,11 @@ import * as path from 'node:path';
|
|
|
26
26
|
import {MIN_NODE_VERSION, isNodeVersionSupported} from '../../foundation/env/node-version.mjs';
|
|
27
27
|
import {CLI_ROOT, findCoreDir, findInstalledPackage} from '../../foundation/fs/paths.mjs';
|
|
28
28
|
import {explainPackageManager, getCliInvocation} from '../../foundation/env/package-manager.mjs';
|
|
29
|
-
import {
|
|
29
|
+
import {
|
|
30
|
+
findConfigPath,
|
|
31
|
+
Project,
|
|
32
|
+
providerLedgerOf,
|
|
33
|
+
} from '../../foundation/config/project.mjs';
|
|
30
34
|
import {DocsCatalog} from '../../foundation/discovery/docs-discovery.mjs';
|
|
31
35
|
import {buildDocsIndexData} from '../../foundation/discovery/docs-section-key.mjs';
|
|
32
36
|
import {
|
|
@@ -76,6 +80,8 @@ import {semverCompare, isValidSemver, satisfiesRange} from '../../foundation/env
|
|
|
76
80
|
* @property {string|null} [docsCatalogError] - Why the project's docs catalog
|
|
77
81
|
* could not be built, when it could not.
|
|
78
82
|
* @property {Array<{package: string, code: string, severity: 'warning'|'error', message: string}>|null} [integrationIssues]
|
|
83
|
+
* @property {Array<{spec: string, error: string}>|null} [autolinkFailures]
|
|
84
|
+
* installed dependencies whose integration manifest could not be loaded
|
|
79
85
|
* Combined project-level integration issues, including cross-package template replacement warnings.
|
|
80
86
|
* @property {Error|null} [configError] - Error thrown while resolving the config
|
|
81
87
|
* path (e.g. multiple config files present), surfaced by checkConfig as a FAIL.
|
|
@@ -417,6 +423,9 @@ export function checkImplicitIntegrations(ctx) {
|
|
|
417
423
|
const implicit = ctx.integrations.filter(
|
|
418
424
|
integration => integration.__autolinked,
|
|
419
425
|
);
|
|
426
|
+
// A dependency whose manifest cannot be loaded leaves no loaded record, so
|
|
427
|
+
// without this line doctor would say no dependency ships a manifest at all.
|
|
428
|
+
const unreadable = describeUnreadableManifests(ctx.autolinkFailures ?? []);
|
|
420
429
|
|
|
421
430
|
if (implicit.length === 0) {
|
|
422
431
|
return {
|
|
@@ -424,9 +433,12 @@ export function checkImplicitIntegrations(ctx) {
|
|
|
424
433
|
label,
|
|
425
434
|
status: 'info',
|
|
426
435
|
message:
|
|
427
|
-
ctx.integrations.length > 0
|
|
436
|
+
(ctx.integrations.length > 0
|
|
428
437
|
? 'None — every loaded integration is named in astryx.config.'
|
|
429
|
-
:
|
|
438
|
+
: unreadable
|
|
439
|
+
? 'None loaded.'
|
|
440
|
+
: 'None — no installed dependency ships an astryx.integration.* manifest.') +
|
|
441
|
+
unreadable,
|
|
430
442
|
};
|
|
431
443
|
}
|
|
432
444
|
|
|
@@ -439,11 +451,25 @@ export function checkImplicitIntegrations(ctx) {
|
|
|
439
451
|
integration.__spec && integration.__spec !== integration.name
|
|
440
452
|
? ` (declared as "${integration.__spec}")`
|
|
441
453
|
: '';
|
|
442
|
-
|
|
454
|
+
// A declared root counts only when it exists: the manifest's keys are a
|
|
455
|
+
// claim, and `integration-issues` reports the ones that are not true.
|
|
456
|
+
const declared = ['components', 'templates', 'themes', 'docs', 'codemods'].filter(
|
|
443
457
|
root => integration[/** @type {'components'} */ (root)],
|
|
444
458
|
);
|
|
459
|
+
const missing = declared.filter(root => {
|
|
460
|
+
const dir = integration[/** @type {'components'} */ (root)];
|
|
461
|
+
if (typeof dir !== 'string') return false;
|
|
462
|
+
const base =
|
|
463
|
+
typeof integration.__packageDir === 'string'
|
|
464
|
+
? integration.__packageDir
|
|
465
|
+
: (ctx.cwd ?? process.cwd());
|
|
466
|
+
return !fs.existsSync(path.isAbsolute(dir) ? dir : path.resolve(base, dir));
|
|
467
|
+
});
|
|
468
|
+
const roots = declared.filter(root => !missing.includes(root));
|
|
445
469
|
const contributes = roots.length > 0 ? roots.join(', ') : 'nothing';
|
|
446
|
-
|
|
470
|
+
const absent =
|
|
471
|
+
missing.length > 0 ? ` (declared ${missing.join(', ')} missing on disk)` : '';
|
|
472
|
+
return `${integration.name}${version}${alias} from ${integration.__dependencyField}, contributing ${contributes}${absent}`;
|
|
447
473
|
});
|
|
448
474
|
|
|
449
475
|
const plural = implicit.length === 1 ? '' : 's';
|
|
@@ -453,7 +479,8 @@ export function checkImplicitIntegrations(ctx) {
|
|
|
453
479
|
status: 'info',
|
|
454
480
|
message:
|
|
455
481
|
`${implicit.length} integration${plural} loaded from installed ` +
|
|
456
|
-
`dependencies with no astryx.config entry: ${described.join('; ')}
|
|
482
|
+
`dependencies with no astryx.config entry: ${described.join('; ')}.` +
|
|
483
|
+
unreadable,
|
|
457
484
|
fix:
|
|
458
485
|
'Nothing to fix. Keep these dependencies installed. The CLI links them ' +
|
|
459
486
|
'from package.json, so an unused-dependency check that looks only for ' +
|
|
@@ -462,6 +489,30 @@ export function checkImplicitIntegrations(ctx) {
|
|
|
462
489
|
};
|
|
463
490
|
}
|
|
464
491
|
|
|
492
|
+
/**
|
|
493
|
+
* The sentence `implicit-integrations` adds for dependencies whose manifest
|
|
494
|
+
* could not be loaded, or '' when there are none. Still informational: the
|
|
495
|
+
* package is a dependency's own bug, which `doctor integration validate`
|
|
496
|
+
* diagnoses, but doctor must not report it as absent.
|
|
497
|
+
* @param {Array<{spec: string, error: string}>} failures
|
|
498
|
+
* @returns {string}
|
|
499
|
+
*/
|
|
500
|
+
function describeUnreadableManifests(failures) {
|
|
501
|
+
if (failures.length === 0) return '';
|
|
502
|
+
const one = failures.length === 1;
|
|
503
|
+
const listed = failures
|
|
504
|
+
.map(({spec, error}) => {
|
|
505
|
+
const reason = String(error).split('\n')[0].slice(0, 160);
|
|
506
|
+
return `${spec} (${reason})`;
|
|
507
|
+
})
|
|
508
|
+
.join('; ');
|
|
509
|
+
return (
|
|
510
|
+
` ${failures.length} installed ${one ? 'dependency ships' : 'dependencies ship'} ` +
|
|
511
|
+
`an astryx.integration.* manifest that could not be loaded, so ${one ? 'it contributes' : 'they contribute'} ` +
|
|
512
|
+
`nothing: ${listed}. Run \`astryx doctor integration validate <package>\` for details.`
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
|
|
465
516
|
/**
|
|
466
517
|
* Check 7 — agent docs exist and contain the Astryx section markers.
|
|
467
518
|
* @param {DoctorContext} ctx
|
|
@@ -738,12 +789,26 @@ export function checkProviderIdentity(ctx) {
|
|
|
738
789
|
integration =>
|
|
739
790
|
integration.providerId != null && integration.__loadError == null,
|
|
740
791
|
).length;
|
|
792
|
+
// An integration that could not be read has no provider ID to check, so the
|
|
793
|
+
// count above is not a complete survey. Say so instead of counting silently.
|
|
794
|
+
const unread = ctx.integrations.filter(
|
|
795
|
+
integration => integration.__loadError != null,
|
|
796
|
+
).length;
|
|
797
|
+
const unreadNote =
|
|
798
|
+
unread === 0
|
|
799
|
+
? ''
|
|
800
|
+
: unread === 1
|
|
801
|
+
? ' 1 loaded integration could not be read, so its provider ID is unknown.'
|
|
802
|
+
: ` ${unread} loaded integrations could not be read, so their provider IDs are unknown.`;
|
|
741
803
|
if (count === 0) {
|
|
742
804
|
return {
|
|
743
805
|
id,
|
|
744
806
|
label,
|
|
745
807
|
status: 'info',
|
|
746
|
-
message:
|
|
808
|
+
message:
|
|
809
|
+
(unread === 0
|
|
810
|
+
? 'None — no loaded integration has a provider identity.'
|
|
811
|
+
: 'No readable integration has a provider identity.') + unreadNote,
|
|
747
812
|
};
|
|
748
813
|
}
|
|
749
814
|
return {
|
|
@@ -751,9 +816,10 @@ export function checkProviderIdentity(ctx) {
|
|
|
751
816
|
label,
|
|
752
817
|
status: 'pass',
|
|
753
818
|
message:
|
|
754
|
-
count === 1
|
|
819
|
+
(count === 1
|
|
755
820
|
? '1 loaded integration has its own provider ID.'
|
|
756
|
-
: `${count} loaded integrations each have their own provider ID
|
|
821
|
+
: `${count} loaded integrations each have their own provider ID.`) +
|
|
822
|
+
unreadNote,
|
|
757
823
|
};
|
|
758
824
|
}
|
|
759
825
|
|
|
@@ -1198,11 +1264,25 @@ export async function runChecks(options = {}) {
|
|
|
1198
1264
|
let docsCatalogError = null;
|
|
1199
1265
|
/** @type {Array<{package: string, code: string, severity: 'warning'|'error', message: string}>|null} */
|
|
1200
1266
|
let integrationIssues = null;
|
|
1267
|
+
/** @type {Array<{spec: string, error: string}>|null} */
|
|
1268
|
+
let autolinkFailures = null;
|
|
1201
1269
|
try {
|
|
1202
1270
|
const project = await Project.load(cwd);
|
|
1203
1271
|
configTheme =
|
|
1204
1272
|
/** @type {{theme?: string}} */ (project.config ?? {}).theme ?? null;
|
|
1205
1273
|
integrations = project.loadedIntegrations;
|
|
1274
|
+
// An installed dependency whose manifest cannot be loaded is kept out of
|
|
1275
|
+
// loadedIntegrations on purpose. The provider ledger still records it.
|
|
1276
|
+
autolinkFailures = [...providerLedgerOf(project).values()]
|
|
1277
|
+
.filter(
|
|
1278
|
+
entry =>
|
|
1279
|
+
entry.outcome === 'load-failed' &&
|
|
1280
|
+
entry.candidate.source === 'autolinked',
|
|
1281
|
+
)
|
|
1282
|
+
.map(entry => ({
|
|
1283
|
+
spec: entry.candidate.spec ?? entry.label,
|
|
1284
|
+
error: entry.error ?? 'its manifest could not be loaded',
|
|
1285
|
+
}));
|
|
1206
1286
|
try {
|
|
1207
1287
|
docsCatalog = await project.docs();
|
|
1208
1288
|
docsCatalogIssues = (await project.issues()).filter(
|
|
@@ -1229,6 +1309,7 @@ export async function runChecks(options = {}) {
|
|
|
1229
1309
|
docsCatalogIssues,
|
|
1230
1310
|
docsCatalogError,
|
|
1231
1311
|
integrationIssues,
|
|
1312
|
+
autolinkFailures,
|
|
1232
1313
|
configError,
|
|
1233
1314
|
};
|
|
1234
1315
|
|
|
@@ -354,16 +354,31 @@ describe('checkImplicitIntegrations', () => {
|
|
|
354
354
|
});
|
|
355
355
|
|
|
356
356
|
it('names the package, the field, and what it contributes', () => {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
357
|
+
// Roots count only when they exist, so this one gives them real folders.
|
|
358
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-implicit-'));
|
|
359
|
+
try {
|
|
360
|
+
const dir = name => {
|
|
361
|
+
fs.mkdirSync(path.join(root, name));
|
|
362
|
+
return path.join(root, name);
|
|
363
|
+
};
|
|
364
|
+
const c = checkImplicitIntegrations({
|
|
365
|
+
integrations: [
|
|
366
|
+
autolinked({
|
|
367
|
+
components: dir('components'),
|
|
368
|
+
templates: dir('templates'),
|
|
369
|
+
themes: dir('themes'),
|
|
370
|
+
}),
|
|
371
|
+
],
|
|
372
|
+
});
|
|
373
|
+
expect(c.message).toContain('@acme/widgets@1.0.0');
|
|
374
|
+
expect(c.message).toContain('from dependencies');
|
|
375
|
+
expect(c.message).toContain(
|
|
376
|
+
'contributing components, templates, themes',
|
|
377
|
+
);
|
|
378
|
+
expect(c.message).not.toContain('missing on disk');
|
|
379
|
+
} finally {
|
|
380
|
+
fs.rmSync(root, {recursive: true, force: true});
|
|
381
|
+
}
|
|
367
382
|
});
|
|
368
383
|
|
|
369
384
|
it('names the declared key too when an npm alias makes them differ', () => {
|
|
@@ -906,3 +921,100 @@ describe('checkDocsProgressiveDisclosure languages', () => {
|
|
|
906
921
|
expect(c.message).not.toMatch(/deploying overview:/);
|
|
907
922
|
});
|
|
908
923
|
});
|
|
924
|
+
|
|
925
|
+
describe('doctor says what it could not check', () => {
|
|
926
|
+
const CORE = {
|
|
927
|
+
'node_modules/@astryxdesign/core/package.json': JSON.stringify({
|
|
928
|
+
name: '@astryxdesign/core',
|
|
929
|
+
version: '0.6.3',
|
|
930
|
+
}),
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
/** Installed dependency whose manifest declares roots that do not exist. */
|
|
934
|
+
const dangling = {
|
|
935
|
+
'node_modules/@acme/dangling/package.json': JSON.stringify({
|
|
936
|
+
name: '@acme/dangling',
|
|
937
|
+
version: '2.0.0',
|
|
938
|
+
}),
|
|
939
|
+
'node_modules/@acme/dangling/astryx.integration.mjs':
|
|
940
|
+
"export default {providerId: 'acme-dangling', components: './components', templates: './templates', docs: './docs'};\n",
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
/** Installed dependency whose manifest cannot be parsed at all. */
|
|
944
|
+
const broken = {
|
|
945
|
+
'node_modules/@acme/broken/package.json': JSON.stringify({
|
|
946
|
+
name: '@acme/broken',
|
|
947
|
+
version: '1.0.0',
|
|
948
|
+
}),
|
|
949
|
+
'node_modules/@acme/broken/astryx.integration.mjs':
|
|
950
|
+
'export default { this is not valid javascript ((\n',
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
/** @param {Record<string, string>} extra @param {string[]} deps */
|
|
954
|
+
const project = (extra, deps) =>
|
|
955
|
+
mkProject({
|
|
956
|
+
'package.json': JSON.stringify({
|
|
957
|
+
name: 'consumer',
|
|
958
|
+
version: '1.0.0',
|
|
959
|
+
dependencies: Object.fromEntries(
|
|
960
|
+
['@astryxdesign/core', ...deps].map(d => [d, '1.0.0']),
|
|
961
|
+
),
|
|
962
|
+
}),
|
|
963
|
+
...CORE,
|
|
964
|
+
...extra,
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
// An unparseable manifest is kept out of the loaded set on purpose, and
|
|
968
|
+
// doctor used to report that no installed dependency ships a manifest.
|
|
969
|
+
it('names an installed dependency whose manifest cannot be loaded', async () => {
|
|
970
|
+
const report = (await doctor({cwd: project(broken, ['@acme/broken'])})).data;
|
|
971
|
+
const check = report.checks.find(c => c.id === 'implicit-integrations');
|
|
972
|
+
|
|
973
|
+
expect(check.status).toBe('info');
|
|
974
|
+
expect(check.message).toContain('@acme/broken');
|
|
975
|
+
expect(check.message).toContain('could not be loaded');
|
|
976
|
+
expect(check.message).not.toContain('no installed dependency ships');
|
|
977
|
+
expect(report.summary.fail).toBe(0);
|
|
978
|
+
}, SLOW);
|
|
979
|
+
|
|
980
|
+
it('does not claim contributions from roots that are missing on disk', async () => {
|
|
981
|
+
const report = (await doctor({cwd: project(dangling, ['@acme/dangling'])}))
|
|
982
|
+
.data;
|
|
983
|
+
const implicit = report.checks.find(c => c.id === 'implicit-integrations');
|
|
984
|
+
const issues = report.checks.find(c => c.id === 'integration-issues');
|
|
985
|
+
|
|
986
|
+
expect(implicit.message).toContain('contributing nothing');
|
|
987
|
+
expect(implicit.message).toContain('missing on disk');
|
|
988
|
+
expect(implicit.message).not.toContain('contributing components');
|
|
989
|
+
expect(issues.status).toBe('warn');
|
|
990
|
+
expect(issues.message).toContain('@acme/dangling');
|
|
991
|
+
}, SLOW);
|
|
992
|
+
|
|
993
|
+
it('still says no dependency ships a manifest when none does', async () => {
|
|
994
|
+
const report = (await doctor({cwd: project({}, [])})).data;
|
|
995
|
+
const check = report.checks.find(c => c.id === 'implicit-integrations');
|
|
996
|
+
|
|
997
|
+
expect(check.message).toBe(
|
|
998
|
+
'None — no installed dependency ships an astryx.integration.* manifest.',
|
|
999
|
+
);
|
|
1000
|
+
}, SLOW);
|
|
1001
|
+
|
|
1002
|
+
it('says how many integrations could not be read, rather than counting silently', () => {
|
|
1003
|
+
/** @type {any} */
|
|
1004
|
+
const ctx = {
|
|
1005
|
+
cwd: '/x',
|
|
1006
|
+
nodeVersion: process.versions.node,
|
|
1007
|
+
coreDir: null,
|
|
1008
|
+
configPath: null,
|
|
1009
|
+
configTheme: null,
|
|
1010
|
+
integrations: [
|
|
1011
|
+
{name: '@acme/ok', __spec: '@acme/ok', providerId: 'ok'},
|
|
1012
|
+
{name: '@acme/bad', __spec: '@acme/bad', __loadError: 'boom'},
|
|
1013
|
+
],
|
|
1014
|
+
};
|
|
1015
|
+
const check = checkProviderIdentity(ctx);
|
|
1016
|
+
|
|
1017
|
+
expect(check.message).toContain('1 loaded integration has its own provider ID.');
|
|
1018
|
+
expect(check.message).toContain('could not be read');
|
|
1019
|
+
});
|
|
1020
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
4
|
+
import * as fs from 'node:fs';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
import {integrationPackCheck} from './pack-check.mjs';
|
|
7
|
+
|
|
8
|
+
let tmpDir;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-pack-check-'));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
fs.rmSync(tmpDir, {recursive: true, force: true});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/** @param {Record<string, string>} scripts */
|
|
19
|
+
function writeThemePackage(scripts) {
|
|
20
|
+
fs.writeFileSync(
|
|
21
|
+
path.join(tmpDir, 'package.json'),
|
|
22
|
+
`${JSON.stringify(
|
|
23
|
+
{
|
|
24
|
+
name: '@acme/widgets',
|
|
25
|
+
version: '1.0.0',
|
|
26
|
+
files: ['astryx.integration.mjs', 'themes'],
|
|
27
|
+
scripts,
|
|
28
|
+
},
|
|
29
|
+
null,
|
|
30
|
+
2,
|
|
31
|
+
)}\n`,
|
|
32
|
+
);
|
|
33
|
+
fs.writeFileSync(
|
|
34
|
+
path.join(tmpDir, 'astryx.integration.mjs'),
|
|
35
|
+
"export default {themes: './themes'};\n",
|
|
36
|
+
);
|
|
37
|
+
const root = path.join(tmpDir, 'themes');
|
|
38
|
+
fs.mkdirSync(path.join(root, 'ocean'), {recursive: true});
|
|
39
|
+
fs.writeFileSync(
|
|
40
|
+
path.join(root, 'ocean', 'oceanTheme.ts'),
|
|
41
|
+
"import {defineTheme} from '@astryxdesign/core/theme';\n\nexport const oceanTheme = defineTheme({name: 'ocean'});\n",
|
|
42
|
+
);
|
|
43
|
+
fs.writeFileSync(
|
|
44
|
+
path.join(root, 'ocean', 'oceanTheme.doc.mjs'),
|
|
45
|
+
`/** @type {import('@astryxdesign/cli/authoring').ThemeDoc} */
|
|
46
|
+
export default {type: 'theme', name: 'ocean', displayName: 'Ocean', description: 'Ocean theme.', maintained: true};
|
|
47
|
+
`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe('integrationPackCheck with lifecycle script output', () => {
|
|
52
|
+
it('checks the tarball when lifecycle scripts print to stdout', async () => {
|
|
53
|
+
writeThemePackage({
|
|
54
|
+
prepack: 'node -e "console.log(\'building the package\')"',
|
|
55
|
+
prepare: 'node -e "console.log(\'preparing\')"',
|
|
56
|
+
postpack: 'node -e "console.log(\'packed\')"',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const result = await integrationPackCheck({cwd: tmpDir});
|
|
60
|
+
|
|
61
|
+
expect(result.data.issues).toEqual([]);
|
|
62
|
+
expect(result.data.packable).toBe(true);
|
|
63
|
+
expect(result.data.tarball?.filename).toBe('acme-widgets-1.0.0.tgz');
|
|
64
|
+
expect(result.data.contributions.packed).toEqual(
|
|
65
|
+
result.data.contributions.local,
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('still runs a chatty lifecycle script and compares what it packed', async () => {
|
|
70
|
+
const renameTheme = [
|
|
71
|
+
"console.log('renaming ocean to storm')",
|
|
72
|
+
"const fs=require('fs')",
|
|
73
|
+
"fs.renameSync('themes/ocean','themes/storm')",
|
|
74
|
+
"const p='themes/storm/oceanTheme.doc.mjs'",
|
|
75
|
+
"let x=fs.readFileSync(p,'utf8')",
|
|
76
|
+
"x=x.replace(/name: 'ocean'/, 'name: '+String.fromCharCode(39)+'storm'+String.fromCharCode(39))",
|
|
77
|
+
'fs.writeFileSync(p,x)',
|
|
78
|
+
].join(';');
|
|
79
|
+
writeThemePackage({prepack: `node -e "${renameTheme}"`});
|
|
80
|
+
|
|
81
|
+
const result = await integrationPackCheck({cwd: tmpDir});
|
|
82
|
+
const codes = result.data.issues.map(issue => issue.code);
|
|
83
|
+
|
|
84
|
+
expect(codes).not.toContain('pack_failed');
|
|
85
|
+
expect(codes).toContain('identity_not_packed');
|
|
86
|
+
expect(codes).toContain('packed_identity_unexpected');
|
|
87
|
+
expect(result.data.packable).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('keeps a failing lifecycle script output in the pack_failed issue', async () => {
|
|
91
|
+
writeThemePackage({
|
|
92
|
+
prepack:
|
|
93
|
+
'node -e "console.error(\'prepack exploded\'); process.exit(3)"',
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const result = await integrationPackCheck({cwd: tmpDir});
|
|
97
|
+
const failure = result.data.issues.find(
|
|
98
|
+
issue => issue.code === 'pack_failed',
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
expect(result.data.packable).toBe(false);
|
|
102
|
+
expect(failure?.message).toContain('npm pack failed (exit 3)');
|
|
103
|
+
expect(failure?.message).toContain('prepack exploded');
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -162,6 +162,22 @@ export function parseNpmPackOutput(output) {
|
|
|
162
162
|
};
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Summarize the JSON error npm prints on stdout when `--json` pack fails.
|
|
167
|
+
* @param {string} output
|
|
168
|
+
* @returns {string}
|
|
169
|
+
*/
|
|
170
|
+
function npmPackErrorDetail(output) {
|
|
171
|
+
try {
|
|
172
|
+
const error = JSON.parse(output)?.error;
|
|
173
|
+
return [error?.summary, error?.detail]
|
|
174
|
+
.filter(part => typeof part === 'string' && part.trim() !== '')
|
|
175
|
+
.join(': ');
|
|
176
|
+
} catch {
|
|
177
|
+
return (output || '').trim();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
165
181
|
/**
|
|
166
182
|
* Run `npm pack --json` with output directed to `destDir` so no preexisting
|
|
167
183
|
* tgz is overwritten. This intentionally runs the package lifecycle, matching
|
|
@@ -175,16 +191,25 @@ export function parseNpmPackOutput(output) {
|
|
|
175
191
|
function runNpmPack(packageDir, destDir) {
|
|
176
192
|
const result = spawnSync(
|
|
177
193
|
'npm',
|
|
178
|
-
[
|
|
194
|
+
[
|
|
195
|
+
'pack',
|
|
196
|
+
'--json',
|
|
197
|
+
'--silent',
|
|
198
|
+
// Lifecycle scripts still run; background mode keeps their output off
|
|
199
|
+
// the stdout that carries the JSON result.
|
|
200
|
+
'--foreground-scripts=false',
|
|
201
|
+
`--pack-destination=${destDir}`,
|
|
202
|
+
],
|
|
179
203
|
{cwd: packageDir, encoding: 'utf-8', timeout: 60_000},
|
|
180
204
|
);
|
|
181
205
|
if (result.error) {
|
|
182
206
|
throw new Error(`Could not start npm pack: ${result.error.message}`);
|
|
183
207
|
}
|
|
184
208
|
if (result.status !== 0) {
|
|
185
|
-
const
|
|
209
|
+
const detail =
|
|
210
|
+
(result.stderr || '').trim() || npmPackErrorDetail(result.stdout);
|
|
186
211
|
throw new Error(
|
|
187
|
-
`npm pack failed (exit ${result.status})${
|
|
212
|
+
`npm pack failed (exit ${result.status})${detail ? `: ${detail}` : ''}.`,
|
|
188
213
|
);
|
|
189
214
|
}
|
|
190
215
|
return parseNpmPackOutput(result.stdout);
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The upgrade receipt counts a file once when a core codemod AND an
|
|
5
|
+
* integration codemod both change it: `filesChanged` is the union of the two
|
|
6
|
+
* runners' files, while `transformsApplied` adds their changes.
|
|
7
|
+
*
|
|
8
|
+
* Uses a real consumer under a repo-local temp dir (Vite blocks dynamic import
|
|
9
|
+
* of config and integration modules from /tmp), the real core registry, and an
|
|
10
|
+
* installed integration whose code codemod stamps the same file.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {describe, it, expect, afterEach} from 'vitest';
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as path from 'node:path';
|
|
16
|
+
import {upgrade} from '../upgrade.mjs';
|
|
17
|
+
import {
|
|
18
|
+
latestVersion,
|
|
19
|
+
versions,
|
|
20
|
+
getTransformsBetween,
|
|
21
|
+
} from '../../../assets/codemods/registry.mjs';
|
|
22
|
+
|
|
23
|
+
const SLOW = 60_000;
|
|
24
|
+
|
|
25
|
+
/** The registry version whose manifest ships the authoring migration. */
|
|
26
|
+
async function authoringTier() {
|
|
27
|
+
const all = await getTransformsBetween('0.0.0', latestVersion);
|
|
28
|
+
const tier = all.find(({transforms}) =>
|
|
29
|
+
transforms.some(t => t.name === 'migrate-authoring-imports'),
|
|
30
|
+
);
|
|
31
|
+
if (!tier) throw new Error('no registry version ships the authoring migration');
|
|
32
|
+
return tier.version;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A consumer with installed core at the registry's latest version and one
|
|
37
|
+
* old-surface file that the core authoring codemods rewrite.
|
|
38
|
+
* @param {string} dir
|
|
39
|
+
* @param {{integrationCodemod: boolean}} options
|
|
40
|
+
*/
|
|
41
|
+
function seed(dir, {integrationCodemod}) {
|
|
42
|
+
fs.writeFileSync(
|
|
43
|
+
path.join(dir, 'package.json'),
|
|
44
|
+
JSON.stringify({name: 'consumer', version: '1.0.0'}),
|
|
45
|
+
);
|
|
46
|
+
const core = path.join(dir, 'node_modules', '@astryxdesign', 'core');
|
|
47
|
+
fs.mkdirSync(core, {recursive: true});
|
|
48
|
+
fs.writeFileSync(
|
|
49
|
+
path.join(core, 'package.json'),
|
|
50
|
+
JSON.stringify({name: '@astryxdesign/core', version: latestVersion}),
|
|
51
|
+
);
|
|
52
|
+
fs.mkdirSync(path.join(dir, 'src'), {recursive: true});
|
|
53
|
+
fs.writeFileSync(
|
|
54
|
+
path.join(dir, 'src', 'Button.doc.mjs'),
|
|
55
|
+
[
|
|
56
|
+
"import {createComponentDoc} from '@astryxdesign/core/authoring';",
|
|
57
|
+
"export default createComponentDoc({name: 'Button', props: []});",
|
|
58
|
+
'',
|
|
59
|
+
].join('\n'),
|
|
60
|
+
);
|
|
61
|
+
if (!integrationCodemod) return;
|
|
62
|
+
fs.writeFileSync(
|
|
63
|
+
path.join(dir, 'astryx.config.mjs'),
|
|
64
|
+
"export default {integrations: ['@acme/widgets']};\n",
|
|
65
|
+
);
|
|
66
|
+
const pkg = path.join(dir, 'node_modules', '@acme', 'widgets');
|
|
67
|
+
fs.mkdirSync(path.join(pkg, 'codemods', latestVersion), {recursive: true});
|
|
68
|
+
fs.writeFileSync(
|
|
69
|
+
path.join(pkg, 'package.json'),
|
|
70
|
+
JSON.stringify({name: '@acme/widgets', version: '1.0.0'}),
|
|
71
|
+
);
|
|
72
|
+
fs.writeFileSync(
|
|
73
|
+
path.join(pkg, 'astryx.integration.mjs'),
|
|
74
|
+
"export default {codemods: './codemods'};\n",
|
|
75
|
+
);
|
|
76
|
+
fs.writeFileSync(
|
|
77
|
+
path.join(pkg, 'codemods', latestVersion, 'acme-stamp.mjs'),
|
|
78
|
+
"export default {type: 'code', title: 'Stamp', transform: file => (file.source.includes('// acme') ? null : `${file.source}// acme\\n`)};\n",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
describe('upgrade receipt — filesChanged across core and integration codemods', () => {
|
|
83
|
+
/** @type {string[]} */
|
|
84
|
+
const dirs = [];
|
|
85
|
+
afterEach(() => {
|
|
86
|
+
for (const dir of dirs.splice(0)) fs.rmSync(dir, {recursive: true, force: true});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/** @param {{integrationCodemod: boolean}} options */
|
|
90
|
+
async function run(options) {
|
|
91
|
+
const dir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-files-changed-'));
|
|
92
|
+
dirs.push(dir);
|
|
93
|
+
seed(dir, options);
|
|
94
|
+
const tier = await authoringTier();
|
|
95
|
+
const from = versions[versions.indexOf(tier) - 1];
|
|
96
|
+
const res = await upgrade({from, path: 'src'}, {cwd: dir});
|
|
97
|
+
expect(res.type).toBe('upgrade.run');
|
|
98
|
+
return res.data;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
it('counts a file changed by both a core and an integration codemod once', async () => {
|
|
102
|
+
const coreOnly = await run({integrationCodemod: false});
|
|
103
|
+
expect(coreOnly.filesChanged).toBe(1);
|
|
104
|
+
expect(coreOnly.transformsApplied).toBeGreaterThan(0);
|
|
105
|
+
|
|
106
|
+
const both = await run({integrationCodemod: true});
|
|
107
|
+
expect(both.integrations).toEqual(['@acme/widgets']);
|
|
108
|
+
expect(both.filesChanged).toBe(1);
|
|
109
|
+
expect(both.transformsApplied).toBe(coreOnly.transformsApplied + 1);
|
|
110
|
+
}, SLOW);
|
|
111
|
+
});
|
package/api/upgrade/run/run.mjs
CHANGED
|
@@ -437,9 +437,11 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
|
|
|
437
437
|
|
|
438
438
|
const registryResult = await reconcileCompositions();
|
|
439
439
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
(
|
|
440
|
+
// A file a core codemod AND an integration codemod both changed is one file.
|
|
441
|
+
const mergedFilesChanged = new Set([
|
|
442
|
+
...(coreResult?.changedFiles ?? []),
|
|
443
|
+
...(integrationResult?.changedFiles ?? []),
|
|
444
|
+
]).size;
|
|
443
445
|
const mergedTransformsApplied =
|
|
444
446
|
(coreResult?.totalTransformsApplied ?? 0) +
|
|
445
447
|
(integrationResult?.totalTransformsApplied ?? 0);
|
|
@@ -119,11 +119,11 @@
|
|
|
119
119
|
* @property {RegistryCompositionSummary} [data.registryCompositions]
|
|
120
120
|
* @property {boolean} [data.complete] False when protected required changes remain.
|
|
121
121
|
* @property {'ERR_CODEMOD_PROTECTED'} [data.errorCode] Stable incomplete-result code when complete is false.
|
|
122
|
-
* @property {number} [data.filesChanged]
|
|
122
|
+
* @property {number} [data.filesChanged] Distinct files changed across core + integration codemods (apply mode). One file that four codemods each changed counts once.
|
|
123
123
|
* @property {string[]} [data.modifiedFiles] Project-relative files changed or previewed.
|
|
124
124
|
* @property {ProtectedCodemodFile[]} [data.protectedFiles] Protected files that still require a codemod change after regeneration.
|
|
125
125
|
* @property {Array<{file: string, location?: string, reason: string}>} [data.declinedCandidates] Candidates left unchanged because proof was insufficient.
|
|
126
|
-
* @property {number} [data.transformsApplied] Total
|
|
126
|
+
* @property {number} [data.transformsApplied] Total codemod changes. A code or config codemod counts once for each file it changed, so one file changed by four of them counts four times; a project codemod counts once, however many files it writes.
|
|
127
127
|
* @property {Array<{file: string, codemod: string, error: string}>} [data.errors] Per-codemod errors, when any codemod failed.
|
|
128
128
|
*/
|
|
129
129
|
|
|
@@ -46,7 +46,9 @@ describe('runCodemods — ordered dry-run state', () => {
|
|
|
46
46
|
silent: true,
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
// One file that two transforms changed is one file and two changes.
|
|
50
|
+
expect(preview.totalFilesChanged).toBe(1);
|
|
51
|
+
expect(preview.totalTransformsApplied).toBe(2);
|
|
50
52
|
expect(preview.changedFiles).toEqual([
|
|
51
53
|
path.join(srcDir, 'a.ts'),
|
|
52
54
|
path.join(srcDir, 'a.ts'),
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file `filesChanged` counts FILES, not (codemod, file) pairs.
|
|
5
|
+
*
|
|
6
|
+
* One source file that four codemods each changed was reported as four files
|
|
7
|
+
* changed — the total was incremented once per transform per file, so it
|
|
8
|
+
* equalled `transformsApplied` in every run and the documented meaning of the
|
|
9
|
+
* field ("Total files changed") was never true. The two numbers answer
|
|
10
|
+
* different questions and both are in the receipt.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {describe, it, expect, beforeEach, afterEach} from 'vitest';
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as os from 'node:os';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import jscodeshift from 'jscodeshift';
|
|
18
|
+
import {runCodemods} from './runner.mjs';
|
|
19
|
+
import {runIntegrationCodemods} from './integration-runner.mjs';
|
|
20
|
+
|
|
21
|
+
let dir;
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-file-count-'));
|
|
25
|
+
});
|
|
26
|
+
afterEach(() => fs.rmSync(dir, {recursive: true, force: true}));
|
|
27
|
+
|
|
28
|
+
/** A transform that rewrites one distinctive token, so several can stack. */
|
|
29
|
+
const renaming = (from, to) => (file) =>
|
|
30
|
+
file.source.includes(from) ? file.source.split(from).join(to) : null;
|
|
31
|
+
|
|
32
|
+
/** @param {string} name @param {string[]} contents */
|
|
33
|
+
function writeSources(...contents) {
|
|
34
|
+
return contents.map((content, i) => {
|
|
35
|
+
const file = path.join(dir, `file${i}.ts`);
|
|
36
|
+
fs.writeFileSync(file, content);
|
|
37
|
+
return file;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('core codemod runner — filesChanged counts files', () => {
|
|
42
|
+
it('reports 1 file for one file changed by four codemods', async () => {
|
|
43
|
+
writeSources('const a = ONE + TWO + THREE + FOUR;\n');
|
|
44
|
+
|
|
45
|
+
const result = await runCodemods(
|
|
46
|
+
[
|
|
47
|
+
{
|
|
48
|
+
version: '0.0.2',
|
|
49
|
+
transforms: [
|
|
50
|
+
{name: 'one', transform: renaming('ONE', '1'), meta: {title: 'one'}},
|
|
51
|
+
{name: 'two', transform: renaming('TWO', '2'), meta: {title: 'two'}},
|
|
52
|
+
{name: 'three', transform: renaming('THREE', '3'), meta: {title: 'three'}},
|
|
53
|
+
{name: 'four', transform: renaming('FOUR', '4'), meta: {title: 'four'}},
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
{apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
expect(result.totalTransformsApplied).toBe(4);
|
|
61
|
+
expect(result.totalFilesChanged).toBe(1);
|
|
62
|
+
expect(new Set(result.changedFiles).size).toBe(1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('still counts two files as two', async () => {
|
|
66
|
+
writeSources('const a = ONE;\n', 'const b = ONE;\n');
|
|
67
|
+
|
|
68
|
+
const result = await runCodemods(
|
|
69
|
+
[
|
|
70
|
+
{
|
|
71
|
+
version: '0.0.2',
|
|
72
|
+
transforms: [
|
|
73
|
+
{name: 'one', transform: renaming('ONE', '1'), meta: {title: 'one'}},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
{apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(result.totalTransformsApplied).toBe(2);
|
|
81
|
+
expect(result.totalFilesChanged).toBe(2);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('reports 0 when nothing matched', async () => {
|
|
85
|
+
writeSources('const a = 1;\n');
|
|
86
|
+
|
|
87
|
+
const result = await runCodemods(
|
|
88
|
+
[
|
|
89
|
+
{
|
|
90
|
+
version: '0.0.2',
|
|
91
|
+
transforms: [
|
|
92
|
+
{name: 'one', transform: renaming('ONE', '1'), meta: {title: 'one'}},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
{apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
expect(result.totalFilesChanged).toBe(0);
|
|
100
|
+
expect(result.totalTransformsApplied).toBe(0);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('integration codemod runner — filesChanged counts files', () => {
|
|
105
|
+
it('reports 1 file for one file changed by three integration codemods', () => {
|
|
106
|
+
writeSources('const a = ONE + TWO + THREE;\n');
|
|
107
|
+
|
|
108
|
+
const entry = (id, from, to) => ({
|
|
109
|
+
id,
|
|
110
|
+
package: '@acme/widgets',
|
|
111
|
+
type: 'code',
|
|
112
|
+
codemod: {title: id, transform: renaming(from, to)},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const result = runIntegrationCodemods(
|
|
116
|
+
[
|
|
117
|
+
{
|
|
118
|
+
version: '1.0.0',
|
|
119
|
+
codemods: [
|
|
120
|
+
entry('one', 'ONE', '1'),
|
|
121
|
+
entry('two', 'TWO', '2'),
|
|
122
|
+
entry('three', 'THREE', '3'),
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
{apply: true, path: dir, root: dir, skipCodemods: new Set(), jscodeshift, silent: true},
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
expect(result.totalTransformsApplied).toBe(3);
|
|
130
|
+
expect(result.totalFilesChanged).toBe(1);
|
|
131
|
+
expect(new Set(result.changedFiles).size).toBe(1);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('core codemod runner — a project codemod', () => {
|
|
136
|
+
it('counts every file it writes, and one change', async () => {
|
|
137
|
+
const result = await runCodemods(
|
|
138
|
+
[
|
|
139
|
+
{
|
|
140
|
+
version: '0.0.2',
|
|
141
|
+
transforms: [
|
|
142
|
+
{
|
|
143
|
+
name: 'project-plan',
|
|
144
|
+
meta: {title: 'project plan', codemodType: 'project'},
|
|
145
|
+
transform: async root => ({
|
|
146
|
+
writes: [
|
|
147
|
+
{path: path.join(root, 'a.ts'), contents: 'a\n'},
|
|
148
|
+
{path: path.join(root, 'b.ts'), contents: 'b\n'},
|
|
149
|
+
],
|
|
150
|
+
deletes: [],
|
|
151
|
+
problems: [],
|
|
152
|
+
}),
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
{apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
expect(result.totalFilesChanged).toBe(2);
|
|
161
|
+
expect(result.totalTransformsApplied).toBe(1);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -67,7 +67,6 @@ export function runIntegrationCodemods(
|
|
|
67
67
|
/** In-memory pipeline state keeps ordered dry-runs equivalent to apply. */
|
|
68
68
|
const virtualContents = new Map(providedContents ?? []);
|
|
69
69
|
|
|
70
|
-
let totalFilesChanged = 0;
|
|
71
70
|
let totalTransformsApplied = 0;
|
|
72
71
|
/** @type {string[]} */
|
|
73
72
|
const changedFiles = [];
|
|
@@ -115,7 +114,6 @@ export function runIntegrationCodemods(
|
|
|
115
114
|
protection,
|
|
116
115
|
contents: virtualContents,
|
|
117
116
|
});
|
|
118
|
-
totalFilesChanged += r.filesChanged;
|
|
119
117
|
totalTransformsApplied += r.filesChanged;
|
|
120
118
|
changedFiles.push(...r.changedFiles);
|
|
121
119
|
writtenFiles.push(...r.writtenFiles);
|
|
@@ -145,7 +143,6 @@ export function runIntegrationCodemods(
|
|
|
145
143
|
protection,
|
|
146
144
|
contents: virtualContents,
|
|
147
145
|
});
|
|
148
|
-
totalFilesChanged += r.filesChanged;
|
|
149
146
|
totalTransformsApplied += r.filesChanged;
|
|
150
147
|
changedFiles.push(...r.changedFiles);
|
|
151
148
|
writtenFiles.push(...r.writtenFiles);
|
|
@@ -154,6 +151,9 @@ export function runIntegrationCodemods(
|
|
|
154
151
|
}
|
|
155
152
|
}
|
|
156
153
|
|
|
154
|
+
// A file several codemods changed is one file; transforms count each change.
|
|
155
|
+
const totalFilesChanged = new Set(changedFiles).size;
|
|
156
|
+
|
|
157
157
|
return {
|
|
158
158
|
totalFilesChanged,
|
|
159
159
|
totalTransformsApplied,
|
|
@@ -486,7 +486,6 @@ export async function runCodemods(
|
|
|
486
486
|
(await import('jscodeshift')).default
|
|
487
487
|
);
|
|
488
488
|
|
|
489
|
-
let totalFilesChanged = 0;
|
|
490
489
|
let totalTransformsApplied = 0;
|
|
491
490
|
let totalValidationBlocked = 0;
|
|
492
491
|
/** @type {Array<{file: string, codemod: string, error: string}>} */
|
|
@@ -554,7 +553,6 @@ export async function runCodemods(
|
|
|
554
553
|
protectionWriteCount = writtenFiles.length;
|
|
555
554
|
}
|
|
556
555
|
if (result.filesChanged > 0) {
|
|
557
|
-
totalFilesChanged += result.filesChanged;
|
|
558
556
|
totalTransformsApplied += 1;
|
|
559
557
|
}
|
|
560
558
|
continue;
|
|
@@ -577,7 +575,6 @@ export async function runCodemods(
|
|
|
577
575
|
changedFiles.push(...result.changedFiles);
|
|
578
576
|
writtenFiles.push(...result.writtenFiles);
|
|
579
577
|
if (result.filesChanged > 0) {
|
|
580
|
-
totalFilesChanged += result.filesChanged;
|
|
581
578
|
totalTransformsApplied += result.filesChanged;
|
|
582
579
|
}
|
|
583
580
|
continue;
|
|
@@ -592,7 +589,6 @@ export async function runCodemods(
|
|
|
592
589
|
protectedFiles.push(...result.protectedFiles);
|
|
593
590
|
changedFiles.push(...result.changedFiles);
|
|
594
591
|
writtenFiles.push(...result.writtenFiles);
|
|
595
|
-
totalFilesChanged += result.filesChanged;
|
|
596
592
|
totalTransformsApplied += result.filesChanged;
|
|
597
593
|
totalValidationBlocked += result.errors.filter(
|
|
598
594
|
error =>
|
|
@@ -630,6 +626,11 @@ export async function runCodemods(
|
|
|
630
626
|
);
|
|
631
627
|
}
|
|
632
628
|
|
|
629
|
+
// A file several codemods changed is one file. `totalTransformsApplied` is
|
|
630
|
+
// unchanged: a code or config codemod counts each file it changed, and a
|
|
631
|
+
// project codemod counts once. The two answer different questions.
|
|
632
|
+
const totalFilesChanged = new Set(changedFiles).size;
|
|
633
|
+
|
|
633
634
|
if (protectedFiles.length > 0) {
|
|
634
635
|
const files = [...new Set(protectedFiles.map(item => item.file))];
|
|
635
636
|
log.warn(
|
|
@@ -33,10 +33,12 @@
|
|
|
33
33
|
* shows help because the invocation failed (`help <unknown>`, or a
|
|
34
34
|
* command group with no subcommand), which exits 1.
|
|
35
35
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
36
|
+
* Commander writes its own "error: ..." line via configureOutput.writeErr.
|
|
37
|
+
* The shim drops that line in both modes. Under --json the error envelope
|
|
38
|
+
* replaces it; in text mode `handleCommanderError` writes the Astryx line
|
|
39
|
+
* instead (`Error: <message>`, the same message the envelope carries), so
|
|
40
|
+
* a parse failure reads like every other CLI error. Other stderr output,
|
|
41
|
+
* such as help printed as the failure report, still passes through.
|
|
40
42
|
*/
|
|
41
43
|
|
|
42
44
|
import {API_VERSION, isJsonMode, toErrorEnvelope} from '../../../foundation/response/json.mjs';
|
|
@@ -249,11 +251,20 @@ function applyShimRecursively(cmd) {
|
|
|
249
251
|
});
|
|
250
252
|
cmd.configureOutput({
|
|
251
253
|
writeOut: (str) => process.stdout.write(str),
|
|
254
|
+
// Commander's own "error: ..." line never reaches the user. Under --json a
|
|
255
|
+
// consumer parsing both streams must not see it alongside the envelope;
|
|
256
|
+
// in text mode it is Commander's format, not Astryx's, so an invalid
|
|
257
|
+
// global option (`--lang zh-Hans`) printed `error: option '--lang
|
|
258
|
+
// <locale>' argument 'zh-Hans' is invalid…` where every other CLI error
|
|
259
|
+
// prints `Error: …`. handleCommanderError writes the Astryx line below,
|
|
260
|
+
// for both modes, from the same message.
|
|
261
|
+
//
|
|
262
|
+
// ONLY that line. Commander also writes HELP through this channel when it
|
|
263
|
+
// shows help because the invocation failed (a command group with no
|
|
264
|
+
// subcommand), and that output is still wanted in text mode.
|
|
252
265
|
writeErr: (str) => {
|
|
253
|
-
// Suppress Commander's "error: ..." stderr line when --json is
|
|
254
|
-
// active, so a JSON consumer parsing both streams doesn't see
|
|
255
|
-
// it alongside the envelope. Non-JSON callers are unaffected.
|
|
256
266
|
if (jsonActive()) return;
|
|
267
|
+
if (/^error:\s/i.test(str)) return;
|
|
257
268
|
process.stderr.write(str);
|
|
258
269
|
},
|
|
259
270
|
});
|
|
@@ -432,16 +443,15 @@ export function handleCommanderError(err) {
|
|
|
432
443
|
code: commanderCodeToErrorCode(code, message),
|
|
433
444
|
});
|
|
434
445
|
|
|
435
|
-
// Real error paths.
|
|
446
|
+
// Real error paths. Strip Commander's "error: " prefix once: in the envelope
|
|
447
|
+
// the key is already `error`, and in text mode the Astryx prefix replaces it.
|
|
448
|
+
const cleaned = message.replace(/^error:\s*/i, '');
|
|
436
449
|
if (jsonActive()) {
|
|
437
|
-
// Strip Commander's "error: " prefix — the envelope key is `error`
|
|
438
|
-
// already, doubled "error" is noise.
|
|
439
|
-
const cleaned = message.replace(/^error:\s*/i, '');
|
|
440
450
|
emitJsonError(cleaned, undefined, commanderCodeToErrorCode(code, cleaned));
|
|
441
451
|
} else {
|
|
442
|
-
//
|
|
443
|
-
//
|
|
444
|
-
|
|
452
|
+
// Commander's own line was suppressed above, so a parse failure reads the
|
|
453
|
+
// same as every other CLI error — the `Error: …` line cliError prints.
|
|
454
|
+
process.stderr.write(`Error: ${cleaned}\n`);
|
|
445
455
|
}
|
|
446
456
|
process.exit(exitCode || 1);
|
|
447
457
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file A parse failure reads like every other Astryx error in text mode.
|
|
5
|
+
*
|
|
6
|
+
* `astryx theme list --lang zh-Hans` printed Commander's own line —
|
|
7
|
+
* `error: option '--lang <locale>' argument 'zh-Hans' is invalid…` — while
|
|
8
|
+
* every other CLI error prints `Error: …`. `--json` was already correct
|
|
9
|
+
* (ERR_INVALID_LANG), so text and JSON disagreed on everything except the exit
|
|
10
|
+
* code. Commander writes that line before any Astryx code runs, so the shim is
|
|
11
|
+
* the only place that can speak for it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {describe, it, expect} from 'vitest';
|
|
15
|
+
import {Command} from 'commander';
|
|
16
|
+
import {runCli} from '../../../test-utils/run-cli.mjs';
|
|
17
|
+
import {installJsonShim} from './json-shim.mjs';
|
|
18
|
+
|
|
19
|
+
describe('a parse error prints the Astryx error format', () => {
|
|
20
|
+
it.each([
|
|
21
|
+
['invalid --lang value', ['theme', 'list', '--lang', 'zh-Hans'], 'ERR_INVALID_LANG'],
|
|
22
|
+
['invalid --detail value', ['theme', 'list', '--detail', 'nope'], 'ERR_INVALID_DETAIL'],
|
|
23
|
+
['unknown option', ['component', '--bogus-flag'], 'ERR_INVALID_OPTION'],
|
|
24
|
+
['missing argument', ['theme', 'build'], 'ERR_MISSING_ARGUMENT'],
|
|
25
|
+
])('%s', async (_label, args, code) => {
|
|
26
|
+
const human = await runCli(args);
|
|
27
|
+
|
|
28
|
+
expect(human.status).toBe(1);
|
|
29
|
+
expect(human.stderr).toContain('Error: ');
|
|
30
|
+
// Commander's own lowercase line must not reach the user.
|
|
31
|
+
expect(human.stderr).not.toMatch(/(^|\n)error: /);
|
|
32
|
+
|
|
33
|
+
// --json is unchanged, and the two modes agree on the exit code.
|
|
34
|
+
const json = await runCli(['--json', ...args]);
|
|
35
|
+
expect(json.status).toBe(human.status);
|
|
36
|
+
expect(JSON.parse(json.stdout).code).toBe(code);
|
|
37
|
+
expect(json.stderr).toBe('');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('carries Commander\'s explanation, not just a generic line', async () => {
|
|
41
|
+
const {stderr} = await runCli(['theme', 'list', '--lang', 'zh-Hans']);
|
|
42
|
+
expect(stderr).toContain("'zh-Hans' is invalid");
|
|
43
|
+
expect(stderr).toContain('en, zh, dense');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('leaves --help and --version at exit 0 with nothing on stderr', async () => {
|
|
47
|
+
for (const args of [['--help'], ['theme', '--help']]) {
|
|
48
|
+
const r = await runCli(args);
|
|
49
|
+
expect(r.status).toBe(0);
|
|
50
|
+
expect(r.stderr).toBe('');
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Commander writes help through the same stderr channel when it shows help
|
|
55
|
+
// BECAUSE the invocation failed, so suppressing that channel wholesale would
|
|
56
|
+
// have taken the help with it. Driven on a throwaway program rather than a
|
|
57
|
+
// real command, so the guard outlives whichever command happens to have a
|
|
58
|
+
// subcommand group today.
|
|
59
|
+
it('still lets help reach stderr when help IS the failure report', () => {
|
|
60
|
+
const program = new Command('probe');
|
|
61
|
+
const group = program.command('group');
|
|
62
|
+
group.command('leaf').action(() => {});
|
|
63
|
+
installJsonShim(program);
|
|
64
|
+
|
|
65
|
+
/** @type {string[]} */
|
|
66
|
+
const written = [];
|
|
67
|
+
const original = process.stderr.write;
|
|
68
|
+
// @ts-expect-error test double for the write signature
|
|
69
|
+
process.stderr.write = str => {
|
|
70
|
+
written.push(String(str));
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
group.outputHelp({error: true});
|
|
75
|
+
} finally {
|
|
76
|
+
process.stderr.write = original;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
expect(written.join('')).toMatch(/Usage: probe group/);
|
|
80
|
+
});
|
|
81
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/cli",
|
|
3
|
-
"version": "0.6.4-canary.
|
|
3
|
+
"version": "0.6.4-canary.9143f03",
|
|
4
4
|
"displayName": "CLI",
|
|
5
5
|
"description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
@@ -101,10 +101,10 @@
|
|
|
101
101
|
"zod": "^4.4.3"
|
|
102
102
|
},
|
|
103
103
|
"peerDependencies": {
|
|
104
|
-
"@astryxdesign/charts": "0.6.4-canary.
|
|
105
|
-
"@astryxdesign/core": "0.6.4-canary.
|
|
106
|
-
"@astryxdesign/lab": "0.6.4-canary.
|
|
107
|
-
"@astryxdesign/theme-neutral": "0.6.4-canary.
|
|
104
|
+
"@astryxdesign/charts": "0.6.4-canary.9143f03",
|
|
105
|
+
"@astryxdesign/core": "0.6.4-canary.9143f03",
|
|
106
|
+
"@astryxdesign/lab": "0.6.4-canary.9143f03",
|
|
107
|
+
"@astryxdesign/theme-neutral": "0.6.4-canary.9143f03"
|
|
108
108
|
},
|
|
109
109
|
"peerDependenciesMeta": {
|
|
110
110
|
"@astryxdesign/charts": {
|
|
@@ -121,10 +121,10 @@
|
|
|
121
121
|
}
|
|
122
122
|
},
|
|
123
123
|
"devDependencies": {
|
|
124
|
-
"@astryxdesign/charts": "0.6.4-canary.
|
|
125
|
-
"@astryxdesign/core": "0.6.4-canary.
|
|
126
|
-
"@astryxdesign/lab": "0.6.4-canary.
|
|
127
|
-
"@astryxdesign/theme-neutral": "0.6.4-canary.
|
|
124
|
+
"@astryxdesign/charts": "0.6.4-canary.9143f03",
|
|
125
|
+
"@astryxdesign/core": "0.6.4-canary.9143f03",
|
|
126
|
+
"@astryxdesign/lab": "0.6.4-canary.9143f03",
|
|
127
|
+
"@astryxdesign/theme-neutral": "0.6.4-canary.9143f03",
|
|
128
128
|
"@heroicons/react": "^2.2.0",
|
|
129
129
|
"@stylexjs/stylex": "^0.19.0",
|
|
130
130
|
"@types/babel__core": "^7.20.5",
|