@magmonium/cli 0.10.10 → 0.10.12

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(
@@ -125,16 +92,20 @@ function emitFrom(root, tests) {
125
92
  * DesignSource through the same codec and codegen the editor runs, its
126
93
  * operations snapshot standing in for the live spec. With `tests`, the Emitted
127
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.
128
101
  */
129
102
  function generate({ root = process.cwd(), tests = false } = {}) {
130
- const { codegen, files } = emitFrom(root, tests);
131
- const removed = clearGeneratedSet(root, codegen.GENERATED_SET);
132
- for (const file of files) {
133
- const target = path.join(root, file.path);
134
- fs.mkdirSync(path.dirname(target), { recursive: true });
135
- fs.writeFileSync(target, file.contents);
136
- }
137
- 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 };
138
109
  }
139
110
 
140
111
  // Every file under the GeneratedSet's folders and top-level globs, as POSIX
@@ -169,10 +140,15 @@ function listGeneratedSet(root, codegen) {
169
140
  * The GeneratedSet brought to what `.magmonium/` emits, touching only what
170
141
  * differs: a file whose contents already match is left alone, so a running
171
142
  * `ng serve` rebuilds for the edit and not for the app. A generated file edited
172
- * by hand is rewritten; one added by hand is removed. Without `tests`, Emitted
173
- * 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.
174
146
  */
175
- function syncGenerated({ root = process.cwd(), tests = false } = {}) {
147
+ function syncGenerated({
148
+ root = process.cwd(),
149
+ tests = false,
150
+ keepTests = !tests,
151
+ } = {}) {
176
152
  const { codegen, files } = emitFrom(root, tests);
177
153
  const next = new Map(files.map((file) => [file.path, file.contents]));
178
154
  const written = [];
@@ -189,13 +165,31 @@ function syncGenerated({ root = process.cwd(), tests = false } = {}) {
189
165
  }
190
166
  for (const rel of listGeneratedSet(root, codegen)) {
191
167
  if (next.has(rel)) continue;
192
- if (!tests && rel.endsWith('.spec.ts')) continue;
168
+ if (keepTests && rel.endsWith('.spec.ts')) continue;
193
169
  fs.rmSync(path.join(root, rel), { force: true });
194
170
  removed.push(rel);
195
171
  }
172
+ removeEmptyDirs(root, removed);
196
173
  return { written, removed, files: next, codegen };
197
174
  }
198
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
+
199
193
  /** Quiet time after the last change before regenerating. */
200
194
  const WATCH_DELAY_MS = 100;
201
195
  /** The same path rewritten this often inside the window means two writers disagree. */
@@ -380,9 +374,9 @@ function genCodeCommand(program) {
380
374
  return;
381
375
  }
382
376
  try {
383
- const { written } = generate({ root });
377
+ const { written, changed, removed } = generate({ root });
384
378
  console.log(
385
- `✅ gen-code: ${written} file(s) generated from ${DESIGN_ROOT}/`
379
+ `✅ gen-code: ${written} file(s) generated from ${DESIGN_ROOT}/ (${changed} changed, ${removed} removed)`
386
380
  );
387
381
  } catch (error) {
388
382
  fail(error);
@@ -393,8 +387,8 @@ function genCodeCommand(program) {
393
387
  module.exports = genCodeCommand;
394
388
  module.exports.generate = generate;
395
389
  module.exports.isNewer = isNewer;
396
- module.exports.clearGeneratedSet = clearGeneratedSet;
397
390
  module.exports.readDesignFiles = readDesignFiles;
398
391
  module.exports.syncGenerated = syncGenerated;
399
392
  module.exports.watchGenerated = watchGenerated;
400
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
@@ -29071,7 +29071,7 @@ __export(package_exports2, {
29071
29071
  version: () => version2
29072
29072
  });
29073
29073
  var name2 = "@magmonium/cli";
29074
- var version2 = "0.10.10";
29074
+ var version2 = "0.10.12";
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.10",
3
+ "version": "0.10.12",
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"