Last active
May 9, 2016 20:31
-
-
Save xixilive/c0569e949b7c98234e5c to your computer and use it in GitHub Desktop.
Setup Android Keystore after android platform added in Ionic/Cordova project
This file contains hidden or 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 | |
| /** | |
| * Setup android keystore | |
| * Save this script in hooks/after_paltform_add/ | |
| * And make sure it has x permission | |
| */ | |
| var exec = require('child_process').exec; | |
| var path = require('path'); | |
| var fs = require('fs'); | |
| var format = require('util').format; | |
| var androidPath = path.resolve(__dirname, '../../platforms/android'); | |
| var packageJSON = require('../../package.json'); | |
| //create .properties files | |
| var create_prop_file = function(filename, content){ | |
| var dest_file = path.resolve(androidPath, filename); | |
| fs.writeFileSync(dest_file, content); | |
| }; | |
| var update_customer_rules = function(){ | |
| var xmlFile = path.resolve(androidPath, 'custom_rules.xml'); | |
| fs.readFile(xmlFile, 'utf-8', function(err, xml){ | |
| if(err){ | |
| console.log(err); | |
| return; | |
| } | |
| //try to find property | |
| if(/\<property\s+file=\"secure\.properties\"\s*\/\>/i.test(xml)){ | |
| return; | |
| } | |
| xml = xml.replace(/\<\/project\>/ig, "\n\t\t<property file=\"secure.properties\" />\n</project>"); | |
| fs.writeFileSync(xmlFile, xml, {encoding: 'utf-8'}); | |
| }); | |
| }; | |
| var generateKeystore = function(name, storePass, keyPass){ | |
| keyPass = keyPass || storePass; | |
| var cmd = 'keytool -genkey -v'; | |
| cmd += ' -keystore ' + path.resolve(androidPath, name +'.keystore'); | |
| cmd += ' -storepass "' + storePass + '"'; | |
| cmd += ' -keypass "' + keyPass + '"'; | |
| cmd += ' -alias "' + name + '"'; | |
| cmd += ' -keyalg RSA'; | |
| cmd += ' -keysize 2048'; | |
| cmd += ' -validity 10000'; | |
| cmd += ' -dname "CN=LK,OU=LINKINWAYS,O=Brainet,L=SH,ST=SH,C=ZH"'; | |
| exec(cmd, function(err, stdout, stderr){ | |
| if(err){ | |
| console.error(stdout, err); | |
| return; | |
| } | |
| create_prop_file('ant.properties', format("key.store=%s.keystore\nkey.alias=%s", name, name)); | |
| create_prop_file('secure.properties', format("key.store.password=%s\nkey.alias.password=%s", storePass, storePass)); | |
| update_customer_rules(); | |
| console.log("Android keystore file %s created, keystore password is %s, and key password is %s", name, storePass, keyPass); | |
| }); | |
| }; | |
| if(fs.existsSync(androidPath)){ | |
| generateKeystore(packageJSON.name, 'passwd%' + packageJSON.name.toUpperCase()); | |
| } | |
| /* | |
| * modulize | |
| */ | |
| // module.exports = generateKeystore; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment