Skip to content

Commit

Permalink
fix(core): recalculate dep-graph when root files are touched
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Feb 16, 2020
1 parent 4ca4268 commit 1d1063f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
30 changes: 22 additions & 8 deletions packages/workspace/src/core/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ function defaultReadFileAtRevision(
}
}

function getFileData(filePath: string): FileData {
const stat = fs.statSync(filePath);
return {
file: path
.relative(appRootPath, filePath)
.split(path.sep)
.join('/'),
ext: path.extname(filePath),
mtime: stat.mtimeMs
};
}

export function allFilesInDir(
dirName: string,
recurse: boolean = true
Expand All @@ -118,14 +130,7 @@ export function allFilesInDir(
const s = fs.statSync(child);
if (!s.isDirectory()) {
// add starting with "apps/myapp/..." or "libs/mylib/..."
res.push({
file: path
.relative(appRootPath, child)
.split(path.sep)
.join('/'),
ext: path.extname(child),
mtime: s.mtimeMs
});
res.push(getFileData(child));
} else if (s.isDirectory() && recurse) {
res = [...res, ...allFilesInDir(child)];
}
Expand Down Expand Up @@ -182,10 +187,19 @@ export function readNxJson(): NxJson {
return config;
}

// TODO: Make this list extensible
export function rootWorkspaceFileNames(): string[] {
return [`package.json`, workspaceFileName(), `nx.json`, `tsconfig.json`];
}

export function readWorkspaceFiles(): FileData[] {
const workspaceJson = readWorkspaceJson();
const files = [];

files.push(
...rootWorkspaceFileNames().map(f => getFileData(`${appRootPath}/${f}`))
);

// Add known workspace files and directories
files.push(...allFilesInDir(appRootPath, false));
files.push(...allFilesInDir(`${appRootPath}/tools`));
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/core/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, readFileSync } from 'fs';
import { mkdirSync } from 'fs';
import { ProjectGraph } from './project-graph-models';
import { ProjectGraphBuilder } from './project-graph-builder';
import { appRootPath } from '../../utils/app-root';
Expand Down
3 changes: 3 additions & 0 deletions packages/workspace/src/tasks-runner/hasher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ describe('Hasher', () => {
hasha.mockImplementation(values => values.join('|'));
hasha.fromFile.mockImplementation(path => Promise.resolve(hashes[path]));
fs.statSync.mockReturnValue({ size: 100 });
fs.readFileSync.mockImplementation(() =>
JSON.stringify({ dependencies: {}, devDependencies: {} })
);
});

it('should create project hash', async done => {
Expand Down
2 changes: 2 additions & 0 deletions packages/workspace/src/tasks-runner/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ProjectGraph } from '../core/project-graph';
import { NxJson } from '../core/shared-interfaces';
import { Task } from './tasks-runner';
import { statSync } from 'fs';
import { rootWorkspaceFileNames } from '../core/file-utils';

const hasha = require('hasha');

Expand Down Expand Up @@ -42,6 +43,7 @@ export class Hasher {
...Object.keys(this.nxJson.implicitDependencies || {}).map(r =>
this.fileHashes.hashFile(r)
),
...rootWorkspaceFileNames().map(r => this.fileHashes.hashFile(r)),
this.fileHashes.hashFile('package-lock.json'),
this.fileHashes.hashFile('yarn.lock')
]);
Expand Down

0 comments on commit 1d1063f

Please sign in to comment.