Skip to content

Commit

Permalink
fix(publish): obey --ignore-scripts flag
Browse files Browse the repository at this point in the history
PR-URL: #3495
Credit: @wraithgar
Close: #3495
Reviewed-by: @nlf
  • Loading branch information
wraithgar committed Jul 12, 2021
1 parent 89483e8 commit feeb8e4
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
100 changes: 100 additions & 0 deletions test/lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit feeb8e4

Please sign in to comment.