Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rootless workspace init provides suggestion #5261

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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