UNPKG

nats-memory-server

Version:
93 lines 3.89 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readFile = readFile; exports.getProjectConfig = getProjectConfig; const path_1 = __importStar(require("path")); const fs_1 = __importDefault(require("fs")); const configKey = `natsMemoryServer`; const configFileBaseName = `nats-memory-server`; const allowedExtensions = [`.ts`, `.js`, `.json`]; const readFileMap = { '.ts': (filePath) => { // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/quotes const tsNode = require('ts-node'); if (tsNode == null) { throw new Error(`ts-node is not installed`); } tsNode.register(); return require(filePath); }, '.js': (filePath) => require(filePath), '.json': (filePath) => JSON.parse(fs_1.default.readFileSync(filePath, `utf8`)), // eslint-disable-next-line @typescript-eslint/no-explicit-any }; // eslint-disable-next-line @typescript-eslint/no-explicit-any function readFile(filePath) { const ext = path_1.default.extname(filePath).toLowerCase(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error return readFileMap[ext](filePath); } const defaultConfig = { download: true, downloadDir: `node_modules/.cache/nats-memory-server`, version: `v2.9.16`, buildFromSource: false, binPath: `node_modules/.cache/nats-memory-server/nats-server`, }; function prepareConfig(config, projectPath) { let downloadDir = config.downloadDir; let binPath = config.binPath; if (!(0, path_1.isAbsolute)(config.downloadDir)) { downloadDir = path_1.default.resolve(projectPath, config.downloadDir); } if (!(0, path_1.isAbsolute)(config.binPath)) { binPath = path_1.default.resolve(projectPath, config.binPath); } return Object.assign(Object.assign({}, config), { downloadDir, binPath }); } function getProjectConfig(projectPath) { const projectConfigPath = allowedExtensions .map((ext) => path_1.default.resolve(projectPath, `./${configFileBaseName}${ext}`)) .find((filePath) => fs_1.default.existsSync(filePath)); const packageJsonPath = path_1.default.resolve(projectPath, `./package.json`); let config; if (projectConfigPath !== undefined) { config = Object.assign(Object.assign({}, defaultConfig), readFile(projectConfigPath)); } else if (fs_1.default.existsSync(packageJsonPath)) { config = Object.assign(Object.assign({}, defaultConfig), readFile(packageJsonPath)[configKey]); } else { config = defaultConfig; } return prepareConfig(config, projectPath); } //# sourceMappingURL=get-project-config.js.map