|
11 | 11 |
|
12 | 12 | // read branch config from config.xml
|
13 | 13 | function read (context) {
|
14 |
| - var configXml = getConfigXml(context) |
| 14 | + var projectRoot = getProjectRoot(context) |
| 15 | + var configXml = getConfigXml(projectRoot) |
15 | 16 | var branchXml = getBranchXml(configXml)
|
16 | 17 | var branchPreferences = getBranchPreferences(context, configXml, branchXml)
|
17 | 18 |
|
|
21 | 22 | }
|
22 | 23 |
|
23 | 24 | // read config.xml
|
24 |
| - function getConfigXml (context) { |
25 |
| - var projectRoot = getProjectRoot(context) |
| 25 | + function getConfigXml (projectRoot) { |
26 | 26 | var pathToConfigXml = path.join(projectRoot, 'config.xml')
|
27 | 27 | var configXml = xmlHelper.readXmlAsJson(pathToConfigXml)
|
28 | 28 |
|
|
46 | 46 |
|
47 | 47 | // read <branch-config> properties within config.xml
|
48 | 48 | function getBranchPreferences (context, configXml, branchXml) {
|
49 |
| - var projectRoot = getProjectRoot(context) |
50 |
| - var projectPlatform = getProjectPlatform(context) |
51 |
| - var bundleId = (configXml.widget['$'].hasOwnProperty('id')) ? configXml.widget['$']['id'] : null |
52 |
| - var bundleName = (configXml.widget.hasOwnProperty('name')) ? configXml.widget.name[0] : null |
53 |
| - var branchKey = (branchXml.hasOwnProperty('branch-key')) ? branchXml['branch-key'][0]['$']['value'] : null |
54 |
| - var linkDomain = (branchXml.hasOwnProperty('link-domain')) ? branchXml['link-domain'][0]['$']['value'] : null |
55 |
| - var uriScheme = (branchXml.hasOwnProperty('uri-scheme')) ? branchXml['uri-scheme'][0]['$']['value'] : null |
56 |
| - var iosTeamRelease = (branchXml.hasOwnProperty('ios-team-release')) ? branchXml['ios-team-release'][0]['$']['value'] : null |
57 |
| - var iosTeamDebug = (branchXml.hasOwnProperty('ios-team-debug')) ? branchXml['ios-team-debug'][0]['$']['value'] : null |
58 |
| - var androidPrefix = (branchXml.hasOwnProperty('android-prefix')) ? branchXml['android-prefix'][0]['$']['value'] : null |
59 |
| - var androidTestMode = (branchXml.hasOwnProperty('android-testmode')) ? branchXml['android-testmode'][0]['$']['value'] : 'false' |
60 |
| - |
61 | 49 | return {
|
62 |
| - 'projectRoot': projectRoot, |
63 |
| - 'projectPlatform': projectPlatform, |
64 |
| - 'bundleId': bundleId, |
65 |
| - 'bundleName': bundleName, |
66 |
| - 'branchKey': branchKey, |
67 |
| - 'uriScheme': uriScheme, |
68 |
| - 'linkDomain': linkDomain, |
69 |
| - 'iosTeamRelease': iosTeamRelease, |
70 |
| - 'iosTeamDebug': iosTeamDebug, // optional |
71 |
| - 'androidPrefix': androidPrefix, // optional |
72 |
| - 'androidTestMode': androidTestMode // optional |
| 50 | + 'projectRoot': getProjectRoot(context), |
| 51 | + 'projectName': getProjectName(configXml), |
| 52 | + 'branchKey': getBranchValue(branchXml, 'branch-key'), |
| 53 | + 'linkDomain': getBranchValue(branchXml, 'link-domain'), |
| 54 | + 'uriScheme': getBranchValue(branchXml, 'uri-scheme'), |
| 55 | + 'iosBundleId': getBundleId(configXml, 'ios'), |
| 56 | + 'iosProjectModule': getProjectModule(context), |
| 57 | + 'iosTeamRelease': getBranchValue(branchXml, 'ios-team-release'), |
| 58 | + 'iosTeamDebug': getBranchValue(branchXml, 'ios-team-debug'), // optional |
| 59 | + 'androidBundleId': getBundleId(configXml, 'android'), // optional |
| 60 | + 'androidPrefix': getBranchValue(branchXml, 'android-prefix'), // optional |
| 61 | + 'androidTestMode': getBranchValue(branchXml, 'android-testmode') // optional |
73 | 62 | }
|
74 | 63 | }
|
75 | 64 |
|
76 |
| - // read app project location |
| 65 | + // read project root from cordova context |
77 | 66 | function getProjectRoot (context) {
|
78 |
| - return context.opts.projectRoot |
| 67 | + return context.opts.projectRoot || null |
| 68 | + } |
| 69 | + |
| 70 | + // read project name from config.xml |
| 71 | + function getProjectName (configXml) { |
| 72 | + return (configXml.widget.hasOwnProperty('name')) ? configXml.widget.name[0] : null |
| 73 | + } |
| 74 | + |
| 75 | + // read branch value from <branch-config> |
| 76 | + function getBranchValue (branchXml, key) { |
| 77 | + return (branchXml.hasOwnProperty(key)) ? branchXml[key][0]['$']['value'] : null |
79 | 78 | }
|
80 | 79 |
|
81 |
| - // read project platform |
82 |
| - function getProjectPlatform (context) { |
| 80 | + // read bundle id from config.xml (optional values override widget-id) |
| 81 | + function getBundleId (configXml, platform) { |
| 82 | + var output = null |
| 83 | + var key = platform === 'ios' ? 'ios-CFBundleIdentifier' : 'android-packageName' |
| 84 | + |
| 85 | + if (configXml.widget['$'].hasOwnProperty(key)) { |
| 86 | + output = configXml.widget['$'][key] |
| 87 | + } else if (configXml.widget['$'].hasOwnProperty('id')) { |
| 88 | + output = configXml.widget['$']['id'] |
| 89 | + } |
| 90 | + |
| 91 | + return output |
| 92 | + } |
| 93 | + |
| 94 | + // read iOS project module from cordova context |
| 95 | + function getProjectModule (context) { |
83 | 96 | // try pre-5.0 cordova structure
|
84 | 97 | try {
|
85 | 98 | return context.requireCordovaModule('cordova-lib/src/plugman/platforms').ios
|
|
90 | 103 |
|
91 | 104 | // validate <branch-config> properties within config.xml
|
92 | 105 | function validateBranchPreferences (preferences) {
|
93 |
| - if (preferences.bundleId === null) { |
94 |
| - throw new Error('BRANCH SDK: Invalid "widget id" in your config.xml. Docs https://goo.gl/GijGKP') |
| 106 | + if (preferences.projectRoot === null) { |
| 107 | + throw new Error('BRANCH SDK: Invalid "root" in your config.xml. Docs https://goo.gl/GijGKP') |
| 108 | + } |
| 109 | + if (preferences.projectPlatform === null) { |
| 110 | + throw new Error('BRANCH SDK: Invalid "platform" in your config.xml. Docs https://goo.gl/GijGKP') |
95 | 111 | }
|
96 |
| - if (preferences.bundleName === null) { |
| 112 | + if (preferences.projectName === null) { |
97 | 113 | throw new Error('BRANCH SDK: Invalid "name" in your config.xml. Docs https://goo.gl/GijGKP')
|
98 | 114 | }
|
99 | 115 | if (preferences.branchKey === null) {
|
|
105 | 121 | if (preferences.linkDomain === null || !/^(?!.*?www).*([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)$/.test(preferences.linkDomain)) {
|
106 | 122 | throw new Error('BRANCH SDK: Invalid "link-domain" in <branch-config> in your config.xml. Docs https://goo.gl/GijGKP')
|
107 | 123 | }
|
| 124 | + if (preferences.iosBundleId === null || !/^[a-zA-Z0-9.]*$/.test(preferences.iosBundleId)) { |
| 125 | + throw new Error('BRANCH SDK: Invalid "id" or "ios-CFBundleIdentifier" in <widget> in your config.xml. Docs https://goo.gl/GijGKP') |
| 126 | + } |
108 | 127 | if (preferences.iosTeamRelease === null || !/^[a-zA-Z0-9]{10}$/.test(preferences.iosTeamRelease)) {
|
109 | 128 | throw new Error('BRANCH SDK: Invalid "ios-team-release" in <branch-config> in your config.xml. Docs https://goo.gl/GijGKP')
|
110 | 129 | }
|
111 | 130 | if (preferences.iosTeamDebug !== null && !/^[a-zA-Z0-9]{10}$/.test(preferences.iosTeamDebug)) {
|
112 | 131 | throw new Error('BRANCH SDK: Invalid "ios-team-debug" in <branch-config> in your config.xml. Docs https://goo.gl/GijGKP')
|
113 | 132 | }
|
| 133 | + if (preferences.androidBundleId !== null && !/^[a-zA-Z0-9.]*$/.test(preferences.androidBundleId)) { |
| 134 | + throw new Error('BRANCH SDK: Invalid "id" or "android-packageName" in <widget> in your config.xml. Docs https://goo.gl/GijGKP') |
| 135 | + } |
114 | 136 | if (preferences.androidPrefix !== null && !/^[/].[a-zA-Z0-9]{3}$/.test(preferences.androidPrefix)) {
|
115 | 137 | throw new Error('BRANCH SDK: Invalid "android-prefix" in <branch-config> in your config.xml. Docs https://goo.gl/GijGKP')
|
116 | 138 | }
|
117 |
| - if (!(preferences.androidTestMode === 'true' || preferences.androidTestMode === 'false')) { |
| 139 | + if (!(preferences.androidTestMode === 'true' || preferences.androidTestMode === 'false' || preferences.androidTestMode === null)) { |
118 | 140 | throw new Error('BRANCH SDK: Invalid "android-testmode" in <branch-config> in your config.xml. Docs https://goo.gl/GijGKP')
|
119 | 141 | }
|
120 | 142 | }
|
|
0 commit comments