fix(core): recalculate dep-graph when root files are touched

This commit is contained in:
Jason Jean 2020-02-15 18:18:47 -05:00 committed by Victor Savkin
parent 4ca4268396
commit 1d1063fb1a
4 changed files with 28 additions and 9 deletions

View File

@ -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
@ -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)];
}
@ -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`));

View File

@ -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';

View File

@ -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 => {

View File

@ -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');
@ -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')
]);