An open source design system that's fully customizable and agent ready
TypeScript
fix(lab,charts): portable build scripts + a guard so they stay portable (#3917)
AK4dd9f2774fee
diff --git a/package.json b/package.jsonindex 755901490d..25538437c8 100644--- a/package.json+++ b/package.json@@ -11,8 +11,9 @@"test:watch": "vitest","test:coverage": "vitest --coverage","check:package-boundaries": "node scripts/check-package-boundaries.js",- "check:repo": "pnpm check:sync && pnpm check:package-boundaries && pnpm check:changesets && pnpm check:demo-media && pnpm check:executable-bits && pnpm check:cli-structure && pnpm check:use-client",+ "check:repo": "pnpm check:sync && pnpm check:package-boundaries && pnpm check:changesets && pnpm check:demo-media && pnpm check:executable-bits && pnpm check:cli-structure && pnpm check:use-client && pnpm check:portable-scripts","check:changesets": "node scripts/check-changesets.mjs",+ "check:portable-scripts": "node scripts/check-portable-scripts.mjs","check:cli-structure": "node scripts/check-cli-structure.mjs","check:executable-bits": "node scripts/check-executable-bits.mjs","check:demo-media": "node scripts/check-demo-media.mjs",diff --git a/packages/charts/package.json b/packages/charts/package.jsonindex 0141132acc..06298d1e31 100644--- a/packages/charts/package.json+++ b/packages/charts/package.json@@ -46,7 +46,7 @@},"scripts": {"prepack": "pnpm build",- "build": "rm -rf dist && pnpm build:esm && tsc --project tsconfig.build.json && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs",+ "build": "rimraf dist && pnpm build:esm && tsc --project tsconfig.build.json && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs","build:esm": "babel src --out-dir dist --extensions .ts,.tsx --out-file-extension .js --ignore \"**/*.test.ts,**/*.test.tsx,**/__tests__/**,**/*.perf.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css,**/*.css.d.ts\" --config-file ./babel.config.js","build:css": "node ../../scripts/build-css.mjs --package charts","dev": "babel src --watch --out-dir dist --extensions .ts,.tsx --out-file-extension .js --ignore \"**/*.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css*\" --config-file ./babel.config.js",@@ -70,6 +70,7 @@"devDependencies": {"@astryxdesign/cli": "workspace:*","@types/d3-scale": "^4.0.9",- "@types/d3-shape": "^3.1.8"+ "@types/d3-shape": "^3.1.8",+ "rimraf": "^6.0.1"}}diff --git a/packages/lab/package.json b/packages/lab/package.jsonindex ce276be299..d2a4c3f300 100644--- a/packages/lab/package.json+++ b/packages/lab/package.json@@ -46,7 +46,7 @@},"scripts": {"prepack": "pnpm build",- "build": "rm -rf dist && pnpm build:esm && tsc --project tsconfig.build.json && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs",+ "build": "rimraf dist && pnpm build:esm && tsc --project tsconfig.build.json && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs","build:esm": "babel src --out-dir dist --extensions .ts,.tsx --out-file-extension .js --ignore \"**/*.test.ts,**/*.test.tsx,**/__tests__/**,**/*.perf.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css,**/*.css.d.ts\" --config-file ./babel.config.js","build:css": "node ../../scripts/build-css.mjs --package lab","dev": "babel src --watch --out-dir dist --extensions .ts,.tsx --out-file-extension .js --ignore \"**/*.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css*\" --config-file ./babel.config.js",@@ -71,6 +71,7 @@"@astryxdesign/cli": "workspace:*","@types/d3-array": "^3.2.2","@types/d3-scale": "^4.0.9",- "@types/d3-shape": "^3.1.8"+ "@types/d3-shape": "^3.1.8",+ "rimraf": "^6.0.1"}}diff --git a/packages/richtext/package.json b/packages/richtext/package.jsonindex 1a97aa01a0..c7aaee4d84 100644--- a/packages/richtext/package.json+++ b/packages/richtext/package.json@@ -24,7 +24,7 @@],"astryx": {"canaryOnly": true,- "_comment": "Published ONLY to the @canary dist-tag (see .github/workflows/release.yml). Never released as a stable `latest` release. `private: true` is intentional and must stay — it is npm's hard guarantee against an accidental stable publish."+ "_comment": "Published ONLY to the @canary dist-tag (see .github/workflows/release.yml). Never released as a stable `latest` release. `private: true` is intentional and must stay — it is npm\"s hard guarantee against an accidental stable publish."},"main": "./dist/index.js","module": "./dist/index.js",@@ -48,10 +48,10 @@},"scripts": {"prepack": "pnpm build",- "build": "rm -rf dist && pnpm build:esm && tsc --project tsconfig.build.json && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs",- "build:esm": "babel src --out-dir dist --extensions '.ts,.tsx' --out-file-extension '.js' --ignore '**/*.test.ts,**/*.test.tsx,**/__tests__/**,**/*.perf.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css,**/*.css.d.ts' --config-file ./babel.config.js",+ "build": "rimraf dist && pnpm build:esm && tsc --project tsconfig.build.json && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs",+ "build:esm": "babel src --out-dir dist --extensions \".ts,.tsx\" --out-file-extension \".js\" --ignore \"**/*.test.ts,**/*.test.tsx,**/__tests__/**,**/*.perf.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css,**/*.css.d.ts\" --config-file ./babel.config.js","build:css": "node ../../scripts/build-css.mjs --package richtext",- "dev": "babel src --watch --out-dir dist --extensions '.ts,.tsx' --out-file-extension '.js' --ignore '**/*.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css*' --config-file ./babel.config.js",+ "dev": "babel src --watch --out-dir dist --extensions \".ts,.tsx\" --out-file-extension \".js\" --ignore \"**/*.test.*,**/*.doc.mjs,**/*.d.ts,**/*.css*\" --config-file ./babel.config.js","test": "vitest run --root ../..","test:watch": "vitest --root ../..","lint": "eslint src",@@ -122,6 +122,7 @@"@lexical/rich-text": "^0.46.0","@lexical/selection": "^0.46.0","@lexical/utils": "^0.46.0",- "lexical": "^0.46.0"+ "lexical": "^0.46.0",+ "rimraf": "^6.0.1"}}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex c749a5d3fd..79fd84fb95 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -595,6 +595,9 @@ importers:'@types/d3-shape':specifier: ^3.1.8version: 3.1.8+ rimraf:+ specifier: ^6.0.1+ version: 6.1.3packages/cli:dependencies:@@ -709,6 +712,9 @@ importers:'@types/d3-shape':specifier: ^3.1.8version: 3.1.8+ rimraf:+ specifier: ^6.0.1+ version: 6.1.3packages/richtext:dependencies:@@ -761,6 +767,9 @@ importers:lexical:specifier: ^0.46.0version: 0.46.0([email protected])+ rimraf:+ specifier: ^6.0.1+ version: 6.1.3packages/themes/butter:dependencies:diff --git a/scripts/check-portable-scripts.mjs b/scripts/check-portable-scripts.mjsnew file mode 100644index 0000000000..1c9cb1e137--- /dev/null+++ b/scripts/check-portable-scripts.mjs@@ -0,0 +1,149 @@+#!/usr/bin/env node+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * Repo check: package.json `scripts` must run on Windows (cmd.exe), not only+ * on a POSIX shell.+ *+ * Two Windows-hostile idioms keep coming back. Both were fixed in+ * packages/core by #3305 (bbd7a59), then copy-pasted verbatim into the newer+ * packages/lab and packages/charts, which regressed both (#3637):+ *+ * 1. `rm -rf dist` — cmd.exe has no `rm`, so the build dies at the first+ * step. Use `rimraf`, the ecosystem's cross-platform recursive delete.+ * 2. Single-quoted args (`--extensions '.ts,.tsx'`) — cmd.exe does not strip+ * single quotes, so babel receives them as LITERAL characters, matches+ * zero files, exits 0, and leaves dist/ empty. Use double quotes, which+ * sh and cmd.exe both strip.+ *+ * Idiom 2 is why this is a check and not a code comment: it fails *silently*,+ * publishing an empty package off a green build.+ *+ * A third rule covers the half-fix: a script may only call `rimraf` if its own+ * package declares it. pnpm does not hoist, so an undeclared `rimraf` is a+ * "command not found" build break on every platform.+ *+ * Not checked: inline env assignment (`FOO=1 cmd`), which cmd.exe also+ * rejects. Root-only dev scripts (`lint:strict`, `dev:sandbox`) use it and+ * removing it needs a cross-env dependency — a separate change from the build+ * regression this guards.+ *+ * Usage: node scripts/check-portable-scripts.mjs+ */+import {execFileSync} from 'node:child_process';+import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';++const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');++/** POSIX-only binaries with no cmd.exe equivalent. */+const POSIX_ONLY = ['rm', 'cp', 'mv', 'mkdir -p', 'touch', 'cat', 'ln -s'];++/** Split a script body on the shell separators (&&, ||, |, ;) into commands. */+function commandsOf(script) {+ return script+ .split(/&&|\|\||[|;]/)+ .map(part => part.trim())+ .filter(Boolean);+}++/** True when `command` invokes `binary` — `rimraf dist` is not an `rm`. */+function invokes(command, binary) {+ return command === binary || command.startsWith(`${binary} `);+}++/**+ * Strip double-quoted spans. Single quotes *inside* a double-quoted arg reach+ * the program intact on both shells (`node -e "console.log('x')"`), so only+ * the quotes still standing after this are the portability bug.+ */+function withoutDoubleQuotedSpans(script) {+ return script.replace(/"[^"]*"/g, '');+}++/**+ * Windows portability offences in one package's `scripts` block.+ *+ * @param {Record<string, string>} [scripts] the package's `scripts`+ * @param {Record<string, string>} [deps] its declared deps, of any kind+ * @returns {{name: string, detail: string}[]}+ */+export function findOffences(scripts = {}, deps = {}) {+ const offences = [];++ for (const [name, script] of Object.entries(scripts)) {+ for (const command of commandsOf(script)) {+ const binary = POSIX_ONLY.find(posix => invokes(command, posix));+ if (binary) {+ offences.push({+ name,+ detail: `POSIX-only command \`${binary}\` — cmd.exe has no such binary; use rimraf or a node script`,+ });+ }+ if (invokes(command, 'rimraf') && !('rimraf' in deps)) {+ offences.push({+ name,+ detail: 'calls `rimraf` without declaring it as a devDependency',+ });+ }+ }++ if (/'[^']*'/.test(withoutDoubleQuotedSpans(script))) {+ offences.push({+ name,+ detail:+ 'single-quoted arg — cmd.exe passes the quotes through literally, so the glob matches nothing and the step silently no-ops',+ });+ }+ }++ return offences;+}++/** Every tracked package.json, excluding installed ones. */+export function trackedPackageJsonFiles() {+ return execFileSync('git', ['ls-files', '--', '*package.json'], {+ cwd: ROOT,+ encoding: 'utf-8',+ })+ .split('\n')+ .filter(Boolean)+ .filter(file => !file.includes('node_modules/'));+}++function main() {+ const errors = [];+ let checked = 0;++ // Every *tracked* package.json is scanned, so a brand-new package cannot+ // reintroduce either idiom without tripping this check.+ for (const file of trackedPackageJsonFiles()) {+ const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, file), 'utf-8'));+ checked += Object.keys(pkg.scripts ?? {}).length;++ const deps = {...pkg.dependencies, ...pkg.devDependencies};+ for (const offence of findOffences(pkg.scripts, deps)) {+ errors.push({file, ...offence});+ }+ }++ if (errors.length > 0) {+ console.error('❌ package.json scripts that break on Windows:\n');+ for (const {file, name, detail} of errors) {+ console.error(` ${file} → scripts.${name}`);+ console.error(` ${detail}`);+ }+ console.error(+ `\n${errors.length} error(s). Fix: replace \`rm -rf\` with \`rimraf\`, and single quotes with double quotes.`,+ );+ process.exit(1);+ }++ console.log(`✅ ${checked} package.json script(s) checked — all portable.`);+}++// Run as a script, but stay importable for unit tests.+if (process.argv[1] === fileURLToPath(import.meta.url)) {+ main();+}diff --git a/scripts/check-portable-scripts.test.mjs b/scripts/check-portable-scripts.test.mjsnew file mode 100644index 0000000000..5cdc3990ba--- /dev/null+++ b/scripts/check-portable-scripts.test.mjs@@ -0,0 +1,159 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file check-portable-scripts.test.mjs+ * Unit tests for the Windows-portability guard, plus the #3637 regression it+ * was written for.+ *+ * The detection rules are exercised against fixtures, not just the repo: a+ * guard that scans the wrong thing passes exactly like one that works. The+ * last block then runs those rules over the real tracked package.json files,+ * so the rules and the data they are pointed at are both covered.+ */++import {describe, it, expect} from 'vitest';+import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {execFileSync} from 'node:child_process';+import {+ findOffences,+ trackedPackageJsonFiles,+} from './check-portable-scripts.mjs';++const __dirname = path.dirname(fileURLToPath(import.meta.url));+const ROOT = path.resolve(__dirname, '..');++const details = offences => offences.map(o => o.detail);++describe('findOffences — POSIX-only commands', () => {+ it('flags `rm -rf dist`, which cmd.exe cannot run', () => {+ expect(findOffences({build: 'rm -rf dist'})).toEqual([+ {name: 'build', detail: expect.stringContaining('POSIX-only command')},+ ]);+ });++ it('flags a POSIX command anywhere in the chain, not just at the head', () => {+ expect(findOffences({build: 'tsc && rm -rf tmp'})).toHaveLength(1);+ expect(findOffences({clean: 'node a.mjs; cp x y'})).toHaveLength(1);+ });++ it('does not flag a command that merely starts with those letters', () => {+ // `rimraf` — the prescribed fix — starts with "rm"'s letters, and+ // `concat` with "cat"'s.+ expect(+ findOffences({build: 'rimraf dist', gen: 'concat a b'}, {rimraf: '^6'}),+ ).toEqual([]);+ });++ it('allows `mkdir` (cmd.exe has it) but not `mkdir -p` (it does not)', () => {+ expect(findOffences({a: 'mkdir dist'})).toEqual([]);+ expect(findOffences({a: 'mkdir -p dist/nested'})).toHaveLength(1);+ });+});++describe('findOffences — single-quoted args', () => {+ it('flags the babel glob that silently matched nothing on cmd.exe', () => {+ const offences = findOffences({+ 'build:esm': "babel src --extensions '.ts,.tsx'",+ });+ expect(details(offences)).toEqual([+ expect.stringContaining('single-quoted arg'),+ ]);+ });++ it('accepts the double-quoted form both shells strip', () => {+ expect(+ findOffences({'build:esm': 'babel src --ignore "**/*.test.ts"'}),+ ).toEqual([]);+ });++ it('accepts single quotes nested inside a double-quoted arg', () => {+ // cmd.exe hands `console.log('x')` to node intact — not the bug.+ expect(findOffences({probe: `node -e "console.log('x')"`})).toEqual([]);+ });++ it('reports one offence per script, however many quoted args it has', () => {+ const offences = findOffences({+ dev: "babel src --extensions '.ts,.tsx' --out-file-extension '.js'",+ });+ expect(offences).toHaveLength(1);+ });+});++describe('findOffences — rimraf must be declared', () => {+ it('flags a script that calls rimraf without the devDependency', () => {+ expect(details(findOffences({build: 'rimraf dist'}))).toEqual([+ 'calls `rimraf` without declaring it as a devDependency',+ ]);+ });++ it('accepts it once declared', () => {+ expect(findOffences({build: 'rimraf dist'}, {rimraf: '^6.0.1'})).toEqual(+ [],+ );+ });+});++describe('findOffences — degenerate input', () => {+ it('returns nothing for a package with no scripts block', () => {+ expect(findOffences(undefined, undefined)).toEqual([]);+ expect(findOffences({})).toEqual([]);+ });++ it('reports every offence in a script that has more than one', () => {+ const offences = findOffences({build: "rm -rf dist && babel src -x '.ts'"});+ expect(offences).toHaveLength(2);+ });+});++describe('the repo itself (#3637)', () => {+ const read = file =>+ JSON.parse(fs.readFileSync(path.join(ROOT, file), 'utf-8'));++ it('scans every tracked package.json, not a hard-coded list', () => {+ const files = trackedPackageJsonFiles();+ expect(files).toContain('packages/lab/package.json');+ expect(files).toContain('packages/charts/package.json');+ expect(files.every(file => !file.includes('node_modules/'))).toBe(true);+ });++ it.each(['packages/lab', 'packages/charts'])(+ '%s builds with the same portable invocation as packages/core',+ dir => {+ const {scripts, devDependencies} = read(`${dir}/package.json`);+ const core = read('packages/core/package.json');++ expect(scripts.build.startsWith('rimraf dist &&')).toBe(true);+ expect(devDependencies.rimraf).toBe(core.devDependencies.rimraf);+ // Same flags, same quoting as core — only the babel config file differs+ // (lab/charts author theirs in .js, core in .json).+ for (const name of ['build:esm', 'dev']) {+ expect(scripts[name].replace('./babel.config.js', 'CONFIG')).toBe(+ core.scripts[name].replace('./babel.config.json', 'CONFIG'),+ );+ }+ },+ );++ it('has no Windows-hostile script in any tracked package.json', () => {+ const offenders = trackedPackageJsonFiles().flatMap(file => {+ const pkg = read(file);+ const deps = {...pkg.dependencies, ...pkg.devDependencies};+ return findOffences(pkg.scripts, deps).map(+ ({name, detail}) => `${file} → scripts.${name}: ${detail}`,+ );+ });++ expect(offenders).toEqual([]);+ });++ it('exits 0 when run the way CI runs it', () => {+ expect(() =>+ execFileSync('node', ['scripts/check-portable-scripts.mjs'], {+ cwd: ROOT,+ encoding: 'utf-8',+ }),+ ).not.toThrow();+ });+});