Bash 脚本未从 Electron 应用程序的生产构建中执行
Bash Scripts Are Not Executing From Production Build of Electron App
更新: 添加了index.js 文件内容。
我有这个电子应用程序正在执行一些 bash scrips(*.sh) 文件来执行一些任务。
在开发环境中一切正常,但是在为 Ubuntu 平台 构建 deb
安装程序的生产版本时,一切正常,就像在应用程序上打开一样,其他 NodeJS 内容,但 bash 脚本未执行。
问题陈述:如何在 Linux(Ubuntu [=101 的电子应用程序的生产构建中执行 shell 脚本=]).收到此错误
app/terminal_scripts/timer.sh Not Found
下面是应用程序的详细说明。
**项目目录设置**:
项目名称
|
应用 > css |图片 | JS |呈现
terminal_scripts
node_modules
package.json
包裹-lock.json
在应用程序目录中,我有所有 CSS、图像、js、HTML 和终端脚本。
package.json:
{
"name": "timer",
"productName": "Timely",
"version": "1.0.25",
"description": "This desktop app shows you system clock",
"main": "app/js/main/index.js",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "nodemon --exec 'electron .'",
"dist": "electron-builder"
},
"homepage": ".",
"keywords": [
"Electron",
"Desktop App"
],
"author": "NotABot Ltd <contact@notabot.com>",
"contributors": [
{
"name": "Not A Bot",
"email": "nab@notabot.com"
}
],
"license": "ISC",
"dependencies": {
"desandro-matches-selector": "^2.0.2",
"electron-context-menu": "^1.0.0",
"electron-is": "^3.0.0",
"fix-path": "^3.0.0",
"isotope-layout": "^3.0.6",
"jquery": "^3.5.0",
"jquery-bridget": "^2.0.1"
},
"build": {
"appId": "com.test.timely",
"productName": "Timely",
"linux": {
"target": "deb",
"category": "System"
}
},
"devDependencies": {
"electron": "^8.1.1",
"electron-builder": "^22.6.0"
}
}
HTML:
<html>
<head>
<title>Timely</title>
</head>
<body>
<button onclick="displayTime()">Display Time</button>
<textarea rows="20" cols="90" id="command-output" disabled="true"></textarea>
<script>
const {app} = require('electron');
function displayTime(){
console.log("button clicked");
let cmd = `bash app/terminal_scripts/timer.sh`;
let completeMessage = 'This is the message';
backgroundProcess(cmd, completeMessage);
}
function getCommandOutput() { return document.getElementById("command-output"); };
function getStatus() { return document.getElementById("status"); };
function appendOutput(msg) { getCommandOutput().value += (msg+'\n'); };
function setStatus(msg) { getStatus().innerHTML = msg; };
function backgroundProcess(cmd, completeMessage){
const process = require('child_process');
var child = process.execFile(cmd, [] , {shell: true} );
appendOutput("Processing......");
child.on('error', function(err) {
appendOutput('stderr: '+err );
});
child.stdout.on('data', function (data) {
appendOutput(data);
});
child.stderr.on('data', function (data) {
appendOutput(data );
});
return new Promise((resolve, reject) => {
child.on('close', function (code) {
console.log(`code is: ${code}`);
if (code == 0){
setStatus(completeMessage);
resolve(1);
}
else{
setStatus('Exited with error code ' + code);
resolve(-1);
}
});
});
}
</script>
</body>
</html>
Bash 脚本:
#!/bin/bash
timer="$(date)"
echo "$timer"
Permission is set 777 for this shell file
平台信息:
- OS:Ubuntu 18.04.4 LTS
- NodeJS: 13.6.0
- NPM:6.14.5
- 电子:8.1.1
- 电子生成器:22.6.0
index.js
const {app, BrowserWindow, Menu, Tray, ipcMain, MenuItem} = require('electron');
const path = require('path');
const contextMenu = require('electron-context-menu');
let splashWindow;
function createMainWindow(){
mainWindow = new BrowserWindow({
minHeight: 700,
minWidth: 800,
webPreferences: {
nodeIntegration: true,
webviewTag: true
},
show: false
});
//For dev only
// mainWindow.webContents.openDevTools();
mainWindow.loadFile('app/renderer/index.html');
mainWindow.maximize();
}
app.on('ready', () =>{
createMainWindow();
});
我在 app
目录旁边创建了一个名为 termainal_scripts
的新目录。
在里面,我有我的 bash 文件 timer.sh.
我想出了如何在 path.join()
中使用 process.resourcesPath
在生产环境中执行 shell 脚本。
所以,让固定路径为:
let fixedURL = path.join(process.resourcesPath, '/terminal_scripts/');
那么要执行的命令将是:
let cmd = `${fixedURL}timer.sh`
另一种方法是将苍蝇移动到 app
目录之外的新目录,并将其命名为 extraResources
。
在该目录中,您可以添加所有 bash 文件,对于生产,您可以使用以下方法。
let urlPath = path.join(process.resourcesPath, '/extraResources/')
然后使用let cmd = `${urlPath}timer.sh`
;
更新: 添加了index.js 文件内容。
我有这个电子应用程序正在执行一些 bash scrips(*.sh) 文件来执行一些任务。
在开发环境中一切正常,但是在为 Ubuntu 平台 构建 deb
安装程序的生产版本时,一切正常,就像在应用程序上打开一样,其他 NodeJS 内容,但 bash 脚本未执行。
问题陈述:如何在 Linux(Ubuntu [=101 的电子应用程序的生产构建中执行 shell 脚本=]).收到此错误
app/terminal_scripts/timer.sh Not Found
下面是应用程序的详细说明。
**项目目录设置**:
项目名称
|
应用 > css |图片 | JS |呈现
terminal_scripts
node_modules
package.json
包裹-lock.json
在应用程序目录中,我有所有 CSS、图像、js、HTML 和终端脚本。
package.json:
{
"name": "timer",
"productName": "Timely",
"version": "1.0.25",
"description": "This desktop app shows you system clock",
"main": "app/js/main/index.js",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "nodemon --exec 'electron .'",
"dist": "electron-builder"
},
"homepage": ".",
"keywords": [
"Electron",
"Desktop App"
],
"author": "NotABot Ltd <contact@notabot.com>",
"contributors": [
{
"name": "Not A Bot",
"email": "nab@notabot.com"
}
],
"license": "ISC",
"dependencies": {
"desandro-matches-selector": "^2.0.2",
"electron-context-menu": "^1.0.0",
"electron-is": "^3.0.0",
"fix-path": "^3.0.0",
"isotope-layout": "^3.0.6",
"jquery": "^3.5.0",
"jquery-bridget": "^2.0.1"
},
"build": {
"appId": "com.test.timely",
"productName": "Timely",
"linux": {
"target": "deb",
"category": "System"
}
},
"devDependencies": {
"electron": "^8.1.1",
"electron-builder": "^22.6.0"
}
}
HTML:
<html>
<head>
<title>Timely</title>
</head>
<body>
<button onclick="displayTime()">Display Time</button>
<textarea rows="20" cols="90" id="command-output" disabled="true"></textarea>
<script>
const {app} = require('electron');
function displayTime(){
console.log("button clicked");
let cmd = `bash app/terminal_scripts/timer.sh`;
let completeMessage = 'This is the message';
backgroundProcess(cmd, completeMessage);
}
function getCommandOutput() { return document.getElementById("command-output"); };
function getStatus() { return document.getElementById("status"); };
function appendOutput(msg) { getCommandOutput().value += (msg+'\n'); };
function setStatus(msg) { getStatus().innerHTML = msg; };
function backgroundProcess(cmd, completeMessage){
const process = require('child_process');
var child = process.execFile(cmd, [] , {shell: true} );
appendOutput("Processing......");
child.on('error', function(err) {
appendOutput('stderr: '+err );
});
child.stdout.on('data', function (data) {
appendOutput(data);
});
child.stderr.on('data', function (data) {
appendOutput(data );
});
return new Promise((resolve, reject) => {
child.on('close', function (code) {
console.log(`code is: ${code}`);
if (code == 0){
setStatus(completeMessage);
resolve(1);
}
else{
setStatus('Exited with error code ' + code);
resolve(-1);
}
});
});
}
</script>
</body>
</html>
Bash 脚本:
#!/bin/bash
timer="$(date)"
echo "$timer"
Permission is set 777 for this shell file
平台信息:
- OS:Ubuntu 18.04.4 LTS
- NodeJS: 13.6.0
- NPM:6.14.5
- 电子:8.1.1
- 电子生成器:22.6.0
index.js
const {app, BrowserWindow, Menu, Tray, ipcMain, MenuItem} = require('electron');
const path = require('path');
const contextMenu = require('electron-context-menu');
let splashWindow;
function createMainWindow(){
mainWindow = new BrowserWindow({
minHeight: 700,
minWidth: 800,
webPreferences: {
nodeIntegration: true,
webviewTag: true
},
show: false
});
//For dev only
// mainWindow.webContents.openDevTools();
mainWindow.loadFile('app/renderer/index.html');
mainWindow.maximize();
}
app.on('ready', () =>{
createMainWindow();
});
我在 app
目录旁边创建了一个名为 termainal_scripts
的新目录。
在里面,我有我的 bash 文件 timer.sh.
我想出了如何在 path.join()
中使用 process.resourcesPath
在生产环境中执行 shell 脚本。
所以,让固定路径为:
let fixedURL = path.join(process.resourcesPath, '/terminal_scripts/');
那么要执行的命令将是:
let cmd = `${fixedURL}timer.sh`
另一种方法是将苍蝇移动到 app
目录之外的新目录,并将其命名为 extraResources
。
在该目录中,您可以添加所有 bash 文件,对于生产,您可以使用以下方法。
let urlPath = path.join(process.resourcesPath, '/extraResources/')
然后使用let cmd = `${urlPath}timer.sh`
;