build-desktop

This commit is contained in:
tengge 2020-06-25 14:55:28 +08:00
parent d3cda0c308
commit 66fd78fafd
7 changed files with 1424 additions and 4 deletions

4
.gitignore vendored
View File

@ -77,4 +77,6 @@ utils/electron/build/*
!utils/electron/build/README.md
utils/electron/out
utils/electron/logs.txt
web/test/WebWorldWind/build
web/test/WebWorldWind/build
utils/mongodb/*
!utils/mongodb/README.md

1258
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,14 +7,15 @@
"build": "node scripts/build.js",
"build-server": "node scripts/build_server.js",
"build-web": "cd web && rollup -c rollup.config.js",
"build-desktop": "node scripts/build_desktop.js",
"dev-web": "cd web && rollup -c rollup.config.js --watch",
"start": "cd build && ShadowEditor serve --config ./config.toml",
"set-proxy": "node scripts/set_proxy.js",
"unset-proxy": "node scripts/unset_proxy.js",
"install-dev": "node scripts/install_dev.js",
"eslint": "eslint web/src --fix",
"eslint": "cd web && eslint src --fix",
"clean": "node scripts/clean.js",
"test": "",
"test": "echo \"Error: no test specified\" && exit 1",
"clear": "npm prune"
},
"dependencies": {
@ -30,6 +31,8 @@
"@babel/core": "^7.9.6",
"@babel/preset-react": "^7.9.4",
"babel-eslint": "^10.1.0",
"electron": "^9.0.5",
"electron-packager": "^14.2.1",
"eslint": "^6.8.0",
"eslint-config-defaults": "^9.0.0",
"eslint-plugin-react": "^7.19.0",
@ -61,4 +64,4 @@
"url": "https://github.com/tengge1/ShadowEditor/issues"
},
"homepage": "https://github.com/tengge1/ShadowEditor"
}
}

73
scripts/build_desktop.js Normal file
View File

@ -0,0 +1,73 @@
/*
* Copyright 2017-2020 The ShadowEditor Authors. All rights reserved.
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file.
*
* For more information, please visit: https://github.com/tengge1/ShadowEditor
* You can also visit: https://gitee.com/tengge1/ShadowEditor
*/
const subprocess = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const os = require('os');
/**
* Execute a command
* @param {String} cmd bat of shell command
* @param {Boolean} showCmd whether to print the command
* @returns the result of the command
*/
function exec(cmd, showCmd = true) {
showCmd && console.log(cmd);
return subprocess.execSync(cmd).toString().trimRight('\n');
}
/**
* The main function
*/
async function main() {
const rootDir = process.cwd(); // The root dir that contains `README.md`.
const buildDir = path.join(rootDir, 'build'); // The build dir.
const mongoDir = path.join(rootDir, 'utils', 'mongodb'); // Download and unzip mongodb here.
console.log(`root dir: ${rootDir}`);
console.log(`build dir: ${buildDir}`);
console.log(`mongo dir: ${mongoDir}`);
// check build
if (!fs.existsSync(buildDir)) {
console.warn('You should run `npm run build` first.');
return;
}
// check mongodb
const isWin32 = os.platform() === 'win32';
const mongoPath = path.join(mongoDir, isWin32 ? 'mongo.exe' : 'mongo');
const mongodPath = path.join(mongoDir, isWin32 ? 'mongod.exe' : 'mongod');
if (!fs.existsSync(mongoPath) || !fs.existsSync(mongodPath)) {
console.warn('You should download mongodb first. See `utils/mongodb/README.md` for detail.');
return;
}
// copy files needed by desktop app
fs.copySync(mongoDir, 'build/mongo');
fs.copySync('scripts/electron/main.js', 'build/main.js');
// Build desktop package
console.log('enter build');
process.chdir(buildDir);
exec('electron-packager . ShadowEditor --platform=win32 ' +
'--arch=x64 --icon=../web/favicon.ico --out=desktop --app-version=0.0.1 ' +
'--overwrite --no-prune --ignore=public/Upload|public/Upload2 ' +
'--electron-version 9.0.4');
console.log('leave build');
process.chdir(rootDir);
console.log('Done!');
}
main();

View File

@ -0,0 +1,3 @@
# Electron
This script is used by `Electron` to create a desktop app.

72
scripts/electron/main.js Normal file
View File

@ -0,0 +1,72 @@
const fs = require('fs');
const process = require('child_process');
const { app, BrowserWindow, Menu } = require('electron');
let mongo, server, win;
function log(data) {
fs.writeFileSync('logs.txt', data, { flag: 'a' });
fs.writeFileSync('logs.txt', '\n', { flag: 'a' });
}
function startMongoDB(root) {
mongo = process.exec(`${root}\\mongo\\mongod.exe --dbpath=${root}\\mongo\\db`, {
cwd: `${root}\\mongo`,
encoding: 'gbk' // only for Chinese
});
mongo.stdout.on('data', data => {
log(`Mongo Out: ${data}`);
});
mongo.stderr.on('data', data => {
log(`Mongo Error: ${data}`);
});
mongo.on('SIGTERM', data => {
mongo.exit(0);
log(`Mongo Close: ${data}`);
});
}
function startServer(root) {
server = process.exec(`${root}\\build\\ShadowEditor.exe serve --config ./config.toml`, {
cwd: `${root}\\build`,
encoding: 'gbk' // only for Chinese
});
server.stdout.on('data', data => {
log(`Server Out: ${data}`);
});
server.stderr.on('data', data => {
log(`Server Error: ${data}`);
});
server.on('SIGTERM', data => {
server.exit(0);
log(`Server Close: ${data}`);
});
}
function start() {
const path = app.getAppPath();
startMongoDB(path);
startServer(path);
Menu.setApplicationMenu(null);
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
// win.maximize();
win.loadURL('http://localhost:2020');
win.on('close', () => {
if (mongo) {
mongo.kill('SIGTERM');
}
if (server) {
server.kill('SIGTERM');
}
});
}
app.whenReady().then(start);

9
utils/mongodb/README.md Normal file
View File

@ -0,0 +1,9 @@
# MongoDB
1. Download mongodb zip package from `https://www.mongodb.com/try/download/community`.
Windows: https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2012plus-4.2.8.zip
Ubuntu: https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz
2. Unzip mongodb-win32-x86_64-2012plus-4.2.7.zip in this folder.