From 83b5140b48ad58402b71770e84e3ae53fad72673 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 9 Jul 2021 15:04:32 +0800 Subject: [PATCH] no message --- electron/.gitignore | 1 + electron/build.js | 211 ++++++++++++++++--------------------- electron/main.js | 21 +++- electron/package.json | 2 + package.json | 3 +- resources/assets/js/app.js | 2 +- webpack.mix.js | 6 +- 7 files changed, 114 insertions(+), 132 deletions(-) diff --git a/electron/.gitignore b/electron/.gitignore index 04209c7c..9d248ce8 100644 --- a/electron/.gitignore +++ b/electron/.gitignore @@ -4,4 +4,5 @@ package-lock.json dist/ +.devload .native diff --git a/electron/build.js b/electron/build.js index 8ff025a1..6d20bd87 100644 --- a/electron/build.js +++ b/electron/build.js @@ -1,17 +1,22 @@ const fs = require('fs'); -const path = require('path') +const path = require('path') const inquirer = require('inquirer'); const child_process = require('child_process'); -const config = require('../package.json') +const config = require('../package.json') +const argv = process.argv; +const env = require('dotenv').config({ path: './.env' }) -// 删除 +/** + * 删除目录及文件 + * @param path + */ function deleteFile(path) { let files = []; - if( fs.existsSync(path) ) { + if (fs.existsSync(path)) { files = fs.readdirSync(path); - files.forEach(function(file,index){ + files.forEach(function (file, index) { let curPath = path + "/" + file; - if(fs.statSync(curPath).isDirectory()) { + if (fs.statSync(curPath).isDirectory()) { deleteFile(curPath); } else { fs.unlinkSync(curPath); @@ -21,7 +26,12 @@ function deleteFile(path) { } } -// 复制文件 +/** + * 复制文件 + * @param srcPath + * @param tarPath + * @param cb + */ function copyFile(srcPath, tarPath, cb) { let rs = fs.createReadStream(srcPath) rs.on('error', function (err) { @@ -43,48 +53,11 @@ function copyFile(srcPath, tarPath, cb) { rs.pipe(ws) } -// 复制文件夹所有 -function copyDir(srcDir, tarDir, cb) { - if (fs.existsSync(tarDir)) { - fs.readdir(srcDir, function (err, files) { - let count = 0 - let checkEnd = function () { - ++count == files.length && cb && cb() - } - if (err) { - checkEnd() - return - } - files.forEach(function (file) { - let srcPath = path.join(srcDir, file) - let tarPath = path.join(tarDir, file) - fs.stat(srcPath, function (err, stats) { - if (stats.isDirectory()) { - fs.mkdir(tarPath, function (err) { - if (err) { - return - } - copyDir(srcPath, tarPath, checkEnd) - }) - } else { - copyFile(srcPath, tarPath, checkEnd) - } - }) - }) - //为空时直接回调 - files.length === 0 && cb && cb() - }) - } else { - fs.mkdir(tarDir, function (err) { - if (err) { - return - } - copyDir(srcDir, tarDir, cb) - }) - } -} - -// 给地址加上前后 +/** + * 给地址加上前后 + * @param str + * @returns {string} + */ function formatUrl(str) { let url; if (str.substring(0, 7) === "http://" || @@ -94,97 +67,89 @@ function formatUrl(str) { url = "http://" + str.trim(); } if (url.substring(url.length - 1) != "/") { - url+= "/" + url += "/" } return url; } -// 运行命令 -function exec(command, quiet) { - return new Promise((resolve, reject) => { - try { - let child = child_process.exec(command, {encoding: 'utf8'}, () => { - resolve(); - }); - if (!quiet) { - child.stdout.pipe(process.stdout); - } - child.stderr.pipe(process.stderr); - } catch (e) { - console.error('execute command failed :', command); - reject(e); - } - }) -} - /** ***************************************************************************************************/ /** ***************************************************************************************************/ /** ***************************************************************************************************/ const electronDir = path.resolve(__dirname, "public"); const nativeCachePath = path.resolve(__dirname, ".native"); -if (fs.existsSync(electronDir)) { - deleteFile(electronDir); -} -fs.mkdirSync(electronDir); -copyFile(path.resolve(__dirname, "index.html"), electronDir + "/index.html") +const devloadCachePath = path.resolve(__dirname, ".devload"); -const platform = ["build-mac-intel", "build-mac-m1", "build-win"]; -const questions = [ - { - type: 'input', - name: 'targetUrl', - message: "请输入网站地址", - default: () => { - if (fs.existsSync(nativeCachePath)) { - return fs.readFileSync(nativeCachePath, 'utf8'); - } - return undefined; - }, - validate: function (value) { - return value !== '' - } - }, - { - type: 'list', - name: 'platform', - message: "选择编译系统平台", - choices: [{ - name: "MacOS Intel", - value: [platform[0]] - }, { - name: "MacOS M1", - value: [platform[1]] - }, { - name: "Window x86_64", - value: [platform[2]] - }, { - name: "All platforms", - value: platform - }] +if (argv[2] === "--build") { + if (fs.existsSync(electronDir)) { + deleteFile(electronDir); } -]; + fs.mkdirSync(electronDir); + copyFile(path.resolve(__dirname, "index.html"), electronDir + "/index.html") -inquirer.prompt(questions).then(answers => { - let data = `window.systemInformation = { + const platform = ["build-mac-intel", "build-mac-m1", "build-win"]; + const questions = [ + { + type: 'input', + name: 'targetUrl', + message: "请输入网站地址", + default: () => { + if (fs.existsSync(nativeCachePath)) { + return fs.readFileSync(nativeCachePath, 'utf8'); + } + return undefined; + }, + validate: function (value) { + return value !== '' + } + }, + { + type: 'list', + name: 'platform', + message: "选择编译系统平台", + choices: [{ + name: "MacOS Intel", + value: [platform[0]] + }, { + name: "MacOS M1", + value: [platform[1]] + }, { + name: "Window x86_64", + value: [platform[2]] + }, { + name: "All platforms", + value: platform + }] + } + ]; + + inquirer.prompt(questions).then(answers => { + let data = `window.systemInformation = { version: "${config.version}", origin: "./", apiUrl: "${formatUrl(answers.targetUrl)}api/" }`; - fs.writeFileSync(nativeCachePath, formatUrl(answers.targetUrl)); - fs.writeFileSync(electronDir + "/config.js", data, 'utf8'); - // - let packageFile = path.resolve(__dirname, "package.json"); - let packageString = fs.readFileSync(packageFile, 'utf8'); - packageString = packageString.replace(/"version":\s*"(.*?)"/, `"version": "${config.version}"`); - packageString = packageString.replace(/"name":\s*"(.*?)"/, `"name": "${config.name}"`); - fs.writeFileSync(packageFile, packageString, 'utf8'); - // - child_process.spawnSync("mix", ["--production", "--", "--env", "--electron"], {stdio: "inherit"}); - answers.platform.forEach(item => { - child_process.spawn("npm", ["run", item], {stdio: "inherit", cwd: "electron"}); - }) -}); + fs.writeFileSync(nativeCachePath, formatUrl(answers.targetUrl)); + fs.writeFileSync(electronDir + "/config.js", data, 'utf8'); + // + fs.writeFileSync(devloadCachePath, "", 'utf8'); + let packageFile = path.resolve(__dirname, "package.json"); + let packageString = fs.readFileSync(packageFile, 'utf8'); + packageString = packageString.replace(/"version":\s*"(.*?)"/, `"version": "${config.version}"`); + packageString = packageString.replace(/"name":\s*"(.*?)"/, `"name": "${config.name}"`); + fs.writeFileSync(packageFile, packageString, 'utf8'); + // + child_process.spawnSync("mix", ["--production", "--", "--env", "--electron"], {stdio: "inherit"}); + answers.platform.forEach(arg => { + child_process.spawn("npm", ["run", arg], {stdio: "inherit", cwd: "electron"}); + }) + }); +} else { + fs.writeFileSync(devloadCachePath, formatUrl("127.0.0.1:" + env.parsed.APP_PORT), 'utf8'); + child_process.spawn("mix", ["watch", "--hot"], {stdio: "inherit"}); + child_process.spawn("npm", ["run", "start-quiet"], {stdio: "inherit", cwd: "electron"}); +} + diff --git a/electron/main.js b/electron/main.js index 2d905916..de24e7cd 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,6 +1,13 @@ -const {app, BrowserWindow} = require('electron') +const fs = require('fs') const path = require('path') -let willQuitApp = false; +const {app, BrowserWindow} = require('electron') + +let willQuitApp = false, + devloadCachePath = path.resolve(__dirname, ".devload"), + devloadUrl = ""; +if (fs.existsSync(devloadCachePath)) { + devloadUrl = fs.readFileSync(devloadCachePath, 'utf8') +} function getCounterValue(title) { const itemCountRegex = /[([{]([\d.,]*)\+?[}\])]/; @@ -17,9 +24,15 @@ function createWindow(setDockBadge) { } }) - mainWindow.loadFile('./public/index.html').then(r => { + if (devloadUrl) { + mainWindow.loadURL(devloadUrl).then(r => { - }) + }) + } else { + mainWindow.loadFile('./public/index.html').then(r => { + + }) + } mainWindow.on('page-title-updated', function (event, title) { const counterValue = getCounterValue(title); diff --git a/electron/package.json b/electron/package.json index b77f3622..ab39691b 100644 --- a/electron/package.json +++ b/electron/package.json @@ -6,6 +6,7 @@ "license": "MIT", "scripts": { "start": "electron-forge start", + "start-quiet": "electron-forge start &> /dev/null", "build": "electron-builder", "build-mac-intel": "electron-builder --mac", "build-mac-m1": "electron-builder --mac --arm64", @@ -20,6 +21,7 @@ "@electron-forge/maker-rpm": "^6.0.0-beta.57", "@electron-forge/maker-squirrel": "^6.0.0-beta.57", "@electron-forge/maker-zip": "^6.0.0-beta.57", + "dotenv": "^9.0.2", "electron": "^13.1.6", "electron-builder": "^22.11.7" }, diff --git a/package.json b/package.json index d02033b0..140e3cf1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "scripts": { "start": "./cmd dev", "build": "./cmd prod", - "build-electron": "node ./electron/build.js", + "electron-start": "node ./electron/build.js", + "electron-build": "node ./electron/build.js --build", "version": "node ./version.js" }, "devDependencies": { diff --git a/resources/assets/js/app.js b/resources/assets/js/app.js index a49225f9..f65a8ffc 100644 --- a/resources/assets/js/app.js +++ b/resources/assets/js/app.js @@ -52,7 +52,7 @@ VueRouter.prototype.push = function push(location) { } const router = new VueRouter({ - mode: __PLATFORM === "web" ? 'history' : 'hash', + mode: !__IS_WEB ? 'hash' : 'history', routes }); diff --git a/webpack.mix.js b/webpack.mix.js index 988bdf05..a9b54fb0 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -21,10 +21,10 @@ if (!['--watch', '--hot'].includes(argv[3])) { output.publicPath = './'; } -let platform = "web"; +let is_web = true; let publicPath = 'public' if (argv[4] === '--electron') { - platform = "electron" + is_web = false publicPath = 'electron/public'; } @@ -38,7 +38,7 @@ mix output, plugins: [ new webpack.DefinePlugin({ - '__PLATFORM': platform + '__IS_WEB': is_web, }) ] }