Comparing version 2.2.0 to 2.2.1
141
lib/index.js
@@ -76,22 +76,22 @@ "use strict"; | ||
let exists = process.env.NODE_ENV !== 'test' && this.existsCache.has(file) ? this.existsCache.get(file) : await pathExists(file); | ||
const exists = process.env.NODE_ENV !== 'test' && this.existsCache.has(file) ? this.existsCache.get(file) : await pathExists(file); | ||
this.existsCache.set(file, exists); | ||
if (exists && options.packageKey && _path.default.basename(file) === 'package.json') { | ||
if (exists) { | ||
if (!options.packageKey || _path.default.basename(file) !== 'package.json') { | ||
return file; | ||
} | ||
const data = require(file); | ||
delete require.cache[file]; | ||
exists = Object.prototype.hasOwnProperty.call(data, options.packageKey); | ||
const hasPackageKey = Object.prototype.hasOwnProperty.call(data, options.packageKey); | ||
if (exists) { | ||
this.packageJsonCache.set(file, data[options.packageKey]); | ||
if (hasPackageKey) { | ||
this.packageJsonCache.set(file, data); | ||
return file; | ||
} | ||
} else { | ||
this.packageJsonCache.delete(file); | ||
} | ||
this.existsCache.set(file, exists); | ||
if (exists) { | ||
return file; | ||
} | ||
continue; | ||
} | ||
@@ -112,22 +112,22 @@ | ||
let exists = process.env.NODE_ENV !== 'test' && this.existsCache.has(file) ? this.existsCache.get(file) : pathExistsSync(file); | ||
const exists = process.env.NODE_ENV !== 'test' && this.existsCache.has(file) ? this.existsCache.get(file) : pathExistsSync(file); | ||
this.existsCache.set(file, exists); | ||
if (exists && options.packageKey && _path.default.basename(file) === 'package.json') { | ||
if (exists) { | ||
if (!options.packageKey || _path.default.basename(file) !== 'package.json') { | ||
return file; | ||
} | ||
const data = require(file); | ||
delete require.cache[file]; | ||
exists = Object.prototype.hasOwnProperty.call(data, options.packageKey); | ||
const hasPackageKey = Object.prototype.hasOwnProperty.call(data, options.packageKey); | ||
if (exists) { | ||
this.packageJsonCache.set(file, data[options.packageKey]); | ||
if (hasPackageKey) { | ||
this.packageJsonCache.set(file, data); | ||
return file; | ||
} | ||
} else { | ||
this.packageJsonCache.delete(file); | ||
} | ||
this.existsCache.set(file, exists); | ||
if (exists) { | ||
return file; | ||
} | ||
continue; | ||
} | ||
@@ -141,27 +141,3 @@ | ||
async resolve(...args) { | ||
const options = Object.assign({}, this.options); | ||
if (Object.prototype.toString.call(args[0]) === '[object Object]') { | ||
Object.assign(options, args[0]); | ||
} else { | ||
if (args[0]) { | ||
options.files = args[0]; | ||
} | ||
if (args[1]) { | ||
options.cwd = args[1]; | ||
} | ||
if (args[2]) { | ||
options.stopDir = args[2]; | ||
} | ||
} | ||
options.cwd = _path.default.resolve(options.cwd); | ||
options.stopDir = options.stopDir ? _path.default.resolve(options.stopDir) : _path.default.parse(options.cwd).root; | ||
if (!options.files || options.files.length === 0) { | ||
throw new Error('files must be an non-empty array!'); | ||
} | ||
const options = this.normalizeOptions(args); | ||
return this.recusivelyResolve(options); | ||
@@ -171,27 +147,3 @@ } | ||
resolveSync(...args) { | ||
const options = Object.assign({}, this.options); | ||
if (Object.prototype.toString.call(args[0]) === '[object Object]') { | ||
Object.assign(options, args[0]); | ||
} else { | ||
if (args[0]) { | ||
options.files = args[0]; | ||
} | ||
if (args[1]) { | ||
options.cwd = args[1]; | ||
} | ||
if (args[2]) { | ||
options.stopDir = args[2]; | ||
} | ||
} | ||
options.cwd = _path.default.resolve(options.cwd); | ||
options.stopDir = options.stopDir ? _path.default.resolve(options.stopDir) : _path.default.parse(options.cwd).root; | ||
if (!options.files || options.files.length === 0) { | ||
throw new Error('files must be an non-empty array!'); | ||
} | ||
const options = this.normalizeOptions(args); | ||
return this.recusivelyResolveSync(options); | ||
@@ -201,3 +153,4 @@ } | ||
async load(...args) { | ||
const filepath = await this.resolve(...args); | ||
const options = this.normalizeOptions(args); | ||
const filepath = await this.recusivelyResolve(options); | ||
@@ -227,3 +180,3 @@ if (filepath) { | ||
path: filepath, | ||
data: this.packageJsonCache.get(filepath) | ||
data: this.packageJsonCache.get(filepath)[options.packageKey] | ||
}; | ||
@@ -249,3 +202,4 @@ } | ||
loadSync(...args) { | ||
const filepath = this.resolveSync(...args); | ||
const options = this.normalizeOptions(args); | ||
const filepath = this.recusivelyResolveSync(options); | ||
@@ -275,3 +229,3 @@ if (filepath) { | ||
path: filepath, | ||
data: this.packageJsonCache.get(filepath) | ||
data: this.packageJsonCache.get(filepath)[options.packageKey] | ||
}; | ||
@@ -308,5 +262,36 @@ } | ||
this.existsCache.clear(); | ||
this.packageJsonCache.clear(); | ||
return this; | ||
} | ||
normalizeOptions(args) { | ||
const options = Object.assign({}, this.options); | ||
if (Object.prototype.toString.call(args[0]) === '[object Object]') { | ||
Object.assign(options, args[0]); | ||
} else { | ||
if (args[0]) { | ||
options.files = args[0]; | ||
} | ||
if (args[1]) { | ||
options.cwd = args[1]; | ||
} | ||
if (args[2]) { | ||
options.stopDir = args[2]; | ||
} | ||
} | ||
options.cwd = _path.default.resolve(options.cwd); | ||
options.stopDir = options.stopDir ? _path.default.resolve(options.stopDir) : _path.default.parse(options.cwd).root; | ||
if (!options.files || options.files.length === 0) { | ||
throw new Error('[joycon] files must be an non-empty array!'); | ||
} | ||
options.__normalized__ = true; | ||
return options; | ||
} | ||
} | ||
@@ -313,0 +298,0 @@ |
{ | ||
"name": "joycon", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Load config with ease.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -54,2 +54,5 @@ export interface Options { | ||
removeLoader(name: string): this | ||
/** Clear internal cache */ | ||
clearCache(): this | ||
} | ||
@@ -56,0 +59,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13939
278