index.js
//Run all the preprocessors on the package
Run all the preprocessors on the package
Run all the preprocessors on the package
function preprocessPackage(path) {
        var manifest = readManifest();
        return new Promise(function (resolvef, rejectf) {
            console.log("Processing levels...");
            var levels = manifest.levels.map(function (oldName, i) {
                if (oldName.indexOf(".xml") != -1) {
                    var newName = oldName.split(".xml").join(".json");
                    manifest.levels[i] = newName;
                    return new Promise(function (resolve, reject) {
                        log("Reading " + path + "/" + manifest.scope + "/" + oldName);
                        fs.readFile(path + "/" + manifest.scope + "/" + oldName, function (err, data) {
                            if (err) {
                                return console.error(err);
                            } else {
                                parseString(data, function (err, result) {
                                    if (err) {
                                        showError("There are errors in " + oldName + ", check your XML.");
                                        resolve();
                                    } else {
                                        log("Writing " + path + "/" + manifest.scope + "/" + newName);
                                        fs.writeFile(path + "/" + manifest.scope + "/" + newName, JSON.stringify(XMLlevelsToJson(result, oldName)), function (err) {
                                            if (err) {
                                                return console.error(err);
                                            }
                                            resolve();
                                        });
                                    }
                                });
                            }
                        });
                    });
                } else {
                    return new Promise(function (resolve, reject) { return resolve() });
                }
            });
            var spritesheets = manifest.spritesheets.map(function (oldName, i) {
                console.log("Processing spritesheets...");
                if (oldName.indexOf(".xml") != -1) {
                    var newName = oldName.split(".xml").join(".json");
                    manifest.spritesheets[i] = newName;
                    return new Promise(function (resolve, reject) {
                        fs.readFile(path + "/" + manifest.scope + "/" + oldName, function (err, data) {
                            if (err) {
                                return console.error(err);
                            } else {
                                parseString(data, function (err, result) {
                                    if (err) {
                                        showError("There are errors in " + oldName + ", check your XML.");
                                        resolve();
                                    } else {
                                        fs.writeFile(path + "/" + manifest.scope + "/" + newName, JSON.stringify(XMLspritesheetsToJson(result)), function (err) {
                                            if (err) {
                                                return console.error(err);
                                            }
                                            resolve();
                                        });
                                    }
                                });
                            }
                        });
                    });
                } else if (oldName.indexOf(".js") == oldName.length - 3) {
                    return new Promise(function (resolve, reject) {
                        fs.readFile(path + "/" + manifest.scope + "/" + oldName, function (err, data) {
                            if (err) {
                                return console.error(err);
                            } else {
                                var spritesheetContent;
                                eval("spritesheetContent = " + data);
                                (function stringifyFunctions(o) {
                                    for (x in o) {
                                        if (typeof o[x] == "object") {
                                            stringifyFunctions(o[x]);
                                        }
                                        if (typeof o[x] == "function") {
                                            o[x] = o[x].toString();
                                        }
                                    }
                                })(spritesheetContent);
                                var newName = oldName.split(".js").join(".json");
                                fs.writeFile(path + "/" + manifest.scope + "/" + newName, JSON.stringify(spritesheetContent), function (err) {
                                    if (err) {
                                        return console.error(err);
                                    }
                                    resolve();
                                });
                            }
                        });
                    });
                }
            });
            Promise.all(levels.concat(spritesheets)).then(function (x) {
                console.log("Updating manifest...");
                fs.writeFile(path + "/manifest.json", JSON.stringify(manifest), function (err) {
                    if (err) {
                        return console.error(err);
                    }
                    resolvef();
                });
            });
        });
    }
function preprocessPackage ( path ) { var manifest = readManifest ( ) ; return new Promise ( function ( resolvef , rejectf ) { console . log ( "Processing levels..." ) ; var levels = manifest . levels . map ( function ( oldName , i ) { if ( oldName . indexOf ( ".xml" ) != - 1 ) { var newName = oldName . split ( ".xml" ) . join ( ".json" ) ; manifest . levels [ i ] = newName ; return new Promise ( function ( resolve , reject ) { log ( "Reading " + path + "/" + manifest . scope + "/" + oldName ) ; fs . readFile ( path + "/" + manifest . scope + "/" + oldName , function ( err , data ) { if ( err ) { return console . error ( err ) ; } else { parseString ( data , function ( err , result ) { if ( err ) { showError ( "There are errors in " + oldName + ", check your XML." ) ; resolve ( ) ; } else { log ( "Writing " + path + "/" + manifest . scope + "/" + newName ) ; fs . writeFile ( path + "/" + manifest . scope + "/" + newName , JSON . stringify ( XMLlevelsToJson ( result , oldName ) ) , function ( err ) { if ( err ) { return console . error ( err ) ; } resolve ( ) ; } ) ; } } ) ; } } ) ; } ) ; } else { return new Promise ( function ( resolve , reject ) { return resolve ( ) } ) ; } } ) ; var spritesheets = manifest . spritesheets . map ( function ( oldName , i ) { console . log ( "Processing spritesheets..." ) ; if ( oldName . indexOf ( ".xml" ) != - 1 ) { var newName = oldName . split ( ".xml" ) . join ( ".json" ) ; manifest . spritesheets [ i ] = newName ; return new Promise ( function ( resolve , reject ) { fs . readFile ( path + "/" + manifest . scope + "/" + oldName , function ( err , data ) { if ( err ) { return console . error ( err ) ; } else { parseString ( data , function ( err , result ) { if ( err ) { showError ( "There are errors in " + oldName + ", check your XML." ) ; resolve ( ) ; } else { fs . writeFile ( path + "/" + manifest . scope + "/" + newName , JSON . stringify ( XMLspritesheetsToJson ( result ) ) , function ( err ) { if ( err ) { return console . error ( err ) ; } resolve ( ) ; } ) ; } } ) ; } } ) ; } ) ; } else if ( oldName . indexOf ( ".js" ) == oldName . length - 3 ) { return new Promise ( function ( resolve , reject ) { fs . readFile ( path + "/" + manifest . scope + "/" + oldName , function ( err , data ) { if ( err ) { return console . error ( err ) ; } else { var spritesheetContent ; eval ( "spritesheetContent = " + data ) ; ( function stringifyFunctions ( o ) { for ( x in o ) { if ( typeof o [ x ] == "object" ) { stringifyFunctions ( o [ x ] ) ; } if ( typeof o [ x ] == "function" ) { o [ x ] = o [ x ] . toString ( ) ; } } } ) ( spritesheetContent ) ; var newName = oldName . split ( ".js" ) . join ( ".json" ) ; fs . writeFile ( path + "/" + manifest . scope + "/" + newName , JSON . stringify ( spritesheetContent ) , function ( err ) { if ( err ) { return console . error ( err ) ; } resolve ( ) ; } ) ; } } ) ; } ) ; } } ) ; Promise . all ( levels . concat ( spritesheets ) ) . then ( function ( x ) { console . log ( "Updating manifest..." ) ; fs . writeFile ( path + "/manifest.json" , JSON . stringify ( manifest ) , function ( err ) { if ( err ) { return console . error ( err ) ; } resolvef ( ) ; } ) ; } ) ; } ) ; }
Run all the preprocessors on the package
Run all the preprocessors on the package
//Convert xml spritesheets to json //If it is an XML file, convert to JSON //If it a .js file, stringify functions //Stringify functions