Skip to content

Commit

Permalink
Don't fail if package.json doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Nov 1, 2018
1 parent b94ac5c commit 5c8d938
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.1.3

* Don't fail if `package.json` doesn't exist

## 1.1.2

* Add debug message
Expand Down
5 changes: 5 additions & 0 deletions src/runner/__tests__/index.ts
Expand Up @@ -187,4 +187,9 @@ describe('run', () => {
})
expect(status).toBe(0)
})

it("should not throw if there's no package.json", async () => {
const dir = tempy.directory()
await index(['', getScriptPath(dir), 'pre-push'])
})
})
12 changes: 11 additions & 1 deletion src/runner/index.ts
Expand Up @@ -17,7 +17,16 @@ export default async function run(
getStdinFn: () => Promise<string> = getStdin // Used for mocking
): Promise<number> {
const cwd = path.resolve(scriptPath.split('node_modules')[0])
const pkg = readPkg.sync({ cwd, normalize: false })
// In some cases, package.json may not exist
// For example, when switching to gh-page branch
let pkg
try {
pkg = readPkg.sync({ cwd, normalize: false })
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
const config = getConf(cwd)

const command: string | undefined =
Expand All @@ -26,6 +35,7 @@ export default async function run(
const oldCommand: string | undefined =
pkg && pkg.scripts && pkg.scripts[hookName.replace('-', '')]

// Run command
try {
const env: IEnv = {}

Expand Down

0 comments on commit 5c8d938

Please sign in to comment.