patriot
Version:
Patriot command-line interface and node.js library.
147 lines (115 loc) • 4.39 kB
JavaScript
/*!
* Module dependencies.
*/
var Command = require("./util/command"),
project = require("./util/project"),
util = require("util"),
fs = require("fs-extra"),
sh = require("execSync"),
build_date = new Date().format("yyyy-MM-dd_hhmm");
/*!
* Command setup.
*/
module.exports = {
create: function (patriot) {
return new BuildCommand(patriot);
}
};
function BuildCommand(patriot) {
return Command.apply(this, arguments);
}
util.inherits(BuildCommand, Command);
/**
* Build an App.
*
* Construct and optimize the www-dev folder into www to use
* in native packages.
*
* Options:
*
* - `options` {Object}
* - `folder` {String} is a list of folders (limited to 1).
* - [`callback`] {Function} is triggered after completion.
* - `e` {Error} is null unless there is an error.
*
* Returns:
*
* {Patriot} for chaining.
*/
BuildCommand.prototype.run = function (options, callback) {
// require options
if (!options) throw new Error("requires option parameter");
// optional parameters
options.folder = options.folder || "www-dev";
callback = callback || function () {
};
// build app
this.execute(options, callback);
return this.patriot;
};
/*!
* Execute command.
*/
BuildCommand.prototype.execute = function (options, callback) {
var self = this,
configData,
text,
lines,
bootstrapData,
depsData,
result;
// change to project directory and delegate errors
if (!project.cd({ emitter: self.patriot, callback: callback })) return;
// announce build id
self.patriot.emit("log", "starting build id: " + build_date);
// Copy www-dev into build/www
fs.copySync("www-dev/", "build/www");
self.patriot.emit("log", "created copy at build/www");
// Read the contents of the file into memory.
configData = fs.readFileSync('build/www/config.prod.js');
// configData is a Buffer, convert to string.
text = configData.toString().replace(/([^\[]+)(\[)([^\]]+)([^¬]+)/, "$3").replace(/\"/g, "").replace(/ /g, "");
// Break up the file into lines.
lines = text.split(',');
lines.forEach(function (line) {
var moduleData,
pathText,
pathLines;
moduleData = fs.readFileSync("build/www/" + line + ".js");
pathText = moduleData.toString().match(/"[^]*\/\/ @build/g).toString().replace(/ /g, "").replace(/\/\/@build/g, "").replace(/^\s*[\r\n|\r|\n]/gm, "");
// Break up the file into lines.
pathLines = pathText.split(",");
pathLines.forEach(function (line) {
fs.appendFileSync('build/www/deps.tmp', " " + line + ",");
});
});
bootstrapData = fs.readFileSync("build/www/bootstrap.js");
depsData = fs.readFileSync("build/www/deps.tmp");
result = bootstrapData.toString().replace(/\/\/ @Patriot/g, depsData.toString()).replace("\" + ENV","mobile\"");
fs.writeFileSync("build/www/bootstrap.js", result);
self.patriot.emit("log", "module configuration inspection finished");
// Optimize the javascript in the public folder and
// copy it to the public_build folder.
self.patriot.emit("log", "optimizing the source code...");
var result = sh.exec("node build/www/vendor/require/r-2.1.9.js -o build/www/config.build.js");
//console.log('stdout + stderr ' + result.stdout);
fs.removeSync("www/");
fs.removeSync("build/www");
fs.removeSync("build/www-built/src");
fs.removeSync("build/www-built/vendor/common");
fs.removeSync("build/www-built/vendor/hybreed");
fs.removeSync("build/www-built/vendor/jquery");
fs.removeSync("build/www-built/vendor/mobile");
fs.removeSync("build/www-built/vendor/require");
fs.removeSync("build/www-built/vendor/test");
fs.removeSync("build/www-built/build.txt");
fs.removeSync("build/www-built/config.build.js");
fs.removeSync("build/www-built/config.test.js");
fs.removeSync("build/www-built/config.prod.js");
fs.removeSync("build/www-built/tests.html");
fs.removeSync("build/www-built/deps.tmp");
// Copy build/www-built into build/{build_date}
fs.renameSync("build/www-built", "build/build_" + build_date);
fs.copySync("build/build_" + build_date, "www/");
self.patriot.emit("log", "hybreed project compiled into www folder");
};