<< November 2015 | Home | January 2016 >>

Using NodeJS to Build a Firefox XPI (Zip) File

I have been a long time JavaScript programmer, but of course this is mainly for development of client-side code running in a web-browser.  Node.js, the server-side JavaScript engine, has been around for a few years now, but I have only had limited opportunity to put it to use.  I really like the idea of using JavaScript to write utilities and automate desktop tasks.  In the past I have turned to shell scripts or Python for these kind of tasks, but Node.js has become quite robust, and it is nice to be able to use the same, familiar language everywhere.

Below is a nifty little build script I wrote in JavaScript using Node.js which creates a zip file named "mine.xpi" for use as a Firefox extension.  It leverages the archiver package available using the npm package manager...

var archiver = require('archiver'),
    fs = require('fs'),
    path = require('path'),

    basePath = path.resolve('mine'),

    walk = function(dir, done) {
        var results = [];
        fs.readdir(dir, function(err, list) {
            if (err) return done(err);
            var pending = list.length;
            if (!pending) return done(null, results);
            list.forEach(function(file) {
                file = path.resolve(dir, file);
                fs.stat(file, function(err, stat) {
                    if (stat && stat.isDirectory()) {
                        walk(file, function(err, res) {
                            results = results.concat(res);
                            if (!--pending) done(null, results);
                        });
                    } else {
                        results.push(file);
                        if (!--pending) done(null, results);
                    }
                });
            });
        });
    };

walk(basePath, function(err, results) {
    var archive = archiver.create('zip', {}),
        backslashRegex = /\\/g,
        baseRegex = new RegExp('^' + basePath.replace(backslashRegex, '\\\\')),
        ignoreRegex = /\.bak$/,
        output = fs.createWriteStream('mine.xpi');

    output.on('close', function() {
        console.log(archive.pointer() + ' total bytes');
        console.log('archiver has been finalized and the output file descriptor has closed.');
    });

    archive.on('error', function(err) {
        throw err;
    });

    archive.pipe(output);

    if (err) throw err;
    results.forEach(function(result) {
        if (!result.match(ignoreRegex)) {
            console.log('ADDING:', result.replace(baseRegex, '').replace(backslashRegex, '/'));
            archive.append(fs.createReadStream(result), {name:result.replace(baseRegex, '').replace(backslashRegex, '/')});
        }
    });
    console.log('FINALIZING');
    archive.finalize();
});
Social Bookmarks :  Add this post to Slashdot    Add this post to Digg    Add this post to Reddit    Add this post to Delicious    Add this post to Stumble it    Add this post to Google    Add this post to Technorati    Add this post to Bloglines    Add this post to Facebook    Add this post to Furl    Add this post to Windows Live    Add this post to Yahoo!

Export this post as PDF document  Export this post to PDF document