Skip to content

Commit

Permalink
fix(laverna): understands bad npm API
Browse files Browse the repository at this point in the history
`npm view <pkg> versions --json` outputs a JSON array of strings _if and only if_ multiple versions of `pkg` have been published. When a _single_ version has been published, it outputs a JSON string.

This change fixes the problem where Lerna always expected a JSON array of strings.
  • Loading branch information
boneskull authored and naugtur committed Feb 29, 2024
1 parent d53806e commit bdcaa1a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/laverna/src/laverna.js
Expand Up @@ -180,6 +180,9 @@ exports.Laverna = class Laverna {
throw err
}

if (typeof json === 'string') {
json = [json]
}
if (!isStringArray(json)) {
throw new TypeError(
`Output from \`npm view\` for ${Laverna.pkgToString(
Expand Down
38 changes: 37 additions & 1 deletion packages/laverna/test/laverna.spec.js
Expand Up @@ -291,7 +291,7 @@ test('publishWorkspaces - invalid workspaces - not strings', async (t) => {
})
})

test('publishWorkspaces - invalid JSON error from npm view', async (t) => {
test('publishWorkspaces - invalid JSON error from `npm view`', async (t) => {
const { fs } = memfs({
'/': {
'package.json': DEFAULT_ROOT_PKG_JSON,
Expand Down Expand Up @@ -322,6 +322,42 @@ test('publishWorkspaces - invalid JSON error from npm view', async (t) => {
)
})

test('publishWorkspaces - `npm view` returns a single version', async (t) => {
const { fs } = memfs({
'/': {
'package.json': DEFAULT_ROOT_PKG_JSON,
packages: {
workspace1: {
'package.json': JSON.stringify({
name: 'workspace1',
version: '1.0.0',
}),
},
},
},
})

await t.notThrowsAsync(
t.context.runLaverna(
{},
{
fs,
execFile: async () => {
return { stdout: '"0.0.1"', stderr: '' }
},
}
)
)
const args = t.context.console.error.mock.calls.flatMap(
(call) => call.arguments
)
t.true(
args.some((arg) =>
`${arg}`.includes('These package(s) will be published:\nworkspace1@1.0.0')
)
)
})

test('publish workspaces - unexpected JSON parsed from npm view', async (t) => {
const { fs } = memfs({
'/': {
Expand Down

0 comments on commit bdcaa1a

Please sign in to comment.