@magmonium/cli 0.10.9 → 0.10.11

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.
@@ -52,41 +52,8 @@ function isNewer(a, b) {
52
52
  return false;
53
53
  }
54
54
 
55
- // Every file the GeneratedSet occupies on disk now, so a Screen deleted in the
56
- // editor does not leave its slice behind: the set is regenerated whole.
57
- function clearGeneratedSet(root, generatedSet) {
58
- let removed = 0;
59
- for (const line of generatedSet) {
60
- const rel = line.replace(/^\//, '');
61
- if (rel.endsWith('/')) {
62
- const dir = path.join(root, rel);
63
- if (fs.existsSync(dir)) {
64
- fs.rmSync(dir, { recursive: true, force: true });
65
- removed++;
66
- }
67
- continue;
68
- }
69
- // `src/app/*.router.ts` — one `*` in the file name, never in a folder.
70
- const dir = path.join(root, path.dirname(rel));
71
- const pattern = new RegExp(
72
- `^${path
73
- .basename(rel)
74
- .replace(/[.+?^${}()|[\]\\]/g, '\\$&')
75
- .replace('*', '[^/]+')}$`
76
- );
77
- if (!fs.existsSync(dir)) continue;
78
- for (const name of fs.readdirSync(dir)) {
79
- if (pattern.test(name)) {
80
- fs.rmSync(path.join(dir, name), { force: true });
81
- removed++;
82
- }
83
- }
84
- }
85
- return removed;
86
- }
87
-
88
55
  // The DesignSource read off disk and emitted, without touching the folder. What
89
- // `generate` writes whole and `syncGenerated` diffs.
56
+ // `syncGenerated` diffs the GeneratedSet on disk against.
90
57
  function emitFrom(root, tests) {
91
58
  if (!fs.existsSync(path.join(root, DESIGN_ROOT, 'index.json'))) {
92
59
  throw new Error(
@@ -105,10 +72,12 @@ function emitFrom(root, tests) {
105
72
  }
106
73
  const source = read.source;
107
74
  if (source.codegen && isNewer(source.codegen, pkg.version)) {
108
- throw new Error(
75
+ const error = new Error(
109
76
  `the design was written by @magmonium/cli ${source.codegen}, newer than this ${pkg.version}.\n` +
110
77
  ` Pin the version it needs: npm install --save-dev --save-exact @magmonium/cli@${source.codegen}`
111
78
  );
79
+ error.needs = source.codegen;
80
+ throw error;
112
81
  }
113
82
 
114
83
  const ws = {
@@ -123,16 +92,20 @@ function emitFrom(root, tests) {
123
92
  * DesignSource through the same codec and codegen the editor runs, its
124
93
  * operations snapshot standing in for the live spec. With `tests`, the Emitted
125
94
  * tests beside the code as well.
95
+ *
96
+ * The set ends up whole — a slice the design dropped does not survive, and an
97
+ * Emitted test is removed unless `tests` writes it — but only what differs is
98
+ * touched. Never a folder cleared and refilled: a `gen-code` run beside a live
99
+ * `ng serve` (`npm test`, `npm run build` in another terminal) would have its
100
+ * watcher see every file vanish and drop them from the compilation.
126
101
  */
127
102
  function generate({ root = process.cwd(), tests = false } = {}) {
128
- const { codegen, files } = emitFrom(root, tests);
129
- const removed = clearGeneratedSet(root, codegen.GENERATED_SET);
130
- for (const file of files) {
131
- const target = path.join(root, file.path);
132
- fs.mkdirSync(path.dirname(target), { recursive: true });
133
- fs.writeFileSync(target, file.contents);
134
- }
135
- return { written: files.length, removed };
103
+ const { files, written, removed } = syncGenerated({
104
+ root,
105
+ tests,
106
+ keepTests: false,
107
+ });
108
+ return { written: files.size, changed: written.length, removed: removed.length };
136
109
  }
137
110
 
138
111
  // Every file under the GeneratedSet's folders and top-level globs, as POSIX
@@ -167,10 +140,15 @@ function listGeneratedSet(root, codegen) {
167
140
  * The GeneratedSet brought to what `.magmonium/` emits, touching only what
168
141
  * differs: a file whose contents already match is left alone, so a running
169
142
  * `ng serve` rebuilds for the edit and not for the app. A generated file edited
170
- * by hand is rewritten; one added by hand is removed. Without `tests`, Emitted
171
- * tests are kept — the editor writes them and `gen-code` does not.
143
+ * by hand is rewritten; one added by hand is removed, and a folder it leaves
144
+ * empty with it. Without `tests`, Emitted tests are kept unless `keepTests` is
145
+ * false — the editor writes them and `gen-code` does not.
172
146
  */
173
- function syncGenerated({ root = process.cwd(), tests = false } = {}) {
147
+ function syncGenerated({
148
+ root = process.cwd(),
149
+ tests = false,
150
+ keepTests = !tests,
151
+ } = {}) {
174
152
  const { codegen, files } = emitFrom(root, tests);
175
153
  const next = new Map(files.map((file) => [file.path, file.contents]));
176
154
  const written = [];
@@ -187,13 +165,31 @@ function syncGenerated({ root = process.cwd(), tests = false } = {}) {
187
165
  }
188
166
  for (const rel of listGeneratedSet(root, codegen)) {
189
167
  if (next.has(rel)) continue;
190
- if (!tests && rel.endsWith('.spec.ts')) continue;
168
+ if (keepTests && rel.endsWith('.spec.ts')) continue;
191
169
  fs.rmSync(path.join(root, rel), { force: true });
192
170
  removed.push(rel);
193
171
  }
172
+ removeEmptyDirs(root, removed);
194
173
  return { written, removed, files: next, codegen };
195
174
  }
196
175
 
176
+ /**
177
+ * The folders the removed files leave empty — a dropped slice's
178
+ * `features/<slice>/` — taken out bottom-up, never `src/app` itself.
179
+ */
180
+ function removeEmptyDirs(root, removed) {
181
+ const stop = path.join(root, 'src', 'app');
182
+ const dirs = new Set(removed.map((rel) => path.dirname(path.join(root, rel))));
183
+ const deepestFirst = [...dirs].sort((a, b) => b.length - a.length);
184
+ for (let dir of deepestFirst) {
185
+ while (dir.startsWith(`${stop}${path.sep}`)) {
186
+ if (!fs.existsSync(dir) || fs.readdirSync(dir).length) break;
187
+ fs.rmdirSync(dir);
188
+ dir = path.dirname(dir);
189
+ }
190
+ }
191
+ }
192
+
197
193
  /** Quiet time after the last change before regenerating. */
198
194
  const WATCH_DELAY_MS = 100;
199
195
  /** The same path rewritten this often inside the window means two writers disagree. */
@@ -240,6 +236,8 @@ async function watchGenerated({
240
236
  `🔁 gen-code: ${reason} → ${result.written.length} written, ${result.removed.length} removed`
241
237
  );
242
238
  } catch (error) {
239
+ // Too old a CLI at start: the caller upgrades and runs the watch again.
240
+ if (error.needs && reason === 'start') throw error;
243
241
  // A design file mid-save or hand-broken: the code on disk stays as the
244
242
  // last good emit until the next save reads cleanly.
245
243
  log(`⚠️ gen-code: ${error.message}`);
@@ -309,6 +307,38 @@ async function watchGenerated({
309
307
  return { close };
310
308
  }
311
309
 
310
+ /** Set on the re-run so a still-too-old install cannot loop. */
311
+ const UPGRADED_ENV = 'MAG_CLI_UPGRADED';
312
+
313
+ /**
314
+ * The design needs a newer CLI than the app has installed: pin that exact
315
+ * version in the app, then run the same `gen-code` again from the new install.
316
+ * Exits with the re-run's code, so `prestart` goes on to `ng serve` after it.
317
+ */
318
+ function upgradeAndRerun(root, needs) {
319
+ const { spawnSync } = require('child_process');
320
+ const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
321
+ console.log(
322
+ `⬆️ gen-code: the design needs @magmonium/cli ${needs} (installed ${pkg.version}) — installing it`
323
+ );
324
+ const install = spawnSync(
325
+ npm,
326
+ ['install', '--save-dev', '--save-exact', `@magmonium/cli@${needs}`],
327
+ { cwd: root, stdio: 'inherit', shell: process.platform === 'win32' }
328
+ );
329
+ if (install.status !== 0) {
330
+ console.error(`❌ gen-code: npm install @magmonium/cli@${needs} failed`);
331
+ process.exit(install.status || 1);
332
+ }
333
+ const bin = path.join(root, 'node_modules', '@magmonium', 'cli', 'bin', 'cli.js');
334
+ const rerun = spawnSync(process.execPath, [bin, ...process.argv.slice(2)], {
335
+ cwd: process.cwd(),
336
+ stdio: 'inherit',
337
+ env: { ...process.env, [UPGRADED_ENV]: needs },
338
+ });
339
+ process.exit(rerun.status ?? 1);
340
+ }
341
+
312
342
  function genCodeCommand(program) {
313
343
  program
314
344
  .command('gen-code')
@@ -321,25 +351,35 @@ function genCodeCommand(program) {
321
351
  'Keep regenerating on every .magmonium/ change, and put back hand edits to generated files'
322
352
  )
323
353
  .option('--run <command>', 'With --watch: run this command beside the watch (e.g. "ng serve")')
354
+ .option(
355
+ '--no-install',
356
+ 'Fail instead of installing the newer @magmonium/cli the design needs'
357
+ )
324
358
  .action(async (options) => {
325
359
  const root = options.root || process.cwd();
360
+ const fail = (error) => {
361
+ if (error.needs && options.install && !process.env[UPGRADED_ENV]) {
362
+ upgradeAndRerun(root, error.needs);
363
+ return;
364
+ }
365
+ console.error(`❌ gen-code: ${error.message}`);
366
+ process.exit(1);
367
+ };
326
368
  if (options.watch) {
327
369
  try {
328
370
  await watchGenerated({ root, run: options.run });
329
371
  } catch (error) {
330
- console.error(`❌ gen-code: ${error.message}`);
331
- process.exit(1);
372
+ fail(error);
332
373
  }
333
374
  return;
334
375
  }
335
376
  try {
336
- const { written } = generate({ root });
377
+ const { written, changed, removed } = generate({ root });
337
378
  console.log(
338
- `✅ gen-code: ${written} file(s) generated from ${DESIGN_ROOT}/`
379
+ `✅ gen-code: ${written} file(s) generated from ${DESIGN_ROOT}/ (${changed} changed, ${removed} removed)`
339
380
  );
340
381
  } catch (error) {
341
- console.error(`❌ gen-code: ${error.message}`);
342
- process.exit(1);
382
+ fail(error);
343
383
  }
344
384
  });
345
385
  }
@@ -347,8 +387,8 @@ function genCodeCommand(program) {
347
387
  module.exports = genCodeCommand;
348
388
  module.exports.generate = generate;
349
389
  module.exports.isNewer = isNewer;
350
- module.exports.clearGeneratedSet = clearGeneratedSet;
351
390
  module.exports.readDesignFiles = readDesignFiles;
352
391
  module.exports.syncGenerated = syncGenerated;
353
392
  module.exports.watchGenerated = watchGenerated;
354
393
  module.exports.listGeneratedSet = listGeneratedSet;
394
+ module.exports.removeEmptyDirs = removeEmptyDirs;
@@ -10,12 +10,12 @@ function genTestCommand(program) {
10
10
  .option('--root <path>', 'App root (default: cwd)')
11
11
  .action((options) => {
12
12
  try {
13
- const { written } = generate({
13
+ const { written, changed, removed } = generate({
14
14
  root: options.root || process.cwd(),
15
15
  tests: true,
16
16
  });
17
17
  console.log(
18
- `✅ gen-test: ${written} file(s) generated from .magmonium/`
18
+ `✅ gen-test: ${written} file(s) generated from .magmonium/ (${changed} changed, ${removed} removed)`
19
19
  );
20
20
  } catch (error) {
21
21
  console.error(`❌ gen-test: ${error.message}`);
package/codegen/index.js CHANGED
@@ -29003,7 +29003,7 @@ __export(package_exports, {
29003
29003
  version: () => version
29004
29004
  });
29005
29005
  var name = "@magmonium/one";
29006
- var version = "0.4.133";
29006
+ var version = "0.4.135";
29007
29007
  var description = "Magmonium One \u2014 Angular design-system primitives for mm Apps";
29008
29008
  var author = "magmonium";
29009
29009
  var license = "MIT";
@@ -29071,7 +29071,7 @@ __export(package_exports2, {
29071
29071
  version: () => version2
29072
29072
  });
29073
29073
  var name2 = "@magmonium/cli";
29074
- var version2 = "0.10.9";
29074
+ var version2 = "0.10.11";
29075
29075
  var description2 = "A powerful and extensible asset compilation tool that can be shared across multiple projects";
29076
29076
  var bin = {
29077
29077
  "mag-cli": "bin/cli.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magmonium/cli",
3
- "version": "0.10.9",
3
+ "version": "0.10.11",
4
4
  "description": "A powerful and extensible asset compilation tool that can be shared across multiple projects",
5
5
  "bin": {
6
6
  "mag-cli": "bin/cli.js"