js-slang
Version:
Javascript-based implementations of Source, written in Typescript
43 lines • 1.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultResolutionOptions = void 0;
const path_1 = require("path");
const loader_1 = require("../loader");
const utils_1 = require("../utils");
exports.defaultResolutionOptions = {
extensions: ['js']
};
/**
* Resolve a relative module path to an absolute path.
*/
async function resolveFile(fromPath, toPath, fileGetter, options = exports.defaultResolutionOptions) {
if ((0, utils_1.isSourceModule)(toPath)) {
const manifest = await (0, loader_1.memoizedGetModuleManifestAsync)();
return toPath in manifest ? { type: 'source' } : undefined;
}
const absPath = path_1.posix.resolve(fromPath, '..', toPath);
let contents = await fileGetter(absPath);
if (contents !== undefined) {
return {
type: 'local',
absPath: absPath,
contents
};
}
if (options.extensions) {
for (const ext of options.extensions) {
const extPath = `${absPath}.${ext}`;
contents = await fileGetter(extPath);
if (contents !== undefined) {
return {
type: 'local',
absPath: extPath,
contents
};
}
}
}
return undefined;
}
exports.default = resolveFile;
//# sourceMappingURL=resolver.js.map
;