bin/tich.js
function select(name, outfilename) {
        var regex = /\$tiapp\.(.*)\$/;

        if (!name) {
            if (fs.existsSync('./app/config.json')) {
                var alloyCfg = JSON.parse(fs.readFileSync("./app/config.json", "utf-8"));
                if (alloyCfg.global.theme) {
                    console.log('\nFound a theme in config.json, trying ' + chalk.cyan(alloyCfg.global.theme));
                    select(alloyCfg.global.theme);
                } else {
                    status();
                }
            }
        } else {
            // find the config name specified
            cfg.configs.forEach(function(config) {

                if (config.name === name) {
                    console.log('\nFound a config for ' + chalk.cyan(config.name) + '\n');
                    if (fs.existsSync('./app/config.json')) {
                        var alloyCfg = JSON.parse(fs.readFileSync("./app/config.json", "utf-8"));
                        if (alloyCfg.global.theme) {
                          var original = alloyCfg.global.theme;
                          alloyCfg.global.theme = name;
                          fs.writeFile("./app/config.json", JSON.stringify(alloyCfg, null, 2), function (err) {
                              if (err) return console.log(err);
                              console.log('\nChanging theme value in config.json from ' + chalk.cyan(original) + ' to ' + chalk.cyan(name));
                          });
                        }
                    }

                    for (var setting in config.settings) {

                        if (!config.settings.hasOwnProperty(setting)) continue;

                        if (setting != "properties" && setting != "raw") {

							var now = new Date();
                            var replaceWith = config.settings[setting]
                                .replace('$DATE$', now.toLocaleDateString())
                                .replace('$TIME$', now.toLocaleTimeString())
                                .replace('$DATETIME$', now.toLocaleString())
                                .replace('$TIME_EPOCH$', now.getTime().toString());

                            var matches = regex.exec(replaceWith);

                            if (matches && matches[1]) {
                                var propName = matches[1];
                                replaceWith = replaceWith.replace(regex, tiapp[propName]);
                            }

                            tiapp[setting] = replaceWith;

                            console.log('Changing ' + chalk.cyan(setting) + ' to ' + chalk.yellow(replaceWith));
                        }

                    }

                    if (config.settings.properties) {
                        for (var property in config.settings.properties) {

                            if (!config.settings.properties.hasOwnProperty(property)) continue;

                            var replaceWith = config.settings.properties[property]
                                .replace('$DATE$', new Date().toLocaleDateString())
                                .replace('$TIME$', new Date().toLocaleTimeString())
                                .replace('$DATETIME$', new Date().toLocaleString())
                                .replace('$TIME_EPOCH$', new Date().getTime().toString());


                            var matches = regex.exec(replaceWith);
                            if (matches && matches[1]) {
                                var propName = matches[1];
                                replaceWith = replaceWith.replace(regex, tiapp[propName]);
                            }

                            tiapp.setProperty(property, replaceWith);

                            console.log('Changing App property ' + chalk.cyan(property) + ' to ' + chalk.yellow(replaceWith));

                        }
                    }

                    if (config.settings.raw) {
                        var doc = tiapp.doc;
                        var select = xpath.useNamespaces({
                            "ti": "http://ti.appcelerator.org",
                            "android": "http://schemas.android.com/apk/res/android"
                        });
                        for (var path in config.settings.raw) {

                            if (!config.settings.raw.hasOwnProperty(path)) continue;

                            var node = select(path, doc, true);
                            if (!node) {
                                console.log(chalk.yellow('Could not find ' + path + ", skipping"));
                                continue;
                            }

                            var replaceWith = config.settings.raw[path]
                                .replace('$DATE$', new Date().toLocaleDateString())
                                .replace('$TIME$', new Date().toLocaleTimeString())
                                .replace('$DATETIME$', new Date().toLocaleString())
                                .replace('$TIME_EPOCH$', new Date().getTime().toString());


                            var matches = regex.exec(replaceWith);
                            if (matches && matches[1]) {
                                var propName = matches[1];
                                replaceWith = replaceWith.replace(regex, tiapp[propName]);
                            }



                            if (typeof(node.value) === 'undefined'){
                                node.firstChild.data = replaceWith;
                            }
                            else{
                                node.value = replaceWith;
                            }

                            console.log('Changing Raw property ' + chalk.cyan(path) + ' to ' + chalk.yellow(replaceWith));

                        }
                    }

                    if (fs.existsSync("./app/themes/" + name + "/assets/iphone/DefaultIcon.png")) {
                        // if it exists in the themes folder, in a platform subfolder
                        console.log(chalk.blue('Found a DefaultIcon.png in the theme\'s assets/iphone folder\n'));
                        copyFile("./app/themes/" + name + "/assets/iphone/DefaultIcon.png", "./DefaultIcon.png")
                    } else if (fs.existsSync("./app/themes/" + name + "/DefaultIcon.png")) {
                        // if it exists in the top level theme folder
                        console.log(chalk.blue('Found a DefaultIcon.png in the theme folder\n'));
                        copyFile("./app/themes/" + name + "/" + "/DefaultIcon.png", "./DefaultIcon.png")
                    }

                    console.log(chalk.green('\n' + outfilename + ' updated\n'));

                    tiapp.write(outfilename);

                }
            });

            //console.log(chalk.red('\nCouldn\'t find a config called: ' + name + '\n'));

        }
    }
function select(name, outfilename) {
        var regex = /\$tiapp\.(.*)\$/;

        if (!name) {
            if (fs.existsSync('./app/config.json')) {
                var alloyCfg = JSON.parse(fs.readFileSync("./app/config.json", "utf-8"));
                if (alloyCfg.global.theme) {
                    console.log('\nFound a theme in config.json, trying ' + chalk.cyan(alloyCfg.global.theme));
                    select(alloyCfg.global.theme);
                } else {
                    status();
                }
            }
        } else {
            // find the config name specified
            cfg.configs.forEach(function(config) {

                if (config.name === name) {
                    console.log('\nFound a config for ' + chalk.cyan(config.name) + '\n');
                    if (fs.existsSync('./app/config.json')) {
                        var alloyCfg = JSON.parse(fs.readFileSync("./app/config.json", "utf-8"));
                        if (alloyCfg.global.theme) {
                          var original = alloyCfg.global.theme;
                          alloyCfg.global.theme = name;
                          fs.writeFile("./app/config.json", JSON.stringify(alloyCfg, null, 2), function (err) {
                              if (err) return console.log(err);
                              console.log('\nChanging theme value in config.json from ' + chalk.cyan(original) + ' to ' + chalk.cyan(name));
                          });
                        }
                    }

                    for (var setting in config.settings) {

                        if (!config.settings.hasOwnProperty(setting)) continue;

                        if (setting != "properties" && setting != "raw") {

							var now = new Date();
                            var replaceWith = config.settings[setting]
                                .replace('$DATE$', now.toLocaleDateString())
                                .replace('$TIME$', now.toLocaleTimeString())
                                .replace('$DATETIME$', now.toLocaleString())
                                .replace('$TIME_EPOCH$', now.getTime().toString());

                            var matches = regex.exec(replaceWith);

                            if (matches && matches[1]) {
                                var propName = matches[1];
                                replaceWith = replaceWith.replace(regex, tiapp[propName]);
                            }

                            tiapp[setting] = replaceWith;

                            console.log('Changing ' + chalk.cyan(setting) + ' to ' + chalk.yellow(replaceWith));
                        }

                    }

                    if (config.settings.properties) {
                        for (var property in config.settings.properties) {

                            if (!config.settings.properties.hasOwnProperty(property)) continue;

                            var replaceWith = config.settings.properties[property]
                                .replace('$DATE$', new Date().toLocaleDateString())
                                .replace('$TIME$', new Date().toLocaleTimeString())
                                .replace('$DATETIME$', new Date().toLocaleString())
                                .replace('$TIME_EPOCH$', new Date().getTime().toString());


                            var matches = regex.exec(replaceWith);
                            if (matches && matches[1]) {
                                var propName = matches[1];
                                replaceWith = replaceWith.replace(regex, tiapp[propName]);
                            }

                            tiapp.setProperty(property, replaceWith);

                            console.log('Changing App property ' + chalk.cyan(property) + ' to ' + chalk.yellow(replaceWith));

                        }
                    }

                    if (config.settings.raw) {
                        var doc = tiapp.doc;
                        var select = xpath.useNamespaces({
                            "ti": "http://ti.appcelerator.org",
                            "android": "http://schemas.android.com/apk/res/android"
                        });
                        for (var path in config.settings.raw) {

                            if (!config.settings.raw.hasOwnProperty(path)) continue;

                            var node = select(path, doc, true);
                            if (!node) {
                                console.log(chalk.yellow('Could not find ' + path + ", skipping"));
                                continue;
                            }

                            var replaceWith = config.settings.raw[path]
                                .replace('$DATE$', new Date().toLocaleDateString())
                                .replace('$TIME$', new Date().toLocaleTimeString())
                                .replace('$DATETIME$', new Date().toLocaleString())
                                .replace('$TIME_EPOCH$', new Date().getTime().toString());


                            var matches = regex.exec(replaceWith);
                            if (matches && matches[1]) {
                                var propName = matches[1];
                                replaceWith = replaceWith.replace(regex, tiapp[propName]);
                            }



                            if (typeof(node.value) === 'undefined'){
                                node.firstChild.data = replaceWith;
                            }
                            else{
                                node.value = replaceWith;
                            }

                            console.log('Changing Raw property ' + chalk.cyan(path) + ' to ' + chalk.yellow(replaceWith));

                        }
                    }

                    if (fs.existsSync("./app/themes/" + name + "/assets/iphone/DefaultIcon.png")) {
                        // if it exists in the themes folder, in a platform subfolder
                        console.log(chalk.blue('Found a DefaultIcon.png in the theme\'s assets/iphone folder\n'));
                        copyFile("./app/themes/" + name + "/assets/iphone/DefaultIcon.png", "./DefaultIcon.png")
                    } else if (fs.existsSync("./app/themes/" + name + "/DefaultIcon.png")) {
                        // if it exists in the top level theme folder
                        console.log(chalk.blue('Found a DefaultIcon.png in the theme folder\n'));
                        copyFile("./app/themes/" + name + "/" + "/DefaultIcon.png", "./DefaultIcon.png")
                    }

                    console.log(chalk.green('\n' + outfilename + ' updated\n'));

                    tiapp.write(outfilename);

                }
            });

            //console.log(chalk.red('\nCouldn\'t find a config called: ' + name + '\n'));

        }
    }
function select ( name , outfilename ) { var regex = / \$tiapp\.(.*)\$ / ; if ( ! name ) { if ( fs . existsSync ( './app/config.json' ) ) { var alloyCfg = JSON . parse ( fs . readFileSync ( "./app/config.json" , "utf-8" ) ) ; if ( alloyCfg . global . theme ) { console . log ( '\nFound a theme in config.json, trying ' + chalk . cyan ( alloyCfg . global . theme ) ) ; select ( alloyCfg . global . theme ) ; } else { status ( ) ; } } } else { // find the config name specified cfg . configs . forEach ( function ( config ) { if ( config . name === name ) { console . log ( '\nFound a config for ' + chalk . cyan ( config . name ) + '\n' ) ; if ( fs . existsSync ( './app/config.json' ) ) { var alloyCfg = JSON . parse ( fs . readFileSync ( "./app/config.json" , "utf-8" ) ) ; if ( alloyCfg . global . theme ) { var original = alloyCfg . global . theme ; alloyCfg . global . theme = name ; fs . writeFile ( "./app/config.json" , JSON . stringify ( alloyCfg , null , 2 ) , function ( err ) { if ( err ) return console . log ( err ) ; console . log ( '\nChanging theme value in config.json from ' + chalk . cyan ( original ) + ' to ' + chalk . cyan ( name ) ) ; } ) ; } } for ( var setting in config . settings ) { if ( ! config . settings . hasOwnProperty ( setting ) ) continue ; if ( setting != "properties" && setting != "raw" ) { var now = new Date ( ) ; var replaceWith = config . settings [ setting ] . replace ( '$DATE$' , now . toLocaleDateString ( ) ) . replace ( '$TIME$' , now . toLocaleTimeString ( ) ) . replace ( '$DATETIME$' , now . toLocaleString ( ) ) . replace ( '$TIME_EPOCH$' , now . getTime ( ) . toString ( ) ) ; var matches = regex . exec ( replaceWith ) ; if ( matches && matches [ 1 ] ) { var propName = matches [ 1 ] ; replaceWith = replaceWith . replace ( regex , tiapp [ propName ] ) ; } tiapp [ setting ] = replaceWith ; console . log ( 'Changing ' + chalk . cyan ( setting ) + ' to ' + chalk . yellow ( replaceWith ) ) ; } } if ( config . settings . properties ) { for ( var property in config . settings . properties ) { if ( ! config . settings . properties . hasOwnProperty ( property ) ) continue ; var replaceWith = config . settings . properties [ property ] . replace ( '$DATE$' , new Date ( ) . toLocaleDateString ( ) ) . replace ( '$TIME$' , new Date ( ) . toLocaleTimeString ( ) ) . replace ( '$DATETIME$' , new Date ( ) . toLocaleString ( ) ) . replace ( '$TIME_EPOCH$' , new Date ( ) . getTime ( ) . toString ( ) ) ; var matches = regex . exec ( replaceWith ) ; if ( matches && matches [ 1 ] ) { var propName = matches [ 1 ] ; replaceWith = replaceWith . replace ( regex , tiapp [ propName ] ) ; } tiapp . setProperty ( property , replaceWith ) ; console . log ( 'Changing App property ' + chalk . cyan ( property ) + ' to ' + chalk . yellow ( replaceWith ) ) ; } } if ( config . settings . raw ) { var doc = tiapp . doc ; var select = xpath . useNamespaces ( { "ti" : "http://ti.appcelerator.org" , "android" : "http://schemas.android.com/apk/res/android" } ) ; for ( var path in config . settings . raw ) { if ( ! config . settings . raw . hasOwnProperty ( path ) ) continue ; var node = select ( path , doc , true ) ; if ( ! node ) { console . log ( chalk . yellow ( 'Could not find ' + path + ", skipping" ) ) ; continue ; } var replaceWith = config . settings . raw [ path ] . replace ( '$DATE$' , new Date ( ) . toLocaleDateString ( ) ) . replace ( '$TIME$' , new Date ( ) . toLocaleTimeString ( ) ) . replace ( '$DATETIME$' , new Date ( ) . toLocaleString ( ) ) . replace ( '$TIME_EPOCH$' , new Date ( ) . getTime ( ) . toString ( ) ) ; var matches = regex . exec ( replaceWith ) ; if ( matches && matches [ 1 ] ) { var propName = matches [ 1 ] ; replaceWith = replaceWith . replace ( regex , tiapp [ propName ] ) ; } if ( typeof ( node . value ) === 'undefined' ) { node . firstChild . data = replaceWith ; } else { node . value = replaceWith ; } console . log ( 'Changing Raw property ' + chalk . cyan ( path ) + ' to ' + chalk . yellow ( replaceWith ) ) ; } } if ( fs . existsSync ( "./app/themes/" + name + "/assets/iphone/DefaultIcon.png" ) ) { // if it exists in the themes folder, in a platform subfolder console . log ( chalk . blue ( 'Found a DefaultIcon.png in the theme\'s assets/iphone folder\n' ) ) ; copyFile ( "./app/themes/" + name + "/assets/iphone/DefaultIcon.png" , "./DefaultIcon.png" ) } else if ( fs . existsSync ( "./app/themes/" + name + "/DefaultIcon.png" ) ) { // if it exists in the top level theme folder console . log ( chalk . blue ( 'Found a DefaultIcon.png in the theme folder\n' ) ) ; copyFile ( "./app/themes/" + name + "/" + "/DefaultIcon.png" , "./DefaultIcon.png" ) } console . log ( chalk . green ( '\n' + outfilename + ' updated\n' ) ) ; tiapp . write ( outfilename ) ; } } ) ; //console.log(chalk.red('\nCouldn\'t find a config called: ' + name + '\n')); } }
select a new config by name
select a new config by name