UNPKG

@takeshape/vitest-docker-dynamodb

Version:

Bootstrap vitest tests with docker-compose and dynamodb.

78 lines (77 loc) 2.68 kB
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) { if (typeof path === "string" && /^\.\.?\//.test(path)) { return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) { return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js"); }); } return path; }; import fs from 'node:fs'; import { resolve } from 'node:path'; import { NAME } from "./constants.js"; import { isFunction, randomId } from "./utils.js"; let configCache; export function loadConfig(options) { const { dynamodb, config, configFile = 'docker-compose.yml', projectName = `${NAME}-${randomId()}`, cwd = process.cwd(), ...dockerComposeOptions } = options; if (dynamodb) { if (dynamodb.basePort === undefined) { dynamodb.basePort = 8000; } else if (!Number.isInteger(dynamodb.basePort) || dynamodb.basePort <= 0 || dynamodb.basePort > 65535) { throw new TypeError(`Option "dynamodb.basePort" must be an number between 1 and 65535.`); } } if (!config && !fs.existsSync(resolve(cwd, configFile))) { throw new TypeError(`Option "configFile" must be exist or "config" must be specified.`); } configCache = { dynamodb, config, configFile, projectName, cwd, ...dockerComposeOptions }; return configCache; } export function getConfig() { if (!configCache) { throw new TypeError(`Config not loaded`); } return configCache; } // Cache the tables result from the config function, so that we // are not calling it over and over let tablesCache; export async function getTables() { if (tablesCache) { return tablesCache; } const config = getConfig(); const { dynamodb, cwd } = config; if (!dynamodb?.tables) { tablesCache = []; return tablesCache; } let tables; if (typeof dynamodb.tables === 'string') { // a path to a file to load for table definitions const importedTables = await import(__rewriteRelativeImportExtension(resolve(cwd, dynamodb.tables))); tables = importedTables.default ?? importedTables; } else { tables = dynamodb.tables; } if (isFunction(tables)) { tablesCache = (await tables(config)); } else { tablesCache = tables ?? []; } if (!Array.isArray(tablesCache)) { throw new Error(`${NAME} requires that the tables configuration is an array`); } return tablesCache; }