UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

59 lines (58 loc) 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const https = tslib_1.__importStar(require("https")); const url_1 = require("url"); const properties = tslib_1.__importStar(require("../../properties.json")); const global_settings_1 = require("../../safira-globals/global-settings"); const safira_license_1 = require("../../safira-globals/safira-license"); const environment_utils_1 = require("../../utils/environment-utils"); const string_utils_1 = require("../../utils/string-utils"); const hook = async function (opts) { const environment = environment_utils_1.EnvironmentUtils.environment; if (opts.argv.includes("--help") || !properties.metrics[environment].enabled || properties.metrics["exclude-commands"].includes(opts?.id || "")) this.exit(); const license = safira_license_1.SafiraLicense.instance.getLicense(); if (!license?.statics) { this.exit(); } new Promise((resolve, reject) => { const url = new url_1.URL(`${properties.metrics[environment].url}/v1/metrics`); const data = { "client-id": global_settings_1.SafiraGlobalsSettings.instance.getInstallationId(), command: opts?.id || "unknown", "license-id": license?.email ?? "", }; const requestBody = JSON.stringify(data); const headers = { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(requestBody), "User-Agent": string_utils_1.StringUtils.getUserAgent(), }; if (properties?.metrics[environment]?.["api-key"]) headers["x-api-key"] = properties.metrics[environment]["api-key"]; const options = { hostname: url.hostname, port: 443, path: url.pathname, method: "POST", headers: headers, }; const req = https.request(options, res => { res.setEncoding("utf8"); let body = ""; res.on("data", d => { body += d; }); res.on("end", () => { resolve(body); }); }); req.on("error", error => { reject(error); }); req.write(requestBody); req.end(); }).then(() => { }); }; exports.default = hook;