Skip to content

Instantly share code, notes, and snippets.

@Papipo
Created October 7, 2015 07:41
Show Gist options
  • Select an option

  • Save Papipo/6df42f5f22395902ce77 to your computer and use it in GitHub Desktop.

Select an option

Save Papipo/6df42f5f22395902ce77 to your computer and use it in GitHub Desktop.
/*
* build.js
*/
var browserify = require('browserify'),
watchify = require('watchify'),
Q = require('q'),
path = require('path'),
fs = require('fs-extra'),
chokidar = require('chokidar'),
less = require('less'),
w, // watchify instance
watcher, // chokidar watcher instance
stylesWatcher,
src = path.join(__dirname, '../src/app.js'),
srcPath = path.join(__dirname, '../src'),
styles = path.join(__dirname, '../src/styles'),
settingsJSON = path.join(__dirname, 'settings.json'),
out = path.join(__dirname, '../www/main.js'),
www = path.join(__dirname, '../www'),
fw7less = path.join(__dirname, '../node_modules/framework7/src/less'),
fw7css = path.join(__dirname, '../node_modules/framework7/dist/css'),
fw7js = path.join(__dirname, '../node_modules/framework7/src/js'),
fontAwesomeFonts = path.join(__dirname, '../node_modules/font-awesome/fonts');
fontAwesomeLess = path.join(__dirname, '../node_modules/font-awesome/less');
function log(o) { if(o) console.log('- browserify - ' + o); }
function rejectOnError(d) {
return function (err) { log(err); if(err) d.reject(err); };
}
function bundle(conf) {
var defer = Q.defer(),
// b = browserify(watchify.args);
b = browserify({paths: [srcPath + "/js", fw7js]});
if(fs.existsSync(settingsJSON)) fs.unlinkSync(settingsJSON);
if(fs.existsSync(out)) fs.unlinkSync(out);
fs.writeFileSync(settingsJSON, JSON.stringify(conf), null, 2);
var ws = fs.createWriteStream(out);
b.add(src)
.exclude('settings')
.require(settingsJSON, { expose : 'settings' })
.bundle()
.pipe(ws);
b.on('error', rejectOnError(defer));
ws.on('finish', function() { ws.end(); defer.resolve(b); });
fs.copy(srcPath + "/index.html", www + "/index.html", function(err) {
if (err) {
return console.error(err);
}
});
fs.copy(fontAwesomeFonts, www + "/fonts", function (err) {
if (err) {
return console.error(err);
}
});
fs.copy(srcPath + "/fonts", www + "/fonts", function(err) {
if (err) {
return console.error(err);
}
});
fs.copy(srcPath + "/images", www + "/images", function(err) {
if (err) {
return console.error(err);
}
});
less.render(fs.readFileSync(styles + '/app.less', 'utf-8').toString(), {filename: path.resolve(styles + '/app.less'), paths: [fontAwesomeLess, fw7less, fw7css]}, function(e, out) {
if (e) {
console.log(" - less error - ");
console.log(e);
} else {
console.log("- update styles -");
fs.writeFileSync(www + '/app.css', out.css);
}
});
return defer.promise;
}
function run(conf, f){
return bundle(conf).then(function (b) {
var w = watchify(b);
b.bundle(function () { w.on('log', log); });
w.on('update', function () {
var ws = fs.createWriteStream(out);
w.bundle().pipe(ws);
ws.on('finish', function() { ws.end(); f(out); });
});
return w;
});
}
module.exports.build = function build(platform, localSettings, config) {
return bundle(localSettings.configurations[platform][config]);
};
module.exports.watch = function watch(f, localSettings, platform, config, confEmitter) {
run(localSettings.configurations[platform][config], f).then(function (bw) {
indexHTMLwatcher = chokidar.watch(srcPath + "/index.html");
watcher = chokidar.watch(www, { ignored: /main\.js/, persistent: true });
stylesWatcher = chokidar.watch(styles, {persistent: true});
setTimeout(function () {
watcher.on('all', function (evt, p) { f(p); });
indexHTMLwatcher.on("change", function() {
fs.copy(srcPath + "/index.html", www + "/index.html", function(err) {
if (err) {
return console.error(err);
}
});
});
stylesWatcher.on('change', function() {
less.render(fs.readFileSync(styles + '/app.less', 'utf-8').toString(), {filename: path.resolve(styles + '/app.less'), paths: [fontAwesomeLess, fw7less, fw7css]}, function(e, out) {
if (e) {
console.log(" - less error - ");
console.log(e);
} else {
console.log("- update styles -");
fs.writeFileSync(www + '/app.css', out.css);
}
});
});
}, 4000);
confEmitter.on('change', function (conf) {
fs.writeFileSync(settingsJSON, JSON.stringify(conf), null, 2);
});
});
};
module.exports.close = function () {
if(w) w.close();
if(watcher) watcher.close();
if(stylesWatcher) stylesWatcher.close();
};
module.exports.test = function (platform, settings, config, caps, appium) {
var Mocha = require('mocha'),
mocha = new Mocha(),
defer = Q.defer(),
settingsPath = path.resolve(__dirname, '../test/settings.json'),
testSettings = {
caps: caps,
appium: appium,
platform: platform,
localSettings: settings,
configuration: config
};
fs.writeFileSync(settingsPath, JSON.stringify(testSettings), null, 2);
fs.readdirSync(path.resolve(__dirname, '../test')).filter(function(file){
return file.substr(-3) === '.js';
}).forEach(function(file){
mocha.addFile(path.join(__dirname, '../test', file));
});
mocha.run(function(failures) {
return failures ? defer.reject(failures + ' failures!') : defer.resolve();
});
return defer.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment