Skip to content

Commit

Permalink
📦 NEW: Support override custom scripts on bug-versions (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Mar 25, 2022
1 parent dcc42c9 commit 810ee29
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async function resolve(pkg, options) {

let realPkgVersion = utils.findMaxSatisfyingVersion(spec, distTags, packageMeta.allVersions);
let fixDependencies;
let fixScripts;

if (!realPkgVersion) {
throw new Error(`[${pkg.displayName}] Can\'t find package ${pkg.name}@${pkg.rawSpec}`);
Expand All @@ -93,6 +94,20 @@ async function resolve(pkg, options) {
options.console.warn(`[${pkg.name}@${realPkgVersion}] use dependencies: ${chalk.green(JSON.stringify(fixVersion.dependencies))} instead, reason: ${chalk.yellow(fixVersion.reason)}`);
fixDependencies = fixVersion.dependencies;
}
// https://github.com/npm/rfcs/pull/488/files
// merge custom scripts
// {
// "foo": {
// "1.0.0": {
// "scripts": { "postinstall": "" },
// "reason": "some description message"
// }
// }
// }
if (fixVersion.scripts) {
options.console.warn(`[${pkg.name}@${realPkgVersion}] use scripts: ${chalk.green(JSON.stringify(fixVersion.scripts))} instead, reason: ${chalk.yellow(fixVersion.reason)}`);
fixScripts = fixVersion.scripts;
}
}
}

Expand All @@ -104,6 +119,9 @@ async function resolve(pkg, options) {
if (fixDependencies) {
realPkg.__fixDependencies = fixDependencies;
}
if (fixScripts) {
realPkg.__fixScripts = fixScripts;
}

debug('[%s@%s] spec: %s, real version: %s, dist-tags: %j',
pkg.name, pkg.rawSpec, pkg.fetchSpec, realPkg.version, distTags);
Expand Down Expand Up @@ -335,6 +353,9 @@ async function download(pkg, options) {
if (pkg.__fixDependencies) {
pkg.dependencies = Object.assign({}, pkg.dependencies, pkg.__fixDependencies);
}
if (pkg.__fixScripts) {
pkg.scripts = Object.assign({}, pkg.scripts, pkg.__fixScripts);
}

await utils.setInstallDone(ungzipDir);

Expand Down
19 changes: 19 additions & 0 deletions test/fix-bug-versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ describe('test/fix-bug-versions.test.js', () => {
assert(getPkg('node_modules/accord/node_modules/less/package.json').version.split('.')[0] === '2');
});

it('should use fix "scripts"', async () => {
await coffee.fork(bin, [
'styled-components@5.3.5',
'-d',
'--fix-bug-versions',
'--no-cache',
], { cwd: tmp })
.debug()
.expect('code', 0)
.expect('stderr', /use scripts: {\"postinstall\":\"\"} instead, reason:/)
.notExpect('stderr', /scripts.postinstall styled-components@5.3.5 finished/)
.notExpect('stdout', /scripts.postinstall styled-components@5.3.5 finished/)
.end();

const pkg = getPkg('node_modules/styled-components/package.json');
assert(pkg.version === '5.3.5');
assert(pkg.scripts.postinstall === 'node ./postinstall.js');
});

it('should support on install and update', async () => {
await coffee.fork(bin, [
'-d',
Expand Down

0 comments on commit 810ee29

Please sign in to comment.