UNPKG

@grouparoo/core

Version:
109 lines (108 loc) • 5.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudCLI = void 0; const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const actionhero_1 = require("actionhero"); const cli_1 = require("./cli"); const pluginDetails_1 = require("./pluginDetails"); const cloud_1 = require("./cloud"); const fs_extra_1 = require("fs-extra"); var CloudCLI; (function (CloudCLI) { async function pack(output) { const configDir = await (0, pluginDetails_1.getConfigDir)(true); const projectPath = (0, pluginDetails_1.getParentPath)(); const tarballPath = path_1.default.isAbsolute(output) ? output : path_1.default.join(projectPath, output); cli_1.GrouparooCLI.logger.log(`Project directory: ${projectPath}`); cli_1.GrouparooCLI.logger.log(`Config directory: ${configDir}`); const configArchive = await (0, cloud_1.packageConfig)(projectPath, configDir, tarballPath); cli_1.GrouparooCLI.logger.log(`✅ Saved config archive to ${configArchive}`); } CloudCLI.pack = pack; async function logJob(cloud, jobId) { if (jobId) { const job = await cloud.getJob(jobId); cli_1.GrouparooCLI.logger.log(cli_1.GrouparooCLI.logger.cyanBold(`\nLogging output for ${job.type} job (${jobId})\n`)); cli_1.GrouparooCLI.logger.log(job.logs); cli_1.GrouparooCLI.logger.log(""); } } async function push(params) { var _a, _b; if (!params.projectId) throw new Error(`projectId required`); if (!params.archivePath) throw new Error(`archivePath required`); if (params.apply === undefined) throw new Error(`apply required`); const projectPath = (0, pluginDetails_1.getParentPath)(); const tarballPath = path_1.default.isAbsolute(params.archivePath) ? params.archivePath : path_1.default.join(projectPath, params.archivePath); const cloudToken = (_a = params.token) !== null && _a !== void 0 ? _a : process.env.GROUPAROO_CLOUD_API_TOKEN; if (!cloudToken) cli_1.GrouparooCLI.logger.fatal("You must provide a Grouparoo Cloud token to upload configuration. Use the --token argument or set the GROUPAROO_CLOUD_TOKEN environment variable to provide the auth token."); try { const toApply = params.apply !== "false" && Boolean(params.apply); const cloud = new cloud_1.CloudClient(params.projectId, cloudToken); const { id: configId } = await cloud.createConfiguration(tarballPath, toApply, params.message, params.externalUrl); let lastState; while (true) { const config = await cloud.getConfiguration(configId); if (config.errorMessage) { await logJob(cloud, (_b = config.applyJobId) !== null && _b !== void 0 ? _b : config.validateJobId); cli_1.GrouparooCLI.logger.fatal(`An error occurred while processing the config: ${config.errorMessage}`); } if (config.state !== lastState) { if (config.state === "validated") { await logJob(cloud, config.validateJobId); } else if (config.state === "applied") { await logJob(cloud, config.applyJobId); } cli_1.GrouparooCLI.logger.log(`Configuration ${config.state}...`); } if (config.state === "finished") { cli_1.GrouparooCLI.logger.log(`\nThe configuration has been successfully ${toApply ? "applied" : "validated"}!`); break; } lastState = config.state; await actionhero_1.utils.sleep(1000); } } catch (err) { let prefix = ""; if (err instanceof cloud_1.CloudError) prefix = "Grouparoo Cloud error: "; cli_1.GrouparooCLI.logger.fatal(prefix + err.message); } } CloudCLI.push = push; async function packAndPush(params) { if (!params.projectId) throw new Error(`archivePath required`); if (params.apply === undefined) throw new Error(`apply required`); const tempPath = await (0, fs_extra_1.mkdtemp)(path_1.default.join(os_1.default.tmpdir(), "grouparoo-cli-")); const archivePath = path_1.default.join(tempPath, "grouparoo.tar.gz"); cli_1.GrouparooCLI.logger.log("\n🛠 Building config archive...\n"); await CloudCLI.pack(archivePath); cli_1.GrouparooCLI.logger.log("\n☁️ Uploading config...\n"); await CloudCLI.push({ archivePath, token: params.token, projectId: params.projectId, message: params.message, externalUrl: params.externalUrl, apply: params.apply, }); await (0, fs_extra_1.remove)(archivePath); } CloudCLI.packAndPush = packAndPush; })(CloudCLI = exports.CloudCLI || (exports.CloudCLI = {}));