Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9859221
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <lin90162@yahoo.co.jp>", | ||
"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" | ||
} | ||
} |