2017-05-28 00:38:50 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
const path = require('path');
|
|
|
|
const Module = require('module');
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const resolveFrom = (fromDir, moduleId, silent) => {
|
|
|
|
if (typeof fromDir !== 'string') {
|
|
|
|
throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDir}\``);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof moduleId !== 'string') {
|
|
|
|
throw new TypeError(`Expected \`moduleId\` to be of type \`string\`, got \`${typeof moduleId}\``);
|
2017-05-28 00:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fromDir = path.resolve(fromDir);
|
2017-08-14 05:01:11 +02:00
|
|
|
const fromFile = path.join(fromDir, 'noop.js');
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const resolveFileName = () => Module._resolveFilename(moduleId, {
|
|
|
|
id: fromFile,
|
|
|
|
filename: fromFile,
|
|
|
|
paths: Module._nodeModulePaths(fromDir)
|
|
|
|
});
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if (silent) {
|
|
|
|
try {
|
|
|
|
return resolveFileName();
|
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-28 00:38:50 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
return resolveFileName();
|
2017-05-28 00:38:50 +02:00
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
module.exports = (fromDir, moduleId) => resolveFrom(fromDir, moduleId);
|
|
|
|
module.exports.silent = (fromDir, moduleId) => resolveFrom(fromDir, moduleId, true);
|