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 authored and lukekarrys committed Jan 2, 2023
1 parent 55093e2 commit 753b98e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 20 additions & 1 deletion test/lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
})

Expand All @@ -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 => {
Expand Down

0 comments on commit 753b98e

Please sign in to comment.