Created
November 4, 2011 18:09
-
-
Save millermedeiros/1340046 to your computer and use it in GitHub Desktop.
node.js CLI to compress TexturePacker JSON-Array data
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
#!/usr/bin/env node | |
//npm install commander | |
/** | |
* Command line tool to convert TexturePacker JSON-Array data into | |
* optimized SpriteAnim.js JSON data file. | |
* @author Miller Medeiros | |
* @version 0.1.0 (2011/10/18) | |
*/ | |
var program = require('commander'), | |
fs = require('fs'), | |
path = require('path'); | |
// --- | |
function list(val){ | |
return val.split(','); | |
} | |
program | |
.version('0.1.0') | |
.description('Convert TexturePacker JSON-Array data into SpriteAnim.js data file.') | |
.option('-i, --input <dir>', 'Specify input dir.') | |
.option('-o, --output <dir>', 'Specify output dir.') | |
.option('-f, --files [files]', 'Specify input json files (comma separated), if empty it will optimize all the ".json" files inside the input dir.', list) | |
.option('-H, --hard', 'Overwrite existing files.') | |
.option('-p, --image-path <path>', 'Base image path.') | |
.parse(process.argv); | |
//input and output are required | |
if(! program.input || ! program.output){ | |
program.parse([process.argv[0], process.argv[1], '-h']); | |
process.exit(0); | |
} | |
// --- | |
var inputFiles; | |
if(! path.existsSync(program.output) ){ | |
fs.mkdirSync(program.output, '0777'); | |
} | |
if(program.files){ | |
inputFiles = program.files; | |
} else { | |
inputFiles = fs.readdirSync(program.input); | |
inputFiles = inputFiles.filter(function(fname){ | |
return (/\.json$/).test(fname); | |
}); | |
} | |
if(! inputFiles.length){ | |
console.log('\n ERROR: no JSON files found inside input directory.\n'); | |
process.exit(1); | |
} | |
console.log('\n Converting %s file%s... ', inputFiles.length, (inputFiles.length > 1)? 's' : ''); | |
inputFiles.forEach(processFile); | |
console.log(''); | |
// --- | |
function processFile(fname){ | |
var fpath = path.join(program.input, fname), | |
content = fs.readFileSync(fpath, "utf-8"), | |
sourceData = JSON.parse(content), | |
outputData = { | |
spriteSheet : path.join(program.imagePath, sourceData.meta.image), | |
frameSize : sourceData.frames[0].sourceSize, | |
frames : sourceData.frames.map(normalizeFrameData) | |
}; | |
var outputPath = path.join(program.output, fname); | |
if(program.hard || ! path.existsSync(outputPath)){ | |
fs.writeFileSync(outputPath, JSON.stringify(outputData), 'utf-8'); | |
console.log(' - '+ fpath +' -> '+outputPath); | |
} else { | |
console.log('\n ERROR: "'+ outputPath +'" already exists, use \'--hard\' to overwrite existing files.\n'); | |
process.exit(1); | |
} | |
} | |
function normalizeFrameData(data){ | |
return { | |
x : data.frame.x, | |
y : data.frame.y, | |
w : data.frame.w, | |
h : data.frame.h, | |
t : data.spriteSourceSize.y, | |
l : data.spriteSourceSize.x | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
used to convert TexturePacker JSON-Array data into a compressed JSON e.g.:
I'm planning to release a library that reads this kind of data and plays sprite sheet animation. Already coded it but not sure if I will open source it and when I will do it (spends too much time writing docs, creating repository, false bug reports, etc...)
Some of the frames data is only needed if each sprite have a different size.. source code should be easy to tweak for your need.
PS: output won't have line breaks..