"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Robot_1 = __importDefault(require("./Robot")); const http_1 = __importDefault(require("http")); const os_1 = __importDefault(require("os")); class BuildWait { constructor(key, port, jobName, waitTime = 20, extendTime = 60) { this.server = null; this.timer = null; this.time = 0; this.isSendedReadyMessage = false; this.key = key; this.port = port; this.jobName = jobName; this.waitTime = waitTime; this.extendTime = extendTime; this.ip = this.getIP(); } getIP() { const networkInterfaces = os_1.default.networkInterfaces(); for (var key in networkInterfaces) { const networkInterface = networkInterfaces[key]; if (!networkInterface) continue; for (const info of networkInterface) { if (info.family === 'IPv4' && info.address !== '127.0.0.1' && !info.internal) { return info.address; } } } return null; } start() { if (this.server) return; if (!this.ip) { console.log('BuildWait IP Error'); return; } console.log('BuildWait Start'); this.time = this.waitTime; this.server = http_1.default.createServer(this.requestListener.bind(this)); this.server.listen(this.port); this.timer = setInterval(this.onTimer.bind(this), 1000); } requestListener(req, res) { const url = req.url; console.log(`BuildWait Receive: ${url}`); switch (url) { case '/extend': //延迟 this.time = this.extendTime; this.isSendedReadyMessage = false; Robot_1.default.sendMessage(this.key, 'text', `${this.jobName}构建推迟到${this.extendTime}秒后`); res.end(''); break; case '/immediately': //立即 Robot_1.default.sendMessage(this.key, 'text', `${this.jobName}立即构建`); this.time = 0; res.end(''); break; case '/': default: res.end('Commond Error: ' + url); break; } } onTimer() { console.log('BuildWait OnTime: ' + this.time); if (this.time <= 0) { this.stopWait(); } else if (!this.isSendedReadyMessage && this.time <= this.waitTime) { this.isSendedReadyMessage = true; this.sendReadyToBuildMessage(); } this.time--; } sendReadyToBuildMessage() { let message = `${this.jobName}即将在${this.waitTime}秒后开始构建\n[立即构建](http://${this.ip}:${this.port}/immediately) | [推迟构建](http://${this.ip}:${this.port}/extend) | [中断构建](http://${this.ip}:${this.port}/extend)`; Robot_1.default.sendMessage(key, 'markdown', message); } stopWait() { var _a; (_a = this.server) === null || _a === void 0 ? void 0 : _a.close(); this.server = null; this.timer && clearInterval(this.timer); this.timer = null; this.time = 0; process.exit(0); } } const args = process.argv; const port = parseInt(args[2]); const jobName = args[3]; const key = args[4]; const waitTime = (args[5] ? parseInt(args[5]) : undefined); const extendTime = (args[6] ? parseInt(args[6]) : undefined); const buildWait = new BuildWait(key, port, jobName, waitTime, extendTime); buildWait.start();