Skip to content

Commit feeb8e4

Browse files
committed
fix(publish): obey --ignore-scripts flag
PR-URL: #3495 Credit: @wraithgar Close: #3495 Reviewed-by: @nlf
1 parent 89483e8 commit feeb8e4

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

lib/publish.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Publish extends BaseCommand {
6666
const dryRun = this.npm.config.get('dry-run')
6767
const json = this.npm.config.get('json')
6868
const defaultTag = this.npm.config.get('tag')
69+
const ignoreScripts = this.npm.config.get('ignore-scripts')
6970
const silent = log.level === 'silent'
7071

7172
if (semver.validRange(defaultTag))
@@ -82,7 +83,7 @@ class Publish extends BaseCommand {
8283
flatten(manifest.publishConfig, opts)
8384

8485
// only run scripts for directory type publishes
85-
if (spec.type === 'directory') {
86+
if (spec.type === 'directory' && !ignoreScripts) {
8687
await runScript({
8788
event: 'prepublishOnly',
8889
path: spec.fetchSpec,
@@ -119,7 +120,7 @@ class Publish extends BaseCommand {
119120
await otplease(opts, opts => libpub(manifest, tarballData, opts))
120121
}
121122

122-
if (spec.type === 'directory') {
123+
if (spec.type === 'directory' && !ignoreScripts) {
123124
await runScript({
124125
event: 'publish',
125126
path: spec.fetchSpec,

test/lib/publish.js

+100
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,103 @@ t.test('private workspaces', (t) => {
762762

763763
t.end()
764764
})
765+
766+
t.test('runs correct lifecycle scripts', t => {
767+
const testDir = t.testdir({
768+
'package.json': JSON.stringify({
769+
name: 'my-cool-pkg',
770+
version: '1.0.0',
771+
scripts: {
772+
prepublishOnly: 'echo test prepublishOnly',
773+
prepublish: 'echo test prepublish', // should NOT run this one
774+
publish: 'echo test publish',
775+
postpublish: 'echo test postpublish',
776+
},
777+
}, null, 2),
778+
})
779+
780+
const scripts = []
781+
const Publish = t.mock('../../lib/publish.js', {
782+
'@npmcli/run-script': (args) => {
783+
scripts.push(args)
784+
},
785+
'../../lib/utils/tar.js': {
786+
getContents: () => ({
787+
id: 'someid',
788+
}),
789+
logTar: () => {
790+
t.pass('logTar is called')
791+
},
792+
},
793+
libnpmpublish: {
794+
publish: () => {
795+
t.pass('publish called')
796+
},
797+
},
798+
})
799+
const npm = mockNpm({
800+
output: () => {
801+
t.pass('output is called')
802+
},
803+
})
804+
npm.config.getCredentialsByURI = (uri) => {
805+
t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry')
806+
return { token: 'some.registry.token' }
807+
}
808+
const publish = new Publish(npm)
809+
publish.exec([testDir], (er) => {
810+
if (er)
811+
throw er
812+
t.same(
813+
scripts.map(s => s.event),
814+
['prepublishOnly', 'publish', 'postpublish'],
815+
'runs only expected scripts, in order'
816+
)
817+
t.end()
818+
})
819+
})
820+
821+
t.test('does not run scripts on --ignore-scripts', t => {
822+
const testDir = t.testdir({
823+
'package.json': JSON.stringify({
824+
name: 'my-cool-pkg',
825+
version: '1.0.0',
826+
}, null, 2),
827+
})
828+
829+
const Publish = t.mock('../../lib/publish.js', {
830+
'@npmcli/run-script': () => {
831+
t.fail('should not call run-script')
832+
},
833+
'../../lib/utils/tar.js': {
834+
getContents: () => ({
835+
id: 'someid',
836+
}),
837+
logTar: () => {
838+
t.pass('logTar is called')
839+
},
840+
},
841+
libnpmpublish: {
842+
publish: () => {
843+
t.pass('publish called')
844+
},
845+
},
846+
})
847+
const npm = mockNpm({
848+
config: { 'ignore-scripts': true },
849+
output: () => {
850+
t.pass('output is called')
851+
},
852+
})
853+
npm.config.getCredentialsByURI = (uri) => {
854+
t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry')
855+
return { token: 'some.registry.token' }
856+
}
857+
const publish = new Publish(npm)
858+
publish.exec([testDir], (er) => {
859+
if (er)
860+
throw er
861+
t.pass('got to callback')
862+
t.end()
863+
})
864+
})

0 commit comments

Comments
 (0)