Skip to content

Commit

Permalink
perf: Windows客户端无法关闭的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Dec 28, 2021
1 parent b695f90 commit cef6646
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions electron/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
public/
package-bak.json
package-lock.json

build/
Expand Down
48 changes: 23 additions & 25 deletions electron/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path')
const inquirer = require('inquirer');
const child_process = require('child_process');
Expand Down Expand Up @@ -102,45 +103,44 @@ function rightExists(string, find) {
const electronDir = path.resolve(__dirname, "public");
const nativeCachePath = path.resolve(__dirname, ".native");
const devloadCachePath = path.resolve(__dirname, ".devload");
const packageFile = path.resolve(__dirname, "package.json");
const packageBakFile = path.resolve(__dirname, "package-bak.json");
const platform = ["build-mac", "build-mac-arm", "build-win"];

// 生成配置、编译应用
function step1(data, publish) {
function start(data, publish) {
console.log("Name: " + data.name);
console.log("AppId: " + data.id);
console.log("Version: " + config.version);
// config.js
let systemInfo = {
title: data.name,
version: config.version,
origin: "./",
apiUrl: formatUrl(data.url) + "api/",
}
fs.writeFileSync(electronDir + "/config.js", "window.systemInformation = " + JSON.stringify(systemInfo), 'utf8');
fs.writeFileSync(electronDir + "/config.js", "window.systemInformation = " + JSON.stringify(systemInfo, null, 2), 'utf8');
fs.writeFileSync(nativeCachePath, formatUrl(data.url));
fs.writeFileSync(devloadCachePath, "", 'utf8');
// index.html
let indexFile = path.resolve(electronDir, "index.html");
let indexString = fs.readFileSync(indexFile, 'utf8');
indexString = indexString.replace(`<title></title>`, `<title>${data.name}</title>`);
fs.writeFileSync(indexFile, indexString, 'utf8');
// package.json
let packageFile = path.resolve(__dirname, "package.json");
let packageString = fs.readFileSync(packageFile, 'utf8');
packageString = packageString.replace(/"name":\s*"(.*?)"/, `"name": "${data.name}"`);
packageString = packageString.replace(/"appId":\s*"(.*?)"/, `"appId": "${data.id}"`);
packageString = packageString.replace(/"version":\s*"(.*?)"/, `"version": "${config.version}"`);
packageString = packageString.replace(/"artifactName":\s*"(.*?)"/g, '"artifactName": "' + getDomain(data.url) + '-v${version}-${os}-${arch}.${ext}"');
fs.writeFileSync(packageFile, packageString, 'utf8');
//
// package.json Backup
fse.copySync(packageFile, packageBakFile)
// package.json Generated
const econfig = require('./package.json')
econfig.name = data.name;
econfig.version = config.version;
econfig.build.appId = data.id;
econfig.build.artifactName = getDomain(data.url) + "-v${version}-${os}-${arch}.${ext}";
econfig.build.nsis.artifactName = getDomain(data.url) + "-v${version}-${os}-${arch}.${ext}";
fs.writeFileSync(packageFile, JSON.stringify(econfig, null, 2), 'utf8');
// build
child_process.spawnSync("npm", ["run", data.platform + (publish === true ? "-publish" : "")], {stdio: "inherit", cwd: "electron"});
}

// 还原配置
function step2() {
let packageFile = path.resolve(__dirname, "package.json");
let packageString = fs.readFileSync(packageFile, 'utf8');
packageString = packageString.replace(/"name":\s*"(.*?)"/, `"name": "${config.name}"`);
packageString = packageString.replace(/"appId":\s*"(.*?)"/, `"appId": "${config.app.id}"`);
packageString = packageString.replace(/"artifactName":\s*"(.*?)"/g, '"artifactName": "${productName}-v${version}-${os}-${arch}.${ext}"');
fs.writeFileSync(packageFile, packageString, 'utf8');
// package.json Recovery
fse.copySync(packageBakFile, packageFile)
}

if (["dev"].includes(argv[2])) {
Expand All @@ -153,10 +153,9 @@ if (["dev"].includes(argv[2])) {
config.app.sites.forEach((data) => {
if (data.name && data.id && data.url) {
data.platform = argv[2];
step1(data, true)
start(data, true)
}
})
step2();
} else {
// 自定义编译
const questions = [
Expand Down Expand Up @@ -198,14 +197,13 @@ if (["dev"].includes(argv[2])) {
];
inquirer.prompt(questions).then(answers => {
answers.platform.forEach(platform => {
step1({
start({
"name": config.name,
"id": config.app.id,
"url": answers.website,
"platform": platform
}, false)
});
step2();
});
}

Expand Down
16 changes: 13 additions & 3 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs')
const path = require('path')
const XLSX = require('xlsx');
const {app, BrowserWindow, ipcMain, dialog, screen} = require('electron')
const {app, BrowserWindow, ipcMain, dialog} = require('electron')

let mainWindow = null,
subWindow = [],
Expand Down Expand Up @@ -47,6 +47,7 @@ function createMainWindow() {
width: 1280,
height: 800,
center: true,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
Expand All @@ -71,7 +72,11 @@ function createMainWindow() {
if (inheritClose) {
mainWindow.webContents.send("windowClose", {})
} else {
app.hide();
if (process.platform === 'darwin') {
app.hide();
} else {
app.quit();
}
}
}
})
Expand Down Expand Up @@ -103,6 +108,7 @@ function createSubWindow(args) {
height: 800,
center: true,
parent: mainWindow,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
devTools: args.devTools !== false,
Expand Down Expand Up @@ -162,7 +168,11 @@ ipcMain.on('windowRouter', (event, args) => {
})

ipcMain.on('windowHidden', (event) => {
app.hide();
if (process.platform === 'darwin') {
app.hide();
} else {
app.quit();
}
event.returnValue = "ok"
})

Expand Down

0 comments on commit cef6646

Please sign in to comment.