diff --git a/lib/download/npm.js b/lib/download/npm.js index 05a77e19..6c6cd1d6 100644 --- a/lib/download/npm.js +++ b/lib/download/npm.js @@ -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}`); @@ -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; + } } } @@ -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); @@ -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); diff --git a/test/fix-bug-versions.test.js b/test/fix-bug-versions.test.js index ba2e3a05..1a7b9af3 100644 --- a/test/fix-bug-versions.test.js +++ b/test/fix-bug-versions.test.js @@ -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',