Skip to content

Commit

Permalink
feat(plugin-pack): reload manifest after prepack script
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed Jul 20, 2021
1 parent 633b69a commit 8abe25a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .yarn/versions/ad4d146d.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
"@yarnpkg/cli": major
"@yarnpkg/plugin-pack": minor

declined:
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-typescript"
Original file line number Diff line number Diff line change
Expand Up @@ -610,5 +610,57 @@ describe(`Commands`, () => {
expect(stdout).not.toMatch(/src\/a\.ts/);
}),
);

test(
`it should reflect changes made to package.json during prepack`,
makeTemporaryEnv({
workspaces: [`./dependency`, `./dependant`],
}, async({path, run, source}) => {
const dependency = `@test/dependency`;
const dependant = `@test/dependant`;

await fsUtils.writeJson(`${path}/dependency/package.json`, {
name: dependency,
version: `1.0.0`,
});

const packageJson = {
name: dependant,
version: `1.0.0`,
scripts: {
prepack: `cp package.json package.json.bak && cp package.json.tmp package.json`,
postpack: `mv package.json.bak package.json`,
},
devDependencies: {
[dependency]: `workspace:*`,
},
};

await fsUtils.writeJson(`${path}/dependant/package.json`, packageJson);
await fsUtils.writeJson(`${path}/dependant/package.json.tmp`, {
...packageJson,
dependencies: {
[dependency]: `workspace:^1.0.0`,
},
});

await run(`install`);
await run(`pack`, {
cwd: `${path}/dependant`,
});

await fsUtils.unpackToDirectory(path, `${path}/dependant/package.tgz`);

const packedManifest = await fsUtils.readJson(`${path}/package/package.json`);

expect(packedManifest.dependencies[dependency]).toBe(`^1.0.0`);
expect(packedManifest.devDependencies[dependency]).toBe(`1.0.0`);

const originalManifest = await fsUtils.readJson(`${path}/dependant/package.json`);

expect(originalManifest.dependencies).toBe(undefined);
expect(originalManifest.devDependencies[dependency]).toBe(`workspace:*`);
}),
);
});
});
6 changes: 5 additions & 1 deletion packages/plugin-pack/sources/packUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Report, Workspace, scriptUtils, tgzUtils} from '@yarnpkg/core';
import {Manifest, Report, Workspace, scriptUtils, tgzUtils} from '@yarnpkg/core';
import {FakeFS, JailFS, xfs, PortablePath, ppath, Filename, npath} from '@yarnpkg/fslib';
import {Hooks as StageHooks} from '@yarnpkg/plugin-stage';
import mm from 'micromatch';
Expand Down Expand Up @@ -57,6 +57,10 @@ export async function prepareForPack(workspace: Workspace, {report}: {report: Re
await scriptUtils.maybeExecuteWorkspaceLifecycleScript(workspace, `prepack`, {report});

try {
const manifestPath = ppath.join(workspace.cwd, Manifest.fileName);
if (await xfs.existsPromise(manifestPath))
await workspace.manifest.loadFile(manifestPath, {baseFs: xfs});

await cb();
} finally {
await scriptUtils.maybeExecuteWorkspaceLifecycleScript(workspace, `postpack`, {report});
Expand Down

0 comments on commit 8abe25a

Please sign in to comment.