以JS文件作为参数启动Electron

时间:2019-05-28 14:19:20

标签: electron

我安装electron quick start并运行

node_modules/.bin/electron --help

它告诉我:

A path to an Electron app may be specified. It must be one of the following:
  - index.js file.
  - Folder containing a package.json file.
  - Folder containing an index.js file.
  - .html/.htm file.
  - http://, https://, or file:// URL.

运行

node_modules/.bin/electron .

(即选项“包含package.json文件的文件夹”)可以正常工作。 package.json指向main.js,其外观如下:

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "^4.1.4"
  }
}

我认为运行该命令:

node_modules/.bin/electron main.js

应该也可以工作,但是不能-仅显示Electron的“虚拟”屏幕。

dummy screen

如何在命令行上指定要运行的JavaScript?

1 个答案:

答案 0 :(得分:0)

原来可以正确执行JavaScript –问题是如何解析主HTML文件的路径。解决方案:替换:

mainWindow.loadFile('index.html')

具有:

const arg = process.argv[1]  // better command line parsing might be needed
const dir = fs.lstatSync(arg).isDirectory() ? arg : path.resolve(arg, '..')
mainWindow.loadFile(path.resolve(dir, 'index.html'))