UNPKG

@metacall/deploy

Version:

Tool for deploying into MetaCall FaaS platform.

227 lines (226 loc) 9.4 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.inspect = void 0; const deployment_1 = require("@metacall/protocol/deployment"); const language_1 = require("@metacall/protocol/language"); const chalk_1 = __importDefault(require("chalk")); const console_table_printer_1 = require("console-table-printer"); const utils_1 = require("./../utils"); const args_1 = __importStar(require("./args")); const messages_1 = require("./messages"); const colorStatus = (status) => { switch (status) { case 'create': status = chalk_1.default.yellowBright(status); break; case 'ready': status = chalk_1.default.greenBright(status); break; default: status = chalk_1.default.redBright(status); } return status; }; const genRow = (Deployments, Status, Version, Ports, Endpoints) => { return { Deployments, Status, Version, Ports, Endpoints }; }; const getFunctionInvokeMethod = (packageType, asyncness) => packageType === 'file' ? 'static' : asyncness ? 'await' : 'call'; const genSingleURL = (packageType, apiURL, app, f) => { const prefix = app.prefix; const suffix = app.suffix; const version = app.version; const funcName = f.name; const funcType = getFunctionInvokeMethod(packageType, f.async); return `${apiURL}/${prefix}/${suffix}/${version}/${funcType}/${funcName}`; }; const genAllURL = (res, apiURL) => { const urls = {}; const languageSupported = Object.keys(language_1.Languages); res.forEach(el => { urls[el.suffix] = []; Object.entries(el.packages).forEach(pack => languageSupported.includes(pack[0]) && pack[1].forEach(ele => ele.scope.funcs.forEach(f => urls[el.suffix].push(genSingleURL(pack[0], apiURL, el, f))))); }); return urls; }; const functionTypeIdToOpenAPIv3 = (type) => { const conversionMap = { [deployment_1.ValueId.METACALL_BOOL]: 'boolean', [deployment_1.ValueId.METACALL_CHAR]: 'string', [deployment_1.ValueId.METACALL_SHORT]: 'number', [deployment_1.ValueId.METACALL_INT]: 'number', [deployment_1.ValueId.METACALL_LONG]: 'number', [deployment_1.ValueId.METACALL_FLOAT]: 'number', [deployment_1.ValueId.METACALL_DOUBLE]: 'number', [deployment_1.ValueId.METACALL_STRING]: 'string', [deployment_1.ValueId.METACALL_BUFFER]: 'string', [deployment_1.ValueId.METACALL_MAP]: 'object', [deployment_1.ValueId.METACALL_OBJECT]: 'object' }; if (type === deployment_1.ValueId.METACALL_ARRAY) { return { type: 'array', items: {} }; } return { type: conversionMap[type] || undefined }; }; const rawInspectToOpenAPIv3 = (baseURL, deployments) => { return deployments.map(deployment => { const paths = {}; Object.keys(deployment.packages).forEach(language => { const handles = deployment.packages[language]; const funcs = handles .map(handle => { return handle.scope.funcs; }) .flatMap(func => func); funcs.forEach(func => { const method = func.signature.args.length === 0 ? 'get' : 'post'; const invokeMethod = getFunctionInvokeMethod(language, func.async); const properties = {}; if (method === 'post') { func.signature.args.forEach(prop => { properties[prop.name] = functionTypeIdToOpenAPIv3(prop.type.id); }); } paths[`/${invokeMethod}/${func.name}`] = { [method]: { summary: '', description: '', requestBody: method === 'post' ? { description: '', required: true, content: { 'application/json': { schema: { type: 'object', properties } } } } : undefined, responses: { '200': { description: '', content: { 'application/json': { schema: functionTypeIdToOpenAPIv3(func.signature.ret.type.id) } } } } } }; }); }); return { openapi: '3.0.0', info: { title: `MetaCall Cloud FaaS deployment '${deployment.suffix}'`, description: '', version: deployment.version }, servers: [ { url: `${baseURL}/${deployment.prefix}/${deployment.suffix}/${deployment.version}`, description: 'MetaCall Cloud FaaS' } ], paths }; }); }; const inspectPrint = { [args_1.InspectFormat.Table]: async (config, api) => { for (;;) { const res = await api.inspect(); console.clear(); const p = new console_table_printer_1.Table({ columns: [ { name: 'Deployments', alignment: 'left' }, { name: 'Status', alignment: 'left' }, { name: 'Version', alignment: 'center' }, { name: 'Ports', alignment: 'center' }, { name: 'Endpoints', alignment: 'left' } ] }); const urls = genAllURL(res, args_1.default['dev'] ? config.devURL : config.apiURL); const allApps = res.map(el => { const suffix = el.suffix; const status = colorStatus(el.status); const version = el.version; const ports = el.ports.length > 0 ? el.ports : '---'; const url = urls[el.suffix].length > 0 ? urls[el.suffix][0] : ' ---'; return genRow(suffix, status, version, ports, url); }); allApps.forEach((app, i) => { p.addRow(app); const appUrls = typeof urls[app.Deployments] !== 'undefined' ? urls[app.Deployments] : []; if (appUrls.length > 0) appUrls .slice(1) .forEach(el => p.addRow(genRow('', '', '', [], el))); if (i < allApps.length - 1) p.addRow(genRow('', '', '', [], '')); }); p.printTable(); await (0, utils_1.sleep)(5000); } }, [args_1.InspectFormat.Raw]: async (_config, api) => { const res = await api.inspect(); console.log(JSON.stringify(res, null, 2)); }, [args_1.InspectFormat.OpenAPIv3]: async (config, api) => { const res = await api.inspect(); console.log(JSON.stringify(rawInspectToOpenAPIv3(args_1.default['dev'] ? config.devURL : config.apiURL, res), null, 2)); }, [args_1.InspectFormat.Invalid]: async ( // eslint-disable-next-line @typescript-eslint/no-unused-vars _config, // eslint-disable-next-line @typescript-eslint/no-unused-vars _api) => { const values = Object.values(args_1.InspectFormat) .filter(x => typeof x === 'string' && x !== 'Invalid') .join(', '); (0, messages_1.error)(`Invalid format passed to inspect, valid formats are: ${values}`); await (0, utils_1.sleep)(100); } }; const inspect = async (format, config, api) => { await inspectPrint[format](config, api); }; exports.inspect = inspect;