Skip to content

Commit

Permalink
fix(core): local plugins should be able to use {projectRoot} in optio…
Browse files Browse the repository at this point in the history
…ns block
  • Loading branch information
AgentEnder committed Apr 29, 2024
1 parent 9bf197f commit 12367d1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('Workspaces', () => {
projectType: 'library',
targets: {
'nx-release-publish': {
configurations: {},
dependsOn: ['^nx-release-publish'],
executor: '@nx/js:release-publish',
options: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,4 @@ describe('workspace-projects', () => {
).toEqual(['b', 'b-1', 'b-2']);
});
});

describe('normalizeTargets', () => {
it('should support {projectRoot}, {workspaceRoot}, and {projectName} tokens', () => {
expect(
normalizeProjectTargets(
{
name: 'project',
root: 'my/project',
targets: {
build: {
executor: 'target',
options: {
a: '{projectRoot}',
b: '{workspaceRoot}',
c: '{projectName}',
},
},
},
},
'build'
).build.options
).toEqual({ a: 'my/project', b: '', c: 'project' });
});
});
});
15 changes: 0 additions & 15 deletions packages/nx/src/project-graph/utils/normalize-project-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,6 @@ export function normalizeProjectTargets(
delete targets[target];
continue;
}

targets[target].options = resolveNxTokensInOptions(
targets[target].options,
project,
`${projectName}:${target}`
);

targets[target].configurations ??= {};
for (const configuration in targets[target].configurations) {
targets[target].configurations[configuration] = resolveNxTokensInOptions(
targets[target].configurations[configuration],
project,
`${projectName}:${target}:${configuration}`
);
}
}
return targets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isCompatibleTarget,
mergeProjectConfigurationIntoRootMap,
mergeTargetConfigurations,
normalizeTarget,
readProjectConfigurationsFromRootMap,
readTargetDefaultsForTarget,
} from './project-configuration-utils';
Expand Down Expand Up @@ -572,6 +573,7 @@ describe('project-configuration-utils', () => {
"root": "libs/lib-a",
"targets": {
"build": {
"configurations": {},
"executor": "nx:run-commands",
"options": {
"command": "tsc",
Expand Down Expand Up @@ -725,8 +727,9 @@ describe('project-configuration-utils', () => {
expect(targets.echo).toMatchInlineSnapshot(`
{
"command": "echo lib-a",
"configurations": {},
"options": {
"cwd": "{projectRoot}",
"cwd": "libs/lib-a",
},
}
`);
Expand Down Expand Up @@ -1548,6 +1551,28 @@ describe('project-configuration-utils', () => {
});
});

describe('normalizeTarget', () => {
it('should support {projectRoot}, {workspaceRoot}, and {projectName} tokens', () => {
const config = {
name: 'project',
root: 'libs/project',
targets: {
foo: { command: 'echo {projectRoot}' },
},
};
expect(normalizeTarget(config.targets.foo, config))
.toMatchInlineSnapshot(`
{
"configurations": {},
"executor": "nx:run-commands",
"options": {
"command": "echo libs/project",
},
}
`);
});
});

describe('createProjectConfigurations', () => {
/* A fake plugin that sets `fake-lib` tag to libs. */
const fakeTagPlugin: NxPluginV2 = {
Expand Down
34 changes: 30 additions & 4 deletions packages/nx/src/project-graph/utils/project-configuration-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function mergeProjectConfigurationIntoRootMap(
sourceInformation?: SourceInformation,
// This function is used when reading project configuration
// in generators, where we don't want to do this.
skipCommandNormalization?: boolean
skipTargetNormalization?: boolean
): void {
if (configurationSourceMaps && !configurationSourceMaps[project.root]) {
configurationSourceMaps[project.root] = {};
Expand Down Expand Up @@ -202,10 +202,12 @@ export function mergeProjectConfigurationIntoRootMap(
continue;
}

const normalizedTarget = skipTargetNormalization
? target
: normalizeTarget(target, project);

const mergedTarget = mergeTargetConfigurations(
skipCommandNormalization
? target
: resolveCommandSyntacticSugar(target, project.root),
normalizedTarget,
matchingProject.targets?.[targetName],
sourceMap,
sourceInformation,
Expand Down Expand Up @@ -836,3 +838,27 @@ function resolveCommandSyntacticSugar(
};
}
}

export function normalizeTarget(
target: TargetConfiguration,
project: ProjectConfiguration
) {
target = resolveCommandSyntacticSugar(target, project.root);

target.options = resolveNxTokensInOptions(
target.options,
project,
`${project.root}:${target}`
);

target.configurations ??= {};
for (const configuration in target.configurations) {
target.configurations[configuration] = resolveNxTokensInOptions(
target.configurations[configuration],
project,
`${project.root}:${target}:${configuration}`
);
}

return target;
}

0 comments on commit 12367d1

Please sign in to comment.