Skip to content

Commit e59c71f

Browse files
authored
Merge pull request #52 from dsanders11/pkg-no-name
Gracefully handle misconfigured packages with no name
2 parents 0620b56 + cfb2553 commit e59c71f

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,14 @@ const packageJsonWorkspacesNohoistFormat = packageJsonWorkspaces && packageJsonW
178178

179179
const workspaceGlobs = packageJsonWorkspacesNohoistFormat || packageJsonWorkspaces || ['packages/*']
180180

181-
const pkgs = listPkgs('./', workspaceGlobs)
181+
let pkgs
182+
try {
183+
pkgs = listPkgs('./', workspaceGlobs)
184+
} catch (err) {
185+
console.error(chalk.red(`\nERROR: ${err.message}`))
186+
process.exit(1)
187+
}
188+
182189
const pkgPaths = _.mapValues(_.keyBy(pkgs, p => p.json.name), v => v.path)
183190

184191
const pkgJsons = _.map(pkgs, pkg => pkg.json)

src/workspace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function listPkgs(wsRoot: string, globs: string[]) {
3838
return
3939
}
4040
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'))
41+
if (!pkgJson.name) throw new Error(`Package in directory ${f} has no name in package.json`)
4142
packages[pkgJson.name] = {
4243
path: path.join(wsRoot, f),
4344
json: pkgJson

tests/basic.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,19 @@ describe('basic', () => {
184184
}
185185
)
186186
})
187+
188+
it('should show an error for pkgs without name', async () => {
189+
await withScaffold(
190+
{
191+
packages: [
192+
echo.makePkg({ path: 'packages/p1', dependencies: {} }),
193+
]
194+
},
195+
async () => {
196+
let tst = await wsrun('doecho')
197+
expect(tst.status).toBeTruthy()
198+
expect(String(tst.output[2])).toEqual('\nERROR: Package in directory packages/p1 has no name in package.json\n')
199+
}
200+
)
201+
})
187202
})

tests/test.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let rimrafAsync = promisify(rimraf)
2020
let mkdirpAsync = promisify(mkdirp)
2121

2222
export type PackageJson = {
23-
name: string
23+
name?: string
2424
path?: string
2525
dependencies?: { [name: string]: string }
2626
devDependencies?: { [name: string]: string }

0 commit comments

Comments
 (0)