Skip to content

Commit 1485cd0

Browse files
committed
feat: ✨ handle missing package.json error gracefully
* Added error handling for the case when `package.json` is not found in the current directory. * The application now logs a clear error message and exits with a status code of 1 if the file is missing.
1 parent dac257e commit 1485cd0

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@ import { render } from 'ink'
44
import ProjectInfo from './components/ProjectInfo'
55

66
import type { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package'
7+
import type { ErrorLike } from 'bun'
8+
9+
const currentDirectory = process.cwd()
710

811
/**
9-
* Load the package.json file from the current working directory.
12+
* The package.json file from the current working directory.
1013
*/
11-
const pkg: JSONSchemaForNPMPackageJsonFiles = await import(
12-
`${process.cwd()}/package.json`,
13-
{
14+
let pkg: JSONSchemaForNPMPackageJsonFiles
15+
try {
16+
pkg = await import(`${currentDirectory}/package.json`, {
1417
assert: { type: 'json' },
18+
})
19+
} catch (error: unknown) {
20+
if ((error as ErrorLike).code === 'ERR_MODULE_NOT_FOUND') {
21+
console.error(
22+
`Error: Could not find \`package.json\` in \`${currentDirectory}\`.`,
23+
)
24+
process.exit(1)
25+
} else {
26+
throw error
1527
}
16-
)
28+
}
1729

1830
render(<ProjectInfo pkg={pkg} />)

0 commit comments

Comments
 (0)