diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/app/main.js b/app/main.js new file mode 100644 index 0000000..8658c30 --- /dev/null +++ b/app/main.js @@ -0,0 +1,59 @@ +const electron = require('electron'); +const app = electron.app; +const BrowserWindow = electron.BrowserWindow; +const fs = require('fs'); + +win = null; + +function getUrlToOpen(argv) { + const url = argv[2]; + if (url.startsWith('https://') || url.startsWith('http://') || url.startsWith('file://')) { + return url; + } + + try { + if (fs.statSync(url).isFile()) { + return 'file://' + url; + } + } catch (e) { + return ''; + } + + return ''; +} + +const shouldQuit = app.makeSingleInstance((argv, workdir) => { + if (win !== null) { + if (win.isMinimized(0)) { + win.restore(); + } + win.loadURL(getUrlToOpen(argv)); + win.focus(); + } +}); + +if (shouldQuit) { + app.quit(); +} + +app.once('ready', () => { + win = new BrowserWindow({ + width: 1000, + height: 800, + show: false, + webPreferrences: { + nodeIntegration: false, + sandbox: true + } + }); + + win.once('closed', () => { + win = null; + }) + + win.once('ready-to-show', () => { + win.show(); + }); + + win.loadURL(getUrlToOpen(process.argv)); +}); diff --git a/app/package.json b/app/package.json new file mode 100644 index 0000000..c7b332f --- /dev/null +++ b/app/package.json @@ -0,0 +1,12 @@ +{ + "name": "electron-open-app", + "version": "1.0.0", + "private": true, + "description": "", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT" +} diff --git a/bin/cli.js b/bin/cli.js new file mode 100755 index 0000000..49f87f3 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,43 @@ +#! /usr/bin/env node + +const openElectron = require('..'); + +function parseArgv(argv) { + if (argv.indexOf('--help') !== -1) { + return { + help: true + }; + } + + const fallback_idx = argv.indexOf('--with-fallback'); + if (fallback_idx >= 0) { + argv.splice(fallback_idx, 1); + } + + if (argv.length === 0) { + return { + error: 'target to open was not found.' + }; + } + + return { + target: argv[0], + fallback: fallback_idx >= 0 + }; +} + +const parsed = parseArgv(process.argv.slice(2)); +if (parsed.help) { + process.stderr.write( +` +$ electron-open {something} [--help|--with-fallback] +` + ); + process.exit(0); +} + +try { + openElectron(parsed); +} catch (e) { + process.stderr.write('Error: ' + e.message + ' Please see --help for more detail.\n'); +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..912854f --- /dev/null +++ b/index.js @@ -0,0 +1,38 @@ +const path = require('path'); +const child_process = require('child_process'); +const openFallback = require('open'); + +let electron; +try { + electron = require('electron'); +} catch (e) { + electron = null; +} + +function open(parsed) { + if (typeof parsed === 'string') { + parsed = { target: parsed }; + } + + if (parsed.error) { + throw new Error(parsed.error); + } + + if (electron === null) { + if (parsed.fallback) { + openFallback(parsed.target); + } else { + throw new Error('Electron binary was not found. Please ensure to install "electron" package in your machine.'); + } + } + + child_process.spawn(electron, [ + path.join(__dirname, 'app'), + parsed.target + ], { + stdio: 'ignore', + detached: true + }).unref(); +} + +module.exports = open; diff --git a/package.json b/package.json new file mode 100644 index 0000000..c65e275 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "electron-open", + "version": "0.0.0", + "description": "open something with Electron window", + "main": "index.js", + "bin": { + "electron-open": "bin/cli.js" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rhysd/electron-open.git" + }, + "keywords": [ + "electron", + "open", + "url", + "gui", + "window" + ], + "author": "rhysd ", + "license": "MIT", + "bugs": { + "url": "https://github.com/rhysd/electron-open/issues" + }, + "homepage": "https://github.com/rhysd/electron-open#readme", + "dependencies": { + "open": "0.0.5" + }, + "peerDependencies": { + "electron": "^1.4.6" + } +}