diff --git a/lib/commands/init.js b/lib/commands/init.js index 039f08e06059e..192812a84dc4b 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -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 = [] diff --git a/package.json b/package.json index 6efd43f49e96f..564c50deece97 100644 --- a/package.json +++ b/package.json @@ -29,16 +29,16 @@ "author": "Isaac Z. Schlueter (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", + "man": "man", + "test": "test" }, "main": "./index.js", "bin": { diff --git a/tap-snapshots/test/lib/commands/init.js.test.cjs b/tap-snapshots/test/lib/commands/init.js.test.cjs index 97a6722f3ece4..9045bee1bbf02 100644 --- a/tap-snapshots/test/lib/commands/init.js.test.cjs +++ b/tap-snapshots/test/lib/commands/init.js.test.cjs @@ -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 [] ` diff --git a/test/lib/commands/init.js b/test/lib/commands/init.js index e7b2739341437..4cd406e4b6323 100644 --- a/test/lib/commands/init.js +++ b/test/lib/commands/init.js @@ -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( @@ -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: { @@ -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') +})