[79a0317] | 1 | 'use strict';
|
---|
| 2 | const path = require('path');
|
---|
| 3 | const {fileURLToPath} = require('url');
|
---|
| 4 | const resolveCwd = require('resolve-cwd');
|
---|
| 5 | const pkgDir = require('pkg-dir');
|
---|
| 6 |
|
---|
| 7 | module.exports = filename => {
|
---|
| 8 | const normalizedFilename = filename.startsWith('file://') ? fileURLToPath(filename) : filename;
|
---|
| 9 | const globalDir = pkgDir.sync(path.dirname(normalizedFilename));
|
---|
| 10 | const relativePath = path.relative(globalDir, normalizedFilename);
|
---|
| 11 | const pkg = require(path.join(globalDir, 'package.json'));
|
---|
| 12 | const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
|
---|
| 13 | const localNodeModules = path.join(process.cwd(), 'node_modules');
|
---|
| 14 |
|
---|
| 15 | const filenameInLocalNodeModules = !path.relative(localNodeModules, normalizedFilename).startsWith('..') &&
|
---|
| 16 | // On Windows, if `localNodeModules` and `normalizedFilename` are on different partitions, `path.relative()` returns the value of `normalizedFilename`, resulting in `filenameInLocalNodeModules` incorrectly becoming `true`.
|
---|
| 17 | path.parse(localNodeModules).root === path.parse(normalizedFilename).root;
|
---|
| 18 |
|
---|
| 19 | // Use `path.relative()` to detect local package installation,
|
---|
| 20 | // because __filename's case is inconsistent on Windows
|
---|
| 21 | // Can use `===` when targeting Node.js 8
|
---|
| 22 | // See https://github.com/nodejs/node/issues/6624
|
---|
| 23 | return !filenameInLocalNodeModules && localFile && path.relative(localFile, normalizedFilename) !== '' && require(localFile);
|
---|
| 24 | };
|
---|