diff --git a/lib/tasks/optimize.js b/lib/tasks/optimize.js index 77e5e23..912468a 100644 --- a/lib/tasks/optimize.js +++ b/lib/tasks/optimize.js @@ -57,20 +57,21 @@ Optimize = (function() { Optimize.prototype._executeOptimize = function(runConfig, callback) { var err, _this = this; - logger.info("Beginning r.js optimization to result in [[ " + runConfig.out + " ]]"); + if (runConfig.out) { + logger.info("Beginning r.js optimization to result in [[ " + runConfig.out + " ]]"); + } else { + logger.info("Beginning r.js optimization"); + } this._logRunConfig(runConfig); try { return requirejs.optimize(runConfig, function(buildResponse) { - var i, reportLine, reportLines, _i, _len; - reportLines = buildResponse.split("\n"); - for (i = _i = 0, _len = reportLines.length; _i < _len; i = ++_i) { - reportLine = reportLines[i]; - if (reportLine.indexOf('---') === 0) { - runConfig.filesUsed = reportLines.splice(i + 1, reportLines.length - (i + 2)); - break; - } + if (runConfig.out) { + _this._reportSingleFileOutput(runConfig, buildResponse); + } else if (runConfig.dir) { + _this._reportMultiFileOutput(runConfig); + } else { + logger.debug("Unexpected exit, not .out, not .dir."); } - logger.success("The compiled file [[ " + runConfig.out + " ]] is ready for use.", true); return callback(); }); } catch (_error) { @@ -80,6 +81,44 @@ Optimize = (function() { } }; + Optimize.prototype._reportSingleFileOutput = function(runConfig, buildResponse) { + var builtFile, i, reportLine, reportLines, _i, _len; + reportLines = buildResponse.split("\n"); + builtFile = void 0; + for (i = _i = 0, _len = reportLines.length; _i < _len; i = ++_i) { + reportLine = reportLines[i]; + if (reportLine.indexOf('---') === 0) { + runConfig.filesUsed = reportLines.splice(i + 1, reportLines.length - (i + 2)).filter(function(used) { + return used !== builtFile; + }); + break; + } else { + builtFile = reportLine; + } + } + return logger.success("The compiled file [[ " + builtFile + " ]] is ready for use.", true); + }; + + Optimize.prototype._reportMultiFileOutput = function(runConfig) { + var buildResponse, buildTxtPath, f, filesBuiltReportLines, filesUsed, _i, _len; + buildTxtPath = path.join(runConfig.dir, "build.txt"); + if (fs.existsSync(buildTxtPath)) { + buildResponse = fs.readFileSync(buildTxtPath, "ascii"); + filesBuiltReportLines = buildResponse.split('\n\n'); + filesUsed = []; + for (_i = 0, _len = filesBuiltReportLines.length; _i < _len; _i++) { + f = filesBuiltReportLines[_i]; + this._reportSingleFileOutput(runConfig, f); + filesUsed = filesUsed.concat(runConfig.filesUsed); + } + return runConfig.filesUsed = filesUsed.map(function(f) { + return path.join(runConfig.dir, f); + }); + } else { + return logger.info("Cannot locate build.txt for post r.js run cleanup purposes."); + } + }; + return Optimize; })(); diff --git a/src/tasks/optimize.coffee b/src/tasks/optimize.coffee index 0c9947c..3dedc3e 100644 --- a/src/tasks/optimize.coffee +++ b/src/tasks/optimize.coffee @@ -37,22 +37,50 @@ class Optimize cache = null _executeOptimize: (runConfig, callback) => - logger.info "Beginning r.js optimization to result in [[ #{runConfig.out} ]]" + if runConfig.out + logger.info "Beginning r.js optimization to result in [[ #{runConfig.out} ]]" + else + logger.info "Beginning r.js optimization" @_logRunConfig runConfig try requirejs.optimize runConfig, (buildResponse) => - reportLines = buildResponse.split("\n") - for reportLine, i in reportLines - if reportLine.indexOf('---') is 0 - runConfig.filesUsed = reportLines.splice(i + 1, reportLines.length - (i + 2)) - break - - logger.success "The compiled file [[ #{runConfig.out} ]] is ready for use.", true + if runConfig.out + @_reportSingleFileOutput runConfig, buildResponse + else if runConfig.dir + @_reportMultiFileOutput runConfig + else + logger.debug "Unexpected exit, not .out, not .dir." callback() catch err logger.error "Error occured inside r.js optimizer, error is as follows... #{err}" callback() + _reportSingleFileOutput: (runConfig, buildResponse)-> + reportLines = buildResponse.split("\n") + builtFile = undefined + for reportLine, i in reportLines + if reportLine.indexOf('---') is 0 + runConfig.filesUsed = reportLines.splice(i + 1, reportLines.length - (i + 2)).filter (used) -> + used isnt builtFile + break + else + builtFile = reportLine + + logger.success "The compiled file [[ #{builtFile} ]] is ready for use.", true + + _reportMultiFileOutput: (runConfig) -> + buildTxtPath = path.join runConfig.dir, "build.txt" + if fs.existsSync buildTxtPath + buildResponse = fs.readFileSync buildTxtPath, "ascii" + filesBuiltReportLines = buildResponse.split('\n\n') + filesUsed = [] + for f in filesBuiltReportLines + @_reportSingleFileOutput runConfig, f + filesUsed = filesUsed.concat runConfig.filesUsed + runConfig.filesUsed = filesUsed.map (f) -> path.join runConfig.dir, f + else + logger.info "Cannot locate build.txt for post r.js run cleanup purposes." + exports.execute = new Optimize().execute \ No newline at end of file