Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 | const path = require('path');
|
---|
| 3 | const Module = require('module');
|
---|
| 4 | const fs = require('fs');
|
---|
| 5 |
|
---|
| 6 | const resolveFrom = (fromDir, moduleId, silent) => {
|
---|
| 7 | if (typeof fromDir !== 'string') {
|
---|
| 8 | throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDir}\``);
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | if (typeof moduleId !== 'string') {
|
---|
| 12 | throw new TypeError(`Expected \`moduleId\` to be of type \`string\`, got \`${typeof moduleId}\``);
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | try {
|
---|
| 16 | fromDir = fs.realpathSync(fromDir);
|
---|
| 17 | } catch (err) {
|
---|
| 18 | if (err.code === 'ENOENT') {
|
---|
| 19 | fromDir = path.resolve(fromDir);
|
---|
| 20 | } else if (silent) {
|
---|
| 21 | return null;
|
---|
| 22 | } else {
|
---|
| 23 | throw err;
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | const fromFile = path.join(fromDir, 'noop.js');
|
---|
| 28 |
|
---|
| 29 | const resolveFileName = () => Module._resolveFilename(moduleId, {
|
---|
| 30 | id: fromFile,
|
---|
| 31 | filename: fromFile,
|
---|
| 32 | paths: Module._nodeModulePaths(fromDir)
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | if (silent) {
|
---|
| 36 | try {
|
---|
| 37 | return resolveFileName();
|
---|
| 38 | } catch (err) {
|
---|
| 39 | return null;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return resolveFileName();
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | module.exports = (fromDir, moduleId) => resolveFrom(fromDir, moduleId);
|
---|
| 47 | module.exports.silent = (fromDir, moduleId) => resolveFrom(fromDir, moduleId, true);
|
---|
Note:
See
TracBrowser
for help on using the repository browser.