Skip to content

Commit

Permalink
Move integrity artifacts tracking to separate method - fixes #3247 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian McKenzie authored and bestander committed Apr 25, 2017
1 parent 7241de1 commit 0150890
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
26 changes: 26 additions & 0 deletions __tests__/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,29 @@ test.concurrent('warns when peer dependency is incorrect during add', (): Promis
'add-with-peer-dependency-incorrect',
);
});

test.concurrent('should retain build artifacts after add', (): Promise<void> => {
return buildRun(
reporters.BufferReporter,
fixturesLoc,
async (args, flags, config, reporter, lockfile): Promise<void> => {
const addA = new Add(args, flags, config, reporter, lockfile);
await addA.init();

const expectedArtifacts = ['foo.txt'];
const integrityLoc = path.join(config.cwd, 'node_modules', constants.INTEGRITY_FILENAME);

const beforeIntegrity = await fs.readJson(integrityLoc);
expect(beforeIntegrity.artifacts['[email protected]']).toEqual(expectedArtifacts);

const addB = new Add(['file:b'], flags, config, reporter, lockfile);
await addB.init();

const afterIntegrity = await fs.readJson(integrityLoc);
expect(afterIntegrity.artifacts['[email protected]']).toEqual(expectedArtifacts);
},
['file:a'],
{},
'retain-build-artifacts-after-add',
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('fs').writeFileSync('foo.txt', 'foobar');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "a",
"version": "0.0.0",
"scripts": {
"postinstall": "node install.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "b",
"version": "0.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
12 changes: 6 additions & 6 deletions src/cli/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,6 @@ export class Install {
return true;
}

const {artifacts} = match;
if (artifacts) {
this.linker.setArtifacts(artifacts);
this.scripts.setArtifacts(artifacts);
}

return false;
}

Expand Down Expand Up @@ -392,6 +386,12 @@ export class Install {
} = await this.fetchRequestFromCwd();
let topLevelPatterns: Array<string> = [];

const artifacts = await this.integrityChecker.getArtifacts();
if (artifacts) {
this.linker.setArtifacts(artifacts);
this.scripts.setArtifacts(artifacts);
}

steps.push(async (curr: number, total: number) => {
this.reporter.step(curr, total, this.reporter.lang('resolvingPackages'), emoji.get('mag'));
await this.resolver.init(this.prepareRequests(depRequests), this.flags.flat);
Expand Down
22 changes: 20 additions & 2 deletions src/integrity-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export type IntegrityCheckResult = {
integrityFileMissing: boolean,
integrityMatches?: boolean,
missingPatterns: Array<string>,
artifacts?: ?InstallArtifacts,
};

type IntegrityHashLocation = {
Expand Down Expand Up @@ -251,10 +250,29 @@ export default class InstallationIntegrityChecker {
integrityFileMissing: false,
integrityMatches,
missingPatterns,
artifacts: expected ? expected.artifacts : null,
};
}

/**
* Get artifacts from integrity file if it exists.
*/
async getArtifacts(): Promise<?InstallArtifacts> {
const loc = await this._getIntegrityHashLocation();
if (!loc.exists) {
return null;
}

const expectedRaw = await fs.readFile(loc.locationPath);
let expected: ?IntegrityFile;
try {
expected = JSON.parse(expectedRaw);
} catch (e) {
// ignore JSON parsing for legacy text integrity files compatibility
}

return expected ? expected.artifacts : null;
}

/**
* Write the integrity hash of the current install to disk.
*/
Expand Down

0 comments on commit 0150890

Please sign in to comment.