Created
March 22, 2012 23:55
-
-
Save limptwiglet/2165606 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'), | |
// CONFIG OPTIONS | |
basePath = './static/js/', | |
buildPath = basePath + 'build/app.js', | |
templatePath = basePath + 'templates/' | |
filePaths = [ | |
'libs/jquery.js', | |
'libs/ember.js', | |
'libs/ember-data.js', | |
'app.js', | |
'libs/rest-adapter.js', | |
'store/', | |
'controllers/', | |
'models/', | |
'fixtures/', | |
'views/', | |
'statechart/' | |
], | |
// HELPERS | |
readPath = function (path) { | |
var stat = fs.statSync(path); | |
if (stat.isDirectory()) { | |
var paths = []; | |
path = path.charAt(path.length-1) === '/' ? path : path + '/'; | |
var files = fs.readdirSync(path); | |
if (files.length) { | |
for (var i = 0, l = files.length; i < l; i++) { | |
paths.push(readPath(path + files[i])); | |
} | |
return paths.join(''); | |
} | |
} else if (stat.isFile() && path.indexOf('.swp') === -1){ | |
var file = fs.readFileSync(path, 'utf-8'); | |
return file; | |
} | |
}, | |
build = function () { | |
var files = []; | |
filePaths.forEach(function (path) { | |
files.push(readPath(basePath + path)); | |
}); | |
files.push(injectTemplates()); | |
files.push(readPath(basePath + 'main.js')); | |
fs.writeFile(buildPath, files.join(''), 'utf-8', function (err, data) { | |
if (err) throw err; | |
var d = new Date(); | |
console.log(d.getHours() + ':' + d.getMinutes() + ' - JS built to ->' + buildPath); | |
}); | |
}, | |
injectTemplates = function () { | |
var templates = []; | |
var files = fs.readdirSync(templatePath); | |
files.forEach(function (file) { | |
if (file.indexOf('.swp') === -1) { | |
var html = fs.readFileSync(templatePath + file, 'utf-8'); | |
html = html.replace(/"/g, '\\"'); | |
html = html.replace(/(\r\n|[\r\n])/g, ''); | |
html = 'Ember.TEMPLATES[\'' + file.replace('.html', '') + '\'] = Ember.Handlebars.compile("' + html + '");'; | |
templates.push(html); | |
} | |
}); | |
return templates.join(''); | |
}; | |
desc('Build our application javascript files into a single file.'); | |
task('build', [], function () { | |
build(); | |
}); | |
desc('Watches our application javascript files and rebuilds when they chagne.'); | |
task('watch', [], function () { | |
var watchFile = function (path) { | |
var stat = fs.statSync(path); | |
if (stat.isDirectory()) { | |
watchDir(path); | |
} else if (!/\.[swp|swo]/.test(path)) { | |
fs.watchFile(path, { interval: 50 }, function (prev, curr) { | |
if (+prev.mtime !== +curr.mtime) { | |
console.log('Change detected in ' + path); | |
build(); | |
} | |
}); | |
} | |
}, | |
watchDir = function (path) { | |
fs.readdir(path, function (err, files) { | |
if (err) throw err; | |
files.forEach(function (file) { | |
watchFile(path + '/' + file); | |
}); | |
}); | |
}; | |
filePaths.forEach(function (path) { | |
watchFile(basePath + path); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment