Skip to content

Commit

Permalink
fix(init): write package.json workspaces paths with / separators
Browse files Browse the repository at this point in the history
This also refactors the test to use `mockNpm`
  • Loading branch information
lukekarrys committed Jan 1, 2023
1 parent 31af1aa commit dc52222
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 333 deletions.
4 changes: 3 additions & 1 deletion lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const PackageJson = require('@npmcli/package-json')
const log = require('../utils/log-shim.js')
const updateWorkspaces = require('../workspaces/update-workspaces.js')

const posixPath = p => p.split('\\').join('/')

const BaseCommand = require('../base-command.js')

class Init extends BaseCommand {
Expand Down Expand Up @@ -203,7 +205,7 @@ class Init extends BaseCommand {
pkgJson.update({
workspaces: [
...(pkgJson.content.workspaces || []),
relative(this.npm.localPrefix, workspacePath),
posixPath(relative(this.npm.localPrefix, workspacePath)),
],
})

Expand Down
35 changes: 13 additions & 22 deletions tap-snapshots/test/lib/commands/init.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/commands/init.js TAP npm init workspces with root > does not print helper info 1`] = `
Array []
`
exports[`test/lib/commands/init.js TAP displays output > displays helper info 1`] = `
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
exports[`test/lib/commands/init.js TAP workspaces no args > should print helper info 1`] = `
Array []
`
See \`npm help init\` for definitive documentation on these fields
and exactly what they do.
Use \`npm install <pkg>\` afterwards to install a package and
save it as a dependency in the package.json file.
exports[`test/lib/commands/init.js TAP workspaces no args, existing folder > should print helper info 1`] = `
Array []
Press ^C at any time to quit.
`

exports[`test/lib/commands/init.js TAP workspaces post workspace-init reify > should print helper info 1`] = `
Array [
Array [
String(
added 1 package in 100ms
),
],
]
exports[`test/lib/commands/init.js TAP workspaces no args -- yes > should print helper info 1`] = `
added 1 package in {TIME}
`

exports[`test/lib/commands/init.js TAP workspaces post workspace-init reify > should reify tree on init ws complete 1`] = `
exports[`test/lib/commands/init.js TAP workspaces no args -- yes > should reify tree on init ws complete 1`] = `
{
"name": "top-level",
"lockfileVersion": 3,
Expand All @@ -53,7 +48,3 @@ exports[`test/lib/commands/init.js TAP workspaces post workspace-init reify > sh
}
`

exports[`test/lib/commands/init.js TAP workspaces with arg but missing workspace folder > should print helper info 1`] = `
Array []
`
Loading

0 comments on commit dc52222

Please sign in to comment.