1 | 'use strict';
|
2 | const path = require('path');
|
3 | const resolveCwd = require('resolve-cwd');
|
4 | const pkgDir = require('pkg-dir');
|
5 |
|
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));
|
11 | const localNodeModules = path.join(process.cwd(), 'node_modules');
|
12 |
|
13 | const filenameInLocalNodeModules = !path.relative(localNodeModules, filename).startsWith('..') &&
|
14 |
|
15 | path.parse(localNodeModules).root === path.parse(filename).root;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | return !filenameInLocalNodeModules && localFile && path.relative(localFile, filename) !== '' && require(localFile);
|
22 | };
|