Created
May 24, 2011 03:53
-
-
Save mashihua/988117 to your computer and use it in GitHub Desktop.
javascript development script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Watch a dir to concat javascript file when it is change. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var fs = require('fs'), | |
config = JSON.parse(fs.readFileSync(__dirname + '/config.json')); | |
//Add or delete ipfw rule | |
//all incoming tcp port 80 will forward to 127.0.0.1:8000 | |
function ipfw(add){ | |
if(typeof child !== 'undefined'){ | |
child.kill(); | |
} | |
if(typeof use_ipfw === 'undefined' || !use_ipfw){ | |
return; | |
} | |
add ? | |
require('child_process').exec('sudo ipfw add 110 fwd 127.0.0.1,8080 tcp from any to any dst-port 80 in') : | |
require('child_process').exec('sudo ipfw del 110'); | |
} | |
//For growl notifaction | |
function growl(message){ | |
var growlnotify = "`which growlnotify`", | |
title = "Node Results", | |
options = " -n Node -m '" + message + "' '" + title + "'"; | |
require('child_process').exec(growlnotify + options); | |
} | |
//Because no support module for FSEvent (http://en.wikipedia.org/wiki/FSEvents) ,Inotify(http://en.wikipedia.org/wiki/Inotify), | |
//Directory Change Notification(http://msdn.microsoft.com/en-us/library/aa365261%28VS.85%29.aspx). | |
//FIXME: samply implement it. | |
function watch(dir,callback){ | |
var files = fs.readdirSync(dir); | |
//watch all file in dir | |
files.forEach(function(f,i){ | |
if(inclused(f)){ | |
fs.watchFile(dir + '/' + f, | |
{ persistent: true, interval: 0 }, | |
function(curr, prev){ | |
if(curr.mtime > prev.mtime){ | |
callback(dir, curr, prev); | |
} | |
} | |
); | |
} | |
}); | |
} | |
//normalize config file | |
function normal(file){ | |
return path.normalize(__dirname + file); | |
} | |
//Exclude file include hidden file,ect:.DS_Store | |
function inclused(f){ | |
var excluse = config.excluse; | |
return !/^\./.test(f) && | |
excluse && | |
!~excluse.indexOf(f); | |
} | |
//Hold SIGN Ctrl-gi | |
process.on('SIGINT', function () { | |
console.log('\nBye.\n'); | |
concatJs(normal(dir)); | |
ipfw(false); | |
setTimeout(function(){process.exit(0);},500); | |
}); | |
//Hold SIGN Ctrl-\, | |
//This sign is not in windows | |
process.on('SIGQUIT', function () { | |
console.log("\nBye.\n"); | |
ipfw(false); | |
process.exit(0); | |
}); | |
//Concat the js file in folder. | |
function concatJs (dir) { | |
var data = '', | |
files = fs.readdirSync(dir); | |
//dependences file | |
config.dependences.forEach(function (v,i) { | |
data += fs.readFileSync(normal(v)); | |
}); | |
//file in folder | |
files.forEach(function(f,i){ | |
var filename = dir + '/' + f ; | |
if(inclused( f) && fs.statSync(filename).isFile()){ | |
data += '\n' + fs.readFileSync( filename , 'utf8'); | |
} | |
}); | |
//write to dist file | |
fs.writeFile(normal(config.dist), data); | |
growl('Dist core file'); | |
console.log('dist file:' , normal(config.dist)); | |
} | |
function start () { | |
if(!path.exists(normal(config.dist))){ | |
concatJs(normal(config.path)) | |
} | |
ipfw(true); | |
watch(normal(config.path),concatJs); | |
growl('Concat service started!'); | |
} | |
var use_ipfw = true; | |
var child = require('child_process').exec("livereload "+ __dirname +"/../../ >/dev/null 2>&1"); | |
start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependences" : ["/../js/core.js","/../third-party/json2.js"], | |
"excluse" : ["core.js"], | |
"dist" : "/../dist/core.js", | |
"path" : "/../js" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment