diff --git a/task.js b/task.js new file mode 100644 index 0000000..83040a9 --- /dev/null +++ b/task.js @@ -0,0 +1,14 @@ +/* jshint node: true */ + +var nopt = require('nopt'); +var Q = require('q'); + +var tasks = nopt({}, {}, process.argv).argv.remain; + +Q.longStackSupport = true; + +Q.all( + tasks.map(function(task) { + return require('./tasks/' + task + '.task'); + }) +).done(); diff --git a/tasks/debug.task.js b/tasks/debug.task.js index e120498..9eec478 100644 --- a/tasks/debug.task.js +++ b/tasks/debug.task.js @@ -12,74 +12,71 @@ var vars = require('./vars'); var io = require('./io'); var when_jsdom = when_node.lift(jsdom.env); -module.exports.task = function() { - - var app = express(); - - app.get('/', function (req, res, next) { - - when([ - io.find(config.scripts), - io.find(config.styles), - io.readText([vars.srcRoot, 'public', 'main.html']).then(when_jsdom) - ]).spread(function(scripts, styles, window){ - - var document = window.document; - var element; - - //document.documentElement.setAttribute('ng-app', config.globals.appModule); - - element = document.createElement('script'); - element.innerHTML = 'window.globals=' + JSON.stringify(config.globals); - document.head.appendChild(element); - - styles.forEach(function (style) { - element = document.createElement('link'); - if (style.substr(-5, 5).toLowerCase() === '.less') { - element.setAttribute('rel', 'stylesheet/less'); - } else { - element.setAttribute('rel', 'stylesheet'); - } - element.setAttribute('type', 'text/css'); - element.setAttribute('href', style); - document.head.appendChild(element); - }); - - element = document.createElement('script'); - element.innerHTML = 'window.less=' + JSON.stringify({ - errorReporting: 'console', - logLevel: 0 - }); +var app = express(); + +app.get('/', function(req, res, next) { + + when([ + io.find(config.scripts), + io.find(config.styles), + io.readText([vars.srcRoot, 'public', 'main.html']).then(when_jsdom) + ]).spread(function(scripts, styles, window) { + + var document = window.document; + var element; + + //document.documentElement.setAttribute('ng-app', config.globals.appModule); + + element = document.createElement('script'); + element.innerHTML = 'window.globals=' + JSON.stringify(config.globals); + document.head.appendChild(element); + + styles.forEach(function(style) { + element = document.createElement('link'); + if (style.substr(-5, 5).toLowerCase() === '.less') { + element.setAttribute('rel', 'stylesheet/less'); + } + else { + element.setAttribute('rel', 'stylesheet'); + } + element.setAttribute('type', 'text/css'); + element.setAttribute('href', style); document.head.appendChild(element); - + }); + + element = document.createElement('script'); + element.innerHTML = 'window.less=' + JSON.stringify({ + errorReporting: 'console', + logLevel: 0 + }); + document.head.appendChild(element); + + element = document.createElement('script'); + element.setAttribute("src", "bower_components/less/dist/less.js"); + document.head.appendChild(element); + + scripts.forEach(function(script) { element = document.createElement('script'); - element.setAttribute("src", "bower_components/less/dist/less.js"); + element.setAttribute('src', script); document.head.appendChild(element); - - scripts.forEach(function (script) { - element = document.createElement('script'); - element.setAttribute('src', script); - document.head.appendChild(element); - }); - - return jsdom.serializeDocument(document); - }).done(function (content) { - res.set('Content-Type', 'text/html'); - res.send(content); - }, next); - }); - - app.use('/public', express.static(vars.srcRoot + '/public')); - app.use('/bower_components', express.static(vars.srcRoot + '/bower_components')); - - return when.promise(function(resolve, reject){ - var server = app.listen(args.port, function () { - var host = server.address().address; - var port = server.address().port; - - console.log('Debug server listening at http://%s:%s', host, port); - resolve(server); }); - }); -}; + return jsdom.serializeDocument(document); + }).done(function(content) { + res.set('Content-Type', 'text/html'); + res.send(content); + }, next); +}); + +app.use('/public', express.static(vars.srcRoot + '/public')); +app.use('/bower_components', express.static(vars.srcRoot + '/bower_components')); + +module.exports = when.promise(function(resolve, reject) { + var server = app.listen(args.port, function() { + var host = server.address().address; + var port = server.address().port; + + console.log('Debug server listening at http://%s:%s', host, port); + resolve(server); + }); +}); diff --git a/tasks/deploy.task.js b/tasks/deploy.task.js index 2f0a8a6..5641ee5 100644 --- a/tasks/deploy.task.js +++ b/tasks/deploy.task.js @@ -19,128 +19,127 @@ var when_listObjects = when_node.lift(s3bucket.listObjects.bind(s3bucket)); var when_deleteObjects = when_node.lift(s3bucket.deleteObjects.bind(s3bucket)); var when_putObject = when_node.lift(s3bucket.putObject.bind(s3bucket)); +module.exports = require('./dist.task').then(function(dist) { -module.exports.task = function(dist) { - return when_listObjects().then(function (objects) { + return when_listObjects().then(function(objects) { if (objects.Contents.length === 0) return; - + return when_deleteObjects({ Delete: { Quiet: true, - Objects: objects.Contents.map(function (content) { + Objects: objects.Contents.map(function(content) { return { Key: content.Key }; }) } }); - }).then(function () { + }).then(function() { return io.find('**/*.jpg', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadBinary.bind(null, 'image/jpeg'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.png', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadBinary.bind(null, 'image/png'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.gif', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadBinary.bind(null, 'image/gif'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.ico', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadBinary.bind(null, 'image/x-icon'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.woff', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadBinary.bind(null, 'application/font-woff'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.ttf', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadBinary.bind(null, 'application/octet-stream'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.svg', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'image/svg+xml'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.html', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'text/html'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.css', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'text/css'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.js', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'text/javascript'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.map', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'application/json'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.txt', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'text/plain'))); }); - - }).then(function () { + + }).then(function() { return io.find('**/*.appcache', { cwd: vars.dstRoot - }).then(function (files) { + }).then(function(files) { return when.all(files.map(uploadText.bind(null, 'text/cache-manifest'))); }); - + }); - -}; +}); function uploadText(type, file) { - return io.readText([vars.dstRoot, file]).then(function (content) { + return io.readText([vars.dstRoot, file]).then(function(content) { return new Buffer(content, 'utf8'); - }).then(when_gzip).then(function (content) { + }).then(when_gzip).then(function(content) { return when_putObject({ Key: file, ContentType: '' + type + '; charset=utf-8', ContentEncoding: 'gzip', Body: content }); - }).then(function () { + }).then(function() { console.log(' ^ ' + file); return file; }); @@ -148,13 +147,13 @@ function uploadText(type, file) { } function uploadBinary(type, file) { - return io.readBinary([vars.dstRoot, file]).then(function (content) { + return io.readBinary([vars.dstRoot, file]).then(function(content) { return when_putObject({ Key: file, ContentType: '' + type + '', Body: content }); - }).then(function () { + }).then(function() { console.log(' ^ ' + file); return file; }); diff --git a/tasks/dist.task.js b/tasks/dist.task.js index ca6eb4c..7a03633 100644 --- a/tasks/dist.task.js +++ b/tasks/dist.task.js @@ -1,4 +1,10 @@ /* jshint node: true */ -module.exports.task = function(script, style, html, robots) { -}; +var Q = require('q'); + +module.exports = Q.all([ + require('./script.task'), + require('./style.task'), + require('./html.task'), + require('./robots.task'), +]); diff --git a/tasks/html.task.js b/tasks/html.task.js index a209833..6c8c556 100644 --- a/tasks/html.task.js +++ b/tasks/html.task.js @@ -12,36 +12,32 @@ var io = require('./io'); var when_jsdom = when_node.lift(jsdom.env); -module.exports.task = function() { +module.exports = io.readText([vars.srcRoot, 'public', 'main.html']).then(when_jsdom).then(function(window) { + var document = window.document; + var element; - return io.readText([vars.srcRoot, 'public', 'main.html']).then(when_jsdom).then(function(window) { - var document = window.document; - var element; + //document.documentElement.setAttribute('ng-app', config.globals.appModule); - //document.documentElement.setAttribute('ng-app', config.globals.appModule); + element = document.createElement('script'); + element.innerHTML = 'window.globals=' + JSON.stringify(config.globals); + document.head.appendChild(element); - element = document.createElement('script'); - element.innerHTML = 'window.globals=' + JSON.stringify(config.globals); - document.head.appendChild(element); + element = document.createElement('link'); + element.setAttribute('rel', 'stylesheet'); + element.setAttribute('type', 'text/css'); + element.setAttribute('href', "main.css"); + document.head.appendChild(element); - element = document.createElement('link'); - element.setAttribute('rel', 'stylesheet'); - element.setAttribute('type', 'text/css'); - element.setAttribute('href', "main.css"); - document.head.appendChild(element); + element = document.createElement('script'); + element.setAttribute("src", "main.js"); + document.head.appendChild(element); - element = document.createElement('script'); - element.setAttribute("src", "main.js"); - document.head.appendChild(element); + trimNode(document); - trimNode(document); - - return jsdom.serializeDocument(document); - }).then(function(content) { - return io.writeText([vars.dstRoot, 'index.html'], content); - }); - -}; + return jsdom.serializeDocument(document); +}).then(function(content) { + return io.writeText([vars.dstRoot, 'index.html'], content); +}); function trimNode(node) { if (node.nodeType === 3) { diff --git a/tasks/robots.task.js b/tasks/robots.task.js index 925bf34..255434e 100644 --- a/tasks/robots.task.js +++ b/tasks/robots.task.js @@ -4,13 +4,11 @@ var path = require('path'); var vars = require('./vars'); var io = require('./io'); -module.exports.task = function() { - var filename = 'robots.txt'; - var content = [ - "User-agent: *", - "Allow: /", - "" - ]; +var filename = 'robots.txt'; +var content = [ + "User-agent: *", + "Allow: /", + "" +]; - return io.writeText([vars.dstRoot, filename], content); -}; +module.exports = io.writeText([vars.dstRoot, filename], content); diff --git a/tasks/script.task.js b/tasks/script.task.js index b68a6e8..83b452c 100644 --- a/tasks/script.task.js +++ b/tasks/script.task.js @@ -7,25 +7,20 @@ var io = require('./io'); var config = require('./config'); -module.exports.task = function() { - var outputFile = "main.js"; - var outputMapFile = outputFile + ".map"; - - return io.find(config.scripts, { - cwd: vars.srcRoot - }).then(function(files){ - var output = UglifyJS.minify(files, { - mangle: false, - outSourceMap: outputMapFile, - sourceMapIncludeSources: true - }); - - return when.all([ - io.writeText([vars.dstRoot, outputFile], output.code), - io.writeText([vars.dstRoot, outputMapFile], output.map) - ]); - }); -}; - +var outputFile = "main.js"; +var outputMapFile = outputFile + ".map"; +module.exports = io.find(config.scripts, { + cwd: vars.srcRoot +}).then(function(files) { + var output = UglifyJS.minify(files, { + mangle: false, + outSourceMap: outputMapFile, + sourceMapIncludeSources: true + }); + return when.all([ + io.writeText([vars.dstRoot, outputFile], output.code), + io.writeText([vars.dstRoot, outputMapFile], output.map) + ]); +}); diff --git a/tasks/static.task.js b/tasks/static.task.js index 58010b9..4aeb5e7 100644 --- a/tasks/static.task.js +++ b/tasks/static.task.js @@ -7,22 +7,20 @@ var config = require('./config'); var args = require('./args'); var vars = require('./vars'); +var app = express(); -module.exports.task = function(dist) { +app.use('/', express.static(vars.dstRoot)); - var app = express(); +module.exports = require('./dist.task').then(function(dist) { - app.use('/', express.static(vars.dstRoot)); - - return when.promise(function(resolve, reject){ - var server = app.listen(args.port, function () { + return when.promise(function(resolve, reject) { + var server = app.listen(args.port, function() { var host = server.address().address; var port = server.address().port; - + console.log('Static server listening at http://%s:%s', host, port); resolve(server); }); }); -}; - +}); diff --git a/tasks/style.task.js b/tasks/style.task.js index 1c8e490..ec847ff 100644 --- a/tasks/style.task.js +++ b/tasks/style.task.js @@ -10,32 +10,24 @@ var config = require('./config'); var outputFile = "main.css"; var outputMapFile = outputFile + ".map"; -module.exports.task = function() { - - return io.find(config.styles, { - cwd: vars.srcRoot - }).then(function(files){ - - return less.render(files.reduce(function(s, file){ - s += '@import "' + file + '";'; - return s; - }, ""), { - filename: io.path([vars.srcRoot, outputFile]), - sourceMap: { - sourceMapURL: outputMapFile, - sourceMapBasepath: vars.srcRoot - } - }).then(function(output){ - return when.all([ - io.writeText([vars.dstRoot, outputFile], output.css), - io.writeText([vars.dstRoot, outputMapFile], output.map) - ]); - }); - +module.exports = io.find(config.styles, { + cwd: vars.srcRoot +}).then(function(files) { + + return less.render(files.reduce(function(s, file) { + s += '@import "' + file + '";'; + return s; + }, ""), { + filename: io.path([vars.srcRoot, outputFile]), + sourceMap: { + sourceMapURL: outputMapFile, + sourceMapBasepath: vars.srcRoot + } + }).then(function(output) { + return when.all([ + io.writeText([vars.dstRoot, outputFile], output.css), + io.writeText([vars.dstRoot, outputMapFile], output.map) + ]); }); -}; - - - - +});