提交 072cb029 编写于 作者: H Hao Sun

* make package script work.

上级 14a04389
......@@ -87,3 +87,9 @@ typings/
# Electron-Forge
out/
# ZTF binary
bin/
# ZTF UI
ui/
......@@ -46,7 +46,7 @@ UI_SERVER_URL=../ui/dist UI_SERVER_PORT=8000 npm run start
**模式三:自定义 ZTF 服务执行文件路径**
```
SERVER_EXE_PATH=bin/ztf npm run start
SERVER_EXE_PATH=bin/darwin/ztf npm run start
```
**模式四:跳过启动 ZTF 服务,使用外部 ZTF 服务**
......@@ -83,6 +83,13 @@ npm run package
参考:https://www.electronforge.io/cli#package
打包之前确保如下目录有相应的资源文件:
* `client/ui/`:包含 UI 服务相关的所有文件,并且包含 `client/ui/index.html` 文件,作为 UI 服务入口;
* `client/bin/win32/`:包含适用于 Windows 的 ZTF 程序;
* `client/bin/darwin/`:包含适用于 macOS 的 ZTF 程序;
* `client/bin/linux/`:包含适用于 Linux 的 ZTF 程序;
## 发布
```
......
module.exports = {
"packagerConfig": {},
"makers": [
packagerConfig: {
extraResource: [
'./bin',
'./ui'
]
},
makers: [
{
"name": "@electron-forge/maker-squirrel",
"config": {
"name": "ztf_client"
name: '@electron-forge/maker-squirrel',
config: {
name: 'ztf_client'
}
},
{
"name": "@electron-forge/maker-zip",
"platforms": [
"darwin"
name: '@electron-forge/maker-zip',
platforms: [
'darwin'
]
},
{
"name": "@electron-forge/maker-deb",
"config": {}
name: '@electron-forge/maker-deb',
config: {}
},
{
"name": "@electron-forge/maker-rpm",
"config": {}
name: '@electron-forge/maker-rpm',
config: {}
}
],
"plugins": [
plugins: [
[
"@electron-forge/plugin-webpack",
'@electron-forge/plugin-webpack',
{
"mainConfig": "./webpack.main.config.js",
"renderer": {
"config": "./webpack.renderer.config.js",
"entryPoints": [
mainConfig: './webpack.main.config.js',
renderer: {
config: './webpack.renderer.config.js',
entryPoints: [
// {
// "html": "./src/index.html",
// "js": "./src/renderer.js",
// "name": "main_window"
// html: './src/index.html',
// js: './src/renderer.js',
// name: 'main_window'
// }
]
}
......
......@@ -29,8 +29,6 @@ async function startApp() {
}
_starting = true;
console.log('>> Starting ZTF Server...');
try {
const ztfServerUrl = await startZtfServer();
console.log(`>> ZTF Server started successfully: ${ztfServerUrl}`);
......
import path from 'path';
import {spawn} from 'child_process';
import os from 'os';
import {app} from 'electron';
import express from 'express';
......@@ -7,6 +8,7 @@ let _ztfServerProcess;
export function startZtfServer() {
if (process.env.SKIP_SERVER) {
console.log(`>> Skip to start ZTF Server by env "SKIP_SERVER=${process.env.SKIP_SERVER}".`);
return Promise.resolve();
}
if (_ztfServerProcess) {
......@@ -14,21 +16,52 @@ export function startZtfServer() {
}
let {SERVER_EXE_PATH: serverExePath} = process.env;
if (!serverExePath && process.env.NODE_ENV !== 'development') {
const platform = os.platform(); // 'darwin', 'linux', 'win32'
const exePath = `bin/${platform}/ztf${platform === 'win32' ? '.exe' : ''}`;
serverExePath = path.resolve(process.resourcesPath, exePath);
}
if (serverExePath) {
if (!path.isAbsolute(serverExePath)) {
serverExePath = path.resolve(app.getAppPath(), serverExePath);
}
return new Promise((resolve) => {
_ztfServerProcess = spawn(serverExePath);
_ztfServerProcess.on('close', () => {
_ztfServerProcess = null;
console.log(`>> Starting ZTF Server with exe path ${serverExePath}`);
const cmd = spawn(serverExePath, ['-P', '8085'], {
cwd: path.dirname(serverExePath),
shell: true,
});
cmd.on('close', () => {
cmd = 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];
if (line.includes('Now listening on: http')) {
resolve(line.split('Now listening on:')[1].trim());
break;
} else if (line.includes('启动HTTP服务于')) {
resolve(line.split(/启动HTTP服务于|,/)[1].trim());
break;
} else if (line.startsWith('[ERRO]')) {
reject(new Error(`Start ztf server failed with error: ${line.substring('[ERRO]'.length)}`));
break;
}
}
});
resolve(_ztfServerProcess);
cmd.on('error', spawnError => {
console.error('>>> Start ztf server failed with error', spawnError);
reject(spawnError)
});
_ztfServerProcess = cmd;
});
}
return new Promise((resolve, reject) => {
const cmd = spawn('go', ['run', 'main.go', '-p', '8085'], {
console.log('>> Starting ZTF development server...');
const cmd = spawn('go', ['run', 'main.go', '-P', '8085'], {
cwd: path.resolve(app.getAppPath(), '../cmd/server'),
shell: true,
});
......@@ -65,6 +98,10 @@ export function getUIServerUrl() {
}
let {UI_SERVER_URL: uiServerUrl} = process.env;
if (!uiServerUrl && process.env.NODE_ENV !== 'development') {
uiServerUrl = path.resolve(process.resourcesPath, 'ui');
}
if (uiServerUrl) {
if (/^https?:\/\//.test(uiServerUrl)) {
return Promise.resolve(uiServerUrl);
......@@ -117,6 +154,9 @@ export function getUIServerUrl() {
const line = lines[i];
if (line.includes('App running at:')) {
const nextLine = lines[i + 1] || lines[i + 2];
if (!nextLine) {
throw new Error(`Cannot grabing running address after line "${line}".`)
}
const url = nextLine.split('Local: ')[1];
if (url) {
resolved = true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册