Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): check the tarball when pack lifecycle scripts print output (#6556)

Joey7683377dd13d
diff --git a/.changeset/cli-spec-pack-check-lifecycle-output.md b/.changeset/cli-spec-pack-check-lifecycle-output.md
new file mode 100644
index 0000000000..7b95c1ef78
--- /dev/null
+++ b/.changeset/cli-spec-pack-check-lifecycle-output.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx integration pack --check` now checks the tarball when a `prepack`, `prepare`, or `postpack` script prints to stdout. Before, any lifecycle output made the check fail with "npm pack produced unparseable JSON output" before it looked at the tarball. A failing lifecycle script still fails the check, and its output stays in the `pack_failed` message.
+
+@josephfarina
diff --git a/packages/cli/api/integration/pack-check.lifecycle-output.test.mjs b/packages/cli/api/integration/pack-check.lifecycle-output.test.mjs
new file mode 100644
index 0000000000..b121e61963
--- /dev/null
+++ b/packages/cli/api/integration/pack-check.lifecycle-output.test.mjs
@@ -0,0 +1,105 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {afterEach, beforeEach, describe, expect, it} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import {integrationPackCheck} from './pack-check.mjs';
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-pack-check-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+/** @param {Record<string, string>} scripts */
+function writeThemePackage(scripts) {
+ fs.writeFileSync(
+ path.join(tmpDir, 'package.json'),
+ `${JSON.stringify(
+ {
+ name: '@acme/widgets',
+ version: '1.0.0',
+ files: ['astryx.integration.mjs', 'themes'],
+ scripts,
+ },
+ null,
+ 2,
+ )}\n`,
+ );
+ fs.writeFileSync(
+ path.join(tmpDir, 'astryx.integration.mjs'),
+ "export default {themes: './themes'};\n",
+ );
+ const root = path.join(tmpDir, 'themes');
+ fs.mkdirSync(path.join(root, 'ocean'), {recursive: true});
+ fs.writeFileSync(
+ path.join(root, 'ocean', 'oceanTheme.ts'),
+ "import {defineTheme} from '@astryxdesign/core/theme';\n\nexport const oceanTheme = defineTheme({name: 'ocean'});\n",
+ );
+ fs.writeFileSync(
+ path.join(root, 'ocean', 'oceanTheme.doc.mjs'),
+ `/** @type {import('@astryxdesign/cli/authoring').ThemeDoc} */
+export default {type: 'theme', name: 'ocean', displayName: 'Ocean', description: 'Ocean theme.', maintained: true};
+`,
+ );
+}
+
+describe('integrationPackCheck with lifecycle script output', () => {
+ it('checks the tarball when lifecycle scripts print to stdout', async () => {
+ writeThemePackage({
+ prepack: 'node -e "console.log(\'building the package\')"',
+ prepare: 'node -e "console.log(\'preparing\')"',
+ postpack: 'node -e "console.log(\'packed\')"',
+ });
+
+ const result = await integrationPackCheck({cwd: tmpDir});
+
+ expect(result.data.issues).toEqual([]);
+ expect(result.data.packable).toBe(true);
+ expect(result.data.tarball?.filename).toBe('acme-widgets-1.0.0.tgz');
+ expect(result.data.contributions.packed).toEqual(
+ result.data.contributions.local,
+ );
+ });
+
+ it('still runs a chatty lifecycle script and compares what it packed', async () => {
+ const renameTheme = [
+ "console.log('renaming ocean to storm')",
+ "const fs=require('fs')",
+ "fs.renameSync('themes/ocean','themes/storm')",
+ "const p='themes/storm/oceanTheme.doc.mjs'",
+ "let x=fs.readFileSync(p,'utf8')",
+ "x=x.replace(/name: 'ocean'/, 'name: '+String.fromCharCode(39)+'storm'+String.fromCharCode(39))",
+ 'fs.writeFileSync(p,x)',
+ ].join(';');
+ writeThemePackage({prepack: `node -e "${renameTheme}"`});
+
+ const result = await integrationPackCheck({cwd: tmpDir});
+ const codes = result.data.issues.map(issue => issue.code);
+
+ expect(codes).not.toContain('pack_failed');
+ expect(codes).toContain('identity_not_packed');
+ expect(codes).toContain('packed_identity_unexpected');
+ expect(result.data.packable).toBe(false);
+ });
+
+ it('keeps a failing lifecycle script output in the pack_failed issue', async () => {
+ writeThemePackage({
+ prepack:
+ 'node -e "console.error(\'prepack exploded\'); process.exit(3)"',
+ });
+
+ const result = await integrationPackCheck({cwd: tmpDir});
+ const failure = result.data.issues.find(
+ issue => issue.code === 'pack_failed',
+ );
+
+ expect(result.data.packable).toBe(false);
+ expect(failure?.message).toContain('npm pack failed (exit 3)');
+ expect(failure?.message).toContain('prepack exploded');
+ });
+});
diff --git a/packages/cli/api/integration/pack-check.mjs b/packages/cli/api/integration/pack-check.mjs
index f8f8ddc8ae..d59e3f11c5 100644
--- a/packages/cli/api/integration/pack-check.mjs
+++ b/packages/cli/api/integration/pack-check.mjs
@@ -162,6 +162,22 @@ export function parseNpmPackOutput(output) {
};
}
+/**
+ * Summarize the JSON error npm prints on stdout when `--json` pack fails.
+ * @param {string} output
+ * @returns {string}
+ */
+function npmPackErrorDetail(output) {
+ try {
+ const error = JSON.parse(output)?.error;
+ return [error?.summary, error?.detail]
+ .filter(part => typeof part === 'string' && part.trim() !== '')
+ .join(': ');
+ } catch {
+ return (output || '').trim();
+ }
+}
+
/**
* Run `npm pack --json` with output directed to `destDir` so no preexisting
* tgz is overwritten. This intentionally runs the package lifecycle, matching
@@ -175,16 +191,25 @@ export function parseNpmPackOutput(output) {
function runNpmPack(packageDir, destDir) {
const result = spawnSync(
'npm',
- ['pack', '--json', '--silent', `--pack-destination=${destDir}`],
+ [
+ 'pack',
+ '--json',
+ '--silent',
+ // Lifecycle scripts still run; background mode keeps their output off
+ // the stdout that carries the JSON result.
+ '--foreground-scripts=false',
+ `--pack-destination=${destDir}`,
+ ],
{cwd: packageDir, encoding: 'utf-8', timeout: 60_000},
);
if (result.error) {
throw new Error(`Could not start npm pack: ${result.error.message}`);
}
if (result.status !== 0) {
- const stderr = (result.stderr || '').trim();
+ const detail =
+ (result.stderr || '').trim() || npmPackErrorDetail(result.stdout);
throw new Error(
- `npm pack failed (exit ${result.status})${stderr ? `: ${stderr}` : ''}.`,
+ `npm pack failed (exit ${result.status})${detail ? `: ${detail}` : ''}.`,
);
}
return parseNpmPackOutput(result.stdout);