UNPKG

@shipengine/connect-loader

Version:

Internal library for loading ShipEngine Connect apps

105 lines 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readDefinition = exports.readDefinitions = exports.readDefinitionValue = void 0; const connect_sdk_1 = require("@shipengine/connect-sdk"); const internal_1 = require("@shipengine/connect-sdk/lib/internal"); const path = require("path"); const resolveFrom = require("resolve-from"); const file_cache_1 = require("./file-cache"); const read_file_1 = require("./read-file"); /** * Reads an ShipEngine Connect definition that is expected to be a single value. * The definition can be any of: * * - an inline value * - a YAML file path * - a JSON file path * - a JavaScript file path * - a dynamic import via `require()` or `import()` */ async function readDefinitionValue(definition, cwd, fieldName) { const [value] = await readDefinition(definition, cwd, fieldName); return value; } exports.readDefinitionValue = readDefinitionValue; /** * Reads a ShipEngine Connect definition that is expected to be an array of values. * The definition can be any of: * * - an inline value * - a YAML file path * - a JSON file path * - a JavaScript file path * - a dynamic import via `require()` or `import()` * * @returns A tuple containing the definition value and the directory path of the definition file */ function readDefinitions(definition, cwd, fieldName) { return readDefinition(definition, cwd, fieldName); } exports.readDefinitions = readDefinitions; /** * Reads a ShipEngine Connect definition that is expected to be a single value. * The definition can be any of: * * - an inline value * - a YAML file path * - a JSON file path * - a JavaScript file path * - a dynamic import via `require()` or `import()` * * @returns A tuple containing the definition value and the directory path of the definition file */ async function readDefinition(definition, cwd, fieldName) { try { if (typeof definition === 'string') { // The definition value is a file path, so return the file's contents const filePath = resolve(definition, cwd); const dir = path.dirname(filePath); let contents; // Get the file from the cache, if possible const cached = file_cache_1.fileCache.get(filePath); if (cached) { contents = await cached; } else { // The file isn't cached, so read it and cache it contents = await file_cache_1.fileCache.add(filePath, read_file_1.readFile(filePath)); } return [contents, dir]; } else if (isDynamicImport(definition)) { // The definition value is a dynamic import, so return the default export const exports = await definition; return [exports.default, cwd]; } else { // The definition value was defined inline, so just return it as-is return [definition, cwd]; } } catch (originalError) { throw internal_1.error(connect_sdk_1.ErrorCode.Invalid, `Invalid ${fieldName}: ${definition}.`, { originalError, }); } } exports.readDefinition = readDefinition; /** * Resolves a Node.js Module ID or file path */ function resolve(moduleId, cwd) { if (!moduleId.startsWith('.') && !path.isAbsolute(moduleId)) { // Relative paths must start with a "./" moduleId = './' + moduleId; } return resolveFrom(cwd, moduleId); } /** * Determines whether the given value is a dynamically imported JavaScript module */ function isDynamicImport(value) { const dynamicImport = value; return !!(value && typeof dynamicImport.then === 'function'); } //# sourceMappingURL=read-definition.js.map