Skip to content

Commit

Permalink
feat(core): add support for npm package dependencies in project graph
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored and vsavkin committed Dec 20, 2019
1 parent 6328b8f commit 3a36c24
Show file tree
Hide file tree
Showing 34 changed files with 635 additions and 176 deletions.
4 changes: 2 additions & 2 deletions e2e/affected.test.ts
Expand Up @@ -75,7 +75,7 @@ forEachCli(() => {
expect(affectedApps).not.toContain(`${myapp}-e2e`);

const implicitlyAffectedApps = runCommand(
'npm run affected:apps -- --files="package.json"'
'npm run affected:apps -- --files="tsconfig.json"'
);
expect(implicitlyAffectedApps).toContain(myapp);
expect(implicitlyAffectedApps).toContain(myapp2);
Expand All @@ -100,7 +100,7 @@ forEachCli(() => {
expect(affectedLibs).not.toContain(mylib2);

const implicitlyAffectedLibs = runCommand(
'npm run affected:libs -- --files="package.json"'
'npm run affected:libs -- --files="tsconfig.json"'
);
expect(implicitlyAffectedLibs).toContain(mypublishablelib);
expect(implicitlyAffectedLibs).toContain(mylib);
Expand Down
5 changes: 4 additions & 1 deletion packages/workspace/src/command-line/affected.ts
Expand Up @@ -7,6 +7,7 @@ import { NxArgs, splitArgsIntoNxArgsAndOverrides } from './utils';
import { filterAffected } from '../core/affected-project-graph';
import {
createProjectGraph,
onlyWorkspaceProjects,
ProjectGraphNode,
ProjectType,
withDeps
Expand All @@ -23,7 +24,9 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
const fileChanges = readFileChanges(nxArgs);
let affectedGraph = filterAffected(projectGraph, fileChanges);
if (parsedArgs.withDeps) {
affectedGraph = withDeps(projectGraph, Object.values(affectedGraph.nodes));
affectedGraph = onlyWorkspaceProjects(
withDeps(projectGraph, Object.values(affectedGraph.nodes))
);
}
const affectedProjects = Object.values(
parsedArgs.all ? projectGraph.nodes : affectedGraph.nodes
Expand Down
22 changes: 15 additions & 7 deletions packages/workspace/src/command-line/dep-graph.ts
Expand Up @@ -3,16 +3,18 @@ import * as http from 'http';
import * as opn from 'opn';
import {
createProjectGraph,
onlyWorkspaceProjects,
ProjectGraph,
ProjectGraphNode
} from '../core/project-graph';
import { output } from '../utils/output';
import { join } from 'path';

export function generateGraph(
args: { file?: string; filter?: string[]; exclude?: string[] },
affectedProjects: string[]
): void {
const graph = createProjectGraph();
const graph = onlyWorkspaceProjects(createProjectGraph());

const renderProjects: ProjectGraphNode[] = filterProjects(
graph,
Expand Down Expand Up @@ -43,7 +45,9 @@ function startServer(
graph: ProjectGraph,
affected: string[]
) {
const f = readFileSync(__dirname + '/dep-graph/dep-graph.html').toString();
const f = readFileSync(
join(__dirname, '../core/dep-graph/dep-graph.html')
).toString();
const html = f
.replace(
`window.projects = null`,
Expand Down Expand Up @@ -90,13 +94,17 @@ function filterProjects(
return filteredProjects;
}

function hasPath(graph, target, node, visited) {
function hasPath(
graph: ProjectGraph,
target: string,
node: string,
visited: string[]
) {
if (target === node) return true;

for (let d of graph.nodes[node]) {
if (visited.indexOf(d.projectName) > -1) continue;
if (hasPath(graph, target, d.projectName, [...visited, d.projectName]))
return true;
for (let d of graph.dependencies[node] || []) {
if (visited.indexOf(d.target) > -1) continue;
if (hasPath(graph, target, d.target, [...visited, d.target])) return true;
}
return false;
}
7 changes: 5 additions & 2 deletions packages/workspace/src/command-line/lint.ts
@@ -1,12 +1,15 @@
import { createProjectGraph } from '../core/project-graph';
import {
createProjectGraph,
onlyWorkspaceProjects
} from '../core/project-graph';
import { WorkspaceIntegrityChecks } from './workspace-integrity-checks';
import * as path from 'path';
import { appRootPath } from '../utils/app-root';
import { allFilesInDir } from '../core/file-utils';
import { output } from '../utils/output';

export function workspaceLint() {
const graph = createProjectGraph();
const graph = onlyWorkspaceProjects(createProjectGraph());

const cliErrorOutputConfigs = new WorkspaceIntegrityChecks(
graph,
Expand Down
1 change: 0 additions & 1 deletion packages/workspace/src/command-line/shared.ts
@@ -1,5 +1,4 @@
import { execSync } from 'child_process';
import * as fs from 'fs';
import { output } from '../utils/output';
import { createProjectGraph, ProjectGraphNode } from '../core/project-graph';
import { NxArgs } from './utils';
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/command-line/utils.spec.ts
Expand Up @@ -10,6 +10,7 @@ describe('splitArgs', () => {
$0: ''
}).nxArgs
).toEqual({
_: ['--override'],
projects: [],
files: ['']
});
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace/src/command-line/utils.ts
Expand Up @@ -30,7 +30,8 @@ const dummyOptions: NxArgs = {
withDeps: false,
'with-deps': false,
projects: [],
select: ''
select: '',
_: []
} as any;

const nxSpecific = Object.keys(dummyOptions);
Expand Down
Expand Up @@ -2,6 +2,6 @@ import { NxJson } from '../shared-interfaces';

export interface AffectedProjectGraphContext {
workspaceJson: any;
nxJson: NxJson;
nxJson: NxJson<string[]>;
touchedProjects: string[];
}
@@ -1,4 +1,5 @@
import { extname } from 'path';
import { jsonDiff } from '../../utils/json-diff';
import { vol } from 'memfs';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import { createProjectGraph } from '../project-graph';
Expand All @@ -22,11 +23,14 @@ describe('project graph', () => {
beforeEach(() => {
packageJson = {
name: '@nrwl/workspace-src',
scripts: {
deploy: 'echo deploy'
},
dependencies: {
'happy-nrwl': '1.0.0'
},
devDependencies: {
'@nrwl/workspace': '*'
'@nrwl/workspace': '8.0.0'
}
};
workspaceJson = {
Expand Down Expand Up @@ -61,6 +65,14 @@ describe('project graph', () => {
nxJson = {
npmScope: 'nrwl',
implicitDependencies: {
'package.json': {
scripts: {
deploy: ['demo', 'api']
},
devDependencies: {
'@nrwl/workspace': '*'
}
},
'something-for-api.txt': ['api']
},
projects: {
Expand Down Expand Up @@ -183,4 +195,120 @@ describe('project graph', () => {
}
});
});

it('should create nodes and dependencies with npm packages', () => {
const graph = createProjectGraph();
const updatedPackageJson = {
...packageJson,
dependencies: {
'happy-nrwl': '2.0.0'
}
};

const affected = filterAffected(graph, [
{
file: 'package.json',
ext: '.json',
mtime: 1,
getChanges: () => jsonDiff(packageJson, updatedPackageJson)
}
]);

expect(affected).toEqual({
nodes: {
'happy-nrwl': {
type: 'npm',
name: 'happy-nrwl',
data: expect.anything()
},
util: {
name: 'util',
type: 'lib',
data: expect.anything()
},
ui: {
name: 'ui',
type: 'lib',
data: expect.anything()
},
demo: {
name: 'demo',
type: 'app',
data: expect.anything()
},
'demo-e2e': {
name: 'demo-e2e',
type: 'e2e',
data: expect.anything()
}
},
dependencies: {
'demo-e2e': [
{
type: 'implicit',
source: 'demo-e2e',
target: 'demo'
}
],
demo: [
{
type: 'static',
source: 'demo',
target: 'ui'
}
],
ui: [{ type: 'static', source: 'ui', target: 'util' }],
util: [{ type: 'static', source: 'util', target: 'happy-nrwl' }]
}
});
});

it('should support implicit JSON file dependencies (some projects)', () => {
const graph = createProjectGraph();
const updatedPackageJson = {
...packageJson,
scripts: {
deploy: 'echo deploy!!!'
}
};

const affected = filterAffected(graph, [
{
file: 'package.json',
ext: '.json',
mtime: 1,
getChanges: () => jsonDiff(packageJson, updatedPackageJson)
}
]);

expect(Object.keys(affected.nodes)).toEqual(['demo', 'demo-e2e', 'api']);
});

it('should support implicit JSON file dependencies (all projects)', () => {
const graph = createProjectGraph();
const updatedPackageJson = {
...packageJson,
devDependencies: {
'@nrwl/workspace': '9.0.0'
}
};

const affected = filterAffected(graph, [
{
file: 'package.json',
ext: '.json',
mtime: 1,
getChanges: () => jsonDiff(packageJson, updatedPackageJson)
}
]);

expect(Object.keys(affected.nodes)).toEqual([
'@nrwl/workspace',
'api',
'demo',
'demo-e2e',
'ui',
'util'
]);
});
});

0 comments on commit 3a36c24

Please sign in to comment.