Skip to content

Commit

Permalink
fixes #20, references dbashford/mimosa#274, adressing messaging and c…
Browse files Browse the repository at this point in the history
…leanup in multi-module r.js setups
  • Loading branch information
dbashford committed Sep 14, 2013
1 parent b5c7241 commit deeac44
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 18 deletions.
59 changes: 49 additions & 10 deletions lib/tasks/optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

})();
Expand Down
44 changes: 36 additions & 8 deletions src/tasks/optimize.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit deeac44

Please sign in to comment.