Skip to content

Commit

Permalink
feat: rootless workspace init provides suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzy committed Aug 17, 2022
1 parent ca756fd commit 97549ae
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
11 changes: 10 additions & 1 deletion lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ class Init extends BaseCommand {
// reads package.json for the top-level folder first, by doing this we
// ensure the command throw if no package.json is found before trying
// to create a workspace package.json file or its folders
const pkg = await rpj(resolve(this.npm.localPrefix, 'package.json'))
let pkg
try {
pkg = await rpj(resolve(this.npm.localPrefix, 'package.json'))
} catch (err) {
if (err.code === 'ENOENT') {
log.warn('Missing package.json. Try with `--include-workspace-root`.')
}

throw err
}
const wPath = filterArg => resolve(this.npm.localPrefix, filterArg)

const workspacesPaths = []
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@
"author": "Isaac Z. Schlueter <[email protected]> (http://blog.izs.me)",
"repository": {
"type": "git",
"url": "https://github.com/npm/cli"
"url": "git+https://github.com/npm/cli.git"
},
"bugs": {
"url": "https://github.com/npm/cli/issues"
},
"directories": {
"bin": "./bin",
"doc": "./doc",
"lib": "./lib",
"man": "./man"
"doc": "docs",
"lib": "lib",
"test": "test"
},
"main": "./index.js",
"bin": {
Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/test/lib/commands/init.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/commands/init.js TAP no args, missing package > should print warn info 1`] = `
Array []
`

exports[`test/lib/commands/init.js TAP npm init workspces with root > does not print helper info 1`] = `
Array []
`
Expand Down
61 changes: 60 additions & 1 deletion test/lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,15 @@ t.test('workspaces', t => {

npm.localPrefix = t.testdir({})

const Init = require('../../../lib/commands/init.js')
const Init = t.mock('../../../lib/commands/init.js', {
...mocks,
'proc-log': {
...mocks['proc-log'],
warn: (msg) => {
t.equal(msg, 'Missing package.json. Try with `--include-workspace-root`.')
},
},
})
const init = new Init(npm)

await t.rejects(
Expand All @@ -482,6 +490,27 @@ t.test('workspaces', t => {
)
})

t.test('other errors pass through when setting workspace', async t => {
// init-package-json prints directly to console.log
// this avoids poluting test output with those logs
console.log = noop

npm.localPrefix = t.testdir({
'package.json': {
name: 'ape-ecs',
version: '1.0.0',
},
})
const Init = require('../../../lib/commands/init.js')
const init = new Init(npm)

await t.rejects(
init.execWorkspaces([], ['a@kl@ijasdf']),
{ code: 'EISDIR' },
'should exit with missing package.json file error'
)
})

t.test('using args', async t => {
npm.localPrefix = t.testdir({
b: {
Expand Down Expand Up @@ -540,3 +569,33 @@ t.test('npm init workspces with root', async t => {
t.equal(pkg.license, 'ISC')
t.matchSnapshot(npm._mockOutputs, 'does not print helper info')
})

t.test('no args, missing package', async t => {
t.teardown(() => {
npm._mockOutputs.length = 0
})

const Init = t.mock('../../../lib/commands/init.js', {
...mocks,
'proc-log': {
...mocks['proc-log'],
warn: (msg) => {
t.equal(msg, 'Missing package.json. Try with `--include-workspace-root`.')
},
},
})
const init = new Init(npm)
// init-package-json prints directly to console.log
// this avoids poluting test output with those logs
console.log = noop

npm.localPrefix = t.testdir({})

await t.rejects(
init.execWorkspaces([], ['a']),
{ code: 'ENOENT' },
'should exit with missing package.json file error'
)

t.matchSnapshot(npm._mockOutputs, 'should print warn info')
})

0 comments on commit 97549ae

Please sign in to comment.