index.js 816 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const path = require('path');
  3. const loadJsonFile = require('load-json-file');
  4. const pathType = require('path-type');
  5. module.exports = (fp, opts) => {
  6. if (typeof fp !== 'string') {
  7. opts = fp;
  8. fp = '.';
  9. }
  10. opts = opts || {};
  11. return pathType.dir(fp)
  12. .then(isDir => {
  13. if (isDir) {
  14. fp = path.join(fp, 'package.json');
  15. }
  16. return loadJsonFile(fp);
  17. })
  18. .then(x => {
  19. if (opts.normalize !== false) {
  20. require('normalize-package-data')(x);
  21. }
  22. return x;
  23. });
  24. };
  25. module.exports.sync = (fp, opts) => {
  26. if (typeof fp !== 'string') {
  27. opts = fp;
  28. fp = '.';
  29. }
  30. opts = opts || {};
  31. fp = pathType.dirSync(fp) ? path.join(fp, 'package.json') : fp;
  32. const x = loadJsonFile.sync(fp);
  33. if (opts.normalize !== false) {
  34. require('normalize-package-data')(x);
  35. }
  36. return x;
  37. };