Create a gist now

Instantly share code, notes, and snippets.

@canuckistani /install-addon forked from anonymous/-
Last active Aug 23, 2017

What would you like to do?
A node script that abuses child_process to run cfx and post the resulting xpi to Firefox. Requires: *nix, a working installation of the Add-on SDK ( with cfx on your PATH ) and a recent version of Firefox with the 'Extension Auto-installer' extension installed.
#!/usr/bin/env node
var util = require('util'),
fs = require('fs'),
path = require('path'),
http = require('http'),
_ = require('lodash'),
url = require('url');
var DEBUG = false,
host = 'http://127.0.0.1:8888/';
if (!module.parent) {
/*
1. get some info from package.json
2. run cfx
3. on completion, post the xpi file to Firefox
*/
var xpi = false;
var cwd = process.cwd();
var spawn = require('child_process').spawn;
var optimist = require('optimist');
var argv = optimist
.usage('addon-install -h http://localhost:888/')
.default({h: host})
.describe('h', 'The host and port that the extension auto-installer is listening on')
.alias('p', 'port')
.argv;
fs.readFile(path.join(cwd, 'package.json'),
{encoding: 'utf8'},
function(err, buffer) {
//
if (err) throw err;
// console.log(buffer);
var package_data = JSON.parse(buffer),
xpi_name = package_data.name + '.xpi';
console.log("Building %s", xpi_name);
var builder = spawn('cfx', ['xpi'], {cwd: cwd});
builder.stdout.on('data', function(data) {
console.log("cfx> %s", data);
});
builder.stderr.on('data', function(data) {
console.log("cfx> %s", data);
});
builder.on('error', function(err) {
console.log([].slice.call(arguments));
});
builder.on('close', function(err, result) {
post_file = spawn('wget', ['--post-file='+xpi_name, host], {cwd: cwd});
post_file.stderr.on('data', function(data) {
console.log("wget> %s", data);
});
post_file.stdout.on('data', function(data) {
console.log("wget> %s", data);
});
post_file.on('close', function(err, result) {
console.log("done?");
});
});
});
}

duzun commented May 9, 2015

What if we use http module instead of wget?

Here is an example:

var req = http.request(host, function (res) {
    console.log('status:', res.statusCode, res.statusMessage);
    res.on('data', function (d) {
        process.stdout.write(d);
    });
});

var xpi = fs.createReadStream(path.resolve(cwd, xpi_name));
xpi.pipe(req);

req.on('error', function (err) {
    console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment