You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. var Promise = require('bluebird');
  3. var get = require('lodash.get');
  4. var readPkgUp = require('read-pkg-up');
  5. var path = require('path');
  6. var debug = require('debug')('get-package-info');
  7. var getInfo = function getInfo(props, dir, result) {
  8. if (!Array.isArray(props)) return Promise.reject(new Error('First argument must be array of properties to retrieve.'));
  9. if (!props.length) return Promise.resolve(result);
  10. debug('Getting props: ', props);
  11. debug('Looking up starting from directory: ', dir);
  12. debug('Result so far:', result);
  13. return Promise.resolve(readPkgUp({ cwd: dir, normalize: false })).then(function (_ref) {
  14. var src = _ref.path;
  15. var pkg = _ref.pkg;
  16. if (!src) {
  17. debug('Couldn\'t find any more package.json files');
  18. var err = new Error('Unable to find all properties in parent package.json files. Missing props: ' + props.map(function (prop) {
  19. return JSON.stringify(prop);
  20. }).join(', '));
  21. err.missingProps = props;
  22. err.result = result;
  23. throw err;
  24. }
  25. debug('Checking props in package.json found at:', src);
  26. var nextProps = [];
  27. props.forEach(function (prop) {
  28. // For props given as array
  29. // Look for props in that order, and when found
  30. // save value under all given props
  31. if (Array.isArray(prop)) {
  32. (function () {
  33. var value = void 0,
  34. sourceProp = void 0;
  35. prop.some(function (p) {
  36. sourceProp = p;
  37. value = get(pkg, p);
  38. return value;
  39. });
  40. if (value !== undefined) {
  41. debug('Found prop:', prop);
  42. prop.forEach(function (p) {
  43. result.values[p] = value;
  44. result.source[p] = { src: src, pkg: pkg, prop: sourceProp };
  45. });
  46. } else {
  47. debug('Couldn\'t find prop:', prop);
  48. nextProps.push(prop);
  49. }
  50. })();
  51. } else {
  52. // For regular string props, just look normally
  53. var _value = get(pkg, prop);
  54. if (_value !== undefined) {
  55. debug('Found prop:', prop);
  56. result.values[prop] = _value;
  57. result.source[prop] = { src: src, pkg: pkg, prop: prop };
  58. } else {
  59. debug('Couldn\'t find prop:', prop);
  60. nextProps.push(prop);
  61. }
  62. }
  63. });
  64. // Still have props to look for, look at another package.json above this one
  65. if (nextProps.length) {
  66. debug('Not all props satisfied, looking for parent package.json');
  67. return getInfo(nextProps, path.join(path.dirname(src), '..'), result);
  68. }
  69. debug('Found all props!');
  70. return result;
  71. });
  72. };
  73. module.exports = function (props, dir, cb) {
  74. return getInfo(props, dir, { values: {}, source: {} }).nodeify(cb);
  75. };