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): skip private workspaces #3285

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const runScript = require('@npmcli/run-script')
const pacote = require('pacote')
const npa = require('npm-package-arg')
const npmFetch = require('npm-registry-fetch')
const chalk = require('chalk')

const otplease = require('./utils/otplease.js')
const { getContents, logTar } = require('./utils/tar.js')
Expand Down Expand Up @@ -154,10 +155,29 @@ class Publish extends BaseCommand {
const results = {}
const json = this.npm.config.get('json')
const silent = log.level === 'silent'
const noop = a => a
const color = this.npm.color ? chalk : { green: noop, bold: noop }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of these days we should pull this repeated logic into npm.js itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 either that or maybe it ought to live in one of the base commands parent classes to be reused

const workspaces =
await getWorkspaces(filters, { path: this.npm.localPrefix })

for (const [name, workspace] of workspaces.entries()) {
const pkgContents = await this.publish([workspace])
let pkgContents
try {
pkgContents = await this.publish([workspace])
} catch (err) {
if (err.code === 'EPRIVATE') {
log.warn(
'publish',
`Skipping workspace ${
color.green(name)
}, marked as ${
color.bold('private')
}`
)
continue
}
throw err
ruyadorno marked this conversation as resolved.
Show resolved Hide resolved
}
// This needs to be in-line w/ the rest of the output that non-JSON
// publish generates
if (!silent && !json)
Expand Down
34 changes: 34 additions & 0 deletions tap-snapshots/test/lib/publish.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/publish.js TAP private workspaces colorless > should output all publishes 1`] = `
Array [
"+ @npmcli/[email protected]",
]
`

exports[`test/lib/publish.js TAP private workspaces colorless > should publish all non-private workspaces 1`] = `
Array [
Object {
"_id": "@npmcli/[email protected]",
"name": "@npmcli/b",
"readme": "ERROR: No README data found!",
"version": "1.0.0",
},
]
`

exports[`test/lib/publish.js TAP private workspaces with color > should output all publishes 1`] = `
Array [
"+ @npmcli/[email protected]",
]
`

exports[`test/lib/publish.js TAP private workspaces with color > should publish all non-private workspaces 1`] = `
Array [
Object {
"_id": "@npmcli/[email protected]",
"name": "@npmcli/b",
"readme": "ERROR: No README data found!",
"version": "1.0.0",
},
]
`

exports[`test/lib/publish.js TAP shows usage with wrong set of arguments > should print usage 1`] = `
Error:
Usage: npm publish
Expand Down
145 changes: 145 additions & 0 deletions test/lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,148 @@ t.test('workspaces', (t) => {
})
t.end()
})

t.test('private workspaces', (t) => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'workspaces-project',
version: '1.0.0',
workspaces: ['packages/*'],
}),
packages: {
a: {
'package.json': JSON.stringify({
name: '@npmcli/a',
version: '1.0.0',
private: true,
}),
},
b: {
'package.json': JSON.stringify({
name: '@npmcli/b',
version: '1.0.0',
}),
},
},
})

const publishes = []
const outputs = []
t.beforeEach(() => {
npm.config.set('json', false)
outputs.length = 0
publishes.length = 0
})
const mocks = {
'../../lib/utils/tar.js': {
getContents: (manifest) => ({
id: manifest._id,
}),
logTar: () => {},
},
libnpmpublish: {
publish: (manifest, tarballData, opts) => {
if (manifest.private) {
throw Object.assign(
new Error('private pkg'),
{ code: 'EPRIVATE' }
)
}
publishes.push(manifest)
},
},
}
const npm = mockNpm({
output: (o) => {
outputs.push(o)
},
})
npm.localPrefix = testDir
npm.config.getCredentialsByURI = (uri) => {
return { token: 'some.registry.token' }
}

t.test('with color', t => {
const Publish = t.mock('../../lib/publish.js', {
...mocks,
npmlog: {
notice () {},
verbose () {},
warn (title, msg) {
t.equal(title, 'publish', 'should use publish warn title')
t.match(
msg,
'Skipping workspace \u001b[32m@npmcli/a\u001b[39m, marked as \u001b[1mprivate\u001b[22m',
'should display skip private workspace warn msg'
)
},
},
})
const publish = new Publish(npm)

npm.color = true
publish.execWorkspaces([], [], (err) => {
t.notOk(err)
t.matchSnapshot(publishes, 'should publish all non-private workspaces')
t.matchSnapshot(outputs, 'should output all publishes')
npm.color = false
t.end()
})
})

t.test('colorless', t => {
const Publish = t.mock('../../lib/publish.js', {
...mocks,
npmlog: {
notice () {},
verbose () {},
warn (title, msg) {
t.equal(title, 'publish', 'should use publish warn title')
t.equal(
msg,
'Skipping workspace @npmcli/a, marked as private',
'should display skip private workspace warn msg'
)
},
},
})
const publish = new Publish(npm)

publish.execWorkspaces([], [], (err) => {
t.notOk(err)
t.matchSnapshot(publishes, 'should publish all non-private workspaces')
t.matchSnapshot(outputs, 'should output all publishes')
t.end()
})
})

t.test('unexpected error', t => {
const Publish = t.mock('../../lib/publish.js', {
...mocks,
libnpmpublish: {
publish: (manifest, tarballData, opts) => {
if (manifest.private)
throw new Error('ERR')

publishes.push(manifest)
},
},
npmlog: {
notice () {},
verbose () {},
},
})
const publish = new Publish(npm)

publish.execWorkspaces([], [], (err) => {
t.match(
err,
/ERR/,
'should throw unexpected error'
)
t.end()
})
})

t.end()
})