source: imaps-frontend/node_modules/import-local/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use strict';
2const path = require('path');
3const {fileURLToPath} = require('url');
4const resolveCwd = require('resolve-cwd');
5const pkgDir = require('pkg-dir');
6
7module.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};
Note: See TracBrowser for help on using the repository browser.