@astryxdesign/cli 0.6.5-canary.24fe2cd → 0.6.5-canary.27a5079

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,6 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
- /**
5
- * Why `theme build` has no Core to generate with, and the fix for where it
6
- * ran: the Astryx repository builds Core, an app installs it, and an installed
7
- * Core that will not load is named.
8
- *
9
- * @param {string} cwd
10
- * @param {any} importError - Why the import failed, when it threw.
11
- * @returns {string}
12
- */
13
- export function coreUnavailableMessage(cwd: string, importError: any): string;
14
4
  /**
15
5
  * Import specifier for install/scaffold instructions. Drops a leading `src/`
16
6
  * from the cwd-relative dir (most consumers import from a file under src/) but
@@ -116,17 +106,3 @@ export function themeBuildFamily(files: string[], options: {
116
106
  * validateAdaptationEnrollment)
117
107
  */
118
108
  export type ComponentValueClass = "builtin" | "enrollment" | "unresolved";
119
- /**
120
- * The bindings one Core provides: the namespaces interception spreads, the
121
- * functions the build calls, and why the import failed, if it did.
122
- */
123
- export type CoreBindings = {
124
- themeModule: any;
125
- rootModule: any;
126
- importError: any;
127
- defineTheme: any;
128
- generateThemeRulesSplit: any;
129
- generateOnMediaCSS: any;
130
- generateAdaptationCSS: any;
131
- dataTokenDefaults: any;
132
- };
@@ -45,11 +45,7 @@ import {fileURLToPath, pathToFileURL} from 'node:url';
45
45
  import {parse} from '@babel/parser';
46
46
  import {createJiti} from 'jiti';
47
47
  import {getCliInvocation} from '../../../foundation/env/package-manager.mjs';
48
- import {
49
- CLI_ROOT,
50
- findCoreDir,
51
- findInstalledPackage,
52
- } from '../../../foundation/fs/paths.mjs';
48
+ import {CLI_ROOT, findCoreDir} from '../../../foundation/fs/paths.mjs';
53
49
  import {
54
50
  assertWithin,
55
51
  sanitizeName,
@@ -129,232 +125,6 @@ try {
129
125
  _coreImportError = e;
130
126
  }
131
127
 
132
- /**
133
- * The bindings one Core provides: the namespaces interception spreads, the
134
- * functions the build calls, and why the import failed, if it did.
135
- * @typedef {{
136
- * themeModule: any,
137
- * rootModule: any,
138
- * importError: any,
139
- * defineTheme: any,
140
- * generateThemeRulesSplit: any,
141
- * generateOnMediaCSS: any,
142
- * generateAdaptationCSS: any,
143
- * dataTokenDefaults: any,
144
- * }} CoreBindings
145
- */
146
-
147
- /**
148
- * The bindings exactly as the CLI's own import left them, kept so each build
149
- * can choose between them and the project's installed Core.
150
- * @type {CoreBindings}
151
- */
152
- const _cliCore = {
153
- themeModule: _coreThemeModule,
154
- rootModule: _coreRootModule,
155
- importError: _coreImportError,
156
- defineTheme: _defineTheme,
157
- generateThemeRulesSplit: _generateThemeRulesSplit,
158
- generateOnMediaCSS: _generateOnMediaCSS,
159
- generateAdaptationCSS: _generateAdaptationCSS,
160
- dataTokenDefaults: _dataTokenDefaults,
161
- };
162
-
163
- /**
164
- * Point the build at one Core: the module-level bindings every step reads.
165
- * @param {CoreBindings} core
166
- */
167
- function setCore(core) {
168
- _coreThemeModule = core.themeModule;
169
- _coreRootModule = core.rootModule;
170
- _coreImportError = core.importError;
171
- _defineTheme = core.defineTheme;
172
- _generateThemeRulesSplit = core.generateThemeRulesSplit;
173
- _generateOnMediaCSS = core.generateOnMediaCSS;
174
- _generateAdaptationCSS = core.generateAdaptationCSS;
175
- _dataTokenDefaults = core.dataTokenDefaults;
176
- }
177
-
178
- /**
179
- * The file an `import` of `subpath` reaches through a package's `exports` map,
180
- * or null when the map has no entry for it. Conditions are taken in the map's
181
- * own order, as Node does, accepting the ones an import matches here: `node`,
182
- * `import`, and `default` (so `source` and `types` are skipped).
183
- *
184
- * @param {string} dir - The package directory.
185
- * @param {string} subpath - `'.'` or a subpath such as `'./theme'`.
186
- * @returns {string|null}
187
- */
188
- function importTarget(dir, subpath) {
189
- let pkg;
190
- try {
191
- pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
192
- } catch {
193
- return null;
194
- }
195
- const map = pkg?.exports;
196
- const bySubpath =
197
- map &&
198
- typeof map === 'object' &&
199
- !Array.isArray(map) &&
200
- Object.keys(map).some(key => key.startsWith('.'));
201
- const entry = bySubpath ? map[subpath] : subpath === '.' ? map : undefined;
202
- /** @param {any} value @returns {string|null} */
203
- const pick = value => {
204
- if (typeof value === 'string') return value;
205
- if (!value || typeof value !== 'object' || Array.isArray(value))
206
- return null;
207
- for (const [condition, target] of Object.entries(value)) {
208
- if (
209
- condition !== 'node' &&
210
- condition !== 'import' &&
211
- condition !== 'default'
212
- )
213
- continue;
214
- const picked = pick(target);
215
- if (picked) return picked;
216
- }
217
- return null;
218
- };
219
- const target = pick(entry);
220
- return target ? path.resolve(dir, target) : null;
221
- }
222
-
223
- /** @param {string} file @returns {string} its real path, or itself */
224
- function realOrSelf(file) {
225
- try {
226
- return fs.realpathSync(file);
227
- } catch {
228
- return file;
229
- }
230
- }
231
-
232
- /**
233
- * The Core the project at `cwd` installed, when it differs from the one the
234
- * CLI's own import found and loads. The app's `<Theme>` runs on the app's
235
- * Core, so building with it keeps the CSS identical; and a CLI run one-off
236
- * (`npx @astryxdesign/cli`) has no Core beside it at all. Null when there is
237
- * nothing better than the CLI's own import: no installed Core (the Astryx
238
- * repository, a bare directory), the same Core, or one that does not load.
239
- *
240
- * @param {string} cwd
241
- * @returns {Promise<CoreBindings | null>}
242
- */
243
- async function loadProjectCore(cwd) {
244
- const dir = findInstalledPackage(cwd, '@astryxdesign/core');
245
- const themeFile = dir ? importTarget(dir, './theme') : null;
246
- if (!dir || !themeFile) return null;
247
- if (_cliCore.themeModule) {
248
- try {
249
- const own = fileURLToPath(
250
- import.meta.resolve('@astryxdesign/core/theme'),
251
- );
252
- if (realOrSelf(own) === realOrSelf(themeFile)) return null;
253
- } catch {
254
- // The CLI's own Core cannot be located; load the project's.
255
- }
256
- }
257
- let themeModule;
258
- try {
259
- themeModule = await import(pathToFileURL(themeFile).href);
260
- } catch {
261
- return null;
262
- }
263
- if (!themeModule.defineTheme || !themeModule.generateThemeRulesSplit)
264
- return null;
265
- let rootModule = null;
266
- const rootFile = importTarget(dir, '.');
267
- if (rootFile) {
268
- try {
269
- rootModule = await import(pathToFileURL(rootFile).href);
270
- } catch {
271
- // As with the CLI's own import, the theme namespace stands in for it.
272
- }
273
- }
274
- return {
275
- themeModule,
276
- rootModule,
277
- importError: null,
278
- defineTheme: themeModule.defineTheme,
279
- generateThemeRulesSplit: themeModule.generateThemeRulesSplit,
280
- generateOnMediaCSS: themeModule.generateOnMediaCSS,
281
- generateAdaptationCSS: themeModule.generateAdaptationCSS,
282
- dataTokenDefaults: themeModule.dataTokenDefaults,
283
- };
284
- }
285
-
286
- /** @type {Map<string, Awaited<ReturnType<typeof loadProjectCore>>>} */
287
- const _projectCores = new Map();
288
-
289
- /**
290
- * Choose the Core this build generates with: the project's installed one when
291
- * {@link loadProjectCore} finds it, else the CLI's own.
292
- * @param {string} cwd
293
- */
294
- async function selectCore(cwd) {
295
- if (!_projectCores.has(cwd))
296
- _projectCores.set(cwd, await loadProjectCore(cwd));
297
- setCore(_projectCores.get(cwd) ?? _cliCore);
298
- }
299
-
300
- /**
301
- * Whether `cwd` sits in the Astryx repository, where Core is built from
302
- * source rather than installed.
303
- * @param {string} cwd
304
- */
305
- function inAstryxRepository(cwd) {
306
- let dir = cwd;
307
- for (let i = 0; i < 6; i++) {
308
- try {
309
- const pkg = JSON.parse(
310
- fs.readFileSync(
311
- path.join(dir, 'packages', 'core', 'package.json'),
312
- 'utf-8',
313
- ),
314
- );
315
- if (pkg?.name === '@astryxdesign/core') return true;
316
- } catch {
317
- // Not here; keep walking up.
318
- }
319
- const parent = path.dirname(dir);
320
- if (parent === dir) break;
321
- dir = parent;
322
- }
323
- return false;
324
- }
325
-
326
- /**
327
- * Why `theme build` has no Core to generate with, and the fix for where it
328
- * ran: the Astryx repository builds Core, an app installs it, and an installed
329
- * Core that will not load is named.
330
- *
331
- * @param {string} cwd
332
- * @param {any} importError - Why the import failed, when it threw.
333
- * @returns {string}
334
- */
335
- export function coreUnavailableMessage(cwd, importError) {
336
- const why =
337
- '`astryx theme build` generates CSS with @astryxdesign/core, the same code the runtime <Theme> uses';
338
- const detail = importError ? `\n Import error: ${importError.message}` : '';
339
- if (inAstryxRepository(cwd)) {
340
- return (
341
- `Could not load @astryxdesign/core/theme: ${why}. Build @astryxdesign/core ` +
342
- `first (e.g. \`pnpm -F @astryxdesign/core build\`).${detail}`
343
- );
344
- }
345
- const installed = findInstalledPackage(cwd, '@astryxdesign/core');
346
- if (installed) {
347
- return (
348
- `Could not load the @astryxdesign/core installed at ` +
349
- `${path.relative(cwd, installed) || '.'}: ${why}. Reinstall it.${detail}`
350
- );
351
- }
352
- return (
353
- `This project does not have @astryxdesign/core installed: ${why}. Install ` +
354
- `it: \`npm install @astryxdesign/core\` (or yarn/pnpm/bun).${detail}`
355
- );
356
- }
357
-
358
128
  /**
359
129
  * Read a package's `version` from a resolved directory. Returns `'unknown'`
360
130
  * when it can't be read so the header always has a value.
@@ -2337,8 +2107,6 @@ async function themeBuildInternal(
2337
2107
 
2338
2108
  logger.log(`\nBuilding theme from ${path.relative(cwd, filePath)}...`);
2339
2109
 
2340
- await selectCore(cwd);
2341
-
2342
2110
  // Standalone builds only need interception when an older Core could erase
2343
2111
  // adaptations. Family preparation always supplies the same recorder as its
2344
2112
  // shared loader so exact authored parent identity stays CLI-private instead
@@ -2465,7 +2233,13 @@ async function themeBuildInternal(
2465
2233
  // capability exports are checked against what the theme actually asks for.
2466
2234
  if (!_defineTheme || !_generateThemeRulesSplit) {
2467
2235
  throw new AstryxError(
2468
- coreUnavailableMessage(cwd, _coreImportError),
2236
+ 'Could not load @astryxdesign/core/theme: `astryx theme build` requires a ' +
2237
+ 'built, resolvable @astryxdesign/core so it emits the same CSS as the ' +
2238
+ 'runtime <Theme>. Build @astryxdesign/core first (e.g. `pnpm -F @astryxdesign/core ' +
2239
+ 'build`)' +
2240
+ (_coreImportError
2241
+ ? `.\n Import error: ${_coreImportError.message}`
2242
+ : '.'),
2469
2243
  undefined,
2470
2244
  ERROR_CODES.ERR_CORE_NOT_FOUND,
2471
2245
  );
@@ -3033,7 +2807,6 @@ export async function themeBuildFamily(
3033
2807
  error instanceof Error ? error.message : 'Invalid family key.';
3034
2808
  throw new AstryxError(message, undefined, ERROR_CODES.ERR_THEME_INVALID);
3035
2809
  }
3036
- await selectCore(cwd);
3037
2810
  const familyInterception = interceptCore(_coreThemeModule, _coreRootModule);
3038
2811
  // prettier-ignore
3039
2812
  const familyLoader = createJiti(import.meta.url, {moduleCache: true, interopDefault: false, jsx: true, extensions: THEME_MODULE_EXTENSIONS, virtualModules: familyInterception.modules});
@@ -61,13 +61,13 @@ const RUNNERS = {
61
61
  sites: ['child_process.execFileSync'],
62
62
  },
63
63
  'api/theme/build/build.mjs': {
64
- runs: "theme source modules, through jiti; eval for legacy theme object literals; the project's installed Core",
64
+ runs: 'theme source modules, through jiti; eval for legacy theme object literals',
65
65
  sites: [
66
66
  '.evalModule() ×2',
67
67
  '.import()',
68
68
  'createJiti ×4',
69
69
  'eval ×2',
70
- 'import(<computed>) ×3',
70
+ 'import(<computed>)',
71
71
  ],
72
72
  },
73
73
  'api/theme/build/core-interception.mjs': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/cli",
3
- "version": "0.6.5-canary.24fe2cd",
3
+ "version": "0.6.5-canary.27a5079",
4
4
  "displayName": "CLI",
5
5
  "description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
6
6
  "author": "Meta Open Source",
@@ -101,10 +101,10 @@
101
101
  "zod": "^4.4.3"
102
102
  },
103
103
  "peerDependencies": {
104
- "@astryxdesign/charts": "0.6.5-canary.24fe2cd",
105
- "@astryxdesign/core": "0.6.5-canary.24fe2cd",
106
- "@astryxdesign/lab": "0.6.5-canary.24fe2cd",
107
- "@astryxdesign/theme-neutral": "0.6.5-canary.24fe2cd"
104
+ "@astryxdesign/charts": "0.6.5-canary.27a5079",
105
+ "@astryxdesign/core": "0.6.5-canary.27a5079",
106
+ "@astryxdesign/lab": "0.6.5-canary.27a5079",
107
+ "@astryxdesign/theme-neutral": "0.6.5-canary.27a5079"
108
108
  },
109
109
  "peerDependenciesMeta": {
110
110
  "@astryxdesign/charts": {
@@ -121,10 +121,10 @@
121
121
  }
122
122
  },
123
123
  "devDependencies": {
124
- "@astryxdesign/charts": "0.6.5-canary.24fe2cd",
125
- "@astryxdesign/core": "0.6.5-canary.24fe2cd",
126
- "@astryxdesign/lab": "0.6.5-canary.24fe2cd",
127
- "@astryxdesign/theme-neutral": "0.6.5-canary.24fe2cd",
124
+ "@astryxdesign/charts": "0.6.5-canary.27a5079",
125
+ "@astryxdesign/core": "0.6.5-canary.27a5079",
126
+ "@astryxdesign/lab": "0.6.5-canary.27a5079",
127
+ "@astryxdesign/theme-neutral": "0.6.5-canary.27a5079",
128
128
  "@heroicons/react": "^2.2.0",
129
129
  "@stylexjs/stylex": "^0.19.0",
130
130
  "@types/babel__core": "^7.20.5",
@@ -1,165 +0,0 @@
1
- // Copyright (c) Meta Platforms, Inc. and affiliates.
2
-
3
- /**
4
- * @file `theme build` in an app generates with the Core the app installed.
5
- *
6
- * A CLI run one-off (`npx @astryxdesign/cli`) has no Core beside it, and the
7
- * app's `<Theme>` runs on the app's Core either way, so the build loads the
8
- * project's installed `@astryxdesign/core` when it differs from the CLI's own.
9
- * When there is no Core to use, the error names the fix for where it ran:
10
- * install it in an app, build it in the Astryx repository.
11
- */
12
-
13
- import {afterEach, beforeEach, describe, expect, it} from 'vitest';
14
- import * as fs from 'node:fs';
15
- import * as os from 'node:os';
16
- import * as path from 'node:path';
17
- import {fileURLToPath, pathToFileURL} from 'node:url';
18
- import {coreUnavailableMessage, themeBuild} from './build.mjs';
19
-
20
- const HERE = path.dirname(fileURLToPath(import.meta.url));
21
- const REPO_ROOT = path.resolve(HERE, '../../../../..');
22
- const CORE_DIST = path.join(REPO_ROOT, 'packages/core/dist');
23
-
24
- /** @type {string[]} */
25
- const tmpDirs = [];
26
- const makeDir = () => {
27
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-project-core-'));
28
- tmpDirs.push(dir);
29
- fs.writeFileSync(
30
- path.join(dir, 'package.json'),
31
- '{"name":"app","type":"module"}',
32
- );
33
- // A plain theme object: the file itself never imports Core, so only the
34
- // build's own Core loading can set the marker below.
35
- fs.writeFileSync(
36
- path.join(dir, 'ocean.mjs'),
37
- "export default {name: 'ocean', tokens: {'--color-bg': '#0a0a0a'}};\n",
38
- );
39
- return dir;
40
- };
41
-
42
- beforeEach(() => {
43
- delete globalThis.__astryxProjectCoreLoads;
44
- });
45
- afterEach(() => {
46
- delete globalThis.__astryxProjectCoreLoads;
47
- for (const dir of tmpDirs.splice(0))
48
- fs.rmSync(dir, {recursive: true, force: true});
49
- });
50
-
51
- /**
52
- * Install a Core in `dir` that is the built one, re-exported, and records each
53
- * entry as it is imported. Its `exports` map lists `source` first, as Core's
54
- * own does, so the build must skip that condition to find the runtime file.
55
- * @param {string} dir
56
- * @param {{broken?: boolean}} [options]
57
- */
58
- function installCore(dir, {broken = false} = {}) {
59
- const core = path.join(dir, 'node_modules', '@astryxdesign', 'core');
60
- fs.mkdirSync(core, {recursive: true});
61
- fs.writeFileSync(
62
- path.join(core, 'package.json'),
63
- JSON.stringify({
64
- name: '@astryxdesign/core',
65
- version: '0.6.5',
66
- type: 'module',
67
- exports: {
68
- '.': {types: './root.d.ts', default: './root.mjs'},
69
- './theme': {
70
- source: './src/theme/index.ts',
71
- types: './theme.d.ts',
72
- default: './theme.mjs',
73
- },
74
- },
75
- }),
76
- );
77
- /** @param {string} entry @param {string} target */
78
- const module = (entry, target) =>
79
- broken
80
- ? "throw new Error('this Core is broken');\n"
81
- : `export * from ${JSON.stringify(pathToFileURL(path.join(CORE_DIST, target)).href)};\n` +
82
- `globalThis.__astryxProjectCoreLoads = [...(globalThis.__astryxProjectCoreLoads ?? []), '${entry}'];\n`;
83
- fs.writeFileSync(
84
- path.join(core, 'theme.mjs'),
85
- module('theme', 'theme/index.js'),
86
- );
87
- fs.writeFileSync(path.join(core, 'root.mjs'), module('root', 'index.js'));
88
- return core;
89
- }
90
-
91
- describe('theme build generates with the Core the project installed', () => {
92
- it('loads the app’s installed Core when it differs from the CLI’s own', async () => {
93
- const app = makeDir();
94
- installCore(app);
95
- await themeBuild('ocean.mjs', {}, {cwd: app});
96
- expect(globalThis.__astryxProjectCoreLoads).toEqual(['theme', 'root']);
97
- // The same Core, re-exported, emits the same CSS as the CLI's own. The
98
- // generated header differs only in the Core version it records.
99
- const bare = makeDir();
100
- await themeBuild('ocean.mjs', {}, {cwd: bare});
101
- /** @param {string} dir */
102
- const rules = dir =>
103
- fs
104
- .readFileSync(path.join(dir, 'ocean.css'), 'utf-8')
105
- .replace(/^\/\*[\s\S]*?\*\/\n/, '');
106
- expect(rules(app)).toBe(rules(bare));
107
- expect(rules(app)).toContain('#0a0a0a');
108
- });
109
-
110
- it('keeps the CLI’s own Core when the project has none', async () => {
111
- const bare = makeDir();
112
- await themeBuild('ocean.mjs', {}, {cwd: bare});
113
- expect(globalThis.__astryxProjectCoreLoads).toBeUndefined();
114
- expect(fs.existsSync(path.join(bare, 'ocean.css'))).toBe(true);
115
- });
116
-
117
- it('keeps the CLI’s own Core when the installed one does not load', async () => {
118
- const app = makeDir();
119
- installCore(app, {broken: true});
120
- await themeBuild('ocean.mjs', {}, {cwd: app});
121
- expect(fs.existsSync(path.join(app, 'ocean.css'))).toBe(true);
122
- });
123
- });
124
-
125
- describe('the error when there is no Core to generate with', () => {
126
- it('tells an app to install Core, not to build it', () => {
127
- const app = makeDir();
128
- const message = coreUnavailableMessage(
129
- app,
130
- new Error("Cannot find package '@astryxdesign/core'"),
131
- );
132
- expect(message).toContain(
133
- 'This project does not have @astryxdesign/core installed',
134
- );
135
- expect(message).toContain('`npm install @astryxdesign/core`');
136
- expect(message).not.toContain('pnpm -F');
137
- expect(message).toContain(
138
- "Import error: Cannot find package '@astryxdesign/core'",
139
- );
140
- });
141
-
142
- it('names an installed Core that does not load', () => {
143
- const app = makeDir();
144
- installCore(app, {broken: true});
145
- const message = coreUnavailableMessage(
146
- app,
147
- new Error('this Core is broken'),
148
- );
149
- expect(message).toContain(
150
- `Could not load the @astryxdesign/core installed at ${path.join('node_modules', '@astryxdesign', 'core')}`,
151
- );
152
- expect(message).toContain('Reinstall it.');
153
- expect(message).not.toContain('pnpm -F');
154
- });
155
-
156
- it('keeps the build instruction in the Astryx repository', () => {
157
- const message = coreUnavailableMessage(
158
- path.join(REPO_ROOT, 'packages', 'cli'),
159
- undefined,
160
- );
161
- expect(message).toContain('Build @astryxdesign/core first');
162
- expect(message).toContain('`pnpm -F @astryxdesign/core build`');
163
- expect(message).not.toContain('Import error');
164
- });
165
- });