requirePackage.js 721 B

1234567891011121314151617181920212223242526
  1. /*
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. 'use strict';
  11. const path = require('path');
  12. module.exports = function requirePackage(name) {
  13. const entry = require.resolve(name);
  14. let dir = path.dirname(entry);
  15. while (dir !== '/') {
  16. try {
  17. const pkg = require(path.join(dir, 'package.json'));
  18. return pkg.name === name ? pkg : {};
  19. } catch(error) {} // eslint-disable-line no-empty
  20. dir = path.dirname(dir);
  21. }
  22. return {};
  23. }