From 8ac7abcc38636bf24959de55cddc8cc4be307b6b Mon Sep 17 00:00:00 2001 From: Nathan Fritz Date: Wed, 3 Aug 2022 21:10:33 -0700 Subject: [PATCH] feat: rootless workspace init provides suggestion --- lib/commands/init.js | 7 ++++++- test/lib/commands/init.js | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/commands/init.js b/lib/commands/init.js index 55c9b85a0c65b..d1bde934374c9 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -54,7 +54,12 @@ 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')) + const 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 + }) // these are workspaces that are being created, so we cant use // this.setWorkspaces() diff --git a/test/lib/commands/init.js b/test/lib/commands/init.js index 997563104055e..2d59f47d9842d 100644 --- a/test/lib/commands/init.js +++ b/test/lib/commands/init.js @@ -335,7 +335,7 @@ t.test('workspaces', async t => { }) await t.test('missing top-level package.json when settting workspace', async t => { - const { npm } = await mockNpm(t, { + const { npm, logs } = await mockNpm(t, { config: { workspace: 'a' }, }) @@ -344,6 +344,25 @@ t.test('workspaces', async t => { { code: 'ENOENT' }, 'should exit with missing package.json file error' ) + + t.equal(logs.warn[0][0], 'Missing package.json. Try with `--include-workspace-root`.') + }) + + await t.test('bad package.json when settting workspace', async t => { + const { npm, logs } = await mockNpm(t, { + prefixDir: { + 'package.json': '{{{{', + }, + config: { workspace: 'a' }, + }) + + await t.rejects( + npm.exec('init', []), + { code: 'EJSONPARSE' }, + 'should exit with parse file error' + ) + + t.strictSame(logs.warn, []) }) await t.test('using args - no package.json', async t => {