Skip to content

Commit

Permalink
removing duplicated files
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-portmen committed Dec 6, 2016
1 parent 74bcd1a commit 102cd84
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 941 deletions.
69 changes: 69 additions & 0 deletions host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

var spawn = require('child_process').spawn;

var config = {
version: '0.1.0',
isWin: /^win/.test(process.platform)
};

function observe (msg, push, done) {
if (msg.cmd === 'version') {
push({
version: config.version
});
done();
}
else if (msg.cmd === 'echo') {
push(msg);
done();
}
else if (msg.cmd === 'spawn') {
let sp = spawn(msg.command, msg.arguments);
sp.stdout.on('data', stdout => push({stdout}));
sp.stderr.on('data', stderr => push({stderr}));
sp.on('close', (code) => {
push({code});
done();
});
}
else if (msg.cmd === 'exec') {
let sp = spawn(msg.command, msg.arguments);
let stderr = '', stdout = '';
sp.stdout.on('data', data => stdout += data);
sp.stderr.on('data', data => stderr += data);
sp.on('close', (code) => {
push({
code,
stderr,
stdout
});
done();
});
}
else if (msg.cmd === 'env') {
push({
env: process.env
});
done();
}
else {
push({
error: 'cmd is unknown',
cmd: msg.cmd
});
done();
}
}
/* message passing */
var nativeMessage = require('./messaging');

var input = new nativeMessage.Input();
var transform = new nativeMessage.Transform(observe);
var output = new nativeMessage.Output();

process.stdin
.pipe(input)
.pipe(transform)
.pipe(output)
.pipe(process.stdout);
119 changes: 119 additions & 0 deletions install-linux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';

var fs = require('fs');
var path = require('path');

function exists (directory, callback) {
let root = '/';
let dirs = directory.split('/');
function one () {
root = path.join(root, dirs.shift());
fs.stat(root, (e) => {
if (!e && dirs.length) {
one();
}
else if (e && e.code === 'ENOENT') {
fs.mkdir(root, (e) => {
if (e) {
callback(e);
}
else if (dirs.length) {
one();
}
else {
callback();
}
});
}
else {
callback(e);
}
});
}
one();
}

var dir = path.join('/usr/share', 'com.add0n.node');
var name = 'com.add0n.node';
var ids = {
chrome: [
'lmeddoobegbaiopohmpmmobpnpjifpii', // open in Firefox (Chrome)
'agaecbnjediafcdopcfidcdiponjlmnk', // open in Explorer (Chrome)
],
firefox: [
'{5610edea-88c1-4370-b93d-86aa131971d1}', // open in Explorer
]
};
function manifest (root, type, callback) {
exists(root, (e) => {
if (e) {
throw e;
}
let origins;
if (type === 'chrome') {
origins = '"allowed_origins": ' + JSON.stringify(ids.chrome.map(id => 'chrome-extension://' + id + '/'));
}
else {
origins = '"allowed_extensions": ' + JSON.stringify(ids.firefox);
}
fs.writeFile(path.join(root, name + '.json'), `{
"name": "${name}",
"description": "Node Host for Native Messaging",
"path": "${path.join(dir, 'run.sh')}",
"type": "stdio",
${origins}
}`, (e) => {
if (e) {
throw e;
}
callback();
});

});
}
function application (callback) {
exists(dir, (e) => {
if (e) {
throw e;
}
let isNode = process.argv[2] !== '--add_node';
let run = isNode ? `#!/bin/bash\n${process.argv[2]} host.js` : '#!/bin/bash\n./node host.js';
fs.writeFile(path.join(dir, 'run.sh'), run, (e) => {
if (e) {
throw e;
}
fs.chmodSync(path.join(dir, 'run.sh'), '0755');
if (!isNode) {
fs.createReadStream('../node').pipe(fs.createWriteStream(path.join(dir, 'node')));
fs.chmodSync(path.join(dir, 'node'), '0755');
}
fs.createReadStream('host.js').pipe(fs.createWriteStream(path.join(dir, 'host.js')));
fs.createReadStream('messaging.js').pipe(fs.createWriteStream(path.join(dir, 'messaging.js')));
callback();
});
});
}
function chrome (callback) {
if (ids.chrome.length) {
manifest('/etc/opt/chrome/native-messaging-hosts', 'chrome', callback);
console.error('Chrome Browser is supported');
}
else {
callback();
}
}
function firefox (callback) {
if (ids.firefox.length) {
manifest('/usr/lib/mozilla/native-messaging-hosts', 'firefox', callback);
console.error('Firefox Browser is supported');
}
else {
callback();
}
}
chrome(() => firefox(() => {
application(() => {
console.error('Native Host is installed in', dir);
console.error('>> Application is ready to use <<');
});
}));
91 changes: 91 additions & 0 deletions install-windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

var fs = require('fs');
var path = require('path');

function exists (directory, callback) {
fs.stat(directory, (e) => {

if (e && e.code === 'ENOENT') {
fs.mkdir(directory, callback);
}
else {
callback(e);
}
});
}

var dir = path.join(process.argv[2], 'com.add0n.node');
var name = 'com.add0n.node';
var ids = {
chrome: [
'lmeddoobegbaiopohmpmmobpnpjifpii', // open in Firefox (Chrome)
'agaecbnjediafcdopcfidcdiponjlmnk', // open in Explorer (Chrome)
],
firefox: [
'{5610edea-88c1-4370-b93d-86aa131971d1}', // open in Explorer
]
};

function manifest (type, callback) {
exists(dir, (e) => {
if (e) {
throw e;
}
let origins;
if (type === 'chrome') {
origins = '"allowed_origins": ' + JSON.stringify(ids.chrome.map(id => 'chrome-extension://' + id + '/'));
}
else {
origins = '"allowed_extensions": ' + JSON.stringify(ids.firefox);
}
fs.writeFile(path.join(dir, 'manifest-' + type + '.json'), `{
"name": "${name}",
"description": "Node Host for Native Messaging",
"path": "run.bat",
"type": "stdio",
${origins}
}`, (e) => {
if (e) {
throw e;
}
callback();
});
});
}
function application (callback) {
fs.writeFile(path.join(dir, 'run.bat'), `@echo off\n\n"%~dp0node.exe" "%~dp0host.js"`, (e) => {
if (e) {
throw e;
}
fs.createReadStream('..\\node.exe').pipe(fs.createWriteStream(path.join(dir, 'node.exe')));
fs.createReadStream('host.js').pipe(fs.createWriteStream(path.join(dir, 'host.js')));
fs.createReadStream('messaging.js').pipe(fs.createWriteStream(path.join(dir, 'messaging.js')));
callback();
});
}

function chrome (callback) {
if (ids.chrome.length) {
manifest('chrome', callback);
console.error('Chrome Browser is supported');
}
else {
callback();
}
}
function firefox (callback) {
if (ids.firefox.length) {
manifest('firefox', callback);
console.error('Firefox Browser is supported');
}
else {
callback();
}
}
chrome(() => firefox(() => {
application(() => {
console.error('Native Host is installed in', dir);
console.error('>> Application is ready to use <<');
});
}));
12 changes: 12 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

cd ./app

if type node 2>/dev/null; then
echo Installer is using your system node.js
sudo node install.js `which node`
else
echo Installer is using the attached node.js
sudo ../node install.js --add_node
fi

69 changes: 0 additions & 69 deletions linux/app/host.js

This file was deleted.

1 change: 1 addition & 0 deletions linux/app/host.js
Loading

0 comments on commit 102cd84

Please sign in to comment.