Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9a2341a

Browse files
vsavkinFrozenPandaz
authored andcommittedAug 13, 2020
feat(core): redesign workspace file hashing
1 parent d336772 commit 9a2341a

31 files changed

+674
-542
lines changed
 

Diff for: ‎e2e/workspace/src/workspace.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ forEachCli((cliName) => {
165165
expect(failedTests).toContain(`- ${myapp}`);
166166
expect(failedTests).toContain(`- ${myapp2}`);
167167
expect(failedTests).toContain(`Failed projects:`);
168-
expect(readJson('dist/.nx-results')).toEqual({
168+
expect(readJson('node_modules/.cache/nx/results.json')).toEqual({
169169
command: 'test',
170170
results: {
171171
[myapp]: false,
@@ -318,7 +318,7 @@ forEachCli((cliName) => {
318318
expect(failedTests).toContain(
319319
'You can isolate the above projects by passing: --only-failed'
320320
);
321-
expect(readJson('dist/.nx-results')).toEqual({
321+
expect(readJson('node_modules/.cache/nx/results.json')).toEqual({
322322
command: 'test',
323323
results: {
324324
[myapp]: false,

Diff for: ‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@
142142
"fork-ts-checker-webpack-plugin": "^3.1.1",
143143
"fs-extra": "7.0.1",
144144
"glob": "7.1.4",
145-
"hasha": "5.1.0",
146145
"html-webpack-plugin": "^3.2.0",
147146
"husky": "^3.0.3",
148147
"identity-obj-proxy": "3.0.0",

Diff for: ‎packages/eslint-plugin-nx/tests/rules/enforce-module-boundaries.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ linter.defineParser('@typescript-eslint/parser', parser);
993993
linter.defineRule(enforceModuleBoundariesRuleName, enforceModuleBoundaries);
994994

995995
function createFile(f) {
996-
return { file: f, ext: extname(f), mtime: 1 };
996+
return { file: f, ext: extname(f), hash: '' };
997997
}
998998

999999
function runRule(

Diff for: ‎packages/workspace/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
name: 'tao',
2+
name: 'workspace',
33
preset: '../../jest.config.js',
44
transform: {
55
'^.+\\.[tj]sx?$': 'ts-jest',

Diff for: ‎packages/workspace/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"dotenv": "8.2.0",
5858
"ignore": "5.0.4",
5959
"npm-run-all": "4.1.5",
60-
"hasha": "5.1.0",
6160
"opn": "^5.3.0",
6261
"rxjs": "^6.5.4",
6362
"semver": "5.4.1",

Diff for: ‎packages/workspace/src/command-line/workspace-integrity-checks.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ describe('WorkspaceIntegrityChecks', () => {
106106
});
107107

108108
function createFile(f) {
109-
return { file: f, ext: extname(f), mtime: 1 };
109+
return { file: f, ext: extname(f), hash: '' };
110110
}

Diff for: ‎packages/workspace/src/command-line/workspace-results.spec.ts

+2-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22

33
import { WorkspaceResults } from './workspace-results';
44
import { serializeJson } from '../utils/fileutils';
5-
import { ProjectType } from '..//core/project-graph';
5+
import { ProjectType } from '../core/project-graph';
66

77
describe('WorkspacesResults', () => {
88
let results: WorkspaceResults;
@@ -43,7 +43,7 @@ describe('WorkspacesResults', () => {
4343
results.saveResults();
4444

4545
expect(fs.writeSync).not.toHaveBeenCalled();
46-
expect(fs.unlinkSync).toHaveBeenCalledWith('dist/.nx-results');
46+
expect(fs.unlinkSync).toHaveBeenCalled();
4747
});
4848
});
4949

@@ -53,23 +53,6 @@ describe('WorkspacesResults', () => {
5353

5454
expect(results.getResult('proj')).toBe(false);
5555
});
56-
57-
it('should save results to file system', () => {
58-
spyOn(fs, 'writeFileSync');
59-
60-
results.setResult('proj', false);
61-
results.saveResults();
62-
63-
expect(fs.writeFileSync).toHaveBeenCalledWith(
64-
'dist/.nx-results',
65-
serializeJson({
66-
command: 'test',
67-
results: {
68-
proj: false,
69-
},
70-
})
71-
);
72-
});
7356
});
7457

7558
describe('when results already exist', () => {
@@ -97,7 +80,6 @@ describe('WorkspacesResults', () => {
9780
},
9881
});
9982

100-
expect(fs.readFileSync).toHaveBeenCalledWith('dist/.nx-results', 'utf-8');
10183
expect(results.getResult('proj')).toBe(false);
10284
});
10385

Diff for: ‎packages/workspace/src/command-line/workspace-results.ts

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import * as fs from 'fs';
2-
import { readJsonFile, writeJsonFile } from '../utils/fileutils';
3-
import { unlinkSync } from 'fs';
2+
import {
3+
directoryExists,
4+
readJsonFile,
5+
writeJsonFile,
6+
} from '../utils/fileutils';
7+
import { existsSync, unlinkSync } from 'fs';
48
import { ProjectGraphNode } from '../core/project-graph';
9+
import { join } from 'path';
10+
import { appRootPath } from '@nrwl/workspace/src/utils/app-root';
11+
import * as fsExtra from 'fs-extra';
512

6-
const RESULTS_FILE = 'dist/.nx-results';
13+
const resultsDir = join(appRootPath, 'node_modules', '.cache', 'nx');
14+
const resultsFile = join(resultsDir, 'results.json');
715

816
interface NxResults {
917
command: string;
@@ -31,11 +39,11 @@ export class WorkspaceResults {
3139
private command: string,
3240
private projects: Record<string, ProjectGraphNode>
3341
) {
34-
const resultsExists = fs.existsSync(RESULTS_FILE);
42+
const resultsExists = fs.existsSync(resultsFile);
3543
this.startedWithFailedProjects = false;
3644
if (resultsExists) {
3745
try {
38-
const commandResults = readJsonFile(RESULTS_FILE);
46+
const commandResults = readJsonFile(resultsFile);
3947
this.startedWithFailedProjects = commandResults.command === command;
4048
if (this.startedWithFailedProjects) {
4149
this.commandResults = commandResults;
@@ -56,10 +64,19 @@ export class WorkspaceResults {
5664
}
5765

5866
saveResults() {
67+
try {
68+
if (!existsSync(resultsDir)) {
69+
fsExtra.ensureDirSync(resultsDir);
70+
}
71+
} catch (e) {
72+
if (!directoryExists(resultsDir)) {
73+
throw new Error(`Failed to create directory: ${resultsDir}`);
74+
}
75+
}
5976
if (Object.values<boolean>(this.commandResults.results).includes(false)) {
60-
writeJsonFile(RESULTS_FILE, this.commandResults);
61-
} else if (fs.existsSync(RESULTS_FILE)) {
62-
unlinkSync(RESULTS_FILE);
77+
writeJsonFile(resultsFile, this.commandResults);
78+
} else if (fs.existsSync(resultsFile)) {
79+
unlinkSync(resultsFile);
6380
}
6481
}
6582

Diff for: ‎packages/workspace/src/core/affected-project-graph/affected-project-graph.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('project graph', () => {
116116
files = Object.keys(filesJson).map((f) => ({
117117
file: f,
118118
ext: extname(f),
119-
mtime: 1,
119+
hash: 'some-hash',
120120
}));
121121
readFileAtRevision = (p, r) => {
122122
const fromFs = filesJson[`./${p}`];
@@ -139,13 +139,13 @@ describe('project graph', () => {
139139
{
140140
file: 'something-for-api.txt',
141141
ext: '.txt',
142-
mtime: 1,
142+
hash: 'some-hash',
143143
getChanges: () => [new WholeFileChange()],
144144
},
145145
{
146146
file: 'libs/ui/src/index.ts',
147147
ext: '.ts',
148-
mtime: 1,
148+
hash: 'some-hash',
149149
getChanges: () => [new WholeFileChange()],
150150
},
151151
]);
@@ -211,7 +211,7 @@ describe('project graph', () => {
211211
{
212212
file: 'package.json',
213213
ext: '.json',
214-
mtime: 1,
214+
hash: 'some-hash',
215215
getChanges: () => jsonDiff(packageJson, updatedPackageJson),
216216
},
217217
]);
@@ -279,7 +279,7 @@ describe('project graph', () => {
279279
{
280280
file: 'package.json',
281281
ext: '.json',
282-
mtime: 1,
282+
hash: 'some-hash',
283283
getChanges: () => jsonDiff(packageJson, updatedPackageJson),
284284
},
285285
]);
@@ -300,7 +300,7 @@ describe('project graph', () => {
300300
{
301301
file: 'package.json',
302302
ext: '.json',
303-
mtime: 1,
303+
hash: 'some-hash',
304304
getChanges: () => jsonDiff(packageJson, updatedPackageJson),
305305
},
306306
]);

Diff for: ‎packages/workspace/src/core/affected-project-graph/locators/implicit-json-changes.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('getImplicitlyTouchedProjectsByJsonChanges', () => {
3535
[
3636
{
3737
file: 'package.json',
38-
mtime: 0,
38+
hash: 'some-hash',
3939
ext: '.json',
4040
getChanges: () => [
4141
{
@@ -60,7 +60,7 @@ describe('getImplicitlyTouchedProjectsByJsonChanges', () => {
6060
[
6161
{
6262
file: 'package.json',
63-
mtime: 0,
63+
hash: 'some-hash',
6464
ext: '.json',
6565
getChanges: () => [new WholeFileChange()],
6666
},

Diff for: ‎packages/workspace/src/core/affected-project-graph/locators/npm-packages.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('getTouchedNpmPackages', () => {
6767
[
6868
{
6969
file: 'package.json',
70-
mtime: 0,
70+
hash: 'some-hash',
7171
ext: '.json',
7272
getChanges: () => [
7373
{
@@ -98,7 +98,7 @@ describe('getTouchedNpmPackages', () => {
9898
[
9999
{
100100
file: 'package.json',
101-
mtime: 0,
101+
hash: 'some-hash',
102102
ext: '.json',
103103
getChanges: () => [
104104
{
@@ -137,7 +137,7 @@ describe('getTouchedNpmPackages', () => {
137137
[
138138
{
139139
file: 'package.json',
140-
mtime: 0,
140+
hash: 'some-hash',
141141
ext: '.json',
142142
getChanges: () => [
143143
{
@@ -177,7 +177,7 @@ describe('getTouchedNpmPackages', () => {
177177
[
178178
{
179179
file: 'package.json',
180-
mtime: 0,
180+
hash: 'some-hash',
181181
ext: '.json',
182182
getChanges: () => [new WholeFileChange()],
183183
},

Diff for: ‎packages/workspace/src/core/affected-project-graph/locators/nx-json-changes.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('getTouchedProjectsInNxJson', () => {
99
{
1010
file: 'source.ts',
1111
ext: '.ts',
12-
mtime: 0,
12+
hash: 'some-hash',
1313
getChanges: () => [new WholeFileChange()],
1414
},
1515
],
@@ -32,7 +32,7 @@ describe('getTouchedProjectsInNxJson', () => {
3232
{
3333
file: 'nx.json',
3434
ext: '.json',
35-
mtime: 0,
35+
hash: 'some-hash',
3636
getChanges: () => [new WholeFileChange()],
3737
},
3838
],
@@ -58,7 +58,7 @@ describe('getTouchedProjectsInNxJson', () => {
5858
{
5959
file: 'nx.json',
6060
ext: '.json',
61-
mtime: 0,
61+
hash: 'some-hash',
6262
getChanges: () => [
6363
{
6464
type: DiffType.Modified,
@@ -93,7 +93,7 @@ describe('getTouchedProjectsInNxJson', () => {
9393
{
9494
file: 'nx.json',
9595
ext: '.json',
96-
mtime: 0,
96+
hash: 'some-hash',
9797
getChanges: () => [
9898
{
9999
type: DiffType.Added,
@@ -138,7 +138,7 @@ describe('getTouchedProjectsInNxJson', () => {
138138
{
139139
file: 'nx.json',
140140
ext: '.json',
141-
mtime: 0,
141+
hash: 'some-hash',
142142
getChanges: () => [
143143
{
144144
type: DiffType.Deleted,
@@ -175,7 +175,7 @@ describe('getTouchedProjectsInNxJson', () => {
175175
{
176176
file: 'nx.json',
177177
ext: '.json',
178-
mtime: 0,
178+
hash: 'some-hash',
179179
getChanges: () => [
180180
{
181181
type: DiffType.Modified,

0 commit comments

Comments
 (0)
Please sign in to comment.