index.js 712 B

123456789101112131415161718
  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. };