Skip to content

Commit

Permalink
feat(core): expand support for projectRoot token to include project.json
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jul 25, 2023
1 parent 7567a65 commit e4410ca
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
67 changes: 67 additions & 0 deletions e2e/nx-run/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,73 @@ describe('Nx Running Tests', () => {

updateProjectConfig(mylib, (c) => original);
}, 1000000);

describe('tokens support', () => {
let app: string;

beforeAll(() => {
app = uniq('myapp');
runCLI(`generate @nx/web:app ${app}`);
});

it('should support using {projectRoot} in options blocks in project.json', async () => {
updateProjectConfig(app, (c) => {
c.targets['echo'] = {
command: 'node -e console.log({projectRoot})',
};
return c;
});

const output = runCLI(`echo ${app}`);
expect(output).toContain(`apps/${app}`);
});

it('should support using {projectName} in options blocks in project.json', async () => {
updateProjectConfig(app, (c) => {
c.targets['echo'] = {
command: 'node -e console.log({projectName})',
};
return c;
});

const output = runCLI(`echo ${app}`);
expect(output).toContain(app);
});

it('should support using {projectRoot} in targetDefaults', async () => {
updateJson(`nx.json`, (json) => {
json.targetDefaults = {
echo: {
command: 'node -e console.log({projectRoot})',
},
};
return json;
});
updateProjectConfig(app, (c) => {
c.targets['echo'] = {};
return c;
});
const output = runCLI(`echo ${app}`);
expect(output).toContain(`apps/${app}`);
});

it('should support using {projectName} in targetDefaults', async () => {
updateJson(`nx.json`, (json) => {
json.targetDefaults = {
echo: {
command: 'node -e console.log({projectName})',
},
};
return json;
});
updateProjectConfig(app, (c) => {
c.targets['echo'] = {};
return c;
});
const output = runCLI(`echo ${app}`);
expect(output).toContain(app);
});
});
});

describe('Nx Bail', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ describe('Workspaces', () => {
}
).configurations
).toEqual({
dev: { a: '{workspaceRoot}' },
dev: { a: '' },
prod: { a: 'dist/my/project' },
});
});
Expand Down
27 changes: 16 additions & 11 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ function mergeOptions<T extends Object>(
): T {
return {
...resolvePathTokensInOptions(defaults, project, key),
...options,
...resolvePathTokensInOptions(options, project, key),
};
}

Expand All @@ -586,16 +586,21 @@ function mergeConfigurations<T extends Object>(
project: ProjectConfiguration,
targetName: string
): Record<string, T> {
const configurations: Record<string, T> = { ...projectDefinedConfigurations };
for (const configuration in defaultConfigurations) {
configurations[configuration] = mergeOptions(
defaultConfigurations[configuration],
configurations[configuration],
const result: Record<string, T> = {};
const configurations = new Set([
...Object.keys(defaultConfigurations),
...Object.keys(projectDefinedConfigurations),
]);
for (const configuration of configurations) {
result[configuration] = mergeOptions(
defaultConfigurations[configuration] ?? ({} as T),
projectDefinedConfigurations[configuration] ?? ({} as T),
project,
`${targetName}.${configuration}`
);
}
return configurations;
console.log({ configurations, result });
return result;
}

function resolvePathTokensInOptions<T extends Object | Array<unknown>>(
Expand All @@ -606,10 +611,10 @@ function resolvePathTokensInOptions<T extends Object | Array<unknown>>(
const result: T = Array.isArray(object) ? ([...object] as T) : { ...object };
for (let [opt, value] of Object.entries(object ?? {})) {
if (typeof value === 'string') {
if (value.startsWith('{workspaceRoot}/')) {
value = value.replace(/^\{workspaceRoot\}\//, '');
}
if (value.includes('{workspaceRoot}')) {
const workspaceRootMatch = /^(\{workspaceRoot\}\/?)/.exec(value);
if (workspaceRootMatch?.length) {
value = value.replace(workspaceRootMatch[0], '');
} else if (value.includes('{workspaceRoot}')) {
throw new Error(
`${NX_PREFIX} The {workspaceRoot} token is only valid at the beginning of an option. (${key})`
);
Expand Down

0 comments on commit e4410ca

Please sign in to comment.