Skip to content

Instantly share code, notes, and snippets.

@1010real
Last active April 18, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1010real/1217adeed464d91c7222 to your computer and use it in GitHub Desktop.
Save 1010real/1217adeed464d91c7222 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = function(grunt) {
grunt.registerMultiTask('cclogdelete', 'delete cclog() with ruby script.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
});
var done = this.async(); // ここでgruntに非同期処理なので待ってと伝える
var fileCount = this.files.length;
var doneCount = 0;
// Iterate over all specified file groups.
this.files.forEach(function(f) {
// Concat specified files.
var src = f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).map(function(filepath) {
var spawn = require('child_process').spawn;
var cmd = spawn('ruby', ['node_modules/grunt-cclogdelete/cclogdelete.rb', filepath], { cwd:"." });
var buff = "";
cmd.stdout.on('data', function(data) {
// grunt.log.writeln('stdout:' + data);
buff += String(data);
});
cmd.stderr.on('data', function(data) {
grunt.log.writeln('stderr: ' + data);
done(false); // なんかエラったら、引数にfalseを渡してdoneを呼び出すとエラー終了になる
});
cmd.on('close', function(code) {
if (code !== 0) {
grunt.log.writeln('child process exited with code : ' + code);
done(false);
}
// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
// Write the destination file.
grunt.file.write(f.dest, buff);
// Count done processes and check end of all.
doneCount++;
if (doneCount >= fileCount) done(); // 非同期処理が全て終了したら、doneを呼び出す
});
grunt.log.writeln('run cclogdelete. filepath = ' + filepath);
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment