Skip to content

Instantly share code, notes, and snippets.

@kakeru
Created December 2, 2013 06:11
Show Gist options
  • Save kakeru/7745791 to your computer and use it in GitHub Desktop.
Save kakeru/7745791 to your computer and use it in GitHub Desktop.
Toolkit for CreateJSで出力された HTMLを監視し、manifestだけ別のJSファイルに書き出すよ!
/*
* Toolkit for CreateJSで出力された HTMLを監視し、
* manifestだけ別のJSファイルに書き出すよ!
* 多分、movieNameも initConfig に入れた方が良い気がするけど、とりあえず動いた!
*/
'use strict';
var movieName = 'fever2';
module.exports = function(grunt) {
grunt.initConfig({
watch: {
files: ['../' + movieName + '/html/' + movieName + '.html'],
tasks: 'extractManifest'
}
});
grunt.task.registerTask('extractManifest', 'Toolkit for CreateJS Flashがはき出したHTMLからmanifestを抽出する', function() {
var fs = require('fs');
var filename = '../' + movieName + '/html/' + movieName + '.html';
var fd = fs.openSync(filename, 'r');
var stat = fs.statSync(filename);
var bytes = fs.readSync(fd, stat.size, 0, 'ascii');
var fileContent = bytes[0];
fs.closeSync(fd);
var lines = fileContent.split("\n");
var startIndex = 0;
var endIndex = 0;
var searchIndex = 0;
for (var i=0; i<lines.length; i++) {
if (startIndex === 0) {
var startFindIndex = lines[i].indexOf('var manifest = [');
if (-1 != startFindIndex) {
startIndex = searchIndex + startFindIndex;
}
} else if (endIndex === 0) {
var endFindIndex = lines[i].indexOf('];');
if (-1 != endFindIndex) {
endIndex = searchIndex + endFindIndex;
break;
}
}
searchIndex += lines[i].length + 1;
}
var str = fileContent.substr(startIndex, endIndex - startIndex + 2);
str = str.replace(/\t/g, '');
var fd = fs.openSync('../' + movieName + '/html/' + movieName + '_manifest.js', 'w');
fs.writeSync(fd, str, 0, 'ascii');
fs.closeSync(fd);
});
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['watch']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment