Skip to content

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

TypeScript

fix(cli): a theme warning prints as one line without control characters (#10764)

Cindy Zhang16436bee4ef0
diff --git a/.changeset/cli-theme-warning-printable.md b/.changeset/cli-theme-warning-printable.md
new file mode 100644
index 0000000000..19a1dd2ca3
--- /dev/null
+++ b/.changeset/cli-theme-warning-printable.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx component` prints a theme warning as one line: control and format characters in the configured `astryx.theme` value or the generated theme record print as `�`, so a newline or an escape sequence in a checkout cannot rewrite the terminal.
+
+@bhamodi @cixzhang
diff --git a/packages/cli/clients/cli/lib/resolve-theme.mjs b/packages/cli/clients/cli/lib/resolve-theme.mjs
index f7dd5cb726..707b9fcf87 100644
--- a/packages/cli/clients/cli/lib/resolve-theme.mjs
+++ b/packages/cli/clients/cli/lib/resolve-theme.mjs
@@ -73,11 +73,22 @@ function extractTheme(mod) {
return null;
}
+/**
+ * Print a theme warning as one inert line. The configured theme value and the
+ * generated record come from the checkout, so a newline, an escape sequence or
+ * a bidi override in them must not rewrite the terminal. Control and format
+ * characters print as U+FFFD.
+ * @param {string} message
+ */
+function warnTheme(message) {
+ console.warn(message.replace(/[\p{Cc}\p{Cf}]/gu, '\uFFFD'));
+}
+
/** @param {unknown} mod @param {string} specifier */
function resolvedThemeData(mod, specifier) {
const theme = extractTheme(mod);
if (!theme) {
- console.warn(
+ warnTheme(
`⚠ theme: loaded "${specifier}" but could not find a theme object`,
);
return null;
@@ -100,7 +111,7 @@ export async function resolveTheme(cwd = process.cwd()) {
const loaded = await resolveRecordedTheme(cwd);
if (loaded.configured) {
if (!loaded.module) {
- console.warn(
+ warnTheme(
`⚠ theme: ${loaded.problem ?? `could not resolve the generated default theme (${loaded.specifier})`}`,
);
return null;
@@ -130,15 +141,13 @@ export async function resolveTheme(cwd = process.cwd()) {
if (specifier.startsWith('.') || specifier.startsWith('/')) {
mod = tryLoadLegacyModule(specifier, cwd);
if (!mod) {
- console.warn(
- `⚠ theme: could not resolve file "${specifier}" from ${cwd}`,
- );
+ warnTheme(`⚠ theme: could not resolve file "${specifier}" from ${cwd}`);
return null;
}
} else if (specifier.startsWith('@')) {
mod = tryLoadLegacyModule(specifier, cwd);
if (!mod) {
- console.warn(`⚠ theme: could not resolve package "${specifier}"`);
+ warnTheme(`⚠ theme: could not resolve package "${specifier}"`);
return null;
}
} else {
@@ -147,7 +156,7 @@ export async function resolveTheme(cwd = process.cwd()) {
tryLoadLegacyModule(conventional, cwd) ??
tryLoadLegacyModule(specifier, cwd);
if (!mod) {
- console.warn(
+ warnTheme(
`⚠ theme: could not resolve "${specifier}" (tried ${conventional} and ${specifier})`,
);
return null;
diff --git a/packages/cli/clients/cli/lib/resolve-theme.test.mjs b/packages/cli/clients/cli/lib/resolve-theme.test.mjs
index b4240ddc40..fa3482a298 100644
--- a/packages/cli/clients/cli/lib/resolve-theme.test.mjs
+++ b/packages/cli/clients/cli/lib/resolve-theme.test.mjs
@@ -280,6 +280,32 @@ describe('resolveTheme package field', () => {
});
});
+describe('resolveTheme warnings', () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it.each([
+ ['a file path', './\u001b[2Jmissing.cjs'],
+ ['a scoped package', '@evil/\u001b]0;owned\u0007\npkg'],
+ ['a bare name', 'theme\u202e\r\nnext'],
+ ])(
+ 'prints %s from astryx.theme as one line without control or format characters',
+ async (_kind, specifier) => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+
+ expect(
+ await resolveTheme(fixture({name: 'app', astryx: {theme: specifier}})),
+ ).toBeNull();
+
+ expect(warn).toHaveBeenCalledTimes(1);
+ const message = String(warn.mock.calls[0][0]);
+ expect(message).not.toMatch(/[\p{Cc}\p{Cf}]/u);
+ expect(message).toContain('\uFFFD');
+ },
+ );
+});
+
describe('resolveTheme source precedence', () => {
it('uses the generated record default and ignores astryx.theme when a module exists', async () => {
const dir = fixture({