UNPKG

@gluestack-v2/glue-plugin-service-gateway

Version:

Gluestack V2 service Gateway Plugin

134 lines (133 loc) 7.34 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "path", "@gluestack-v2/framework-cli/build/helpers/file/write-file", "@gluestack-v2/framework-cli/build/helpers/file/read-file", "./cron-service-template", "./cron-service-action-template", "./cron-service-webhook-template"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const write_file_1 = __importDefault(require("@gluestack-v2/framework-cli/build/helpers/file/write-file")); const read_file_1 = __importDefault(require("@gluestack-v2/framework-cli/build/helpers/file/read-file")); const cron_service_template_1 = __importDefault(require("./cron-service-template")); const cron_service_action_template_1 = __importDefault(require("./cron-service-action-template")); const cron_service_webhook_template_1 = __importDefault(require("./cron-service-webhook-template")); // function filePathExtension(filePath: string) { // return filePath.split(".").pop() ?? ""; // } function getCamelCaseName(name) { // clean up the name if (name.split('/')[0] === '') { let nameArr = name.split('/'); nameArr.splice(0, 1); name = nameArr.join('/'); } return name .split('/') .map((word, ind) => { var _a, _b; return (ind !== 0 ? (_a = word[0]) === null || _a === void 0 ? void 0 : _a.toUpperCase() : (_b = word[0]) === null || _b === void 0 ? void 0 : _b.toLowerCase()) + word.slice(1); }) .join(''); } // Usage: Pass the directory path as an argument to the function const writeCronService = (installationPath, generatedServiceGatewayPath, instanceName) => __awaiter(void 0, void 0, void 0, function* () { const moleculerCronServiceTemplate = (0, cron_service_template_1.default)(); const moleculerCronWebhookServiceTemplate = cron_service_webhook_template_1.default.http(); const moleculerCronInternalServiceTemplate = cron_service_webhook_template_1.default.internal(); const cronPath = installationPath; // const moleculerFunctionsPath = path.join(installationPath, "functions"); const moleculerCronServiceTemplatePath = path_1.default.join(generatedServiceGatewayPath, 'services', `${instanceName}.service.js`); // console.log("> Writing cron service", moleculerCronServiceTemplatePath); let crons = yield (0, read_file_1.default)(path_1.default.join(cronPath, 'index.json')); crons = JSON.parse(crons); let cronsObj = ''; let actionsObj = ''; crons.forEach((cron, _index) => { let { name, schedule, path } = cron, restProps = __rest(cron, ["name", "schedule", "path"]); let method = 'post'; if (restProps.method) { method = restProps.method; } let data = {}; if (restProps.data) { data = restProps.data; } let cronObj = ''; if (path.startsWith('http')) { cronObj = moleculerCronWebhookServiceTemplate .replace('// **--- Cron Name ---**', `"${name}"`) .replace('// **--- Cron Name ---**', `${name}`) .replace('// **--- Cron Schedule ---**', `"${schedule}"`) .replace('// **--- Webhook Method ---**', `"${method}"`) .replace('// **--- Webhook URL ---**', `"${path}"`) .replace('// **--- Webhook Data ---**', JSON.stringify(data)); cronsObj += cronObj; } else { let internalServiceMethodName = getCamelCaseName(path) + 'Action'; let actionTemplate = (0, cron_service_action_template_1.default)(); // clean up the path if (path.split('/')[0] === '') { let nameArr = path.split('/'); nameArr.splice(0, 1); path = nameArr.join('/'); } let serviceName = path.split('/').join('.'); cronObj = moleculerCronInternalServiceTemplate .replace('// **--- Cron Name ---**', `"${name}"`) .replace('// **--- Cron Name ---**', `${name}`) .replace('// **--- Cron Schedule ---**', `"${schedule}"`) .replace('**--- Action CallMethod ---**', `${internalServiceMethodName}`) .replace('**--- Cron Data ---**', `${JSON.stringify(data)}`); let actionObj = actionTemplate .replace('**--- Action CallMethod ---**', `${internalServiceMethodName}`) .replace('**--- Action Data ---**', `${JSON.stringify(data)}`) .replace('**--- Service Method ---**', `${serviceName}`); cronsObj += cronObj; actionsObj += actionObj; } }); let finalString = moleculerCronServiceTemplate.replace('// **---Add Cron Jobs Here---**', cronsObj); finalString = finalString.replace('// **---Add Cron Actions Here---**', actionsObj); // Create functions service with all the actions and imports (0, write_file_1.default)(moleculerCronServiceTemplatePath, finalString); // Create SDK index file with all the functions // createFileWithPath( // sdkSrcIndex, // sdkIndexTemplate.replace( // "// **---Functions will be added after this---**", // sdkFunctions // ) // ); // } }); exports.default = writeCronService; });