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.

17 lines
712 B

4 years ago
  1. 'use strict';
  2. const path = require('path');
  3. const resolveCwd = require('resolve-cwd');
  4. const pkgDir = require('pkg-dir');
  5. module.exports = filename => {
  6. const globalDir = pkgDir.sync(path.dirname(filename));
  7. const relativePath = path.relative(globalDir, filename);
  8. const pkg = require(path.join(globalDir, 'package.json'));
  9. const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
  10. // Use `path.relative()` to detect local package installation,
  11. // because __filename's case is inconsistent on Windows
  12. // Can use `===` when targeting Node.js 8
  13. // See https://github.com/nodejs/node/issues/6624
  14. return localFile && path.relative(localFile, filename) !== '' ? require(localFile) : null;
  15. };