代码拉取完成,页面将自动刷新
同步操作将从 GLUESTICK/auto-deployment 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
'use strict';
const http = require('http');
const exec = require('child_process').exec;
const execFile = require('child_process').execFile;
const os = require('os');
const iconv = require('iconv-lite');
const buffer = require('buffer');
const system = os.platform();
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
let binaryEncoding = 'binary';
let encoding = 'cp936';
let options = {
encoding: binaryEncoding,
windowsHide:false,
};
function func(){}
func.prototype = {
execute:function(cmd,callback){
exec(cmd,options, function(error, stdout, stderr) {
if(error){
console.log(cmd+ ' fail');
if(callback && typeof callback === 'function') {
let result = iconv.decode(Buffer.from(stderr,binaryEncoding), encoding);
callback(true,result);
}
} else{
console.log(cmd+ ' success');
if(callback && typeof callback === 'function') {
console.log('cmd:'+cmd);
let result = iconv.decode(Buffer.from(stdout,binaryEncoding), encoding);
callback(true,result);
}
}
});
return this;
}
};
function run() { return new func(); }
let init = function(option){
http.createServer(function(req, res){
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('X-Foo', 'bar');
console.log(req.method + ':' + req.url);
if(req.headers['x-gitee-token']===option.acceptToken && req.method===option.method && req.url === option.url && req.headers['user-agent']==='git-oschina-hook') {
// 验证成功
// 判断执行类型 file or command 默认:command
console.log('async:'+option.async);
if(option.type!=='file') {
let resText = '';
let loop = function loop(i){
run().execute(option.cmd[i],(success,result)=>{
resText+=result;
if(i<option.cmd.length-1) {
if(success===true) {
i++;
loop(i);
} else {
if(option.async===false) {
console.log('fail');
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(resText);
}
return false;
}
} else {
if(option.async===false) {
console.log('All success');
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(resText);
return false;
}
}
});
};
if(option.async===true) {
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('async success');
loop(0);
} else {
loop(0);
}
} else {
let bash = option.executeFile;
if(option.async===true) {
execFile(bash);
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('async success');
} else {
execFile(bash,options,(err, stdout, stderr)=>{
if(!stderr) {
console.log('All success');
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
let result = iconv.decode(Buffer.from(stdout,binaryEncoding), encoding);
res.end(result);
} else {
console.log('fail');
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
let result = iconv.decode(Buffer.from(stderr,binaryEncoding), encoding);
res.end(result);
}
return false;
});
}
}
} else {
res.writeHead(402, { 'Content-Type': 'text/plain' });
res.end('fail');
}
}).listen(option.port);
console.log('自动部署服务启动于:' + system + '操作系统,端口号:' + option.port);
};
module.exports = init;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。