diff options
author | Gar <gar+gh@danger.computer> | 2021-07-01 23:44:41 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-07-12 19:06:37 +0300 |
commit | feeb8e42a7b0510023175dc86269edb544d97601 (patch) | |
tree | 830d330ff0fc67d0565e3ffe1c6efb0068e3363f | |
parent | 89483e888acc56386b9ebc4d70a4676e4a5a5cb1 (diff) |
fix(publish): obey --ignore-scripts flag
PR-URL: https://github.com/npm/cli/pull/3495
Credit: @wraithgar
Close: #3495
Reviewed-by: @nlf
-rw-r--r-- | lib/publish.js | 5 | ||||
-rw-r--r-- | test/lib/publish.js | 100 |
2 files changed, 103 insertions, 2 deletions
diff --git a/lib/publish.js b/lib/publish.js index f35388a30..9c747eb50 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 56590478f..4aa3e5592 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() + }) +}) |