Last active
December 12, 2015 02:28
-
-
Save chrisirhc/4698776 to your computer and use it in GitHub Desktop.
Log grunt output into file.
Intercepts grunt.log.write to do its job.
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
function gruntLogToFile(grunt, logFilePath) { | |
logFilePath = logFilePath || grunt.option('log-file'); | |
if (!logFilePath) { | |
throw new Error('No log file path specified'); | |
return null; | |
} | |
var namespace = '_gruntLogToFile_' + logFilePath; | |
var origLogFunc = grunt.log.write; | |
// Check if stream/hook has already been created | |
while(origLogFunc && !origLogFunc[namespace]) { | |
origLogFunc = grunt.util.hooker.orig(grunt.log, 'write'); | |
} | |
if (origLogFunc && origLogFunc[namespace]) { | |
// Return stream if it has already been created | |
return origLogFunc[namespace]; | |
} | |
var logFileStream = fs.createWriteStream(logFilePath, {flags: 'a'}); | |
if (logFileStream.writable) { | |
grunt.util.hooker.hook(grunt.log, 'write', function writeAndLog() { | |
if (logFileStream.writable) { | |
logFileStream.write(arguments[0].stripColors); | |
} | |
}); | |
grunt.util.hooker.hook(grunt.util, 'exit', function drainLogBefExit(exitCode) { | |
if (logFileStream._queue.length) { | |
logFileStream.once('drain', function () { | |
grunt.util.exit(exitCode); | |
}) | |
return grunt.util.hooker.preempt(); | |
} | |
}); | |
grunt.log.write[namespace] = logFileStream; | |
} | |
return logFileStream; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment