@graphql-mesh/utils
Version:
27 lines (26 loc) • 1.43 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFromModuleExportExpression = loadFromModuleExportExpression;
/* eslint-disable @typescript-eslint/return-await */
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
const utils_1 = require("@graphql-tools/utils");
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
const defaultImportFn_js_1 = require("./defaultImportFn.js");
function loadFromModuleExportExpression(expression, options) {
if (typeof expression !== 'string') {
return (0, utils_1.fakePromise)(expression);
}
const { defaultExportName, cwd, importFn = defaultImportFn_js_1.defaultImportFn } = options || {};
const [modulePath, exportName = defaultExportName] = expression.split('#');
return (0, promise_helpers_1.handleMaybePromise)(() => tryImport(modulePath, cwd, importFn), mod => mod[exportName] || (mod.default && mod.default[exportName]) || mod.default || mod);
}
function tryImport(modulePath, cwd, importFn) {
return (0, promise_helpers_1.handleMaybePromise)(() => importFn(modulePath), m => m, () => {
if (!cross_helpers_1.path.isAbsolute(modulePath)) {
const absoluteModulePath = cross_helpers_1.path.isAbsolute(modulePath)
? modulePath
: cross_helpers_1.path.join(cwd, modulePath);
return importFn(absoluteModulePath);
}
});
}
;