UNPKG

@grouparoo/core

Version:
172 lines (171 loc) 7.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNodeVersion = exports.getCoreVersion = exports.runningCoreDirectly = exports.getPluginManifest = exports.getCoreRootPath = exports.getConfigDir = exports.getParentPath = exports.readPackageJson = exports.grouparooMonorepoApp = void 0; const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const tar_1 = __importDefault(require("tar")); const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch")); let initialPackageJSON = {}; if (process.env.INIT_CWD) { initialPackageJSON = readPackageJson(path_1.default.join(process.env.INIT_CWD, "package.json")); } else if (process.env.JEST_WORKER_ID && process.env.PWD) { initialPackageJSON = readPackageJson(path_1.default.join(process.env.PWD, "package.json")); } exports.grouparooMonorepoApp = initialPackageJSON.grouparoo ? initialPackageJSON.grouparoo.grouparoo_monorepo_app : null; function readPackageJson(path) { if (!fs_1.default.existsSync(path)) return {}; return JSON.parse(fs_1.default.readFileSync(path).toString()); } exports.readPackageJson = readPackageJson; function getParentPath() { if (process.env.GROUPAROO_PARENT_PATH) { if (path_1.default.isAbsolute(process.env.GROUPAROO_PARENT_PATH)) { return process.env.GROUPAROO_PARENT_PATH; } else { return fs_1.default.realpathSync(path_1.default.join(process.cwd(), process.env.GROUPAROO_PARENT_PATH)); } } if (exports.grouparooMonorepoApp) { return path_1.default.join(__dirname, "..", "..", "..", "apps", exports.grouparooMonorepoApp); } if (runningCoreDirectly()) return getCoreRootPath(); return path_1.default.join(__dirname, "..", "..", "..", "..", ".."); } exports.getParentPath = getParentPath; let extractedConfigDir; async function getConfigDir(throwIfDisabled) { if (process.env.GROUPAROO_CONFIG_DIR === "false") { if (throwIfDisabled) throw new Error("The config directory has been disabled. Make sure that the `GROUPAROO_CONFIG_DIR` environment variable is not set to `false`."); return false; } let configDir = process.env.GROUPAROO_CONFIG_DIR || path_1.default.join(getParentPath(), "config"); if (process.env.GROUPAROO_CONFIG_ARCHIVE) { if (!extractedConfigDir) { const workingDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "grouparoo-")); let archivePath = process.env.GROUPAROO_CONFIG_ARCHIVE; if (archivePath.startsWith("http://") || archivePath.startsWith("https://")) { const res = await (0, isomorphic_fetch_1.default)(archivePath); archivePath = path_1.default.join(workingDir, "grouparoo.tar.gz"); const buffer = await res.arrayBuffer(); fs_1.default.writeFileSync(archivePath, Buffer.from(buffer)); } extractedConfigDir = path_1.default.join(workingDir, "extracted"); fs_1.default.mkdirSync(extractedConfigDir); await tar_1.default.extract({ cwd: extractedConfigDir, file: archivePath }); } configDir = path_1.default.join(extractedConfigDir, process.env.GROUPAROO_CONFIG_DIR || "config"); } return configDir; } exports.getConfigDir = getConfigDir; function getCoreRootPath() { return fs_1.default.realpathSync(path_1.default.join(__dirname, "..", "..")); } exports.getCoreRootPath = getCoreRootPath; function getPluginManifest() { var _a, _b; const manifest = { parent: { path: null, grouparoo: { plugins: [] }, }, plugins: [], missingPlugins: [], }; // parent const parentPath = getParentPath(); const parentPkg = readPackageJson(path_1.default.join(parentPath, "package.json")); manifest.parent = { grouparoo: parentPkg.grouparoo, path: parentPath }; // plugins let pluginNames = [...(((_b = (_a = manifest === null || manifest === void 0 ? void 0 : manifest.parent) === null || _a === void 0 ? void 0 : _a.grouparoo) === null || _b === void 0 ? void 0 : _b.plugins) || [])]; const availableUiPlugins = [ "@grouparoo/ui-enterprise", "@grouparoo/ui-community", "@grouparoo/ui-config", ]; const installedPackages = Object.keys((parentPkg === null || parentPkg === void 0 ? void 0 : parentPkg.devDependencies) || []).concat(Object.keys((parentPkg === null || parentPkg === void 0 ? void 0 : parentPkg.dependencies) || [])); for (let availableUiPlugin of availableUiPlugins) { if (installedPackages.includes(availableUiPlugin)) { pluginNames.push(availableUiPlugin); } } pluginNames = Array.from(new Set(pluginNames)); for (const pluginName of pluginNames) { if (pluginName === "@grouparoo/core") continue; let pluginPath = ""; try { pluginPath = require.resolve(pluginName); // require("@grouparoo/mysql") } catch { pluginPath = path_1.default.join(parentPath, "node_modules", pluginName); // require("../../../staging-enterprise/@grouparoo/mysql") if (!fs_1.default.existsSync(pluginPath)) { pluginPath = path_1.default.join(exports.grouparooMonorepoApp ? path_1.default.join(__dirname, "..", "..", "..", "apps", exports.grouparooMonorepoApp) : path_1.default.join(__dirname, "..", "..", "..", "..", "..", ".."), "node_modules", pluginName); } } if (!fs_1.default.existsSync(pluginPath)) { if (!manifest.missingPlugins.includes(pluginName)) { manifest.missingPlugins.push(pluginName); } continue; } pluginPath = fs_1.default.realpathSync(pluginPath); const pluginPkg = readPackageJson(path_1.default.join(pluginPath, "package.json")); if (pluginPkg.name) { manifest.plugins.push({ name: pluginPkg.name, version: pluginPkg.version, license: pluginPkg.license, url: pluginPkg.repository ? typeof pluginPkg.repository === "string" ? pluginPkg.repository || null : pluginPkg.repository.url : null || pluginPkg.homepage, path: pluginPath, grouparoo: pluginPkg.grouparoo || null, }); } } manifest.plugins.sort((a, b) => { if (a.name > b.name) return 1; if (a.name < b.name) return -1; }); return manifest; } exports.getPluginManifest = getPluginManifest; function runningCoreDirectly() { const monorepoPackageJSON = path_1.default.join(__dirname, "..", "..", "..", "package.json"); if (fs_1.default.existsSync(monorepoPackageJSON)) { const pkgPkg = readPackageJson(monorepoPackageJSON); if (pkgPkg.name === "@grouparoo/grouparoo") { return true; } } return false; } exports.runningCoreDirectly = runningCoreDirectly; function getCoreVersion() { const corePkgJson = readPackageJson(path_1.default.join(__dirname, "..", "..", "package.json")); return corePkgJson.version; } exports.getCoreVersion = getCoreVersion; function getNodeVersion() { return process.version; } exports.getNodeVersion = getNodeVersion;