Last active
June 11, 2016 14:34
-
-
Save poychang/bd065ce5b118752338926ae6c45e3054 to your computer and use it in GitHub Desktop.
Create Android resource for app name
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 | |
/* jshint esversion: 6 */ | |
// ref: https://developer.android.com/training/basics/supporting-devices/languages.html | |
// Save hook under `project-root/hooks/after_compile/` | |
// | |
// Don't forget to install xml2js using npm | |
// `$ npm install xml2js` | |
// Modules | |
var fs = require('fs'); | |
var xml2js = require('xml2js'); | |
// Process | |
var cliCommand = process.env.CORDOVA_CMDLINE; | |
var isRelease = (cliCommand.indexOf('--release') > -1); | |
var isBuildAndroid = (cliCommand.indexOf('build') > -1) && (cliCommand.indexOf('android') > -1); | |
// var isBuildIOS = (cliCommand.indexOf('build') > -1) && (cliCommand.indexOf('ios') > -1); | |
// Exit | |
if (!isRelease) { | |
return; | |
} | |
// Run add Android resource | |
run(); | |
function run() { | |
if (isBuildAndroid) addAndroidResource(); | |
} | |
function addAndroidResource() { | |
var files = [{ | |
folder: 'platforms/android/res/values-zh/', | |
file: 'string.xml', | |
data: ` | |
<?xml version='1.0' encoding='utf-8'?> | |
<resources> | |
<string name="app_name">App Name</string> | |
<string name="launcher_name">@string/app_name</string> | |
<string name="activity_name">@string/launcher_name</string> | |
</resources> | |
` | |
}]; | |
files.forEach(function (obj) { | |
var path = obj.folder + obj.file; | |
console.log(path); | |
if (checkDirectory(obj.folder)) { | |
xml2js.parseString(obj.data, function (err, result) { | |
// Build XML from JS Obj | |
var builder = new xml2js.Builder(); | |
var xml = builder.buildObject(result); | |
// Write to file | |
fs.writeFile(path, xml, function (err) { | |
if (err) throw err; | |
console.log('Save ' + path); | |
}); | |
}); | |
} else { | |
console.log('Faild to check directory.'); | |
} | |
}, this); | |
} | |
function checkDirectory(path) { | |
if (isDirectoryExists(path)) return true; | |
try { | |
fs.mkdirSync(path); | |
return true; | |
} | |
catch (err) { | |
return false; | |
} | |
} | |
function isDirectoryExists(path) { | |
try { | |
return fs.statSync(path).isDirectory(); | |
} | |
catch (err) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment