Skip to content

Commit

Permalink
feat: add logic to check for existence of package.json
Browse files Browse the repository at this point in the history
- Implemented logic to check if package.json exists in the current working directory.
  • Loading branch information
RaulCatalinas committed May 2, 2024
1 parent 6282ffa commit 905f570
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export async function addScript({ key, value }: Props) {
try {
const packageJsonPath = `${process.cwd()}/package.json`

const existPackageJsonInTheCurrentDirectory =
await existPackageJson(packageJsonPath)

if (!existPackageJsonInTheCurrentDirectory) {
throw new Error(
"The package.json file wasn't found in the current directory."
)
}

const packageJsonData = await fs.readFile(packageJsonPath, {
encoding: UTF8_ENCODING
})
Expand All @@ -37,3 +46,12 @@ export async function addScript({ key, value }: Props) {
throw new Error('Something went wrong, try again later.')
}
}

async function existPackageJson(path: string) {
try {
return await fs.exists(path)
} catch (error) {
console.error(error)
throw new Error('Something went wrong, try again later.')
}
}

0 comments on commit 905f570

Please sign in to comment.