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