Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(publish): obey --ignore-scripts flag #3495

Merged
merged 1 commit into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/publish.js
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
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()
})
})