提交 3bce7dc3 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

Merge remote-tracking branch 'origin/main'

# Conflicts:
#	client/src/services.js
......@@ -4,6 +4,8 @@ import os from 'os';
import {app} from 'electron';
import express from 'express';
const DEBUG = process.env.NODE_ENV === 'development';
let _ztfServerProcess;
export function startZtfServer() {
......@@ -16,7 +18,7 @@ export function startZtfServer() {
}
let {SERVER_EXE_PATH: serverExePath} = process.env;
if (!serverExePath && process.env.NODE_ENV !== 'development') {
if (!serverExePath && !DEBUG) {
const platform = os.platform(); // 'darwin', 'linux', 'win32'
const exePath = `bin/${platform}/ztf${platform === 'win32' ? '.exe' : ''}`;
serverExePath = path.resolve(process.resourcesPath, exePath);
......@@ -25,30 +27,39 @@ export function startZtfServer() {
if (!path.isAbsolute(serverExePath)) {
serverExePath = path.resolve(app.getAppPath(), serverExePath);
}
return new Promise((resolve) => {
console.log(`>> Starting ZTF Server with exe path ${serverExePath}`);
return new Promise((resolve, reject) => {
console.log(`>> Starting ZTF Server from exe path with command "${serverExePath} -P 8085"...`);
const cmd = spawn(serverExePath, ['-P', '8085'], {
cwd: path.dirname(serverExePath),
shell: true,
});
cmd.on('close', () => {
cmd = null;
cmd.on('close', (code) => {
console.log(`>> ZTF server closed with code ${code}`);
_ztfServerProcess = null;
});
cmd.stdout.on('data', data => {
const dataString = String(data);
const lines = dataString.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
console.log('\t>', line);
if (DEBUG) {
console.log('\t', line);
}
if (line.includes('Now listening on: http')) {
resolve(line.split('Now listening on:')[1].trim());
break;
if (!DEBUG) {
break;
}
} else if (line.includes('启动HTTP服务于')) {
resolve(line.split(/启动HTTP服务于|,/)[1].trim());
break;
if (!DEBUG) {
break;
}
} else if (line.startsWith('[ERRO]')) {
reject(new Error(`Start ztf server failed with error: ${line.substring('[ERRO]'.length)}`));
break;
if (!DEBUG) {
break;
}
}
}
});
......@@ -61,12 +72,13 @@ export function startZtfServer() {
}
return new Promise((resolve, reject) => {
console.log('>> Starting ZTF development server...');
console.log(`>> Starting ZTF development server from source with command "go run main.go -P 8085"`);
const cmd = spawn('go', ['run', 'main.go', '-P', '8085'], {
cwd: path.resolve(app.getAppPath(), '../cmd/server'),
shell: true,
});
cmd.on('close', () => {
cmd.on('close', (code) => {
console.log(`>> ZTF server closed with code ${code}`);
_ztfServerProcess = null;
});
cmd.stdout.on('data', data => {
......@@ -74,13 +86,19 @@ export function startZtfServer() {
const lines = dataString.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
console.log('\t>', line);
if (DEBUG) {
console.log('\t', line);
}
if (line.includes('Now listening on: http')) {
resolve(line.split('Now listening on:')[1].trim());
break;
if (!DEBUG) {
break;
}
} else if (line.startsWith('[ERRO]')) {
reject(new Error(`Start ztf server failed with error: ${line.substring('[ERRO]'.length)}`));
break;
if (!DEBUG) {
break;
}
}
}
});
......@@ -100,7 +118,7 @@ export function getUIServerUrl() {
}
let {UI_SERVER_URL: uiServerUrl} = process.env;
if (!uiServerUrl && process.env.NODE_ENV !== 'development') {
if (!uiServerUrl && !DEBUG) {
uiServerUrl = path.resolve(process.resourcesPath, 'ui');
}
......@@ -113,19 +131,19 @@ export function getUIServerUrl() {
uiServerUrl = path.resolve(app.getAppPath(), uiServerUrl);
}
console.log(`>> Starting UI serer at ${uiServerUrl}`);
const port = process.env.UI_SERVER_PORT || 8000;
console.log(`>> Starting UI serer at ${uiServerUrl} with port ${port}`);
const uiServer = express();
uiServer.use(express.static(uiServerUrl));
const server = uiServer.listen(process.env.UI_SERVER_PORT || 8000, serverError => {
const server = uiServer.listen(port, serverError => {
if (serverError) {
console.error('>>> Start ui server failed with error', serverError);
_uiServerApp = null;
reject(serverError);
} else {
const address = server.address();
console.log(`>> UI server started successfully on http://localhost:${address.port}.`);
resolve(`http://localhost:${address.port}`);
console.log(`>> UI server started successfully on http://localhost:${port}.`);
resolve(`http://localhost:${port}`);
}
});
server.on('close', () => {
......@@ -136,14 +154,16 @@ export function getUIServerUrl() {
}
return new Promise((resolve, reject) => {
console.log('>> Starting UI development serve...');
const cwd = path.resolve(app.getAppPath(), '../ui');
console.log(`>> Starting UI development server with command "npm run serve" in "${cwd}"...`);
let resolved = false;
const cmd = spawn('npm', ['run', 'serve'], {
cwd: path.resolve(app.getAppPath(), '../ui'),
cwd,
shell: true,
});
cmd.on('close', () => {
cmd.on('close', (code) => {
console.log(`>> ZTF server closed with code ${code}`);
_uiServerApp = null;
});
cmd.stdout.on('data', data => {
......@@ -154,18 +174,26 @@ export function getUIServerUrl() {
const lines = dataString.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
console.log('\t>', line);
if (DEBUG) {
console.log('\t', line);
}
if (line.includes('App running at:')) {
const nextLine = lines[i + 1] || lines[i + 2];
if (DEBUG) {
console.log('\t', nextLine);
}
if (!nextLine) {
throw new Error(`Cannot grabing running address after line "${line}".`)
console.error('\t', `Cannot grabing running address after line "${line}".`);
throw new Error(`Cannot grabing running address after line "${line}".`);
}
const url = nextLine.split('Local: ')[1];
if (url) {
resolved = true;
resolve(url);
}
break;
if (!DEBUG) {
break;
}
}
}
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册