nx 19.4.0 → 19.5.0-beta.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.
Files changed (35) hide show
  1. package/package.json +30 -26
  2. package/src/command-line/graph/graph.js +7 -2
  3. package/src/command-line/reset/reset.js +1 -1
  4. package/src/command-line/watch/watch.js +6 -0
  5. package/src/commands-runner/create-command-graph.js +32 -10
  6. package/src/core/graph/main.js +1 -1
  7. package/src/core/graph/styles.css +1 -1
  8. package/src/daemon/client/client.d.ts +1 -0
  9. package/src/daemon/client/client.js +14 -2
  10. package/src/devkit-exports.d.ts +1 -0
  11. package/src/devkit-exports.js +3 -1
  12. package/src/native/browser.js +1 -0
  13. package/src/native/index.d.ts +178 -138
  14. package/src/native/index.js +16 -0
  15. package/src/native/native-bindings.js +352 -238
  16. package/src/native/nx.wasi-browser.js +108 -0
  17. package/src/native/nx.wasi.cjs +139 -0
  18. package/src/native/nx.wasm32-wasi.wasm +0 -0
  19. package/src/native/wasi-worker-browser.mjs +32 -0
  20. package/src/native/wasi-worker.mjs +63 -0
  21. package/src/plugins/js/utils/register.js +29 -13
  22. package/src/project-graph/plugins/internal-api.js +0 -1
  23. package/src/project-graph/plugins/isolation/plugin-pool.js +1 -1
  24. package/src/project-graph/utils/project-configuration-utils.d.ts +8 -6
  25. package/src/project-graph/utils/project-configuration-utils.js +109 -44
  26. package/src/project-graph/utils/retrieve-workspace-files.d.ts +3 -3
  27. package/src/tasks-runner/pseudo-terminal.js +3 -0
  28. package/src/tasks-runner/task-graph-utils.d.ts +1 -1
  29. package/src/tasks-runner/task-graph-utils.js +4 -4
  30. package/src/tasks-runner/utils.js +1 -1
  31. package/src/utils/nx-plugin.deprecated.js +0 -2
  32. package/src/plugins/target-defaults/symbols.d.ts +0 -17
  33. package/src/plugins/target-defaults/symbols.js +0 -20
  34. package/src/plugins/target-defaults/target-defaults-plugin.d.ts +0 -121
  35. package/src/plugins/target-defaults/target-defaults-plugin.js +0 -176
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeTarget = exports.readTargetDefaultsForTarget = exports.resolveNxTokensInOptions = exports.isCompatibleTarget = exports.mergeTargetConfigurations = exports.validateProject = exports.readProjectConfigurationsFromRootMap = exports.createProjectConfigurations = exports.mergeMetadata = exports.mergeProjectConfigurationIntoRootMap = void 0;
3
+ exports.normalizeTarget = exports.readTargetDefaultsForTarget = exports.resolveNxTokensInOptions = exports.isCompatibleTarget = exports.mergeTargetConfigurations = exports.mergeTargetDefaultWithTargetDefinition = exports.validateProject = exports.readProjectConfigurationsFromRootMap = exports.createProjectConfigurations = exports.mergeMetadata = exports.mergeProjectConfigurationIntoRootMap = void 0;
4
4
  const logger_1 = require("../../utils/logger");
5
5
  const fileutils_1 = require("../../utils/fileutils");
6
6
  const workspace_root_1 = require("../../utils/workspace-root");
7
- const symbols_1 = require("../../plugins/target-defaults/symbols");
8
7
  const minimatch_1 = require("minimatch");
9
8
  const path_1 = require("path");
10
9
  const perf_hooks_1 = require("perf_hooks");
@@ -120,22 +119,13 @@ skipTargetNormalization) {
120
119
  // Always set source map info for the target, but don't overwrite info already there
121
120
  // if augmenting an existing target.
122
121
  const target = project.targets?.[targetName];
123
- if (sourceMap && !target?.[symbols_1.ONLY_MODIFIES_EXISTING_TARGET]) {
122
+ if (sourceMap) {
124
123
  sourceMap[`targets.${targetName}`] = sourceInformation;
125
124
  }
126
- // If ONLY_MODIFIES_EXISTING_TARGET is true, and its not on the matching project
127
- // we shouldn't merge its info into the graph
128
- if (target?.[symbols_1.ONLY_MODIFIES_EXISTING_TARGET] &&
129
- !matchingProject.targets?.[targetName]) {
130
- continue;
131
- }
132
125
  const normalizedTarget = skipTargetNormalization
133
126
  ? target
134
127
  : resolveCommandSyntacticSugar(target, project.root);
135
128
  const mergedTarget = mergeTargetConfigurations(normalizedTarget, matchingProject.targets?.[targetName], sourceMap, sourceInformation, `targets.${targetName}`);
136
- // We don't want the symbol to live on past the merge process
137
- if (mergedTarget?.[symbols_1.ONLY_MODIFIES_EXISTING_TARGET])
138
- delete mergedTarget?.[symbols_1.ONLY_MODIFIES_EXISTING_TARGET];
139
129
  updatedProjectConfiguration.targets[targetName] = mergedTarget;
140
130
  }
141
131
  }
@@ -263,7 +253,7 @@ plugins) {
263
253
  results.push(r);
264
254
  }
265
255
  return Promise.all(results).then((results) => {
266
- const { projectRootMap, externalNodes, rootMap, configurationSourceMaps } = mergeCreateNodesResults(results, errors);
256
+ const { projectRootMap, externalNodes, rootMap, configurationSourceMaps } = mergeCreateNodesResults(results, nxJson, errors);
267
257
  perf_hooks_1.performance.mark('build-project-configs:end');
268
258
  perf_hooks_1.performance.measure('build-project-configs', 'build-project-configs:start', 'build-project-configs:end');
269
259
  if (errors.length === 0) {
@@ -287,7 +277,7 @@ plugins) {
287
277
  });
288
278
  }
289
279
  exports.createProjectConfigurations = createProjectConfigurations;
290
- function mergeCreateNodesResults(results, errors) {
280
+ function mergeCreateNodesResults(results, nxJsonConfiguration, errors) {
291
281
  perf_hooks_1.performance.mark('createNodes:merge - start');
292
282
  const projectRootMap = {};
293
283
  const externalNodes = {};
@@ -296,9 +286,6 @@ function mergeCreateNodesResults(results, errors) {
296
286
  const [pluginName, file, nodes] = result;
297
287
  const { projects: projectNodes, externalNodes: pluginExternalNodes } = nodes;
298
288
  const sourceInfo = [file, pluginName];
299
- if (result[symbols_1.OVERRIDE_SOURCE_FILE]) {
300
- sourceInfo[0] = result[symbols_1.OVERRIDE_SOURCE_FILE];
301
- }
302
289
  for (const node in projectNodes) {
303
290
  // Handles `{projects: {'libs/foo': undefined}}`.
304
291
  if (!projectNodes[node]) {
@@ -322,7 +309,7 @@ function mergeCreateNodesResults(results, errors) {
322
309
  Object.assign(externalNodes, pluginExternalNodes);
323
310
  }
324
311
  try {
325
- validateAndNormalizeProjectRootMap(projectRootMap);
312
+ validateAndNormalizeProjectRootMap(projectRootMap, nxJsonConfiguration, configurationSourceMaps);
326
313
  }
327
314
  catch (e) {
328
315
  if ((0, error_types_1.isProjectsWithNoNameError)(e) ||
@@ -401,7 +388,7 @@ function readProjectConfigurationsFromRootMap(projectRootMap) {
401
388
  return projects;
402
389
  }
403
390
  exports.readProjectConfigurationsFromRootMap = readProjectConfigurationsFromRootMap;
404
- function validateAndNormalizeProjectRootMap(projectRootMap) {
391
+ function validateAndNormalizeProjectRootMap(projectRootMap, nxJsonConfiguration, sourceMaps = {}) {
405
392
  // Name -> Project, used to validate that all projects have unique names
406
393
  const projects = {};
407
394
  // If there are projects that have the same name, that is an error.
@@ -434,24 +421,7 @@ function validateAndNormalizeProjectRootMap(projectRootMap) {
434
421
  throw e;
435
422
  }
436
423
  }
437
- for (const targetName in project.targets) {
438
- project.targets[targetName] = normalizeTarget(project.targets[targetName], project);
439
- if (
440
- // If the target has no executor or command, it doesn't do anything
441
- !project.targets[targetName].executor &&
442
- !project.targets[targetName].command) {
443
- // But it may have dependencies that do something
444
- if (project.targets[targetName].dependsOn &&
445
- project.targets[targetName].dependsOn.length > 0) {
446
- project.targets[targetName].executor = 'nx:noop';
447
- }
448
- else {
449
- // If it does nothing, and has no depenencies,
450
- // we can remove it.
451
- delete project.targets[targetName];
452
- }
453
- }
454
- }
424
+ normalizeTargets(project, sourceMaps, nxJsonConfiguration);
455
425
  }
456
426
  if (conflicts.size > 0) {
457
427
  throw new error_types_1.MultipleProjectsWithSameNameError(conflicts, projects);
@@ -461,6 +431,33 @@ function validateAndNormalizeProjectRootMap(projectRootMap) {
461
431
  }
462
432
  return projectRootMap;
463
433
  }
434
+ function normalizeTargets(project, sourceMaps, nxJsonConfiguration) {
435
+ for (const targetName in project.targets) {
436
+ project.targets[targetName] = normalizeTarget(project.targets[targetName], project);
437
+ const projectSourceMaps = sourceMaps[project.root];
438
+ const targetConfig = project.targets[targetName];
439
+ const targetDefaults = readTargetDefaultsForTarget(targetName, nxJsonConfiguration.targetDefaults, targetConfig.executor);
440
+ // We only apply defaults if they exist
441
+ if (targetDefaults && isCompatibleTarget(targetConfig, targetDefaults)) {
442
+ project.targets[targetName] = mergeTargetDefaultWithTargetDefinition(targetName, project, normalizeTarget(targetDefaults, project), projectSourceMaps);
443
+ }
444
+ if (
445
+ // If the target has no executor or command, it doesn't do anything
446
+ !project.targets[targetName].executor &&
447
+ !project.targets[targetName].command) {
448
+ // But it may have dependencies that do something
449
+ if (project.targets[targetName].dependsOn &&
450
+ project.targets[targetName].dependsOn.length > 0) {
451
+ project.targets[targetName].executor = 'nx:noop';
452
+ }
453
+ else {
454
+ // If it does nothing, and has no depenencies,
455
+ // we can remove it.
456
+ delete project.targets[targetName];
457
+ }
458
+ }
459
+ }
460
+ }
464
461
  function validateProject(project,
465
462
  // name -> project
466
463
  knownProjects) {
@@ -482,6 +479,75 @@ knownProjects) {
482
479
  }
483
480
  }
484
481
  exports.validateProject = validateProject;
482
+ function targetDefaultShouldBeApplied(key, sourceMap) {
483
+ const sourceInfo = sourceMap[key];
484
+ if (!sourceInfo) {
485
+ return true;
486
+ }
487
+ // The defined value of the target is from a plugin that
488
+ // isn't part of Nx's core plugins, so target defaults are
489
+ // applied on top of it.
490
+ const [, plugin] = sourceInfo;
491
+ return !plugin?.startsWith('nx/');
492
+ }
493
+ function mergeTargetDefaultWithTargetDefinition(targetName, project, targetDefault, sourceMap) {
494
+ const targetDefinition = project.targets[targetName] ?? {};
495
+ const result = JSON.parse(JSON.stringify(targetDefinition));
496
+ for (const key in targetDefault) {
497
+ switch (key) {
498
+ case 'options': {
499
+ const normalizedDefaults = resolveNxTokensInOptions(targetDefault.options, project, targetName);
500
+ for (const optionKey in normalizedDefaults) {
501
+ const sourceMapKey = `targets.${targetName}.options.${optionKey}`;
502
+ if (targetDefinition.options[optionKey] === undefined ||
503
+ targetDefaultShouldBeApplied(sourceMapKey, sourceMap)) {
504
+ result.options[optionKey] = targetDefault.options[optionKey];
505
+ sourceMap[sourceMapKey] = ['nx.json', 'nx/target-defaults'];
506
+ }
507
+ }
508
+ break;
509
+ }
510
+ case 'configurations': {
511
+ if (!result.configurations) {
512
+ result.configurations = {};
513
+ sourceMap[`targets.${targetName}.configurations`] = [
514
+ 'nx.json',
515
+ 'nx/target-defaults',
516
+ ];
517
+ }
518
+ for (const configuration in targetDefault.configurations) {
519
+ if (!result.configurations[configuration]) {
520
+ result.configurations[configuration] = {};
521
+ sourceMap[`targets.${targetName}.configurations.${configuration}`] =
522
+ ['nx.json', 'nx/target-defaults'];
523
+ }
524
+ const normalizedConfigurationDefaults = resolveNxTokensInOptions(targetDefault.configurations[configuration], project, targetName);
525
+ for (const configurationKey in normalizedConfigurationDefaults) {
526
+ const sourceMapKey = `targets.${targetName}.configurations.${configuration}.${configurationKey}`;
527
+ if (targetDefinition.configurations?.[configuration]?.[configurationKey] === undefined ||
528
+ targetDefaultShouldBeApplied(sourceMapKey, sourceMap)) {
529
+ result.configurations[configuration][configurationKey] =
530
+ targetDefault.configurations[configuration][configurationKey];
531
+ sourceMap[sourceMapKey] = ['nx.json', 'nx/target-defaults'];
532
+ }
533
+ }
534
+ }
535
+ break;
536
+ }
537
+ default: {
538
+ const sourceMapKey = `targets.${targetName}.${key}`;
539
+ if (targetDefinition[key] === undefined ||
540
+ targetDefaultShouldBeApplied(sourceMapKey, sourceMap)) {
541
+ result[key] = targetDefault[key];
542
+ sourceMap[sourceMapKey] = ['nx.json', 'nx/target-defaults'];
543
+ }
544
+ break;
545
+ }
546
+ }
547
+ }
548
+ return result;
549
+ }
550
+ exports.mergeTargetDefaultWithTargetDefinition = mergeTargetDefaultWithTargetDefinition;
485
551
  /**
486
552
  * Merges two targets.
487
553
  *
@@ -500,13 +566,6 @@ function mergeTargetConfigurations(target, baseTarget, projectConfigSourceMap, s
500
566
  // Target is "compatible", e.g. executor is defined only once or is the same
501
567
  // in both places. This means that it is likely safe to merge
502
568
  const isCompatible = isCompatibleTarget(baseTarget ?? {}, target);
503
- // If the targets are not compatible, we would normally overwrite the old target
504
- // with the new one. However, we have a special case for targets that have the
505
- // ONLY_MODIFIES_EXISTING_TARGET symbol set. This prevents the merged target
506
- // equaling info that should have only been used to modify the existing target.
507
- if (!isCompatible && target[symbols_1.ONLY_MODIFIES_EXISTING_TARGET]) {
508
- return baseTarget;
509
- }
510
569
  if (!isCompatible && projectConfigSourceMap) {
511
570
  // if the target is not compatible, we will simply override the options
512
571
  // we have to delete old entries from the source map
@@ -680,6 +739,12 @@ function resolveCommandSyntacticSugar(target, key) {
680
739
  };
681
740
  }
682
741
  }
742
+ /**
743
+ * Expand's `command` syntactic sugar and replaces tokens in options.
744
+ * @param target The target to normalize
745
+ * @param project The project that the target belongs to
746
+ * @returns The normalized target configuration
747
+ */
683
748
  function normalizeTarget(target, project) {
684
749
  target = resolveCommandSyntacticSugar(target, project.root);
685
750
  target.options = resolveNxTokensInOptions(target.options, project, `${project.root}:${target}`);
@@ -9,12 +9,12 @@ import { LoadedNxPlugin } from '../plugins/internal-api';
9
9
  * @param nxJson
10
10
  */
11
11
  export declare function retrieveWorkspaceFiles(workspaceRoot: string, projectRootMap: Record<string, string>): Promise<{
12
- allWorkspaceFiles: import("nx/src/devkit-exports").FileData[];
12
+ allWorkspaceFiles: import("../file-utils").FileData[];
13
13
  fileMap: {
14
14
  projectFileMap: ProjectFiles;
15
- nonProjectFiles: import("nx/src/native").FileData[];
15
+ nonProjectFiles: import("../../native").FileData[];
16
16
  };
17
- rustReferences: import("nx/src/native").NxWorkspaceFilesExternals;
17
+ rustReferences: import("../../native").NxWorkspaceFilesExternals;
18
18
  }>;
19
19
  /**
20
20
  * Walk through the workspace and return `ProjectConfigurations`. Only use this if the projectFileMap is not needed.
@@ -139,6 +139,9 @@ function messageToCode(message) {
139
139
  }
140
140
  }
141
141
  function supportedPtyPlatform() {
142
+ if (native_1.IS_WASM) {
143
+ return false;
144
+ }
142
145
  if (process.platform !== 'win32') {
143
146
  return true;
144
147
  }
@@ -1,4 +1,4 @@
1
- export declare function findCycle(taskGraph: {
1
+ export declare function findCycle(graph: {
2
2
  dependencies: Record<string, string[]>;
3
3
  }): string[] | null;
4
4
  export declare function makeAcyclic(graph: {
@@ -14,13 +14,13 @@ function _findCycle(graph, id, visited, path) {
14
14
  }
15
15
  return null;
16
16
  }
17
- function findCycle(taskGraph) {
17
+ function findCycle(graph) {
18
18
  const visited = {};
19
- for (const t of Object.keys(taskGraph.dependencies)) {
19
+ for (const t of Object.keys(graph.dependencies)) {
20
20
  visited[t] = false;
21
21
  }
22
- for (const t of Object.keys(taskGraph.dependencies)) {
23
- const cycle = _findCycle(taskGraph, t, visited, [t]);
22
+ for (const t of Object.keys(graph.dependencies)) {
23
+ const cycle = _findCycle(graph, t, visited, [t]);
24
24
  if (cycle)
25
25
  return cycle;
26
26
  }
@@ -131,7 +131,7 @@ function getOutputsForTargetAndConfiguration(taskTargetOrTask, overridesOrNode,
131
131
  const { target, configuration } = taskTarget;
132
132
  const targetConfiguration = node.data.targets[target];
133
133
  const options = {
134
- ...targetConfiguration.options,
134
+ ...targetConfiguration?.options,
135
135
  ...targetConfiguration?.configurations?.[configuration],
136
136
  ...overrides,
137
137
  };
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getDefaultPluginsSync = void 0;
4
4
  const angular_json_1 = require("../adapter/angular-json");
5
5
  const project_json_1 = require("../plugins/project-json/build-nodes/project-json");
6
- const target_defaults_plugin_1 = require("../plugins/target-defaults/target-defaults-plugin");
7
6
  const PackageJsonWorkspacesPlugin = require("../plugins/package-json-workspaces");
8
7
  /**
9
8
  * @todo(@agentender) v20: Remove this fn when we remove readWorkspaceConfig
@@ -14,7 +13,6 @@ function getDefaultPluginsSync(root) {
14
13
  ...((0, angular_json_1.shouldMergeAngularProjects)(root, false)
15
14
  ? [require('../adapter/angular-json').NxAngularJsonPlugin]
16
15
  : []),
17
- target_defaults_plugin_1.default,
18
16
  PackageJsonWorkspacesPlugin,
19
17
  project_json_1.default,
20
18
  ];
@@ -1,17 +0,0 @@
1
- /**
2
- * This marks that a target provides information which should modify a target already registered
3
- * on the project via other plugins. If the target has not already been registered, and this symbol is true,
4
- * the information provided by it will be discarded.
5
- *
6
- * NOTE: This cannot be a symbol, as they are not serialized in JSON the communication
7
- * between the plugin-worker and the main process.
8
- */
9
- export declare const ONLY_MODIFIES_EXISTING_TARGET = "NX_ONLY_MODIFIES_EXISTING_TARGET";
10
- /**
11
- * This is used to override the source file for the target defaults plugin.
12
- * This allows the plugin to use the project files as the context, but point to nx.json as the source file.
13
- *
14
- * NOTE: This cannot be a symbol, as they are not serialized in JSON the communication
15
- * between the plugin-worker and the main process.
16
- */
17
- export declare const OVERRIDE_SOURCE_FILE = "NX_OVERRIDE_SOURCE_FILE";
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OVERRIDE_SOURCE_FILE = exports.ONLY_MODIFIES_EXISTING_TARGET = void 0;
4
- /**
5
- * This marks that a target provides information which should modify a target already registered
6
- * on the project via other plugins. If the target has not already been registered, and this symbol is true,
7
- * the information provided by it will be discarded.
8
- *
9
- * NOTE: This cannot be a symbol, as they are not serialized in JSON the communication
10
- * between the plugin-worker and the main process.
11
- */
12
- exports.ONLY_MODIFIES_EXISTING_TARGET = 'NX_ONLY_MODIFIES_EXISTING_TARGET';
13
- /**
14
- * This is used to override the source file for the target defaults plugin.
15
- * This allows the plugin to use the project files as the context, but point to nx.json as the source file.
16
- *
17
- * NOTE: This cannot be a symbol, as they are not serialized in JSON the communication
18
- * between the plugin-worker and the main process.
19
- */
20
- exports.OVERRIDE_SOURCE_FILE = 'NX_OVERRIDE_SOURCE_FILE';
@@ -1,121 +0,0 @@
1
- import { TargetConfiguration } from '../../config/workspace-json-project-json';
2
- import { NxPluginV2 } from '../../project-graph/plugins';
3
- export declare const TargetDefaultsPlugin: NxPluginV2;
4
- export default TargetDefaultsPlugin;
5
- /**
6
- * This fn gets target info that would make a target uniquely compatible
7
- * with what is described by project.json or package.json. As the merge process
8
- * for config happens, without this, the target defaults may be compatible
9
- * with a config from a plugin and then that combined target be incompatible
10
- * with the project json configuration resulting in the target default values
11
- * being scrapped. By adding enough information from the project.json / package.json,
12
- * we can make sure that the target after merging is compatible with the defined target.
13
- */
14
- export declare function getTargetInfo(target: string, projectJsonTargets: Record<string, TargetConfiguration>, packageJsonTargets: Record<string, TargetConfiguration>): {
15
- command: string;
16
- metadata: {
17
- [x: string]: any;
18
- description?: string;
19
- technologies?: string[];
20
- nonAtomizedTarget?: string;
21
- help?: {
22
- command: string;
23
- example: {
24
- options?: Record<string, unknown>;
25
- args?: string[];
26
- };
27
- };
28
- };
29
- executor?: undefined;
30
- options?: undefined;
31
- } | {
32
- executor: string;
33
- options: {
34
- command: any;
35
- commands?: undefined;
36
- script?: undefined;
37
- };
38
- metadata: {
39
- [x: string]: any;
40
- description?: string;
41
- technologies?: string[];
42
- nonAtomizedTarget?: string;
43
- help?: {
44
- command: string;
45
- example: {
46
- options?: Record<string, unknown>;
47
- args?: string[];
48
- };
49
- };
50
- };
51
- command?: undefined;
52
- } | {
53
- executor: string;
54
- options: {
55
- commands: any;
56
- command?: undefined;
57
- script?: undefined;
58
- };
59
- metadata: {
60
- [x: string]: any;
61
- description?: string;
62
- technologies?: string[];
63
- nonAtomizedTarget?: string;
64
- help?: {
65
- command: string;
66
- example: {
67
- options?: Record<string, unknown>;
68
- args?: string[];
69
- };
70
- };
71
- };
72
- command?: undefined;
73
- } | {
74
- executor: string;
75
- metadata: {
76
- [x: string]: any;
77
- description?: string;
78
- technologies?: string[];
79
- nonAtomizedTarget?: string;
80
- help?: {
81
- command: string;
82
- example: {
83
- options?: Record<string, unknown>;
84
- args?: string[];
85
- };
86
- };
87
- };
88
- command?: undefined;
89
- options?: undefined;
90
- } | {
91
- executor: string;
92
- options: {
93
- script: any;
94
- command?: undefined;
95
- commands?: undefined;
96
- };
97
- metadata: {
98
- [x: string]: any;
99
- description?: string;
100
- technologies?: string[];
101
- nonAtomizedTarget?: string;
102
- help?: {
103
- command: string;
104
- example: {
105
- options?: Record<string, unknown>;
106
- args?: string[];
107
- };
108
- };
109
- };
110
- command?: undefined;
111
- } | {
112
- executor: string;
113
- command?: undefined;
114
- metadata?: undefined;
115
- options?: undefined;
116
- } | {
117
- command?: undefined;
118
- metadata?: undefined;
119
- executor?: undefined;
120
- options?: undefined;
121
- };
@@ -1,176 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTargetInfo = exports.TargetDefaultsPlugin = void 0;
4
- const minimatch_1 = require("minimatch");
5
- const node_fs_1 = require("node:fs");
6
- const node_path_1 = require("node:path");
7
- const fileutils_1 = require("../../utils/fileutils");
8
- const globs_1 = require("../../utils/globs");
9
- const package_json_1 = require("../../utils/package-json");
10
- const package_json_workspaces_1 = require("../package-json-workspaces");
11
- const symbols_1 = require("./symbols");
12
- exports.TargetDefaultsPlugin = {
13
- name: 'nx/core/target-defaults',
14
- createNodes: [
15
- '{package.json,**/package.json,project.json,**/project.json}',
16
- (configFile, _, ctx) => {
17
- const fileName = (0, node_path_1.basename)(configFile);
18
- const root = (0, node_path_1.dirname)(configFile);
19
- const packageManagerWorkspacesGlob = (0, globs_1.combineGlobPatterns)((0, package_json_workspaces_1.getGlobPatternsFromPackageManagerWorkspaces)(ctx.workspaceRoot));
20
- // Only process once if package.json + project.json both exist
21
- if (fileName === 'package.json' &&
22
- (0, node_fs_1.existsSync)((0, node_path_1.join)(ctx.workspaceRoot, root, 'project.json'))) {
23
- return {};
24
- }
25
- else if (fileName === 'package.json' &&
26
- !(0, minimatch_1.minimatch)(configFile, packageManagerWorkspacesGlob)) {
27
- return {};
28
- }
29
- // If no target defaults, this does nothing
30
- const targetDefaults = ctx.nxJsonConfiguration?.targetDefaults;
31
- if (!targetDefaults) {
32
- return {};
33
- }
34
- const projectJson = readJsonOrNull((0, node_path_1.join)(ctx.workspaceRoot, root, 'project.json'));
35
- const packageJson = readJsonOrNull((0, node_path_1.join)(ctx.workspaceRoot, root, 'package.json'));
36
- const packageJsonTargets = (0, package_json_1.readTargetsFromPackageJson)(packageJson);
37
- const projectDefinedTargets = new Set([
38
- ...Object.keys(projectJson?.targets ?? {}),
39
- ...(packageJson ? Object.keys(packageJsonTargets) : []),
40
- ]);
41
- const executorToTargetMap = getExecutorToTargetMap(packageJsonTargets, projectJson?.targets);
42
- const modifiedTargets = {};
43
- for (const defaultSpecifier in targetDefaults) {
44
- const targetNames = executorToTargetMap.get(defaultSpecifier) ?? new Set();
45
- targetNames.add(defaultSpecifier);
46
- for (const targetName of targetNames) {
47
- // Prevents `build` from overwriting `@nx/js:tsc` if both are present
48
- // and build is specified later in the ordering.
49
- if (!modifiedTargets[targetName] || targetName !== defaultSpecifier) {
50
- const defaults = JSON.parse(JSON.stringify(targetDefaults[defaultSpecifier]));
51
- modifiedTargets[targetName] = {
52
- ...getTargetInfo(targetName, projectJson?.targets, packageJsonTargets),
53
- ...defaults,
54
- };
55
- }
56
- // TODO: Remove this after we figure out a way to define new targets
57
- // in target defaults
58
- if (!projectDefinedTargets.has(targetName)) {
59
- modifiedTargets[targetName][symbols_1.ONLY_MODIFIES_EXISTING_TARGET] = true;
60
- }
61
- }
62
- }
63
- return {
64
- projects: {
65
- [root]: {
66
- targets: modifiedTargets,
67
- },
68
- },
69
- [symbols_1.OVERRIDE_SOURCE_FILE]: 'nx.json',
70
- };
71
- },
72
- ],
73
- };
74
- exports.default = exports.TargetDefaultsPlugin;
75
- function getExecutorToTargetMap(packageJsonTargets, projectJsonTargets) {
76
- const executorToTargetMap = new Map();
77
- const targets = Object.keys({
78
- ...projectJsonTargets,
79
- ...packageJsonTargets,
80
- });
81
- for (const target of targets) {
82
- const executor = getTargetExecutor(target, projectJsonTargets, packageJsonTargets);
83
- const targetsForExecutor = executorToTargetMap.get(executor) ?? new Set();
84
- targetsForExecutor.add(target);
85
- executorToTargetMap.set(executor, targetsForExecutor);
86
- }
87
- return executorToTargetMap;
88
- }
89
- function readJsonOrNull(path) {
90
- if ((0, node_fs_1.existsSync)(path)) {
91
- return (0, fileutils_1.readJsonFile)(path);
92
- }
93
- else {
94
- return null;
95
- }
96
- }
97
- /**
98
- * This fn gets target info that would make a target uniquely compatible
99
- * with what is described by project.json or package.json. As the merge process
100
- * for config happens, without this, the target defaults may be compatible
101
- * with a config from a plugin and then that combined target be incompatible
102
- * with the project json configuration resulting in the target default values
103
- * being scrapped. By adding enough information from the project.json / package.json,
104
- * we can make sure that the target after merging is compatible with the defined target.
105
- */
106
- function getTargetInfo(target, projectJsonTargets, packageJsonTargets) {
107
- const projectJsonTarget = projectJsonTargets?.[target];
108
- const packageJsonTarget = packageJsonTargets?.[target];
109
- const executor = getTargetExecutor(target, projectJsonTargets, packageJsonTargets);
110
- const targetOptions = {
111
- ...packageJsonTarget?.options,
112
- ...projectJsonTarget?.options,
113
- };
114
- const metadata = {
115
- ...packageJsonTarget?.metadata,
116
- ...projectJsonTarget?.metadata,
117
- };
118
- if (projectJsonTarget?.command) {
119
- return {
120
- command: projectJsonTarget?.command,
121
- metadata,
122
- };
123
- }
124
- if (executor === 'nx:run-commands') {
125
- if (targetOptions?.command) {
126
- return {
127
- executor: 'nx:run-commands',
128
- options: {
129
- command: targetOptions?.command,
130
- },
131
- metadata,
132
- };
133
- }
134
- else if (targetOptions?.commands) {
135
- return {
136
- executor: 'nx:run-commands',
137
- options: {
138
- commands: targetOptions.commands,
139
- },
140
- metadata,
141
- };
142
- }
143
- return {
144
- executor: 'nx:run-commands',
145
- metadata,
146
- };
147
- }
148
- if (executor === 'nx:run-script') {
149
- return {
150
- executor: 'nx:run-script',
151
- options: {
152
- script: targetOptions?.script ?? target,
153
- },
154
- metadata,
155
- };
156
- }
157
- if (executor) {
158
- return { executor };
159
- }
160
- return {};
161
- }
162
- exports.getTargetInfo = getTargetInfo;
163
- function getTargetExecutor(target, projectJsonTargets, packageJsonTargets) {
164
- const projectJsonTargetConfiguration = projectJsonTargets?.[target];
165
- const packageJsonTargetConfiguration = packageJsonTargets?.[target];
166
- if (!projectJsonTargetConfiguration && packageJsonTargetConfiguration) {
167
- return packageJsonTargetConfiguration?.executor;
168
- }
169
- if (projectJsonTargetConfiguration?.executor) {
170
- return projectJsonTargetConfiguration.executor;
171
- }
172
- if (projectJsonTargetConfiguration?.command) {
173
- return 'nx:run-commands';
174
- }
175
- return null;
176
- }