forked from SamVerschueren/cordova-config
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
393 lines (333 loc) · 10 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
'use strict';
/**
* This module represents the config.xml file.
*
* @author Sam Verschueren <[email protected]>
* @since 20 May 2015
*/
// module dependencies
const fs = require('fs-extra');
const et = require('elementtree');
const semver = require('semver');
module.exports = (function () {
const _this = {
/**
* This method parses the xml file provided.
*
* @param {string} file The XML file that should be parsed.
* @return {ElementTree} The XML document.
*/
parse: file => {
let contents = fs.readFileSync(file, 'utf-8');
if (contents) {
// Windows is the BOM. Skip the Byte Order Mark.
contents = contents.substring(contents.indexOf('<'));
}
const doc = new et.ElementTree(et.XML(contents)); // eslint-disable-line new-cap
const root = doc.getroot();
if (root.tag !== 'widget') {
// Throw an error if widget is not the root tag
throw new Error(file + ' has incorrect root node name (expected "widget", was "' + root.tag + '")');
}
return doc;
}
};
/**
* Creates a new Config object.
*
* @param {string} file The path to the XML config file.
*/
function Config(file) {
this._file = file;
this._doc = _this.parse(file);
this._root = this._doc.getroot();
}
/**
* Sets the ID of the config file.
*
* @param {string} id The ID of the config file.
*/
Config.prototype.setID = function (id) {
const regex = new RegExp('^[0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,\'/\\+&%$#_]*)?$');
if (!regex.test(id)) {
// If the id is not IRI, throw an error.
throw new Error('Please provide a valid id.');
}
// Set the id of the widget tag
this._root.attrib.id = id;
};
/**
* Sets the name tag of the config.xml file.
*
* @param {string} name The name of the config.xml name tag.
*/
Config.prototype.setName = function (name) {
this.setElement('name', name);
};
/**
* Sets a named element in the config.xml file.
*
* @param {string} tag The config.xml tag to set
* @param {string} [text] The text to set
* @param {object} [attribs] The attributes to set
*/
Config.prototype.setElement = function (tag, text, attribs) {
if (typeof text === 'object') {
attribs = text;
text = '';
}
// Find the tag
let elementTag = this._doc.find('./' + tag);
if (!elementTag) {
// If no tag exists, create one
elementTag = new et.Element(tag);
// Add the tag to the root
this._root.append(elementTag);
}
// Set the text of the tag
elementTag.text = text || '';
elementTag.attrib = {};
if (attribs !== undefined) {
Object.keys(attribs).forEach(key => {
elementTag.set(key, attribs[key]);
});
}
};
/**
* Sets the description tag of the config.xml file.
*
* @param {string} description The description of the config.xml description tag.
*/
Config.prototype.setDescription = function (description) {
this.setElement('description', description);
};
/**
* Sets the author in the config file.
*
* @param {string} name The name of the author.
* @param {string} [email] The email address of the author.
* @param {string} [website] The website of the author.
*/
Config.prototype.setAuthor = function (name, email, website) {
const attribs = {};
if (email) {
attribs.email = email;
}
if (website) {
attribs.href = website;
}
this.setElement('author', name, attribs);
};
/**
* Sets the version of the config file.
*
* @param {string} version The version number.
*/
Config.prototype.setVersion = function (version) {
// Const regex = new RegExp('^[0-9]+.[0-9]+.[0-9]+$');
if (!semver.valid(version)) {
// If the version is not valid, throw an error.
throw new Error('Please provide a valid version number.');
}
// Set the version of the widget tag
this._root.attrib.version = version;
};
/**
* Sets the Android version code of the config file.
*
* @param {number} versionCode The android version code.
*/
Config.prototype.setAndroidVersionCode = function (versionCode) {
const regex = new RegExp('^[0-9]+$'); // eslint-disable-line unicorn/regex-shorthand
if (!regex.test(versionCode)) {
// If the version is not valid, throw an error.
throw new Error('Please provide a valid Android version code.');
}
// Set the version of the widget tag
this._root.attrib['android-versionCode'] = versionCode;
};
/**
* Sets the Android package name of the config file.
*
* @param {string} packageName The android package name.
*/
Config.prototype.setAndroidPackageName = function (packageName) {
const regex = new RegExp('^[\\w.]+$');
if (!regex.test(packageName)) {
// If the name is not valid, throw an error.
throw new Error('Please provide a valid Android package name.');
}
// Set the version of the widget tag
this._root.attrib['android-packageName'] = packageName;
};
/**
* Sets the iOS CFBundleVersion of the config file.
*
* @param {string} version The iOS CFBundleVersion.
*/
Config.prototype.setIOSBundleVersion = function (version) {
const regex = new RegExp('^[1-9][0-9]*(.[0-9]+){0,2}$'); // eslint-disable-line unicorn/regex-shorthand
if (!regex.test(version)) {
// If the version is not valid, throw an error.
throw new Error('Please provide a valid iOS bundle version number.');
}
// Set the version of the widget tag
this._root.attrib['ios-CFBundleVersion'] = version;
};
/**
* Sets the iOS CFBundleIdentifier of the config file.
*
* @param {string} identifier The iOS CFBundleIdentifier.
*/
Config.prototype.setIOSBundleIdentifier = function (identifier) {
const regex = new RegExp('^[\\w.]+$');
if (!regex.test(identifier)) {
// If the identifier is not valid, throw an error.
throw new Error('Please provide a valid iOS bundle identifier number.');
}
// Set the identifier of the widget tag
this._root.attrib['ios-CFBundleIdentifier'] = identifier;
};
/**
* Adds or updates the preference `name` with the
* @param {string} name The name of the preference.
* @param {any} value The value of the preference.
*/
Config.prototype.setPreference = function (name, value) {
// Retrieve the correct preference
let preference = this._doc.find('./preference/[@name="' + name + '"]');
if (preference) {
// If the preference already exists, remove it first
this._root.remove(preference);
}
// Create the preference element
preference = new et.Element('preference');
preference.attrib.name = name;
preference.attrib.value = value;
// Append the preference to the root tag
this._root.append(preference);
};
/**
* Removes all the <access /> tags out of the config file.
*/
Config.prototype.removeAccessOrigins = function () {
// Find all the access tags and remove them
this._doc.findall('./access').forEach(tag => {
// Remove the tag
this._root.remove(tag);
});
};
/**
* Removes the access origin tag from the XML file if it exists.
*
* @param {string} origin The origin that should be removed.
*/
Config.prototype.removeAccessOrigin = function (origin) {
const accessOrigin = this._doc.find('./access/[@origin="' + origin + '"]');
if (accessOrigin) {
// If the access tag exists, remove it
this._root.remove(accessOrigin);
}
};
/**
* Adds a new <access /> tag to the XML file. If an access tag with that origin
* already exist, it will be overwritten.
*
* @param {string} origin The origin of the access tag.
* @param {object} [options] Extra properties that should be added to the access tag.
*/
Config.prototype.setAccessOrigin = function (origin, options) {
options = options || {};
// Remove the origin if it already exists.
this.removeAccessOrigin(origin);
// Create an access tag
const accessOrigin = new et.Element('access');
accessOrigin.attrib.origin = origin;
Object.keys(options).forEach(key => {
// Iterate over the options object and add the properties
accessOrigin.attrib[key] = options[key];
});
// Add the access tag to the root
this._root.append(accessOrigin);
};
/**
* Adds the hook with type and src.
* see [Apache Cordova API Documentation](https://goo.gl/5QZlqu) for more info
*
* @param {string} type The cordova hook type.
* @param {string} src The source of the script.
*/
Config.prototype.addHook = function (type, src) {
const cordovaHookTypes = [
'after_build',
'after_compile',
'after_clean',
'after_docs',
'after_emulate',
'after_platform_add',
'after_platform_rm',
'after_platform_ls',
'after_plugin_add',
'after_plugin_ls',
'after_plugin_rm',
'after_plugin_search',
'after_plugin_install',
'after_prepare',
'after_run',
'after_serve',
'before_build',
'before_clean',
'before_compile',
'before_docs',
'before_emulate',
'before_platform_add',
'before_platform_rm',
'before_platform_ls',
'before_plugin_add',
'before_plugin_ls',
'before_plugin_rm',
'before_plugin_search',
'before_plugin_install',
'before_plugin_uninstall',
'before_prepare',
'before_run',
'before_serve',
'pre_package'
];
if (cordovaHookTypes.indexOf(type) === -1) {
throw new Error('Please provide a valid hook target');
}
// Create the hook element
const hook = new et.Element('hook');
hook.attrib.type = type;
hook.attrib.src = src;
// Append the hook to the root tag
this._root.append(hook);
};
/**
* This method adds the raw XML provided to the config.xml file.
*
* @param {string} raw The raw XML that should be added to the config file.
*/
Config.prototype.addRawXML = function (raw) {
// Parse the raw XML
const xml = et.XML(raw); // eslint-disable-line new-cap
// Append the XML
this._root.append(xml);
};
/**
* Writes the config file async.
*
* @returns {Promise} A promise that resolves when the file is written.
*/
Config.prototype.write = function () {
return fs.writeFile(this._file, this._doc.write({indent: 4}), 'utf-8');
};
/**
* The same as `write` but sync.
*/
Config.prototype.writeSync = function () {
fs.writeFileSync(this._file, this._doc.write({indent: 4}), 'utf-8');
};
return Config;
})();