Skip to content

Commit

Permalink
fix: lazy-load package.json and cache. get rid of pkg-conf dependency. (
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Jul 9, 2016
1 parent 588a8c1 commit 2609b2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -16,7 +16,6 @@
"decamelize": "^1.1.1",
"lodash.assign": "^4.0.3",
"os-locale": "^1.4.0",
"pkg-conf": "^1.1.2",
"read-pkg-up": "^1.0.1",
"require-directory": "^2.1.1",
"require-main-filename": "^1.0.1",
Expand Down
35 changes: 20 additions & 15 deletions yargs.js
Expand Up @@ -7,8 +7,6 @@ const path = require('path')
const Usage = require('./lib/usage')
const Validation = require('./lib/validation')
const Y18n = require('y18n')
const readPkgUp = require('read-pkg-up')
const pkgConf = require('pkg-conf')
const requireMainFilename = require('require-main-filename')
const objFilter = require('./lib/obj-filter')
const setBlocking = require('set-blocking')
Expand All @@ -24,6 +22,7 @@ function Yargs (processArgs, cwd, parentRequire) {
var preservedGroups = {}
var usage = null
var validation = null

const y18n = Y18n({
directory: path.resolve(__dirname, './locales'),
updateFiles: false
Expand Down Expand Up @@ -345,19 +344,30 @@ function Yargs (processArgs, cwd, parentRequire) {
self.pkgConf = function (key, path) {
var conf = null

var obj = readPkgUp.sync({
cwd: path || requireMainFilename(parentRequire || require)
})
var obj = pkgUp(path)

// If an object exists in the key, add it to options.configObjects
if (obj.pkg && obj.pkg[key] && typeof obj.pkg[key] === 'object') {
conf = obj.pkg[key]
if (obj[key] && typeof obj[key] === 'object') {
conf = obj[key]
options.configObjects = (options.configObjects || []).concat(conf)
}

return self
}

var pkg = null
function pkgUp (path) {
if (pkg && !path) return pkg
const readPkgUp = require('read-pkg-up')

var obj = readPkgUp.sync({
cwd: path || requireMainFilename(parentRequire || require)
})

if (obj.pkg) pkg = obj.pkg
return pkg || {}
}

self.parse = function (args, shortCircuit) {
return parseArgs(args, shortCircuit)
}
Expand Down Expand Up @@ -499,11 +509,9 @@ function Yargs (processArgs, cwd, parentRequire) {
}

function guessVersion () {
var obj = readPkgUp.sync({
cwd: requireMainFilename(parentRequire || require)
})
var obj = pkgUp()

return obj.pkg ? obj.pkg.version : 'unknown'
return obj.version || 'unknown'
}

var helpOpt = null
Expand Down Expand Up @@ -624,10 +632,7 @@ function Yargs (processArgs, cwd, parentRequire) {

function parseArgs (args, shortCircuit) {
options.__ = y18n.__
options.configuration = pkgConf.sync('yargs', {
defaults: {},
cwd: requireMainFilename(require)
})
options.configuration = pkgUp()['yargs'] || {}
const parsed = Parser.detailed(args, options)
const argv = parsed.argv
var aliases = parsed.aliases
Expand Down

0 comments on commit 2609b2e

Please sign in to comment.