An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): copy binary theme files byte for byte in theme add (#6530)
Joeydc6a8fd3f5b9
diff --git a/.changeset/cli-spec-theme-add-binary-files.md b/.changeset/cli-spec-theme-add-binary-files.mdnew file mode 100644index 0000000000..bb69601a1e--- /dev/null+++ b/.changeset/cli-spec-theme-add-binary-files.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx theme add` now copies every file a theme catalog lists byte for byte, so a theme that ships a font or an image arrives intact. Before, it decoded each file as text, which corrupted any file that was not UTF-8.++@josephfarinadiff --git a/packages/cli/api/theme/add/add.binary.test.mjs b/packages/cli/api/theme/add/add.binary.test.mjsnew file mode 100644index 0000000000..032063b580--- /dev/null+++ b/packages/cli/api/theme/add/add.binary.test.mjs@@ -0,0 +1,91 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {afterEach, beforeEach, describe, expect, it} from 'vitest';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import {themeAdd} from './add.mjs';++const HEADER = '// Copyright (c) Meta Platforms, Inc. and affiliates.\n\n';++let tmpDir;++/** Every byte value, so any lossy text decoding shows up as a changed file. */+const FONT_BYTES = Buffer.concat([+ Buffer.from('wOF2'),+ Buffer.from(Array.from({length: 256}, (_, byte) => byte)),+]);++beforeEach(() => {+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-themeadd-binary-'));+ fs.writeFileSync(+ path.join(tmpDir, 'package.json'),+ JSON.stringify({+ name: 'consumer',+ dependencies: {'@acme/themes': '^1.0.0'},+ }),+ );+ const packageDir = path.join(tmpDir, 'node_modules', '@acme', 'themes');+ const themeDir = path.join(packageDir, 'themes', 'ocean');+ fs.mkdirSync(path.join(themeDir, 'fonts'), {recursive: true});+ fs.writeFileSync(+ path.join(packageDir, 'package.json'),+ JSON.stringify({name: '@acme/themes', version: '1.0.0'}),+ );+ fs.writeFileSync(+ path.join(packageDir, 'astryx.integration.mjs'),+ "export default {themes: './themes'};\n",+ );+ fs.writeFileSync(+ path.join(packageDir, 'themes', 'manifest.json'),+ JSON.stringify({+ version: 1,+ themes: [+ {+ slug: 'ocean',+ displayName: 'Ocean',+ description: 'Integration theme that ships a font.',+ maintained: true,+ entry: 'oceanTheme.ts',+ exportName: 'oceanTheme',+ files: ['oceanTheme.ts', 'fonts/ocean.woff2'],+ },+ ],+ }),+ );+ fs.writeFileSync(+ path.join(themeDir, 'oceanTheme.ts'),+ `${HEADER}export const oceanTheme = {};\n`,+ );+ fs.writeFileSync(path.join(themeDir, 'fonts', 'ocean.woff2'), FONT_BYTES);+});++afterEach(() => {+ fs.rmSync(tmpDir, {recursive: true, force: true});+});++describe('themeAdd copies every listed file', () => {+ it('copies a binary file byte for byte', async () => {+ const result = await themeAdd('ocean', {+ cwd: tmpDir,+ package: '@acme/themes',+ });++ expect(result.data.files).toEqual(['oceanTheme.ts', 'fonts/ocean.woff2']);+ const copied = fs.readFileSync(+ path.join(tmpDir, 'src', 'themes', 'ocean', 'fonts', 'ocean.woff2'),+ );+ expect(copied.equals(FONT_BYTES)).toBe(true);+ });++ it('still strips the repo header from a text source', async () => {+ await themeAdd('ocean', {cwd: tmpDir, package: '@acme/themes'});++ expect(+ fs.readFileSync(+ path.join(tmpDir, 'src', 'themes', 'ocean', 'oceanTheme.ts'),+ 'utf-8',+ ),+ ).toBe('export const oceanTheme = {};\n');+ });+});diff --git a/packages/cli/api/theme/add/add.mjs b/packages/cli/api/theme/add/add.mjsindex cfc1028927..a04e6d17be 100644--- a/packages/cli/api/theme/add/add.mjs+++ b/packages/cli/api/theme/add/add.mjs@@ -26,6 +26,20 @@ function defaultTargetDir(slug) {return path.join('src', 'themes', slug);}+/**+ * The bytes to write for one listed file. Only UTF-8 text loses our header;+ * anything else (a font, an image) is copied verbatim, since decoding it would+ * corrupt it.+ * @param {Buffer} bytes+ * @returns {Buffer | string}+ */+function scaffoldContents(bytes) {+ const text = bytes.toString('utf-8');+ return Buffer.from(text, 'utf-8').equals(bytes)+ ? stripCopyrightHeader(text)+ : bytes;+}+/*** Copy a bundled theme's files into the consumer's project (defaults to* `src/themes/<slug>/`). Writes are staged to temp files then renamed, rolling@@ -135,8 +149,7 @@ export async function themeAdd(slug, options = {}) {});fs.mkdirSync(path.dirname(dest), {recursive: true});const tmp = `${dest}.${process.pid}.tmp`;- const contents = stripCopyrightHeader(fs.readFileSync(w.src, 'utf-8'));- fs.writeFileSync(tmp, contents);+ fs.writeFileSync(tmp, scaffoldContents(fs.readFileSync(w.src)));staged.push({tmp, dest});}for (const s of staged) {