-
Notifications
You must be signed in to change notification settings - Fork 111
/
apply-diagnostic-modules.js
51 lines (40 loc) · 1.74 KB
/
apply-diagnostic-modules.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
// a workaround for cordova-diagnostic-plugin: the plugin ignores Capacitor config setting and installs all its features
const fs = require('fs')
const path = require('path')
const rootdir = ''
const pluginConfig = path.join(rootdir, 'node_modules/cordova.plugins.diagnostic/plugin.xml')
const configFiles = [pluginConfig]
const usedModules = ['CAMERA', 'MICROPHONE']
const diagnosticModuleStartRegex = getModuleFeatureStartRegex('.+')
function getModuleFeatureStartRegex(module) {
return RegExp(`(<!--BEGIN_MODULE (?<moduleName>${module.toUpperCase()})-->).*`, 'g')
}
function getModuleFeatureEndRegex(module) {
return RegExp(`.*(<!--END_MODULE (?<moduleName>${module.toUpperCase()})-->)`, 'g')
}
function removeUnusedModules(configFile) {
fs.readFile(configFile, 'utf8', function (err, data) {
if (err) {
return console.log(err)
}
let result = data
let unusedModules = []
while (match = diagnosticModuleStartRegex.exec(result)) {
const moduleName = match.groups.moduleName
const isUsed = usedModules.map(function (item) { return item.toUpperCase() }).indexOf(moduleName.toUpperCase()) > -1
if (!isUsed) {
unusedModules.push(moduleName)
}
}
if (unusedModules.length > 0) {
result = result.replace(getModuleFeatureStartRegex(unusedModules.join('|')), '$1 <!--')
result = result.replace(getModuleFeatureEndRegex(unusedModules.join('|')), '--> $1')
fs.writeFile(configFile, result, 'utf8', function (err) {
if (err) {
console.log(err)
}
})
}
})
}
configFiles.forEach(configFile => removeUnusedModules(configFile));