Skip to content

Commit

Permalink
fix(run): detect Nx target configuration in package.json files (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Nov 22, 2022
1 parent dad936e commit be2af28
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 40 deletions.
4 changes: 0 additions & 4 deletions .github/renovate.json5
Expand Up @@ -24,10 +24,6 @@
packageNames: ['npm'],
allowedVersions: '8.0.0',
},
{
packageNames: ['nx'],
allowedVersions: '15.0.8',
},
],
schedule: ['on Thursday'],
ignoreDeps: [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -85,7 +85,7 @@
"normalize-newline": "^3.0.0",
"normalize-path": "^3.0.0",
"npmlog": "^7.0.1",
"nx": "15.0.8",
"nx": "15.2.1",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"semver": "^7.3.8",
Expand Down
Expand Up @@ -65,7 +65,7 @@ describe('RunCommand', () => {

const logMessages = loggingOutput('verbose');
expect(logMessages).toContain(
'nx.json was not found or is missing targetDefaults. Task dependencies will not be automatically included.'
'Nx target configuration was not found. Task dependencies will not be automatically included.'
);
});
});
Expand Down
8 changes: 3 additions & 5 deletions packages/run/src/__tests__/run-command.spec.ts
Expand Up @@ -455,7 +455,7 @@ describe('RunCommand', () => {

const logMessages = loggingOutput('info');
expect(logMessages).toContain(
'Using the "ignore" option when nx.json has targetDefaults defined will exclude only tasks that are not determined to be required by Nx.'
'Using the "ignore" option when Nx targets are configured will exclude only tasks that are not determined to be required by Nx.'
);
});

Expand Down Expand Up @@ -498,9 +498,7 @@ describe('RunCommand', () => {
await lernaRun(testDir)('my-script', '--sort');

const [logMessage] = loggingOutput('warn');
expect(logMessage).toContain(
'"parallel", "sort", and "no-sort" are ignored when nx.json has targetDefaults defined.'
);
expect(logMessage).toContain('"parallel", "sort", and "no-sort" are ignored when Nx targets are configured.');
expect(collectedOutput).toContain('package-1');
});

Expand All @@ -511,7 +509,7 @@ describe('RunCommand', () => {

const logMessages = loggingOutput('info');
expect(logMessages).toContain(
'Using the "include-dependencies" option when nx.json has targetDefaults defined will include both task dependencies detected by Nx and project dependencies detected by Lerna.'
'Using the "include-dependencies" option when Nx targets are configured will include both task dependencies detected by Nx and project dependencies detected by Lerna.'
);
expect(collectedOutput).toContain('package-1');
});
Expand Down
21 changes: 11 additions & 10 deletions packages/run/src/run-command.ts
Expand Up @@ -268,7 +268,11 @@ export class RunCommand extends Command<RunCommandOption & FilterOptions> {
const nxJson = readNxJson();
const targetDependenciesAreDefined =
Object.keys(nxJson.targetDependencies || nxJson.targetDefaults || {}).length > 0;
const mimicLernaDefaultBehavior = !(nxJsonExists && targetDependenciesAreDefined);

const hasProjectSpecificNxConfiguration = this.packagesWithScript.some((p) => !!p.get('nx'));
const hasCustomizedNxConfiguration =
(nxJsonExists && targetDependenciesAreDefined) || hasProjectSpecificNxConfiguration;
const mimicLernaDefaultBehavior = !hasCustomizedNxConfiguration;

const targetDependencies =
this.toposort && !this.options.parallel && mimicLernaDefaultBehavior
Expand Down Expand Up @@ -307,36 +311,33 @@ export class RunCommand extends Command<RunCommandOption & FilterOptions> {
__overrides__: this.args.map((t) => t.toString()),
};

if (!mimicLernaDefaultBehavior) {
if (hasCustomizedNxConfiguration) {
this.logger.verbose(
this.name,
'nx.json with targetDefaults was found. Task dependencies will be automatically included.'
'Nx target configuration was found. Task dependencies will be automatically included.'
);

if (this.options.parallel || this.options.sort !== undefined) {
this.logger.warn(
this.name,
`"parallel", "sort", and "no-sort" are ignored when nx.json has targetDefaults defined.`
);
this.logger.warn(this.name, `"parallel", "sort", and "no-sort" are ignored when Nx targets are configured.`);
}

if (this.options.includeDependencies) {
this.logger.info(
this.name,
`Using the "include-dependencies" option when nx.json has targetDefaults defined will include both task dependencies detected by Nx and project dependencies detected by Lerna.`
`Using the "include-dependencies" option when Nx targets are configured will include both task dependencies detected by Nx and project dependencies detected by Lerna.`
);
}

if (this.options.ignore) {
this.logger.info(
this.name,
`Using the "ignore" option when nx.json has targetDefaults defined will exclude only tasks that are not determined to be required by Nx.`
`Using the "ignore" option when Nx targets are configured will exclude only tasks that are not determined to be required by Nx.`
);
}
} else {
this.logger.verbose(
this.name,
'nx.json was not found or is missing targetDefaults. Task dependencies will not be automatically included.'
'Nx target configuration was not found. Task dependencies will not be automatically included.'
);
}

Expand Down
33 changes: 14 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be2af28

Please sign in to comment.