Skip to content

Commit

Permalink
first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 12, 2016
0 parents commit 9859221
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
59 changes: 59 additions & 0 deletions 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));
});
12 changes: 12 additions & 0 deletions 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"
}
43 changes: 43 additions & 0 deletions 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');
}
38 changes: 38 additions & 0 deletions 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;
35 changes: 35 additions & 0 deletions 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 <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"
}
}

0 comments on commit 9859221

Please sign in to comment.