Skip to content

Commit

Permalink
feat(nx): add ability to use interpolated arguments in affected
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Apr 24, 2019
1 parent 08505f5 commit 27e0780
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions e2e/schematics/affected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,10 @@ describe('Affected', () => {
`npm run affected -- --target extract-i18n --files="libs/${mylib}/src/index.ts"`
);
expect(i18n).toContain(`Running extract-i18n for ${myapp}`);

const interpolatedTests = runCommand(
`npm run affected -- --target test --files="libs/${mylib}/src/index.ts" -- --jest-config {project.root}jest.config.js`
);
expect(interpolatedTests).toContain(`Running test for ${mylib}`);
}, 1000000);
});
52 changes: 47 additions & 5 deletions packages/schematics/src/command-line/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
getProjectNames,
parseFiles,
getAllProjectNamesWithTarget,
getAffectedProjectsWithTarget
getAffectedProjectsWithTarget,
readAngularJson
} from './shared';
import { generateGraph } from './dep-graph';
import { GlobalNxArgs } from './nx';
Expand Down Expand Up @@ -155,6 +156,11 @@ async function runCommand(
if (args.length > 0) {
console.log(`With flags: ${args.join(' ')}`);
}
const angularJson = readAngularJson();
const projectMetadata = new Map<string, any>();
projects.forEach(project => {
projectMetadata.set(project, angularJson.projects[project]);
});
if (parsedArgs.parallel) {
// Make sure the `package.json` has the `ng: "ng"` command needed by `npm-run-all`
const packageJson = JSON.parse(
Expand All @@ -170,8 +176,16 @@ async function runCommand(
await runAll(
projects.map(app => {
return ngCommands.includes(command)
? `ng -- ${command} --project=${app} ${args.join(' ')} `
: `ng -- run ${app}:${command} ${args.join(' ')} `;
? `ng -- ${command} --project=${app} ${transformArgs(
args,
app,
projectMetadata.get(app)
).join(' ')} `
: `ng -- run ${app}:${command} ${transformArgs(
args,
app,
projectMetadata.get(app)
).join(' ')} `;
}),
{
parallel: parsedArgs.parallel,
Expand Down Expand Up @@ -209,8 +223,16 @@ async function runCommand(
projects.forEach(project => {
console.log(`${iterationMessage} ${project}`);
const task = ngCommands.includes(command)
? `node ${ngPath()} ${command} --project=${project} ${args.join(' ')} `
: `node ${ngPath()} run ${project}:${command} ${args.join(' ')} `;
? `node ${ngPath()} ${command} --project=${project} ${transformArgs(
args,
project,
projectMetadata.get(project)
).join(' ')} `
: `node ${ngPath()} run ${project}:${command} ${transformArgs(
args,
project,
projectMetadata.get(project)
).join(' ')} `;
try {
execSync(task, {
stdio: [0, 1, 2]
Expand All @@ -235,6 +257,26 @@ async function runCommand(
}
}

function transformArgs(
args: string[],
projectName: string,
projectMetadata: any
) {
return args.map(arg => {
const regex = /{project\.([^}]+)}/g;
arg.replace(regex, (_, group: string) => {
if (group.includes('.')) {
throw new Error('Only top-level properties can be interpolated');
}

if (group === 'name') {
return projectName;
}
return projectMetadata[group];
});
});
}

function filterNxSpecificArgs(parsedArgs: YargsAffectedOptions): string[] {
const filteredArgs = { ...parsedArgs };
// Delete Nx arguments from parsed Args
Expand Down

0 comments on commit 27e0780

Please sign in to comment.