readPkgUp.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. exports.__esModule = true;
  3. const fs = require('fs');
  4. const pkgUp = require('./pkgUp').default;
  5. /** @type {(str: string) => string} */
  6. function stripBOM(str) {
  7. return str.replace(/^\uFEFF/, '');
  8. }
  9. /**
  10. * Derived significantly from read-pkg-up@2.0.0. See license below.
  11. *
  12. * @copyright Sindre Sorhus
  13. * MIT License
  14. *
  15. * Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to deal
  19. * in the Software without restriction, including without limitation the rights
  20. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included in
  25. * all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. * THE SOFTWARE.
  34. */
  35. /** @type {import('./readPkgUp').default} */
  36. exports.default = function readPkgUp(opts) {
  37. const fp = pkgUp(opts);
  38. if (!fp) {
  39. return {};
  40. }
  41. try {
  42. return {
  43. pkg: JSON.parse(stripBOM(fs.readFileSync(fp, { encoding: 'utf-8' }))),
  44. path: fp,
  45. };
  46. } catch (e) {
  47. return {};
  48. }
  49. };