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 1, 2023
1 parent 054226c commit d7dea4e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 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
49 changes: 25 additions & 24 deletions tap-snapshots/test/lib/commands/init.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,29 @@ exports[`test/lib/commands/init.js TAP workspaces no args -- yes > should print
added 1 package in {TIME}
`

exports[`test/lib/commands/init.js TAP workspaces no args -- yes > should reify tree on init ws complete 1`] = `
{
"name": "top-level",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "top-level",
"workspaces": [
"a"
]
},
"a": {
"version": "1.0.0",
"license": "ISC",
"devDependencies": {}
},
"node_modules/a": {
"resolved": "a",
"link": true
}
}
}
exports[`test/lib/commands/init.js TAP workspaces no args -- yes > should reify tree on init ws complete 1`] = `
{
"name": "top-level",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "top-level",
"workspaces": [
"a"
]
},
"a": {
"version": "1.0.0",
"license": "ISC",
"devDependencies": {}
},
"node_modules/a": {
"resolved": "a",
"link": true
}
}
}
`
`

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 d7dea4e

Please sign in to comment.