From 2564b9c37bbba8b2b7e7182cfe4b54a8c01dcb81 Mon Sep 17 00:00:00 2001 From: lykmapipo Date: Thu, 2 May 2019 15:50:21 +0300 Subject: [PATCH] fix: allow read package info from specified path or process.cwd --- src/index.js | 93 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/src/index.js b/src/index.js index 6fe6878..ce406c9 100644 --- a/src/index.js +++ b/src/index.js @@ -285,10 +285,40 @@ export const sortedUniq = value => { return value; }; +/** + * @function mergeObjects + * @name mergeObjects + * @description merge a list on objects into a single object + * @param {...Object} objects list of objects + * @return {Object} a merged object + * @author lally elias + * @license MIT + * @since 0.10.0 + * @version 0.1.0 + * @static + * @public + * @example + * + * const obj = mergeObjects({ a: 1 }, { b: 1 }, { c: 2}, { c: 2}, {b: null}) + * //=> { a: 1, b: 1, c: 2 } + */ +export const mergeObjects = (...objects) => { + // ensure source objects + let sources = compactify([...objects]); + sources = map(sources, compact); + + // merged objects + const merged = mergify({}, ...sources); + + // return merged object + return merged; +}; + /** * @function pkg * @name pkg - * @description read current process package information + * @description read package information + * @param {String} [path] valid path to package.json file * @param {String|String[]|...String} field fields to pick from package * @return {Object} current process package information * @author lally elias @@ -301,15 +331,33 @@ export const sortedUniq = value => { * * const { name, version } = pkg(); * // => { name: ..., version: ...} + * + * const { name, version } = pkg(__dirname); + * // => { name: ..., version: ...} */ -export const pkg = (...field) => { - const cwd = process.cwd(); - const copyOfPkg = readPackage({ cwd }); - const fields = uniq([...field]); - if (!isEmpty(fields)) { - return { ...pick(copyOfPkg, ...fields) }; +export const pkg = (path, ...field) => { + // try read from path or process cwd + const read = () => { + try { + return readPackage({ cwd: path }); + } catch (e) { + return readPackage({ cwd: process.cwd() }); + } + }; + + // try read package data + try { + const packageInfo = mergeObjects(read()); + const fields = uniq([...field, path]); + if (!isEmpty(fields)) { + const info = { ...pick(packageInfo, ...fields) }; + return isEmpty(info) ? { ...packageInfo } : info; + } + return packageInfo; + } catch (e) { + // no package data found + return {}; } - return copyOfPkg; }; /** @@ -413,35 +461,6 @@ export const abbreviate = (...words) => { */ export const idOf = data => get(data, '_id') || get(data, 'id'); -/** - * @function mergeObjects - * @name mergeObjects - * @description merge a list on objects into a single object - * @param {...Object} objects list of objects - * @return {Object} a merged object - * @author lally elias - * @license MIT - * @since 0.10.0 - * @version 0.1.0 - * @static - * @public - * @example - * - * const obj = mergeObjects({ a: 1 }, { b: 1 }, { c: 2}, { c: 2}, {b: null}) - * //=> { a: 1, b: 1, c: 2 } - */ -export const mergeObjects = (...objects) => { - // ensure source objects - let sources = compactify([...objects]); - sources = map(sources, compact); - - // merged objects - const merged = mergify({}, ...sources); - - // return merged object - return merged; -}; - /** * @function variableNameFor * @name variableNameFor