Skip to content

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

TypeScript

fix(cli): report theme template write failures with ERR_WRITE_FAILED (#6532)

Joey1ee19dafa86e
diff --git a/.changeset/cli-spec-theme-template-write-errors.md b/.changeset/cli-spec-theme-template-write-errors.md
new file mode 100644
index 0000000000..bb074f1f03
--- /dev/null
+++ b/.changeset/cli-spec-theme-template-write-errors.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx theme template` now reports a file it cannot write with the stable `ERR_WRITE_FAILED` code. Before, the `--json` error envelope carried the raw system error name, such as `EEXIST` or `EISDIR`, as its `code`.
+
+@josephfarina
diff --git a/packages/cli/api/theme/template/template.mjs b/packages/cli/api/theme/template/template.mjs
index b1fb08e692..3c7fb04e3d 100644
--- a/packages/cli/api/theme/template/template.mjs
+++ b/packages/cli/api/theme/template/template.mjs
@@ -62,8 +62,17 @@ export function themeTemplate(options = {}) {
// Our repo header has no business in someone else's source tree.
const contents = stripCopyrightHeader(fs.readFileSync(THEME_TEMPLATE_SRC, 'utf-8'));
- fs.mkdirSync(path.dirname(resolved), {recursive: true});
- fs.writeFileSync(resolved, contents);
+ try {
+ fs.mkdirSync(path.dirname(resolved), {recursive: true});
+ fs.writeFileSync(resolved, contents);
+ } catch (err) {
+ const message = err instanceof Error ? err.message : String(err);
+ throw new AstryxError(
+ `Failed to write the theme template to ${relative}: ${message}`,
+ undefined,
+ ERROR_CODES.ERR_WRITE_FAILED,
+ );
+ }
return {type: 'theme.template', data: {path: relative, written: true, reason: null}};
}
diff --git a/packages/cli/api/theme/template/template.test.mjs b/packages/cli/api/theme/template/template.test.mjs
index 70bb376ee9..267c30e3d7 100644
--- a/packages/cli/api/theme/template/template.test.mjs
+++ b/packages/cli/api/theme/template/template.test.mjs
@@ -14,6 +14,7 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import {themeTemplate, THEME_TEMPLATE_DEFAULT_PATH} from './template.mjs';
+import {isErrorCode} from '../../../foundation/response/error-codes.mjs';
let tmpDir;
beforeEach(() => {
@@ -79,4 +80,18 @@ describe('themeTemplate()', () => {
);
expect(fs.existsSync(path.join(path.dirname(tmpDir), 'escaped.ts'))).toBe(false);
});
+
+ it('reports a failed write with a registered error code', () => {
+ fs.writeFileSync(path.join(tmpDir, 'blocker'), '');
+
+ let error;
+ try {
+ themeTemplate({cwd: tmpDir, targetPath: 'blocker/theme.ts'});
+ } catch (caught) {
+ error = caught;
+ }
+
+ expect(error).toMatchObject({code: 'ERR_WRITE_FAILED'});
+ expect(isErrorCode(error.code)).toBe(true);
+ });
});
diff --git a/packages/cli/api/theme/themeTemplate.doc.mjs b/packages/cli/api/theme/themeTemplate.doc.mjs
index 71964b8d22..ac1f49d83b 100644
--- a/packages/cli/api/theme/themeTemplate.doc.mjs
+++ b/packages/cli/api/theme/themeTemplate.doc.mjs
@@ -51,7 +51,10 @@ export const doc = {
'Receipt: the path (relative to cwd), whether it was written, and the reason it was not. `exists` when a file was already there, which is a success, not a failure.',
},
],
- throws: [{code: 'ERR_PATH_TRAVERSAL', when: 'the target path escapes cwd'}],
+ throws: [
+ {code: 'ERR_PATH_TRAVERSAL', when: 'the target path escapes cwd'},
+ {code: 'ERR_WRITE_FAILED', when: 'the template file cannot be written'},
+ ],
examples: [
{label: 'Write it at the project root', code: 'themeTemplate();'},
{
diff --git a/packages/cli/clients/cli/commands/theme-template.behavior.test.mjs b/packages/cli/clients/cli/commands/theme-template.behavior.test.mjs
index fb58372e52..53c80d2d5c 100644
--- a/packages/cli/clients/cli/commands/theme-template.behavior.test.mjs
+++ b/packages/cli/clients/cli/commands/theme-template.behavior.test.mjs
@@ -82,4 +82,16 @@ describe('astryx theme template', () => {
expect(stderr).toMatch(/outside the project root/);
expect(fs.existsSync(path.join(path.dirname(tmpDir), 'escaped.ts'))).toBe(false);
});
+
+ it('fails a write with ERR_WRITE_FAILED under --json', async () => {
+ fs.writeFileSync(path.join(tmpDir, 'blocker'), '');
+
+ const {status, stdout} = await runCli(
+ ['--json', 'theme', 'template', 'blocker/theme.ts'],
+ {cwd: tmpDir},
+ );
+
+ expect(status).toBe(1);
+ expect(JSON.parse(stdout).code).toBe('ERR_WRITE_FAILED');
+ });
});
diff --git a/packages/cli/clients/cli/commands/theme-template.doc.mjs b/packages/cli/clients/cli/commands/theme-template.doc.mjs
index b7117b20c0..57189ffabd 100644
--- a/packages/cli/clients/cli/commands/theme-template.doc.mjs
+++ b/packages/cli/clients/cli/commands/theme-template.doc.mjs
@@ -36,7 +36,7 @@ export const doc = {
],
exitCodes: [
{code: 0, when: 'success, including when an existing file was left untouched'},
- {code: 1, when: 'the target path escapes the project'},
+ {code: 1, when: 'the target path escapes the project, or the file cannot be written'},
],
related: ['theme add', 'theme list', 'theme build'],
};