Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: handle invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed May 11, 2018
1 parent c90dfcf commit eb49618
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
"debug": "^3.1.0"
},
"devDependencies": {
"@oclif/errors": "^1.0.9",
"@oclif/errors": "^1.0.12",
"@oclif/parser": "^3.3.3",
"@oclif/tslint": "^1.1.1",
"@types/chai": "^4.1.3",
"@types/globby": "^6.1.0",
"@types/indent-string": "^3.0.0",
"@types/lodash": "^4.14.108",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.4",
"@types/node": "^10.0.8",
"@types/wrap-ansi": "^3.0.0",
"chai": "^4.1.2",
"fancy-test": "^1.0.6",
"fancy-test": "^1.0.8",
"globby": "^8.0.1",
"lodash": "^4.17.10",
"mocha": "^5.1.1",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export class Config implements IConfig {
if (this.options.userPlugins !== false) {
try {
const userPJSONPath = path.join(this.dataDir, 'package.json')
debug('reading user plugins pjson %s', userPJSONPath)
const pjson = this.userPJSON = await loadJSON(userPJSONPath)
if (!pjson.oclif) pjson.oclif = {schema: 1}
await this.loadPlugins(userPJSONPath, 'user', pjson.oclif.plugins.filter((p: any) => p.type === 'user'))
Expand Down
12 changes: 10 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// tslint:disable no-implicit-dependencies
import * as fs from 'fs'

const debug = require('debug')('@oclif/config')

export function flatMap<T, U>(arr: T[], fn: (i: T) => U[]): U[] {
return arr.reduce((arr, i) => arr.concat(fn(i)), [] as U[])
}
Expand All @@ -14,6 +16,7 @@ export function mapValues<T extends object, TResult>(obj: {[P in keyof T]: T[P]}
}

export function loadJSONSync(path: string): any {
debug('loadJSONSync %s', path)
// let loadJSON
// try { loadJSON = require('load-json-file') } catch {}
// if (loadJSON) return loadJSON.sync(path)
Expand All @@ -26,13 +29,18 @@ export function exists(path: string): Promise<boolean> {
}

export function loadJSON(path: string): Promise<any> {
debug('loadJSON %s', path)
// let loadJSON
// try { loadJSON = require('load-json-file') } catch {}
// if (loadJSON) return loadJSON.sync(path)
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, d) => {
if (err) reject(err)
else resolve(JSON.parse(d))
try {
if (err) reject(err)
else resolve(JSON.parse(d))
} catch (err) {
reject(err)
}
})
})
}
Expand Down
26 changes: 13 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# yarn lockfile v1


"@oclif/errors@^1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.0.9.tgz#6fe3afd8e7683c436df63674550899adec10c81c"
"@oclif/errors@^1.0.12":
version "1.0.12"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.0.12.tgz#6c8ea932d3fcfc26f5b3bc77e45da8f33189f188"
dependencies:
clean-stack "^1.3.0"
fs-extra "^6.0.0"
fs-extra "^6.0.1"
indent-string "^3.2.0"
strip-ansi "^4.0.0"
wrap-ansi "^3.0.1"
Expand Down Expand Up @@ -71,9 +71,9 @@
version "9.4.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.5.tgz#d2a90c634208173d1b1a0a6ba9f1df3de62edcf5"

"@types/node@^10.0.4":
version "10.0.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.0.4.tgz#130598ee56e9a1210a53f557d64935571f05390d"
"@types/node@^10.0.8":
version "10.0.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.0.8.tgz#37b4d91d4e958e4c2ba0be2b86e7ed4ff19b0858"

"@types/wrap-ansi@^3.0.0":
version "3.0.0"
Expand Down Expand Up @@ -425,9 +425,9 @@ extglob@^2.0.2:
snapdragon "^0.8.1"
to-regex "^3.0.1"

fancy-test@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/fancy-test/-/fancy-test-1.0.6.tgz#e0a061ecdcfb9d8fbea1f86375ed26e258ed7243"
fancy-test@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/fancy-test/-/fancy-test-1.0.8.tgz#d66a5ffab73a0792bf3c8404812c5a774f0e3180"
dependencies:
lodash "^4.17.10"
stdout-stderr "^0.1.9"
Expand Down Expand Up @@ -460,9 +460,9 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

fs-extra@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.0.tgz#0f0afb290bb3deb87978da816fcd3c7797f3a817"
fs-extra@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.1.tgz#8abc128f7946e310135ddc93b98bddb410e7a34b"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
Expand Down

0 comments on commit eb49618

Please sign in to comment.