From 656fbe8aa89bc012feeb1b6a5b45b09e56fec3e6 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 25 Mar 2022 09:32:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Support=20override=20?= =?UTF-8?q?allow-scripts=20on=20bug-versions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deps https://github.com/cnpm/bug-versions/pull/185 --- lib/download/npm.js | 14 ++++++++++++++ test/fix-bug-versions.test.js | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/download/npm.js b/lib/download/npm.js index 05a77e19..b258cd44 100644 --- a/lib/download/npm.js +++ b/lib/download/npm.js @@ -93,6 +93,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 + // disable all install scripts + // { + // "foo": { + // "1.0.0": { + // "allow-scripts": true, + // "reason": "some description message" + // } + // } + // } + if (fixVersion['allow-scripts'] === false) { + options.console.warn(`[${pkg.name}@${realPkgVersion}] allow-scripts: ${chalk.green(JSON.stringify(fixVersion['allow-scripts']))}, reason: ${chalk.yellow(fixVersion.reason)}`); + options.ignoreScripts = true; + } } } diff --git a/test/fix-bug-versions.test.js b/test/fix-bug-versions.test.js index ba2e3a05..c2e25c20 100644 --- a/test/fix-bug-versions.test.js +++ b/test/fix-bug-versions.test.js @@ -9,7 +9,7 @@ const helper = require('./helper'); const bin = helper.npminstall; const update = path.join(path.dirname(helper.npminstall), 'update.js'); -describe('test/fix-bug-versions.test.js', () => { +describe.only('test/fix-bug-versions.test.js', () => { const demo = helper.fixtures('fix-bug-versions-app'); const cleanupModules = helper.cleanup(demo); const [ tmp, cleanupTmp ] = helper.tmp(); @@ -58,6 +58,21 @@ 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 "allow-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', /\[styled-components@05\.3\.5\] allow-scripts: false, reason:/) + .end(); + + assert(getPkg('node_modules/styled-components/package.json').version === '5.3.5'); + }); + it('should support on install and update', async () => { await coffee.fork(bin, [ '-d', From 1c58a62ae09f61baeb3a8da83fe703fc34c48895 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 25 Mar 2022 13:32:14 +0800 Subject: [PATCH 2/2] f --- lib/download/npm.js | 17 ++++++++++++----- test/fix-bug-versions.test.js | 12 ++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/download/npm.js b/lib/download/npm.js index b258cd44..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}`); @@ -94,18 +95,18 @@ async function resolve(pkg, options) { fixDependencies = fixVersion.dependencies; } // https://github.com/npm/rfcs/pull/488/files - // disable all install scripts + // merge custom scripts // { // "foo": { // "1.0.0": { - // "allow-scripts": true, + // "scripts": { "postinstall": "" }, // "reason": "some description message" // } // } // } - if (fixVersion['allow-scripts'] === false) { - options.console.warn(`[${pkg.name}@${realPkgVersion}] allow-scripts: ${chalk.green(JSON.stringify(fixVersion['allow-scripts']))}, reason: ${chalk.yellow(fixVersion.reason)}`); - options.ignoreScripts = true; + 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; } } } @@ -118,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); @@ -349,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 c2e25c20..1a7b9af3 100644 --- a/test/fix-bug-versions.test.js +++ b/test/fix-bug-versions.test.js @@ -9,7 +9,7 @@ const helper = require('./helper'); const bin = helper.npminstall; const update = path.join(path.dirname(helper.npminstall), 'update.js'); -describe.only('test/fix-bug-versions.test.js', () => { +describe('test/fix-bug-versions.test.js', () => { const demo = helper.fixtures('fix-bug-versions-app'); const cleanupModules = helper.cleanup(demo); const [ tmp, cleanupTmp ] = helper.tmp(); @@ -58,7 +58,7 @@ describe.only('test/fix-bug-versions.test.js', () => { assert(getPkg('node_modules/accord/node_modules/less/package.json').version.split('.')[0] === '2'); }); - it('should use fix "allow-scripts"', async () => { + it('should use fix "scripts"', async () => { await coffee.fork(bin, [ 'styled-components@5.3.5', '-d', @@ -67,10 +67,14 @@ describe.only('test/fix-bug-versions.test.js', () => { ], { cwd: tmp }) .debug() .expect('code', 0) - .expect('stderr', /\[styled-components@05\.3\.5\] allow-scripts: false, reason:/) + .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(); - assert(getPkg('node_modules/styled-components/package.json').version === '5.3.5'); + 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 () => {