From feeb8e42a7b0510023175dc86269edb544d97601 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 1 Jul 2021 13:44:41 -0700 Subject: [PATCH] fix(publish): obey --ignore-scripts flag PR-URL: https://github.com/npm/cli/pull/3495 Credit: @wraithgar Close: #3495 Reviewed-by: @nlf --- lib/publish.js | 5 ++- test/lib/publish.js | 100 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index f35388a30f4ed..9c747eb5068f0 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -66,6 +66,7 @@ class Publish extends BaseCommand { const dryRun = this.npm.config.get('dry-run') const json = this.npm.config.get('json') const defaultTag = this.npm.config.get('tag') + const ignoreScripts = this.npm.config.get('ignore-scripts') const silent = log.level === 'silent' if (semver.validRange(defaultTag)) @@ -82,7 +83,7 @@ class Publish extends BaseCommand { flatten(manifest.publishConfig, opts) // only run scripts for directory type publishes - if (spec.type === 'directory') { + if (spec.type === 'directory' && !ignoreScripts) { await runScript({ event: 'prepublishOnly', path: spec.fetchSpec, @@ -119,7 +120,7 @@ class Publish extends BaseCommand { await otplease(opts, opts => libpub(manifest, tarballData, opts)) } - if (spec.type === 'directory') { + if (spec.type === 'directory' && !ignoreScripts) { await runScript({ event: 'publish', path: spec.fetchSpec, diff --git a/test/lib/publish.js b/test/lib/publish.js index 56590478fc1ae..4aa3e5592751e 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -762,3 +762,103 @@ t.test('private workspaces', (t) => { t.end() }) + +t.test('runs correct lifecycle scripts', t => { + const testDir = t.testdir({ + 'package.json': JSON.stringify({ + name: 'my-cool-pkg', + version: '1.0.0', + scripts: { + prepublishOnly: 'echo test prepublishOnly', + prepublish: 'echo test prepublish', // should NOT run this one + publish: 'echo test publish', + postpublish: 'echo test postpublish', + }, + }, null, 2), + }) + + const scripts = [] + const Publish = t.mock('../../lib/publish.js', { + '@npmcli/run-script': (args) => { + scripts.push(args) + }, + '../../lib/utils/tar.js': { + getContents: () => ({ + id: 'someid', + }), + logTar: () => { + t.pass('logTar is called') + }, + }, + libnpmpublish: { + publish: () => { + t.pass('publish called') + }, + }, + }) + const npm = mockNpm({ + output: () => { + t.pass('output is called') + }, + }) + npm.config.getCredentialsByURI = (uri) => { + t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry') + return { token: 'some.registry.token' } + } + const publish = new Publish(npm) + publish.exec([testDir], (er) => { + if (er) + throw er + t.same( + scripts.map(s => s.event), + ['prepublishOnly', 'publish', 'postpublish'], + 'runs only expected scripts, in order' + ) + t.end() + }) +}) + +t.test('does not run scripts on --ignore-scripts', t => { + const testDir = t.testdir({ + 'package.json': JSON.stringify({ + name: 'my-cool-pkg', + version: '1.0.0', + }, null, 2), + }) + + const Publish = t.mock('../../lib/publish.js', { + '@npmcli/run-script': () => { + t.fail('should not call run-script') + }, + '../../lib/utils/tar.js': { + getContents: () => ({ + id: 'someid', + }), + logTar: () => { + t.pass('logTar is called') + }, + }, + libnpmpublish: { + publish: () => { + t.pass('publish called') + }, + }, + }) + const npm = mockNpm({ + config: { 'ignore-scripts': true }, + output: () => { + t.pass('output is called') + }, + }) + npm.config.getCredentialsByURI = (uri) => { + t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry') + return { token: 'some.registry.token' } + } + const publish = new Publish(npm) + publish.exec([testDir], (er) => { + if (er) + throw er + t.pass('got to callback') + t.end() + }) +})