2023-10-06 18:59:59 +08:00
|
|
|
import axios from 'axios';
|
2023-09-27 20:00:17 +08:00
|
|
|
import dotenv from 'dotenv';
|
|
|
|
import type { CommonOptions } from 'execa';
|
|
|
|
import execa from 'execa';
|
|
|
|
import net from 'net';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
export const commonConfig: any = {
|
|
|
|
stdio: 'inherit',
|
|
|
|
};
|
|
|
|
|
|
|
|
export const runCommand = (command, argv, options = {}) => {
|
|
|
|
return execa(command, argv, {
|
|
|
|
shell: true,
|
|
|
|
stdio: 'inherit',
|
|
|
|
...options,
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查端口是否被占用
|
|
|
|
* @param port
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
function checkPort(port) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const socket = net.createConnection(port, '127.0.0.1');
|
|
|
|
|
|
|
|
socket.on('connect', () => {
|
|
|
|
socket.destroy();
|
|
|
|
resolve(true); // 端口可用
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', (error) => {
|
|
|
|
resolve(false); // 端口被占用或不可访问
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-06 18:59:59 +08:00
|
|
|
/**
|
|
|
|
* 检查服务是否启动成功
|
|
|
|
*/
|
2023-10-10 07:46:24 +08:00
|
|
|
const checkServer = async (duration = 1000, max = 60) => {
|
2023-10-06 18:59:59 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-10-06 23:29:45 +08:00
|
|
|
let count = 0;
|
2023-10-06 18:59:59 +08:00
|
|
|
const timer = setInterval(async () => {
|
2023-10-06 23:29:45 +08:00
|
|
|
if (count++ > max) {
|
|
|
|
clearInterval(timer);
|
|
|
|
return reject(new Error('Server start timeout.'));
|
|
|
|
}
|
|
|
|
|
2023-10-06 18:59:59 +08:00
|
|
|
if (!(await checkPort(process.env.APP_PORT))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
axios
|
|
|
|
.get(`http://localhost:${process.env.APP_PORT}/api/__health_check`)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
clearInterval(timer);
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-10-06 23:29:45 +08:00
|
|
|
console.error('Request error:', error.message);
|
2023-10-06 18:59:59 +08:00
|
|
|
});
|
|
|
|
}, duration);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查 UI 是否启动成功
|
|
|
|
* @param duration
|
|
|
|
*/
|
2023-10-10 07:46:24 +08:00
|
|
|
const checkUI = async (duration = 1000, max = 60) => {
|
2023-10-06 18:59:59 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-10-06 23:29:45 +08:00
|
|
|
let count = 0;
|
2023-10-06 18:59:59 +08:00
|
|
|
const timer = setInterval(async () => {
|
2023-10-06 23:29:45 +08:00
|
|
|
if (count++ > max) {
|
|
|
|
clearInterval(timer);
|
|
|
|
return reject(new Error('UI start timeout.'));
|
|
|
|
}
|
|
|
|
|
2023-10-06 18:59:59 +08:00
|
|
|
axios
|
|
|
|
.get(`http://localhost:${process.env.APP_PORT}/__umi/api/bundle-status`)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.data.bundleStatus.done) {
|
|
|
|
clearInterval(timer);
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-10-06 23:29:45 +08:00
|
|
|
console.error('Request error:', error.message);
|
2023-10-06 18:59:59 +08:00
|
|
|
});
|
|
|
|
}, duration);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-09-27 20:00:17 +08:00
|
|
|
export const runNocoBase = async (options?: CommonOptions<any>) => {
|
|
|
|
// 用于存放 playwright 自动生成的相关的文件
|
|
|
|
if (!fs.existsSync('playwright')) {
|
|
|
|
fs.mkdirSync('playwright');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync('.env.e2e') && fs.existsSync('.env.e2e.example')) {
|
|
|
|
const env = fs.readFileSync('.env.e2e.example');
|
|
|
|
fs.writeFileSync('.env.e2e', env);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync('.env.e2e')) {
|
|
|
|
throw new Error('Please create .env.e2e file first!');
|
|
|
|
}
|
|
|
|
|
|
|
|
dotenv.config({ path: path.resolve(process.cwd(), '.env.e2e') });
|
|
|
|
|
2023-10-06 18:59:59 +08:00
|
|
|
const awaitForNocoBase = async () => {
|
2023-10-06 23:29:45 +08:00
|
|
|
if (process.env.CI) {
|
|
|
|
console.log('check server...');
|
|
|
|
await checkServer();
|
|
|
|
} else {
|
|
|
|
console.log('check server...');
|
|
|
|
await checkServer();
|
|
|
|
console.log('server is ready, check UI...');
|
|
|
|
await checkUI();
|
|
|
|
console.log('UI is ready.');
|
|
|
|
}
|
2023-10-06 18:59:59 +08:00
|
|
|
};
|
|
|
|
|
2023-09-27 20:00:17 +08:00
|
|
|
if (process.env.CI) {
|
|
|
|
console.log('yarn nocobase install');
|
|
|
|
await runCommand('yarn', ['nocobase', 'install'], options);
|
|
|
|
console.log(`yarn start -d -p ${process.env.APP_PORT}`);
|
|
|
|
await runCommand('yarn', ['start', '-d', `-p ${process.env.APP_PORT}`], options);
|
2023-10-06 18:59:59 +08:00
|
|
|
return { awaitForNocoBase };
|
2023-09-27 20:00:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 加上 -f 会清空数据库
|
|
|
|
console.log('yarn nocobase install -f');
|
|
|
|
await runCommand('yarn', ['nocobase', 'install', '-f'], options);
|
|
|
|
|
2023-10-06 18:59:59 +08:00
|
|
|
if (await checkPort(process.env.APP_PORT)) {
|
|
|
|
console.log('Server is running, skip starting server.');
|
|
|
|
return { awaitForNocoBase };
|
|
|
|
}
|
|
|
|
|
2023-09-27 20:00:17 +08:00
|
|
|
console.log('starting server...');
|
|
|
|
const { cancel, kill } = runCommand('yarn', ['dev', `-p ${process.env.APP_PORT}`, ...process.argv.slice(2)], options);
|
|
|
|
|
2023-10-06 18:59:59 +08:00
|
|
|
return { cancel, kill, awaitForNocoBase };
|
2023-09-27 20:00:17 +08:00
|
|
|
};
|