2018-03-20 12:59:29 -04:00
|
|
|
var JSZip = require('jszip');
|
|
|
|
|
|
|
|
/**
|
2019-03-15 12:23:40 -04:00
|
|
|
* Unpacks a zip file.
|
|
|
|
* @param {string} input Zip file provided as a string
|
2018-05-02 18:17:32 -04:00
|
|
|
* @param {boolean} isSprite Whether the input should be treated as
|
|
|
|
* a sprite (true) or whole project (false)
|
2019-03-15 12:23:40 -04:00
|
|
|
* @param {array} callback Array including both the project and zip archive
|
2018-03-24 11:36:17 -04:00
|
|
|
* @return {void}
|
2018-03-20 12:59:29 -04:00
|
|
|
*/
|
2019-03-15 12:23:40 -04:00
|
|
|
module.exports = function (input, isSprite, callback) {
|
2018-04-05 13:32:06 -04:00
|
|
|
var msg = 'Failed to unzip and extract project.json, with error: ';
|
2019-03-15 12:23:40 -04:00
|
|
|
|
2018-03-20 12:59:29 -04:00
|
|
|
return JSZip.loadAsync(input)
|
|
|
|
.then(function (zip) {
|
2019-02-01 11:33:40 +01:00
|
|
|
// look for json in the list of files, or in a subdirectory
|
|
|
|
// assumes there is only one sprite or project json in the zipfile
|
|
|
|
const file = isSprite ?
|
|
|
|
zip.file(/^([^/]*\/)?sprite\.json$/)[0] :
|
|
|
|
zip.file(/^([^/]*\/)?project\.json$/)[0];
|
|
|
|
if (file) {
|
|
|
|
return file.async('string')
|
|
|
|
.then(function (project) {
|
|
|
|
return callback(null, [project, zip]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return callback(msg + 'missing project or sprite json');
|
2018-03-20 12:59:29 -04:00
|
|
|
})
|
2018-03-24 11:36:17 -04:00
|
|
|
.catch(function (err) {
|
2018-03-20 12:59:29 -04:00
|
|
|
return callback(msg + JSON.stringify(err));
|
|
|
|
});
|
2018-04-05 13:32:06 -04:00
|
|
|
|
2018-03-20 12:59:29 -04:00
|
|
|
};
|