@ngrs/nx 0.0.0-stage → 0.1.0
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.
- package/README.md +10 -2
- package/executors.json +76 -0
- package/generators.json +31 -0
- package/package.json +85 -4
- package/src/executors/application/index.cjs +10 -0
- package/src/executors/application/index.mjs +17 -0
- package/src/executors/application/schema.json +32 -0
- package/src/executors/context-adapter.mjs +62 -0
- package/src/executors/dev-server/index.cjs +10 -0
- package/src/executors/dev-server/index.mjs +17 -0
- package/src/executors/dev-server/schema.json +19 -0
- package/src/executors/extract-i18n/index.cjs +10 -0
- package/src/executors/extract-i18n/index.mjs +17 -0
- package/src/executors/extract-i18n/schema.json +13 -0
- package/src/executors/package/index.cjs +10 -0
- package/src/executors/package/index.mjs +209 -0
- package/src/executors/package/schema.json +19 -0
- package/src/executors/unit-test/index.cjs +10 -0
- package/src/executors/unit-test/index.mjs +17 -0
- package/src/executors/unit-test/schema.json +26 -0
- package/src/generators/init/index.cjs +434 -0
- package/src/generators/init/schema.json +19 -0
- package/src/index.cjs +19 -0
- package/src/index.d.ts +53 -0
- package/src/index.mjs +20 -0
- package/src/plugin.cjs +78 -0
- package/src/plugin.mjs +114 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema",
|
|
3
|
+
"title": "@ngrs/nx Library Package Executor Schema",
|
|
4
|
+
"description": "Build a publishable/buildable Nx Angular library in Angular Package Format (APF).",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"project": { "type": "string" },
|
|
8
|
+
"tsConfig": { "type": "string" },
|
|
9
|
+
"outputPath": { "type": "string" },
|
|
10
|
+
"compilationMode": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"enum": ["partial", "full"],
|
|
13
|
+
"default": "partial"
|
|
14
|
+
},
|
|
15
|
+
"deleteOutputPath": { "type": "boolean", "default": true },
|
|
16
|
+
"sourceMap": { "type": "boolean", "default": true }
|
|
17
|
+
},
|
|
18
|
+
"additionalProperties": true
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
async function* unitTestExecutorCjs(options, context) {
|
|
4
|
+
const mod = await import('./index.mjs');
|
|
5
|
+
yield* mod.unitTestExecutor(options, context);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
module.exports = unitTestExecutorCjs;
|
|
9
|
+
module.exports.default = unitTestExecutorCjs;
|
|
10
|
+
module.exports.unitTestExecutor = unitTestExecutorCjs;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { executeUnitTestBuilder } from '@ngrs/build';
|
|
2
|
+
import { toArchitectContext } from '../context-adapter.mjs';
|
|
3
|
+
|
|
4
|
+
export async function* unitTestExecutor(options = {}, context = {}) {
|
|
5
|
+
const { builderContext } = await toArchitectContext(
|
|
6
|
+
options,
|
|
7
|
+
context,
|
|
8
|
+
'@ngrs/nx:unit-test',
|
|
9
|
+
);
|
|
10
|
+
try {
|
|
11
|
+
yield* executeUnitTestBuilder(options, builderContext);
|
|
12
|
+
} finally {
|
|
13
|
+
await builderContext.runTeardowns();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default unitTestExecutor;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema",
|
|
3
|
+
"title": "@ngrs/nx Unit-Test Executor Schema",
|
|
4
|
+
"description": "Run Vitest unit tests for an Nx Angular application or library.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"tsConfig": { "type": "string" },
|
|
8
|
+
"buildTarget": { "type": "string" },
|
|
9
|
+
"environment": {
|
|
10
|
+
"type": "string",
|
|
11
|
+
"enum": ["happy-dom", "node"],
|
|
12
|
+
"default": "happy-dom"
|
|
13
|
+
},
|
|
14
|
+
"include": { "type": "array", "items": { "type": "string" } },
|
|
15
|
+
"exclude": { "type": "array", "items": { "type": "string" } },
|
|
16
|
+
"filter": { "type": "string" },
|
|
17
|
+
"watch": { "type": "boolean" },
|
|
18
|
+
"coverage": { "type": "boolean", "default": false },
|
|
19
|
+
"reporters": { "type": "array" },
|
|
20
|
+
"providersFile": { "type": "string" },
|
|
21
|
+
"setupFiles": { "type": "array", "items": { "type": "string" } },
|
|
22
|
+
"polyfills": { "type": "array", "items": { "type": "string" } },
|
|
23
|
+
"listTests": { "type": "boolean", "default": false }
|
|
24
|
+
},
|
|
25
|
+
"additionalProperties": true
|
|
26
|
+
}
|
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const { applyEdits, modify, parse } = require('jsonc-parser');
|
|
6
|
+
|
|
7
|
+
const NX_EXECUTOR_MIGRATIONS = Object.freeze({
|
|
8
|
+
// Application & Browser builders/executors
|
|
9
|
+
'@nx/angular:application': '@ngrs/nx:application',
|
|
10
|
+
'@angular/build:application': '@ngrs/nx:application',
|
|
11
|
+
'@angular-devkit/build-angular:application': '@ngrs/nx:application',
|
|
12
|
+
'@nx/angular:browser-esbuild': '@ngrs/nx:browser-esbuild',
|
|
13
|
+
'@angular-devkit/build-angular:browser-esbuild': '@ngrs/nx:browser-esbuild',
|
|
14
|
+
'@nx/angular:webpack-browser': '@ngrs/nx:browser',
|
|
15
|
+
'@nx/angular:browser': '@ngrs/nx:browser',
|
|
16
|
+
'@angular-devkit/build-angular:browser': '@ngrs/nx:browser',
|
|
17
|
+
// Dev-server
|
|
18
|
+
'@nx/angular:dev-server': '@ngrs/nx:dev-server',
|
|
19
|
+
'@nx/angular:webpack-dev-server': '@ngrs/nx:dev-server',
|
|
20
|
+
'@angular/build:dev-server': '@ngrs/nx:dev-server',
|
|
21
|
+
'@angular-devkit/build-angular:dev-server': '@ngrs/nx:dev-server',
|
|
22
|
+
// Extract-i18n
|
|
23
|
+
'@nx/angular:extract-i18n': '@ngrs/nx:extract-i18n',
|
|
24
|
+
'@angular/build:extract-i18n': '@ngrs/nx:extract-i18n',
|
|
25
|
+
'@angular-devkit/build-angular:extract-i18n': '@ngrs/nx:extract-i18n',
|
|
26
|
+
// Unit-test (Vitest)
|
|
27
|
+
'@nx/angular:unit-test': '@ngrs/nx:unit-test',
|
|
28
|
+
'@angular/build:unit-test': '@ngrs/nx:unit-test',
|
|
29
|
+
// Buildable/Publishable Angular libraries
|
|
30
|
+
'@nx/angular:package': '@ngrs/nx:package',
|
|
31
|
+
'@nx/angular:ng-packagr-lite': '@ngrs/nx:package',
|
|
32
|
+
'@angular/build:ng-packagr': '@ngrs/nx:package',
|
|
33
|
+
'@angular-devkit/build-angular:ng-packagr': '@ngrs/nx:package',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const IGNORE_DIRS = new Set([
|
|
37
|
+
'node_modules',
|
|
38
|
+
'dist',
|
|
39
|
+
'.nx',
|
|
40
|
+
'.angular',
|
|
41
|
+
'.git',
|
|
42
|
+
'coverage',
|
|
43
|
+
'target',
|
|
44
|
+
'out-tsc',
|
|
45
|
+
'_submodules',
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
function migrateProjectJsonText(rawText, defaultProjectName, requestedProject) {
|
|
49
|
+
const parsed = parse(rawText, [], { allowTrailingComma: true });
|
|
50
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
51
|
+
return { updatedText: rawText, changed: false, updates: [] };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const projectName = parsed.name || defaultProjectName;
|
|
55
|
+
if (requestedProject && projectName !== requestedProject) {
|
|
56
|
+
return { updatedText: rawText, changed: false, updates: [] };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Detect whether this Nx project is an Angular project (either via angular tags/executors or tsconfig)
|
|
60
|
+
let updatedText = rawText;
|
|
61
|
+
const updates = [];
|
|
62
|
+
|
|
63
|
+
for (const sectionKey of ['targets', 'architect']) {
|
|
64
|
+
const targetsObj = parsed[sectionKey];
|
|
65
|
+
if (!targetsObj || typeof targetsObj !== 'object') continue;
|
|
66
|
+
|
|
67
|
+
const hasAngularSibling = Object.values(targetsObj).some((t) => {
|
|
68
|
+
const ex = t && (t.executor || t.builder);
|
|
69
|
+
return (
|
|
70
|
+
typeof ex === 'string' &&
|
|
71
|
+
(ex.startsWith('@nx/angular:') ||
|
|
72
|
+
ex.startsWith('@angular/') ||
|
|
73
|
+
ex.startsWith('@angular-devkit/') ||
|
|
74
|
+
ex.startsWith('@ngrs/'))
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
for (const [targetName, targetDef] of Object.entries(targetsObj)) {
|
|
79
|
+
if (!targetDef || typeof targetDef !== 'object') continue;
|
|
80
|
+
const propKey = 'executor' in targetDef ? 'executor' : 'builder' in targetDef ? 'builder' : null;
|
|
81
|
+
if (!propKey) continue;
|
|
82
|
+
const current = targetDef[propKey];
|
|
83
|
+
if (typeof current !== 'string' || current.startsWith('@ngrs/')) continue;
|
|
84
|
+
|
|
85
|
+
let nextExecutor = NX_EXECUTOR_MIGRATIONS[current];
|
|
86
|
+
if (!nextExecutor && current === '@nx/vite:test' && hasAngularSibling) {
|
|
87
|
+
nextExecutor = '@ngrs/nx:unit-test';
|
|
88
|
+
}
|
|
89
|
+
if (!nextExecutor) continue;
|
|
90
|
+
|
|
91
|
+
const edits = modify(updatedText, [sectionKey, targetName, propKey], nextExecutor, {
|
|
92
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
93
|
+
});
|
|
94
|
+
updatedText = applyEdits(updatedText, edits);
|
|
95
|
+
updates.push({
|
|
96
|
+
project: projectName,
|
|
97
|
+
target: targetName,
|
|
98
|
+
from: current,
|
|
99
|
+
to: nextExecutor,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
updatedText,
|
|
106
|
+
changed: updates.length > 0 && updatedText !== rawText,
|
|
107
|
+
updates,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function migrateNxJsonText(rawText, options = {}) {
|
|
112
|
+
const parsed = parse(rawText, [], { allowTrailingComma: true });
|
|
113
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
114
|
+
return { updatedText: rawText, changed: false, updates: [] };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let updatedText = rawText;
|
|
118
|
+
const updates = [];
|
|
119
|
+
|
|
120
|
+
// 1. Migrate `targetDefaults` keys & `.executor` fields if they reference `@nx/angular:*` or `@angular/build:*`
|
|
121
|
+
const targetDefaults = parsed.targetDefaults;
|
|
122
|
+
if (targetDefaults && typeof targetDefaults === 'object') {
|
|
123
|
+
for (const [defKey, defVal] of Object.entries(targetDefaults)) {
|
|
124
|
+
if (defVal && typeof defVal === 'object' && typeof defVal.executor === 'string') {
|
|
125
|
+
const mapped = NX_EXECUTOR_MIGRATIONS[defVal.executor];
|
|
126
|
+
if (mapped) {
|
|
127
|
+
const edits = modify(updatedText, ['targetDefaults', defKey, 'executor'], mapped, {
|
|
128
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
129
|
+
});
|
|
130
|
+
updatedText = applyEdits(updatedText, edits);
|
|
131
|
+
updates.push({ targetDefault: defKey, from: defVal.executor, to: mapped });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (NX_EXECUTOR_MIGRATIONS[defKey]) {
|
|
135
|
+
const mappedKey = NX_EXECUTOR_MIGRATIONS[defKey];
|
|
136
|
+
if (!targetDefaults[mappedKey]) {
|
|
137
|
+
const cloned = structuredClone(defVal);
|
|
138
|
+
if (cloned && typeof cloned === 'object' && NX_EXECUTOR_MIGRATIONS[cloned.executor]) {
|
|
139
|
+
cloned.executor = NX_EXECUTOR_MIGRATIONS[cloned.executor];
|
|
140
|
+
}
|
|
141
|
+
const addEdits = modify(updatedText, ['targetDefaults', mappedKey], cloned, {
|
|
142
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
143
|
+
});
|
|
144
|
+
updatedText = applyEdits(updatedText, addEdits);
|
|
145
|
+
const delEdits = modify(updatedText, ['targetDefaults', defKey], undefined, {
|
|
146
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
147
|
+
});
|
|
148
|
+
updatedText = applyEdits(updatedText, delEdits);
|
|
149
|
+
updates.push({ targetDefault: defKey, from: defKey, to: mappedKey });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 2. Register `@ngrs/nx/plugin` in `nx.json` `plugins` if `addPlugin !== false`
|
|
156
|
+
if (options.addPlugin !== false) {
|
|
157
|
+
const currentPlugins = Array.isArray(parsed.plugins) ? parsed.plugins : [];
|
|
158
|
+
const alreadyHasPlugin = currentPlugins.some((p) =>
|
|
159
|
+
typeof p === 'string' ? p === '@ngrs/nx/plugin' || p === '@ngrs/nx' : p?.plugin === '@ngrs/nx/plugin',
|
|
160
|
+
);
|
|
161
|
+
if (!alreadyHasPlugin) {
|
|
162
|
+
const nextPlugins = [...currentPlugins, '@ngrs/nx/plugin'];
|
|
163
|
+
const edits = modify(updatedText, ['plugins'], nextPlugins, {
|
|
164
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
165
|
+
});
|
|
166
|
+
updatedText = applyEdits(updatedText, edits);
|
|
167
|
+
updates.push({ plugin: '@ngrs/nx/plugin' });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
updatedText,
|
|
173
|
+
changed: updatedText !== rawText,
|
|
174
|
+
updates,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function migrateAngularJsonForNx(rawText, requestedProject) {
|
|
179
|
+
const parsed = parse(rawText, [], { allowTrailingComma: true });
|
|
180
|
+
if (!parsed || typeof parsed !== 'object' || !parsed.projects) {
|
|
181
|
+
return { updatedText: rawText, changed: false, updates: [] };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let updatedText = rawText;
|
|
185
|
+
const updates = [];
|
|
186
|
+
for (const [projName, projDef] of Object.entries(parsed.projects)) {
|
|
187
|
+
if (requestedProject && projName !== requestedProject) continue;
|
|
188
|
+
if (!projDef || typeof projDef !== 'object') continue;
|
|
189
|
+
for (const sectionKey of ['targets', 'architect']) {
|
|
190
|
+
const targetsObj = projDef[sectionKey];
|
|
191
|
+
if (!targetsObj || typeof targetsObj !== 'object') continue;
|
|
192
|
+
for (const [targetName, targetDef] of Object.entries(targetsObj)) {
|
|
193
|
+
const propKey =
|
|
194
|
+
targetDef && ('executor' in targetDef ? 'executor' : 'builder' in targetDef ? 'builder' : null);
|
|
195
|
+
if (!propKey) continue;
|
|
196
|
+
const current = targetDef[propKey];
|
|
197
|
+
const nextExecutor = NX_EXECUTOR_MIGRATIONS[current];
|
|
198
|
+
if (!nextExecutor) continue;
|
|
199
|
+
const edits = modify(
|
|
200
|
+
updatedText,
|
|
201
|
+
['projects', projName, sectionKey, targetName, propKey],
|
|
202
|
+
nextExecutor,
|
|
203
|
+
{ formattingOptions: { tabSize: 2, insertSpaces: true } },
|
|
204
|
+
);
|
|
205
|
+
updatedText = applyEdits(updatedText, edits);
|
|
206
|
+
updates.push({ project: projName, target: targetName, from: current, to: nextExecutor });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
updatedText,
|
|
213
|
+
changed: updates.length > 0 && updatedText !== rawText,
|
|
214
|
+
updates,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function readTreeFile(tree, relPath) {
|
|
219
|
+
const candidates = [relPath.replace(/^\/+/, ''), '/' + relPath.replace(/^\/+/, '')];
|
|
220
|
+
for (const c of candidates) {
|
|
221
|
+
if (tree.exists(c)) {
|
|
222
|
+
const buf = tree.read(c);
|
|
223
|
+
if (buf !== null && buf !== undefined) {
|
|
224
|
+
return { key: c, text: typeof buf === 'string' ? buf : buf.toString('utf8') };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function writeTreeFile(tree, key, content) {
|
|
232
|
+
if (typeof tree.write === 'function') {
|
|
233
|
+
tree.write(key, content);
|
|
234
|
+
} else if (typeof tree.overwrite === 'function') {
|
|
235
|
+
tree.overwrite(key, content);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function collectProjectJsonPathsInTree(tree) {
|
|
240
|
+
const found = [];
|
|
241
|
+
|
|
242
|
+
if (typeof tree.children === 'function') {
|
|
243
|
+
function walkNxTree(relDir) {
|
|
244
|
+
const children = tree.children(relDir) || [];
|
|
245
|
+
for (const child of children) {
|
|
246
|
+
if (IGNORE_DIRS.has(child) || child.startsWith('.')) continue;
|
|
247
|
+
const childRel = relDir ? `${relDir}/${child}` : child;
|
|
248
|
+
if (child === 'project.json') {
|
|
249
|
+
found.push(childRel);
|
|
250
|
+
} else if (typeof tree.isFile === 'function' ? !tree.isFile(childRel) : true) {
|
|
251
|
+
if (!child.includes('.')) {
|
|
252
|
+
walkNxTree(childRel);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
walkNxTree('');
|
|
258
|
+
return found.sort();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (tree.root && typeof tree.visit === 'function') {
|
|
262
|
+
tree.visit((filePath) => {
|
|
263
|
+
const norm = filePath.replace(/\\/g, '/');
|
|
264
|
+
if (
|
|
265
|
+
norm.endsWith('/project.json') &&
|
|
266
|
+
!norm.includes('/node_modules/') &&
|
|
267
|
+
!norm.includes('/dist/')
|
|
268
|
+
) {
|
|
269
|
+
found.push(norm);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return found.sort();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const HAPPY_DOM_VERSION = '^20.0.0';
|
|
278
|
+
|
|
279
|
+
function migratePackageJsonForHappyDom(rawPkgText) {
|
|
280
|
+
const pkg = parse(rawPkgText, [], { allowTrailingComma: true });
|
|
281
|
+
if (!pkg || typeof pkg !== 'object') {
|
|
282
|
+
return { updatedText: rawPkgText, changed: false, updates: [] };
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
let updatedText = rawPkgText;
|
|
286
|
+
const updates = [];
|
|
287
|
+
|
|
288
|
+
for (const section of ['devDependencies', 'dependencies']) {
|
|
289
|
+
if (pkg[section] && typeof pkg[section] === 'object') {
|
|
290
|
+
for (const legacyPkg of ['jsdom', '@types/jsdom']) {
|
|
291
|
+
if (legacyPkg in pkg[section]) {
|
|
292
|
+
const edits = modify(updatedText, [section, legacyPkg], undefined, {
|
|
293
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
294
|
+
});
|
|
295
|
+
updatedText = applyEdits(updatedText, edits);
|
|
296
|
+
updates.push({ packageJson: 'remove', dependency: legacyPkg });
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const hasHappyDom =
|
|
303
|
+
(pkg.devDependencies && 'happy-dom' in pkg.devDependencies) ||
|
|
304
|
+
(pkg.dependencies && 'happy-dom' in pkg.dependencies);
|
|
305
|
+
|
|
306
|
+
if (!hasHappyDom) {
|
|
307
|
+
const edits = modify(updatedText, ['devDependencies', 'happy-dom'], HAPPY_DOM_VERSION, {
|
|
308
|
+
formattingOptions: { tabSize: 2, insertSpaces: true },
|
|
309
|
+
});
|
|
310
|
+
updatedText = applyEdits(updatedText, edits);
|
|
311
|
+
updates.push({ packageJson: 'add', dependency: 'happy-dom', version: HAPPY_DOM_VERSION });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
updatedText,
|
|
316
|
+
changed: updatedText !== rawPkgText,
|
|
317
|
+
updates,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function executeInitOnTree(tree, options = {}, logger) {
|
|
322
|
+
const allUpdates = [];
|
|
323
|
+
|
|
324
|
+
// 0. Update root `package.json` (`happy-dom` in devDependencies, prune legacy `jsdom`)
|
|
325
|
+
const pkgFile = readTreeFile(tree, 'package.json');
|
|
326
|
+
if (pkgFile) {
|
|
327
|
+
const pkgRes = migratePackageJsonForHappyDom(pkgFile.text);
|
|
328
|
+
if (pkgRes.changed) {
|
|
329
|
+
writeTreeFile(tree, pkgFile.key, pkgRes.updatedText);
|
|
330
|
+
allUpdates.push(...pkgRes.updates);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// 1. Update `nx.json`
|
|
335
|
+
const nxFile = readTreeFile(tree, 'nx.json');
|
|
336
|
+
if (nxFile) {
|
|
337
|
+
const nxRes = migrateNxJsonText(nxFile.text, options);
|
|
338
|
+
if (nxRes.changed) {
|
|
339
|
+
writeTreeFile(tree, nxFile.key, nxRes.updatedText);
|
|
340
|
+
allUpdates.push(...nxRes.updates);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// 2. Update `angular.json` if present
|
|
345
|
+
const ngFile = readTreeFile(tree, 'angular.json');
|
|
346
|
+
if (ngFile) {
|
|
347
|
+
const ngRes = migrateAngularJsonForNx(ngFile.text, options.project);
|
|
348
|
+
if (ngRes.changed) {
|
|
349
|
+
writeTreeFile(tree, ngFile.key, ngRes.updatedText);
|
|
350
|
+
allUpdates.push(...ngRes.updates);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// 3. Update all `**/project.json` files across `apps/` & `libs/`
|
|
355
|
+
const projectJsonFiles = collectProjectJsonPathsInTree(tree);
|
|
356
|
+
for (const projPath of projectJsonFiles) {
|
|
357
|
+
const fileEntry = readTreeFile(tree, projPath);
|
|
358
|
+
if (!fileEntry) continue;
|
|
359
|
+
const defaultName = path.posix.basename(path.posix.dirname(projPath.replace(/^\/+/, '')));
|
|
360
|
+
const res = migrateProjectJsonText(fileEntry.text, defaultName, options.project);
|
|
361
|
+
if (res.changed) {
|
|
362
|
+
writeTreeFile(tree, fileEntry.key, res.updatedText);
|
|
363
|
+
allUpdates.push(...res.updates);
|
|
364
|
+
for (const u of res.updates) {
|
|
365
|
+
logger?.info?.(`Configured ${u.project}:${u.target} (${u.from} -> ${u.to})`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return allUpdates;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Dual-mode `@ngrs/nx` generator supporting BOTH:
|
|
375
|
+
* - **Nx Devkit** (`await initGenerator(tree, options)` via `nx add @ngrs/nx` / `nx g @ngrs/nx:init`)
|
|
376
|
+
* - **Angular Schematics** (`initGenerator(options)(tree, context)` via `ng add @ngrs/nx`)
|
|
377
|
+
*/
|
|
378
|
+
function initGenerator(firstArg, secondArg) {
|
|
379
|
+
// Mode 1: Nx Devkit `(tree, options)` — `firstArg` is an Nx `Tree` with `.exists` & `.read`
|
|
380
|
+
if (firstArg && typeof firstArg === 'object' && typeof firstArg.exists === 'function') {
|
|
381
|
+
const tree = firstArg;
|
|
382
|
+
const options = secondArg || {};
|
|
383
|
+
executeInitOnTree(tree, options, console);
|
|
384
|
+
return async () => {};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Mode 2: Angular Schematics `(options) => (tree, context)`
|
|
388
|
+
const options = firstArg || {};
|
|
389
|
+
return (tree, context) => {
|
|
390
|
+
executeInitOnTree(tree, options, context?.logger);
|
|
391
|
+
return tree;
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Direct filesystem helper to migrate an Nx monorepo on disk (`nx.json`, `angular.json`, and
|
|
397
|
+
* all discovered `project.json` files).
|
|
398
|
+
*/
|
|
399
|
+
async function migrateNxWorkspaceOnDisk(workspaceRoot, options = {}) {
|
|
400
|
+
const absRoot = path.resolve(workspaceRoot);
|
|
401
|
+
const diskTree = {
|
|
402
|
+
exists(rel) {
|
|
403
|
+
return fs.existsSync(path.join(absRoot, rel.replace(/^\/+/, '')));
|
|
404
|
+
},
|
|
405
|
+
read(rel) {
|
|
406
|
+
return fs.readFileSync(path.join(absRoot, rel.replace(/^\/+/, '')));
|
|
407
|
+
},
|
|
408
|
+
write(rel, content) {
|
|
409
|
+
if (!options.dryRun) {
|
|
410
|
+
fs.writeFileSync(path.join(absRoot, rel.replace(/^\/+/, '')), content, 'utf8');
|
|
411
|
+
}
|
|
412
|
+
},
|
|
413
|
+
children(relDir) {
|
|
414
|
+
const target = path.join(absRoot, relDir);
|
|
415
|
+
if (!fs.existsSync(target) || !fs.statSync(target).isDirectory()) return [];
|
|
416
|
+
return fs.readdirSync(target);
|
|
417
|
+
},
|
|
418
|
+
isFile(rel) {
|
|
419
|
+
const target = path.join(absRoot, rel);
|
|
420
|
+
return fs.existsSync(target) && fs.statSync(target).isFile();
|
|
421
|
+
},
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
const updates = executeInitOnTree(diskTree, options, options.logger);
|
|
425
|
+
return { updates };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
module.exports = initGenerator;
|
|
429
|
+
module.exports.default = initGenerator;
|
|
430
|
+
module.exports.initGenerator = initGenerator;
|
|
431
|
+
module.exports.NX_EXECUTOR_MIGRATIONS = NX_EXECUTOR_MIGRATIONS;
|
|
432
|
+
module.exports.migrateProjectJsonText = migrateProjectJsonText;
|
|
433
|
+
module.exports.migrateNxJsonText = migrateNxJsonText;
|
|
434
|
+
module.exports.migrateNxWorkspaceOnDisk = migrateNxWorkspaceOnDisk;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema",
|
|
3
|
+
"$id": "NgrsNxInitGenerator",
|
|
4
|
+
"title": "@ngrs/nx Init Generator Schema",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"description": "Initializes @ngrs/nx in an Nx monorepo and migrates Angular project.json / nx.json / angular.json targets.",
|
|
7
|
+
"properties": {
|
|
8
|
+
"project": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Optional specific Nx project name to migrate. Defaults to all Angular projects in the workspace."
|
|
11
|
+
},
|
|
12
|
+
"addPlugin": {
|
|
13
|
+
"type": "boolean",
|
|
14
|
+
"default": true,
|
|
15
|
+
"description": "Register '@ngrs/nx/plugin' in nx.json plugins array for automatic cache outputs inference."
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"additionalProperties": true
|
|
19
|
+
}
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const plugin = require('./plugin.cjs');
|
|
4
|
+
const initMod = require('./generators/init/index.cjs');
|
|
5
|
+
const { applicationExecutor } = require('./executors/application/index.cjs');
|
|
6
|
+
const { devServerExecutor } = require('./executors/dev-server/index.cjs');
|
|
7
|
+
const { unitTestExecutor } = require('./executors/unit-test/index.cjs');
|
|
8
|
+
const { extractI18nExecutor } = require('./executors/extract-i18n/index.cjs');
|
|
9
|
+
const { packageExecutor } = require('./executors/package/index.cjs');
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
...plugin,
|
|
13
|
+
...initMod,
|
|
14
|
+
applicationExecutor,
|
|
15
|
+
devServerExecutor,
|
|
16
|
+
unitTestExecutor,
|
|
17
|
+
extractI18nExecutor,
|
|
18
|
+
packageExecutor,
|
|
19
|
+
};
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export declare const createNodesV2: readonly [
|
|
2
|
+
string,
|
|
3
|
+
(
|
|
4
|
+
configFiles: readonly string[],
|
|
5
|
+
options: Record<string, unknown> | undefined,
|
|
6
|
+
context: { workspaceRoot?: string },
|
|
7
|
+
) => Promise<Array<[string, Record<string, unknown>]>>,
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
export declare const createNodes: readonly [
|
|
11
|
+
string,
|
|
12
|
+
(
|
|
13
|
+
configFile: string,
|
|
14
|
+
options: Record<string, unknown> | undefined,
|
|
15
|
+
context: { workspaceRoot?: string },
|
|
16
|
+
) => Promise<Record<string, unknown>>,
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export declare const NX_EXECUTOR_MIGRATIONS: Readonly<Record<string, string>>;
|
|
20
|
+
|
|
21
|
+
export declare function applicationExecutor(
|
|
22
|
+
options?: Record<string, any>,
|
|
23
|
+
context?: any,
|
|
24
|
+
extensions?: Record<string, any>,
|
|
25
|
+
): AsyncGenerator<any>;
|
|
26
|
+
|
|
27
|
+
export declare function devServerExecutor(
|
|
28
|
+
options?: Record<string, any>,
|
|
29
|
+
context?: any,
|
|
30
|
+
extensions?: Record<string, any>,
|
|
31
|
+
): AsyncGenerator<any>;
|
|
32
|
+
|
|
33
|
+
export declare function unitTestExecutor(
|
|
34
|
+
options?: Record<string, any>,
|
|
35
|
+
context?: any,
|
|
36
|
+
): AsyncGenerator<any>;
|
|
37
|
+
|
|
38
|
+
export declare function extractI18nExecutor(
|
|
39
|
+
options?: Record<string, any>,
|
|
40
|
+
context?: any,
|
|
41
|
+
): Promise<{ success: boolean; outputFiles: string[] }>;
|
|
42
|
+
|
|
43
|
+
export declare function packageExecutor(
|
|
44
|
+
options?: Record<string, any>,
|
|
45
|
+
context?: any,
|
|
46
|
+
): Promise<{ success: boolean; outputPath?: string; error?: string }>;
|
|
47
|
+
|
|
48
|
+
export declare function initGenerator(treeOrOptions?: any, maybeOptions?: any): any;
|
|
49
|
+
|
|
50
|
+
export declare function migrateNxWorkspaceOnDisk(
|
|
51
|
+
workspaceRoot: string,
|
|
52
|
+
options?: { project?: string; addPlugin?: boolean; dryRun?: boolean; logger?: any },
|
|
53
|
+
): Promise<{ updates: Array<Record<string, string>> }>;
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
|
|
3
|
+
const localRequire = createRequire(import.meta.url);
|
|
4
|
+
const initMod = localRequire('./generators/init/index.cjs');
|
|
5
|
+
|
|
6
|
+
export { applicationExecutor } from './executors/application/index.mjs';
|
|
7
|
+
export { toArchitectContext } from './executors/context-adapter.mjs';
|
|
8
|
+
export { devServerExecutor } from './executors/dev-server/index.mjs';
|
|
9
|
+
export { extractI18nExecutor } from './executors/extract-i18n/index.mjs';
|
|
10
|
+
export { packageExecutor } from './executors/package/index.mjs';
|
|
11
|
+
export { unitTestExecutor } from './executors/unit-test/index.mjs';
|
|
12
|
+
export { createNodes, createNodesV2 } from './plugin.mjs';
|
|
13
|
+
|
|
14
|
+
export const {
|
|
15
|
+
initGenerator,
|
|
16
|
+
NX_EXECUTOR_MIGRATIONS,
|
|
17
|
+
migrateProjectJsonText,
|
|
18
|
+
migrateNxJsonText,
|
|
19
|
+
migrateNxWorkspaceOnDisk,
|
|
20
|
+
} = initMod;
|