github-pages-deploy-action/node_modules/resolve-from/index.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-01-28 13:07:56 +08:00
'use strict';
const path = require('path');
const Module = require('module');
const fs = require('fs');
2020-03-07 11:45:40 +08:00
const resolveFrom = (fromDir, moduleId, silent) => {
if (typeof fromDir !== 'string') {
throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDir}\``);
2020-01-28 13:07:56 +08:00
}
if (typeof moduleId !== 'string') {
throw new TypeError(`Expected \`moduleId\` to be of type \`string\`, got \`${typeof moduleId}\``);
}
try {
2020-03-07 11:45:40 +08:00
fromDir = fs.realpathSync(fromDir);
} catch (err) {
if (err.code === 'ENOENT') {
fromDir = path.resolve(fromDir);
2020-01-28 13:07:56 +08:00
} else if (silent) {
2020-03-07 11:45:40 +08:00
return null;
2020-01-28 13:07:56 +08:00
} else {
2020-03-07 11:45:40 +08:00
throw err;
2020-01-28 13:07:56 +08:00
}
}
2020-03-07 11:45:40 +08:00
const fromFile = path.join(fromDir, 'noop.js');
2020-01-28 13:07:56 +08:00
const resolveFileName = () => Module._resolveFilename(moduleId, {
id: fromFile,
filename: fromFile,
2020-03-07 11:45:40 +08:00
paths: Module._nodeModulePaths(fromDir)
2020-01-28 13:07:56 +08:00
});
if (silent) {
try {
return resolveFileName();
2020-03-07 11:45:40 +08:00
} catch (err) {
return null;
2020-01-28 13:07:56 +08:00
}
}
return resolveFileName();
};
2020-03-07 11:45:40 +08:00
module.exports = (fromDir, moduleId) => resolveFrom(fromDir, moduleId);
module.exports.silent = (fromDir, moduleId) => resolveFrom(fromDir, moduleId, true);