@daiyu-5577/quickbuild
Version:
front-end build service
219 lines (218 loc) • 6.84 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import express from 'express';
import { encrypt } from '../../utils/crypto.js';
import dayjs from 'dayjs';
import fs from 'fs-extra';
import path from 'path';
import controller_io, { commands } from './controller.io.js';
import { baseRoute, catchBuildLogPath, catchImagePath } from '../config.js';
import { catchHttpLogPath } from '../config.js';
import { generateUid, updateDatabaseFile } from '../../database/index.js';
const __dirname = new URL('.', import.meta.url).pathname;
const router = express.Router();
router.post('/login', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { username, password } = req.body;
const builder = req.app.get('database');
const table_user = builder.get('user');
if (!table_user) {
res.status(500).send({
message: 'table_user not found',
success: false
});
return;
}
const curUser = table_user.find(v => v.name === username && v.password === password);
if (!curUser) {
res.status(500).send({
message: '账号或密码错误',
success: false
});
return;
}
const token = yield encrypt(JSON.stringify({
id: curUser.id,
name: curUser.name,
time: +dayjs(),
}));
res.send({
data: {
id: curUser.id,
name: curUser.name,
token
},
message: 'login success',
success: true
});
}));
router.post('/register', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { username, password } = req.body;
const builder = req.app.get('database');
const table_user = builder.get('user');
if (!table_user) {
res.status(500).send({
message: 'table_user not found',
success: false
});
return;
}
const curUser = table_user.find(v => v.name === username);
if (!!curUser) {
res.status(500).send({
message: '当前用户名已存在',
success: false
});
return;
}
let uid = generateUid();
while (!!table_user.find(v => v.id === uid)) {
uid = generateUid();
}
table_user.push({
id: uid,
name: username,
password,
});
updateDatabaseFile('user', table_user);
res.send({
message: 'register success',
success: true
});
}));
router.post('/addTask', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const builder = req.app.get('builder');
const port = req.app.get('port');
let { packagePaths = ['.'], branch = '', commit = '', notify = `http://127.0.0.1:${port}/api/notify` } = req.body;
packagePaths = typeof packagePaths === 'string' ? [packagePaths] : packagePaths;
if (!branch) {
res.status(500).send({
message: 'branch is required',
success: false
});
}
for (let i = 0; i < packagePaths.length; i++) {
const curPath = packagePaths[i];
yield builder.addTask({
packagePath: curPath,
branch,
commit,
notify,
});
}
res.send({
message: 'add task success',
success: true
});
}));
router.get('/getAllTask', (req, res) => {
const builder = req.app.get('builder');
res.send({
curTack: builder.curTask,
list: builder.buildTasks,
success: true
});
});
router.get('/getAllCommand', (req, res) => {
const list = commands.map(v => ({
command: v.command,
desc: v.desc
}));
res.send({
list,
success: true
});
});
router.get('/getAllPackage', (req, res) => {
const builder = req.app.get('builder');
res.send({
list: builder.packages,
success: true
});
});
router.get('/getLogFiles', (req, res) => {
const httpList = [];
const buildList = [];
const checkLogDir = (type, logDir, arr) => {
if (fs.existsSync(logDir)) {
const files = fs.readdirSync(logDir);
if (!files.length)
return;
files.sort((a, b) => {
return (fs.statSync(`${logDir}/${a}`).mtime.getTime() - fs.statSync(`${logDir}/${b}`).mtime.getTime()) * -1;
});
for (let i = 0; i < files.length; i++) {
const curFile = files[i];
if (curFile.endsWith('.log')) {
arr.push({
name: curFile,
path: `${baseRoute}/api/showLog?type=${type}&name=${curFile}`
});
}
}
}
};
checkLogDir('log', catchHttpLogPath, httpList);
checkLogDir('buildLog', catchBuildLogPath, buildList);
res.send({
tabs: [
{
title: 'httpLog',
list: httpList
},
{
title: 'buildLog',
list: buildList
}
],
success: true
});
});
router.get('/showLog', (req, res, next) => {
const { type, name = '' } = req.query;
const logDirMap = new Map([
['log', catchHttpLogPath],
['buildLog', catchBuildLogPath],
['image', catchImagePath],
]);
if (!type || !name || !logDirMap.get(type)) {
res.status(500).send({
message: `${type} or ${name} 不存在`,
success: false
});
return;
}
const absLogPath = path.join(logDirMap.get(type), name);
if (!fs.existsSync(absLogPath)) {
res.status(500).send({
message: `文件: ${name} 不存在`,
success: false
});
return;
}
res.sendFile(absLogPath, {
dotfiles: 'allow',
maxAge: 14 * 24 * 60 * 60 * 1000
}, next);
});
router.post('/notify', (req, res) => {
const io = req.app.get('io');
const body = req.body;
if (!!body.type) {
controller_io.sendMsg(body.type, body);
}
else {
io.emit('on:msg-chat', body);
}
res.send({
message: 'notify success',
success: true
});
});
export default router;