UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

86 lines (85 loc) 4.58 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.seedSystemRoutes = void 0; const express_list_endpoints_1 = __importDefault(require("express-list-endpoints")); const lodash_1 = require("lodash"); const server_1 = require("../server"); const seedSystemRoutes = async () => { const { DB } = await Promise.resolve().then(() => __importStar(require("../modules/api/DB"))); // get routes from the database let dbRoutes = await DB.find("route", {}); // console.log("dbRoutes >>", dbRoutes.length); // get all routes of Express const expressRoutes = (0, express_list_endpoints_1.default)(server_1.app).filter((r) => r.path.indexOf("/empty") === -1 && r.path.indexOf("/auth") === -1); // console.log("expressRoutes >>", expressRoutes); // if (expressRoutes.length > 0) return; // compare database's routes with all routes of Express const missingRoutes = expressRoutes .filter((exr) => typeof dbRoutes.find((route) => route.path === exr.path) === "undefined") .map((exr) => { const generatedName = (0, lodash_1.upperFirst)(exr.path.replace("/api/v1/", "").split("/").join(" ").split("-").join(" ").split("_").join("-")); return { name: generatedName, path: exr.path, methods: exr.methods }; }); // console.log("missingRoutes >>", missingRoutes); if (!(0, lodash_1.isEmpty)(missingRoutes)) { // log(`[MIGRATION] migrateAllRoutes > Found ${missingRoutes.length} missing routes.`); const results = []; for (const route of missingRoutes) { const item = await DB.create("route", route).catch((e) => undefined); if (typeof item !== "undefined") results.push(item); } // const results = (await Promise.all(missingRoutes.map(async (route) => DB.create("route", route)))).filter( // (item) => typeof item !== "undefined" // ); // log(`[MIGRATION] migrateAllRoutes > FINISH MIGRATION >> Created ${results.length} missing routes.`); } // compare methods of database routes with methods of Express routes dbRoutes = await DB.find("route", {}); // <-- fetch database routes again to get latest ones const updateRoutes = expressRoutes .filter((exr) => { const _route = dbRoutes.find((route) => route.path === exr.path); if (!_route) return false; const routeMethods = _route.methods.join(",").toUpperCase(); const expRouteMethods = exr.methods.join(",").toUpperCase(); if (routeMethods !== expRouteMethods) // log(`[MIGRATION] migrateAllRoutes > Update "${_route.path}" route's methods from [${routeMethods}] to [${expRouteMethods}]`); return routeMethods !== expRouteMethods; }) .map((exr) => { return { path: exr.path, methods: exr.methods }; }); if (!(0, lodash_1.isEmpty)(updateRoutes)) { // log(`[MIGRATION] migrateAllRoutes > Found ${updateRoutes.length} routes that need to update methods.`); const results = (await Promise.all(updateRoutes.map(async (updateData) => DB.update("route", { path: updateData.path }, updateData)))).filter((items) => typeof items !== "undefined" && !(0, lodash_1.isEmpty)(items)); // log(`[MIGRATION] migrateAllRoutes > Update methods of ${results.length} routes.`); } }; exports.seedSystemRoutes = seedSystemRoutes;