|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var path = require('path'); |
| 5 | + |
| 6 | +function exists (directory, callback) { |
| 7 | + let root = '/'; |
| 8 | + let dirs = directory.split('/'); |
| 9 | + function one () { |
| 10 | + root = path.join(root, dirs.shift()); |
| 11 | + fs.stat(root, (e) => { |
| 12 | + if (!e && dirs.length) { |
| 13 | + one(); |
| 14 | + } |
| 15 | + else if (e && e.code === 'ENOENT') { |
| 16 | + fs.mkdir(root, (e) => { |
| 17 | + if (e) { |
| 18 | + callback(e); |
| 19 | + } |
| 20 | + else if (dirs.length) { |
| 21 | + one(); |
| 22 | + } |
| 23 | + else { |
| 24 | + callback(); |
| 25 | + } |
| 26 | + }); |
| 27 | + } |
| 28 | + else { |
| 29 | + callback(e); |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | + one(); |
| 34 | +} |
| 35 | + |
| 36 | +var dir = path.join('/usr/share', 'com.add0n.node'); |
| 37 | +var name = 'com.add0n.node'; |
| 38 | +var ids = require('./config.js').ids; |
| 39 | + |
| 40 | +function manifest (root, type, callback) { |
| 41 | + exists(root, (e) => { |
| 42 | + if (e) { |
| 43 | + throw e; |
| 44 | + } |
| 45 | + let origins; |
| 46 | + if (type === 'chrome') { |
| 47 | + origins = '"allowed_origins": ' + JSON.stringify(ids.chrome.map(id => 'chrome-extension://' + id + '/')); |
| 48 | + } |
| 49 | + else { |
| 50 | + origins = '"allowed_extensions": ' + JSON.stringify(ids.firefox); |
| 51 | + } |
| 52 | + fs.writeFile(path.join(root, name + '.json'), `{ |
| 53 | + "name": "${name}", |
| 54 | + "description": "Node Host for Native Messaging", |
| 55 | + "path": "${path.join(dir, 'run.sh')}", |
| 56 | + "type": "stdio", |
| 57 | + ${origins} |
| 58 | + }`, (e) => { |
| 59 | + if (e) { |
| 60 | + throw e; |
| 61 | + } |
| 62 | + callback(); |
| 63 | + }); |
| 64 | + |
| 65 | + }); |
| 66 | +} |
| 67 | +function application (callback) { |
| 68 | + exists(dir, (e) => { |
| 69 | + if (e) { |
| 70 | + throw e; |
| 71 | + } |
| 72 | + let isNode = process.argv[2] !== '--add_node'; |
| 73 | + let run = isNode ? `#!/bin/bash\n${process.argv[2]} host.js` : '#!/bin/bash\n./node host.js'; |
| 74 | + fs.writeFile(path.join(dir, 'run.sh'), run, (e) => { |
| 75 | + if (e) { |
| 76 | + throw e; |
| 77 | + } |
| 78 | + fs.chmodSync(path.join(dir, 'run.sh'), '0755'); |
| 79 | + if (!isNode) { |
| 80 | + fs.createReadStream('../node').pipe(fs.createWriteStream(path.join(dir, 'node'))); |
| 81 | + fs.chmodSync(path.join(dir, 'node'), '0755'); |
| 82 | + } |
| 83 | + fs.createReadStream('host.js').pipe(fs.createWriteStream(path.join(dir, 'host.js'))); |
| 84 | + fs.createReadStream('messaging.js').pipe(fs.createWriteStream(path.join(dir, 'messaging.js'))); |
| 85 | + callback(); |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
| 89 | +function chrome (callback) { |
| 90 | + if (ids.chrome.length) { |
| 91 | + manifest('/etc/opt/chrome/native-messaging-hosts', 'chrome', callback); |
| 92 | + console.error('Chrome Browser is supported'); |
| 93 | + } |
| 94 | + else { |
| 95 | + callback(); |
| 96 | + } |
| 97 | +} |
| 98 | +function firefox (callback) { |
| 99 | + if (ids.firefox.length) { |
| 100 | + manifest('/usr/lib/mozilla/native-messaging-hosts', 'firefox', callback); |
| 101 | + console.error('Firefox Browser is supported'); |
| 102 | + } |
| 103 | + else { |
| 104 | + callback(); |
| 105 | + } |
| 106 | +} |
| 107 | +chrome(() => firefox(() => { |
| 108 | + application(() => { |
| 109 | + console.error('Native Host is installed in', dir); |
| 110 | + console.error('>> Application is ready to use <<'); |
| 111 | + }); |
| 112 | +})); |
0 commit comments