requirePackage.js 595 B

123456789101112131415161718192021222324
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const path = require('path');
  9. module.exports = function requirePackage(name) {
  10. const entry = require.resolve(name);
  11. let dir = path.dirname(entry);
  12. while (dir !== '/') {
  13. try {
  14. const pkg = require(path.join(dir, 'package.json'));
  15. return pkg.name === name ? pkg : {};
  16. } catch(error) {} // eslint-disable-line no-empty
  17. dir = path.dirname(dir);
  18. }
  19. return {};
  20. }