Skip to content

Instantly share code, notes, and snippets.

@kakeru
Created December 26, 2013 14:40
Show Gist options
  • Save kakeru/8134564 to your computer and use it in GitHub Desktop.
Save kakeru/8134564 to your computer and use it in GitHub Desktop.
Toolkit for CreateJSから出力されたファイルを、○○_manifest.jsとして出力する。
'use strict';
module.exports = function(grunt) {
var setting = {
//CreateJS パブリッシュボタンを押した際に出力されるディレクトリ
cjsout : 'fla/html/',
//JSのコーディングなどを行うディレクトリ
work : 'work/',
//CreateJSから出力されるファイル(glob形式・拡張子なし)
files : 'challenge',
// jpegに変換する画像ファイル(正規表現)
img_jpeg : [],
// 使用しない画像ファイル(正規表現)
img_ignore : [/^guide_.*$/]
};
grunt.initConfig({
watch: {
files: [setting.cjsout + setting.files + '.html'],
tasks: 'work'
},
clean : {
workpre : [
setting.work + 'js/' + setting.files + '.js',
setting.work + 'js/' + setting.files + '_manifest.js',
setting.work + 'img/cjs/*'
],
workpost : [
setting.cjsout + setting.files + '.js',
setting.cjsout + 'images/'
]
},
copy : {
work: {
files: [
{ expand: true, flatten: true, filter: 'isFile', src: [setting.cjsout + setting.files + '.js'], dest: setting.work + 'js/' }
]
}
},
manifest : {
work : {}
},
imgcopy : {
work : {}
}
});
grunt.registerTask('cjsout_check', function() {
var done = this.async();
done(grunt.file.isDir(setting.cjsout + 'images'));
});
grunt.registerMultiTask('imgcopy', '', function() {
var expandFiles = grunt.file.expand(setting.cjsout + 'images/*');
var i, j;
var convertCommands = [];
var convertCommandIndex = 0;
if (!grunt.file.isDir(setting.work + 'img')) {
grunt.file.mkdir(setting.work + 'img');
}
if (!grunt.file.isDir(setting.work + 'img/cjs')) {
grunt.file.mkdir(setting.work + 'img/cjs');
}
for (var i=0; i<expandFiles.length; i++) {
var paths = expandFiles[i].split('/');
var filename = paths[paths.length - 1];
var filetype = 0;
setting.img_jpeg.forEach(function(v) {
if (filename.match(new RegExp(v))) { filetype = 1; }
});
setting.img_ignore.forEach(function(v) {
if (filename.match(new RegExp(v))) { filetype = 2; }
});
switch (filetype) {
case 0:
grunt.file.copy(expandFiles[i], setting.work + 'img/cjs/' + filename);
break;
case 1:
var filename2 = filename.substr(0, filename.length - 4);
var convertCommand = 'sips -s format jpeg ' + expandFiles[i] + ' --out ' + setting.work + 'img/cjs/' + filename2 + '.jpg';
convertCommands.push(convertCommand);
break;
}
}
if (0 < convertCommands.length) {
var done = this.async();
var exec = require('child_process').exec;
var options = {};
var callback = function(error, stdout, stderr) {
if (error) {
console.log('ERR', error, stderr);
done(false);
} else {
console.log(stdout);
convertCommandIndex++;
if (convertCommandIndex < convertCommands.length) {
exec(convertCommands[convertCommandIndex], options, callback);
} else {
done();
}
}
};
exec(convertCommands[convertCommandIndex], options, callback);
}
});
grunt.registerMultiTask('manifest', '', function() {
var fs = require('fs');
var expandFiles = grunt.file.expand(setting.cjsout + setting.files + '.html');
console.log(expandFiles);
for (var i=0; i<expandFiles.length; i++) {
var htmlpaths = expandFiles[i].split('/');
var htmlfilename = htmlpaths[htmlpaths.length - 1];
var fd = fs.openSync(expandFiles[i], 'r');
var stat = fs.statSync(expandFiles[i]);
var bytes = fs.readSync(fd, stat.size, 0, 'utf-8');
var fileContent = bytes[0];
fs.closeSync(fd);
var lines = fileContent.split("\n");
var startIndex = 0;
var endIndex = 0;
var searchIndex = 0;
var j;
for (j=0; j<lines.length; j++) {
if (startIndex === 0) {
var startFindIndex = lines[j].indexOf('var manifest = [');
if (-1 != startFindIndex) {
startIndex = searchIndex + startFindIndex;
}
} else if (endIndex === 0) {
var endFindIndex = lines[j].indexOf('];');
if (-1 != endFindIndex) {
endIndex = searchIndex + endFindIndex;
break;
}
}
searchIndex += lines[j].length + 1;
}
// manifestが検出できたのでファイル作成。
if (startIndex != 0 && endIndex != 0) {
var str = fileContent.substr(startIndex, endIndex - startIndex + 2);
str = str.replace(/\t/g, '');
str = str.replace(/src:"images\//g, 'src:"img/cjs/');
var strLines = str.split("\n");
var saveLines = [];
for (j=0; j<strLines.length; j++) {
if (strLines[j].indexOf('{src:"img/cjs/') == 0) {
var filenameLast = strLines[j].indexOf('"', 14);
var filename = strLines[j].substr(14, filenameLast - 14);
var filetype = 0;
setting.img_jpeg.forEach(function(v) {
if (filename.match(new RegExp(v))) { filetype = 1; }
});
setting.img_ignore.forEach(function(v) {
if (filename.match(new RegExp(v))) { filetype = 2; }
});
switch (filetype) {
case 0:
saveLines.push(strLines[j]);
break;
case 1:
var filename2 = filename.substr(0, filename.length - 4);
saveLines.push(
'{src:"img/cjs/' + filename2 + '.jpg' + strLines[j].substr(14 + filename.length)
);
break;
}
} else {
saveLines.push(strLines[j]);
}
}
saveLines.forEach(function(v) {
console.info(v);
});
var fd = fs.openSync(setting.work + 'js/' + htmlfilename.substr(0, htmlfilename.length - 5) + '_manifest.js', 'w+');
fs.writeSync(fd, saveLines.join("\n"), 0, 'ascii');
fs.closeSync(fd);
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
// CreateJS -> workディレクトリへの組み込み
grunt.registerTask('work', [
'cjsout_check',
'clean:workpre',
'copy:work',
'manifest:work',
'imgcopy:work',
'clean:workpost'
]);
grunt.registerTask('default', 'watch');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment