UNPKG

ci-executer

Version:
132 lines (131 loc) 5.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Job = void 0; const chalk = require("chalk"); const ci = require("ci-info"); const os_1 = require("os"); const child_process_1 = require("child_process"); __exportStar(require("./interfaces"), exports); class Job { constructor(scope, _config, _logger) { this._config = _config; this._logger = _logger; this._fail_flag = false; this._os_type = "linux"; if (os_1.EOL === "\r\n") { this._os_type = "win"; } if (this._os_type === "win" && this._config.when.os === "linux") { this._config.enable = false; } if (this._os_type === "linux" && this._config.when.os === "win") { this._config.enable = false; } if (ci.isCI === true && this._config.when.environment === "local") { this._config.enable = false; } if (ci.isCI === false && this._config.when.environment === "ci") { this._config.enable = false; } for (const item of this._config.when.env) { const key = item.split("=", 1)[0].replace(/^\^/i, ""); const value = item.replace(`${key}`, "").replace("=", "").replace(/^\^/i, ""); const invert_flag = /^\^/i.test(item); if (invert_flag === true) { if (process.env[key] !== undefined) { if (value === undefined || value === "") { this._config.enable = false; break; } if (process.env[key] === value) { this._config.enable = false; break; } } } else { if (process.env[key] === undefined) { this._config.enable = false; break; } if (value !== undefined && value !== "") { if (process.env[key] !== value) { this._config.enable = false; } } } } if (this._config.scope !== scope) { this._config.enable = false; } if (this._config.enable === false) { this._logger.log(`${chalk.cyan("🚧")} Skip job ${chalk.gray.bold(this._config.name)}`); } } run(current_number_job, total_jobs) { return new Promise((resolve) => { this._logger.log(`🏃 Job ${chalk.gray.bold(this._config.name)} started (${current_number_job - 1}+/${total_jobs})`); if (this._config.description !== undefined && this._config.description !== "") { this._logger.log(`${chalk.cyan(" ℹ ")}> ${this._config.description}`); } const env = {}; for (const key in process.env) { env[key] = process.env[key]; } for (const key in this._config.env) { env[key] = this._config.env[key]; } this._logger.log(`Exec: ${this._config.exec}`, "debug"); const options = { env: env, cwd: process.cwd(), stdio: ["ignore", "ignore", "ignore"], shell: true }; if (this._config.logs === true) { options.stdio = ["inherit", "inherit", "inherit"]; } const app = child_process_1.spawn(this._config.exec, [], options); app.on("close", (code) => { if (code > 0) { this._fail_flag = true; this._logger.error(`Job ${chalk.gray.bold(this._config.name)} closed with code ${code}`); this._logger.log(`${chalk.red("❌")} Job ${chalk.gray.bold(this._config.name)} failed (${current_number_job}/${total_jobs})`); } else { this._logger.log(`${chalk.green("✔")} Job ${chalk.gray.bold(this._config.name)} complete (${current_number_job}/${total_jobs})`); } resolve(); }); app.on("error", (error) => { this._logger.error(`Job ${chalk.gray.bold(this._config.name)} error: ${error.message}`); this._logger.log(error, "debug"); this._fail_flag = true; resolve(); }); }); } get enable() { return this._config.enable; } get fail() { if (this._config.allow_failure === true) { return false; } return this._fail_flag; } get name() { return this._config.name; } } exports.Job = Job;