Skip to content

Commit

Permalink
add build for client/now.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Qian authored and ericz committed Jan 16, 2012
1 parent 501e393 commit 176050b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Makefile
@@ -0,0 +1,3 @@
build:
@node ./bin/build_now.js;

44 changes: 44 additions & 0 deletions bin/build_now.js
@@ -0,0 +1,44 @@
/*
* Module dependencies.
*/
var fs = require('fs')
, package = JSON.parse(fs.readFileSync(__dirname + '/../package.json'))
, jsp = require("uglify-js").parser
, pro = require("uglify-js").uglify;



var template = '/*! now.%ext% build:' + package.version + ', %type%. Copyright(c) 2011 Flotype <team@flotype.com> MIT Licensed */\n'
, development = template.replace('%type%', 'development').replace('%ext%', 'js')
, production = template.replace('%type%', 'production').replace('%ext%', 'min.js');



//array base containing all the files to be read.
base = ['now.js'];
var files = [];
base.forEach(function (file) {
files.push(__dirname + '/../lib/client/' + file);
});

var results = {};
files.forEach(function (file) {
fs.readFile(file, function (err, content) {

if (err) throw err;
var code = content.toString();
var ast = jsp.parse(code);
ast = pro.ast_squeeze(ast, {make_seqs: false, dead_code: false});
var code = production + pro.gen_code(ast, {ascii_only: true});

code += ';';
fs.write(
fs.openSync(__dirname + '/../lib/dist/now.js', 'w')
, code
, 0
, 'utf8'
);

});
});

2 changes: 2 additions & 0 deletions lib/dist/now.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 24 additions & 7 deletions lib/fileServer.js
@@ -1,6 +1,7 @@
var fs = require('fs');
var nowUtil = require('./nowUtil').nowUtil;

//redirect the client/now.js to ../dist/now.min.js
var clientNow = '/dist/now.js';
var fileCache = {};
var nowFileCache = {};

Expand Down Expand Up @@ -30,7 +31,16 @@ handleResponse = function (request, response) {

// Detect if request involves the now.js file
if (request.url.split('?')[0] === '/nowjs/now.js') {
serveFile(__dirname + '/client/now.js', request, response, options);
try {
console.log("serveFile: the directory is " + clientNow);
serveFile(__dirname + clientNow, request, response, options);
}
catch(err)
{
console.log("/client/now.js not found, while serving file");
return;
};

} else {
// Make sure default listeners are still handled
for (i in defaultListeners) {
Expand Down Expand Up @@ -99,15 +109,22 @@ serveFile = function (filename, request, response, options) {

// Takes host and port and call callback with now.js as parameter
generateClientLibs = function (hostServer, hostPort, callback) {
fs.readFile(__dirname + '/client/now.js', function (err, data) {
var nowText = data.toString();
var initString = options.scope + '.now = nowInitialize("' +
try {
console.log("readFile: the directory is " + clientNow);
fs.readFile(__dirname + clientNow, function (err, data) {
var nowText = data.toString();
var initString = options.scope + '.now = nowInitialize("' +
(options.protocol !== undefined ? options.protocol + ':' : '') +
'//' + hostServer + ':' + hostPort + '", ' + options.client + ');\n';
nowText += initString;
nowText += initString;

callback(nowText);
callback(nowText);
});
} catch(err){
console.log("/client/now.js not found while reading file");
return;

};
};

exports.generateClientLibs = generateClientLibs;

0 comments on commit 176050b

Please sign in to comment.