forked from platanus/bitrise-step-cordova-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (66 loc) · 2.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const Config = require('cordova-config');
const XmlReader = require('xml-reader');
const xmlQuery = require('xml-query');
const path = require('path');
const fs = require('fs');
const {log} = require('./lib/utils');
const SOURCE_PATH = process.env['BITRISE_SOURCE_DIR'];
const CONFIG_FILE_PATH = process.env['config_path'] || 'config.xml';
const NAME = process.env['name'];
const ID = process.env['id'];
const VERSION = process.env['app_version'];
const ANDROID_VERSION_CODE = process.env['android_version_code'];
const ANDROID_PACKAGE_NAME = process.env['android_package_name'];
const IOS_BUNDLE_VERSION = process.env['ios_bundle_version'];
const IOS_BUNDLE_IDENTIFIER = process.env['ios_bundle_identifier'];
// Load and parse the config.xml
const config = new Config(path.join(SOURCE_PATH, CONFIG_FILE_PATH));
if (NAME) {
config.setName(NAME);
log('name', NAME);
}
if (ID) {
config.setID(ID);
log('id', ID);
}
if (VERSION) {
config.setVersion(VERSION);
log('version', VERSION);
}
if (ANDROID_VERSION_CODE) {
config.setAndroidVersionCode(ANDROID_VERSION_CODE);
log('android-versionCode', ANDROID_VERSION_CODE);
}
if (ANDROID_PACKAGE_NAME) {
config.setAndroidPackageName(ANDROID_PACKAGE_NAME);
log('android-packageName', ANDROID_PACKAGE_NAME);
}
if (IOS_BUNDLE_VERSION) {
config.setIOSBundleVersion(IOS_BUNDLE_VERSION);
log('ios-CFBundleVersion', IOS_BUNDLE_VERSION);
}
if (IOS_BUNDLE_IDENTIFIER) {
config.setIOSBundleIdentifier(IOS_BUNDLE_IDENTIFIER);
log('ios-CFBundleIdentifier', IOS_BUNDLE_IDENTIFIER);
}
// Write the config file
config.writeSync();
// Replace the id or package name in the google-services.json files
if (ID || ANDROID_PACKAGE_NAME) {
const pkg = ID ? ID : ANDROID_PACKAGE_NAME;
const xml = require('fs').readFileSync(path.join(SOURCE_PATH, CONFIG_FILE_PATH)).toString().replace(/<\?xml.*\?>/, '');
const ast = XmlReader.parseSync(xml);
const gsJsonFiles = [];
xmlQuery(ast).find('resource-file').each(node => {
const src = xmlQuery(node).attr('src');
if (/.*google-services.json/.test(src)) {
gsJsonFiles.push(xmlQuery(node).attr('src'))
}
});
gsJsonFiles.forEach(gsJsonFile => {
const gsJsonFilePath = path.join(SOURCE_PATH, gsJsonFile);
const gsData = JSON.stringify(fs.readFileSync(gsJsonFilePath, 'utf8')).replace(/(\\"package_name\\":(| )\\")(.*?)(\\")/, `$1${pkg}$4`);
fs.writeFileSync(gsJsonFilePath, JSON.parse(gsData), 'utf8');
log(`${gsJsonFilePath} package_name`, pkg);
});
}