@ngrs/nx 0.0.0-stage → 0.2.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/src/plugin.cjs ADDED
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const { parse: parseJsonc } = require('jsonc-parser');
6
+
7
+ const NGRS_BUILD_EXECUTORS = new Set([
8
+ '@ngrs/nx:application',
9
+ '@ngrs/nx:browser-esbuild',
10
+ '@ngrs/nx:browser',
11
+ '@ngrs/nx:package',
12
+ '@ngrs/build:application',
13
+ '@ngrs/build:browser-esbuild',
14
+ '@ngrs/build:browser',
15
+ ]);
16
+
17
+ const NGRS_TEST_EXECUTORS = new Set(['@ngrs/nx:unit-test', '@ngrs/build:unit-test']);
18
+
19
+ function processSync(configFilePath, context) {
20
+ const workspaceRoot = (context && context.workspaceRoot) || process.cwd();
21
+ const absPath = path.resolve(workspaceRoot, configFilePath);
22
+ let raw;
23
+ try {
24
+ raw = fs.readFileSync(absPath, 'utf8');
25
+ } catch {
26
+ return {};
27
+ }
28
+ const parsed = parseJsonc(raw, [], { allowTrailingComma: true });
29
+ if (!parsed || typeof parsed !== 'object') return {};
30
+
31
+ const projectRoot =
32
+ path.relative(workspaceRoot, path.dirname(absPath)).replace(/\\/g, '/') || '.';
33
+ const targets = parsed.targets || parsed.architect || {};
34
+ const augmentedTargets = {};
35
+
36
+ for (const [targetName, targetDef] of Object.entries(targets)) {
37
+ const executor = targetDef && (targetDef.executor || targetDef.builder);
38
+ if (NGRS_BUILD_EXECUTORS.has(executor)) {
39
+ const rawOut = targetDef.options && targetDef.options.outputPath;
40
+ const outputs =
41
+ typeof rawOut === 'string'
42
+ ? ['{options.outputPath}']
43
+ : rawOut && typeof rawOut === 'object' && rawOut.base
44
+ ? ['{options.outputPath.base}']
45
+ : [`{workspaceRoot}/dist/${projectRoot}`];
46
+ augmentedTargets[targetName] = {
47
+ ...targetDef,
48
+ cache: targetDef.cache ?? true,
49
+ outputs: targetDef.outputs ?? outputs,
50
+ };
51
+ } else if (NGRS_TEST_EXECUTORS.has(executor)) {
52
+ augmentedTargets[targetName] = {
53
+ ...targetDef,
54
+ cache: targetDef.cache ?? true,
55
+ outputs: targetDef.outputs ?? [`{workspaceRoot}/coverage/${projectRoot}`],
56
+ };
57
+ }
58
+ }
59
+
60
+ if (Object.keys(augmentedTargets).length === 0) return {};
61
+ return {
62
+ projects: {
63
+ [projectRoot]: {
64
+ targets: augmentedTargets,
65
+ },
66
+ },
67
+ };
68
+ }
69
+
70
+ const createNodesV2 = [
71
+ '**/project.json',
72
+ async (configFiles, _options, context) =>
73
+ configFiles.map((file) => [file, processSync(file, context)]),
74
+ ];
75
+
76
+ const createNodes = ['**/project.json', (file, _options, context) => processSync(file, context)];
77
+
78
+ module.exports = { createNodes, createNodesV2 };
package/src/plugin.mjs ADDED
@@ -0,0 +1,114 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { parse as parseJsonc } from 'jsonc-parser';
4
+
5
+ const NGRS_BUILD_EXECUTORS = new Set([
6
+ '@ngrs/nx:application',
7
+ '@ngrs/nx:browser-esbuild',
8
+ '@ngrs/nx:browser',
9
+ '@ngrs/nx:package',
10
+ '@ngrs/build:application',
11
+ '@ngrs/build:browser-esbuild',
12
+ '@ngrs/build:browser',
13
+ ]);
14
+
15
+ const NGRS_TEST_EXECUTORS = new Set(['@ngrs/nx:unit-test', '@ngrs/build:unit-test']);
16
+
17
+ function inferTargetCacheMetadata(targetDef, projectRoot) {
18
+ const executor = targetDef?.executor || targetDef?.builder;
19
+ if (typeof executor !== 'string') {
20
+ return null;
21
+ }
22
+
23
+ if (NGRS_BUILD_EXECUTORS.has(executor)) {
24
+ const rawOut = targetDef?.options?.outputPath;
25
+ let outputs;
26
+ if (typeof rawOut === 'string') {
27
+ outputs = ['{options.outputPath}'];
28
+ } else if (rawOut && typeof rawOut === 'object' && rawOut.base) {
29
+ outputs = ['{options.outputPath.base}'];
30
+ } else {
31
+ outputs = [`{workspaceRoot}/dist/${projectRoot}`];
32
+ }
33
+ return {
34
+ cache: targetDef.cache ?? true,
35
+ outputs: targetDef.outputs ?? outputs,
36
+ };
37
+ }
38
+
39
+ if (NGRS_TEST_EXECUTORS.has(executor)) {
40
+ return {
41
+ cache: targetDef.cache ?? true,
42
+ outputs: targetDef.outputs ?? [`{workspaceRoot}/coverage/${projectRoot}`],
43
+ };
44
+ }
45
+
46
+ return null;
47
+ }
48
+
49
+ async function processProjectConfigFile(configFilePath, context) {
50
+ const workspaceRoot = context?.workspaceRoot || process.cwd();
51
+ const absPath = path.resolve(workspaceRoot, configFilePath);
52
+ let raw;
53
+ try {
54
+ raw = await fs.readFile(absPath, 'utf8');
55
+ } catch {
56
+ return {};
57
+ }
58
+
59
+ const parsed = parseJsonc(raw, [], { allowTrailingComma: true });
60
+ if (!parsed || typeof parsed !== 'object') {
61
+ return {};
62
+ }
63
+
64
+ const projectRoot =
65
+ path.relative(workspaceRoot, path.dirname(absPath)).replace(/\\/g, '/') || '.';
66
+ const targets = parsed.targets || parsed.architect || {};
67
+ const augmentedTargets = {};
68
+
69
+ for (const [targetName, targetDef] of Object.entries(targets)) {
70
+ const inferred = inferTargetCacheMetadata(targetDef, projectRoot);
71
+ if (inferred) {
72
+ augmentedTargets[targetName] = {
73
+ ...targetDef,
74
+ ...inferred,
75
+ };
76
+ }
77
+ }
78
+
79
+ if (Object.keys(augmentedTargets).length === 0) {
80
+ return {};
81
+ }
82
+
83
+ return {
84
+ projects: {
85
+ [projectRoot]: {
86
+ targets: augmentedTargets,
87
+ },
88
+ },
89
+ };
90
+ }
91
+
92
+ /**
93
+ * Nx Crystal (`createNodesV2`) plugin automatically inferring `cache: true` and `outputs`
94
+ * for `@ngrs/nx:*` and `@ngrs/build:*` build & test targets so Nx Computation Caching and
95
+ * `nx affected` work out-of-the-box.
96
+ */
97
+ export const createNodesV2 = [
98
+ '**/project.json',
99
+ async (configFiles, _options, context) => {
100
+ const results = [];
101
+ for (const configFile of configFiles) {
102
+ const nodeResult = await processProjectConfigFile(configFile, context);
103
+ results.push([configFile, nodeResult]);
104
+ }
105
+ return results;
106
+ },
107
+ ];
108
+
109
+ export const createNodes = [
110
+ '**/project.json',
111
+ (configFile, _options, context) => processProjectConfigFile(configFile, context),
112
+ ];
113
+
114
+ export default { createNodes, createNodesV2 };