UNPKG

chext

Version:

CHrome EXtension Tester

133 lines (109 loc) 2.98 kB
var fs = require("fs") var TestState = require("./TestState.js") var events = require("events"); var watchify = require('watchify'); var connect = require('gulp-connect'); var browserify = require('browserify'); var source = require('vinyl-source-stream'); var buffer = require('vinyl-buffer'); var assign = require('lodash.assign'); var tinylr = require("tiny-lr") var gulp = require("gulp") var WebSocket = require("ws") // from http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js function copyFile(source, target, cb) { var cbCalled = false; try{ var rd = fs.createReadStream(source); rd.on("error", function(err) { done(err); }); var wr = fs.createWriteStream(target); wr.on("error", function(err) { done(err); }); wr.on("close", function(ex) { done(); }); rd.pipe(wr); function done(err) { console.log("DONE", err) if (!cbCalled) { cb(err); cbCalled = true; } } } catch(e){ console.log("try", e) } } function Chext (){ this._events = new events.EventEmitter() var self = this; this._server = connect.server({ root: __dirname, port: 9999 }); this._lr = tinylr() this._lr.listen(35729); this._wss = new WebSocket.Server({port:10101}); this._wss.on('connection',function(socket){ socket.on('message',function(msg){ console.log("got test results from browser", msg) var testResult = JSON.parse(msg); TestState.insert(testResult); if (TestState.complete){ self._events.emit("testing_complete", TestState.All()) TestState.clear() } }) }) return this; } Chext.prototype.on = function(a1, a2, a3){ this._events.on(a1,a2,a3) } function trimPath(path){ if (path.indexOf("./") === 0 ){ return path.substr(1) } else if (path.indexOf("/") !== 0){ return "/" + path; } } Chext.prototype.watchify = function(paths){ var self = this; var customOpts = { entries: paths.map(function(path){ return process.cwd() + trimPath(path); }), debug: true }; console.log("entries", customOpts.entries) var opts = assign({}, watchify.args, customOpts); var b = watchify(browserify(opts)); function bundle() { console.log("bundle event") return b.bundle() // log errors if they happen .on('error', console.log.bind(console.log, 'Browserify Error')) .pipe(source('test.bundle.js')) // optional, remove if you don't need to buffer file contents .pipe(buffer()) .pipe(gulp.dest(__dirname + '/mocha/')) .on('end',function(){ console.log("bundle end!") self.refresh() }); } b.on('update', bundle); // on any dep update, runs the bundler b.on('log', console.log); bundle() } Chext.prototype.refresh = function(){ this._lr.changed({ body: { files: [__dirname + "/mocha/test.bundle.js"] } }); } module.exports = Chext;