Permalink
Browse files

first implementation

  • Loading branch information...
0 parents commit 9859221aea4799d838bb747de31b2e330b4eae35 @rhysd committed Nov 12, 2016
Showing with 188 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +59 −0 app/main.js
  3. +12 −0 app/package.json
  4. +43 −0 bin/cli.js
  5. +38 −0 index.js
  6. +35 −0 package.json
@@ -0,0 +1 @@
+node_modules
@@ -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));
+});
@@ -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"
+}
@@ -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');
+}
@@ -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;
@@ -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.