forked from mablack/cordova-diagnostic-plugin
/
apply-modules.js
166 lines (136 loc) · 4.35 KB
/
apply-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
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
#!/usr/bin/env node
/**********
* Globals
**********/
const PLUGIN_NAME = "Diagnostic plugin";
const PLUGIN_ID = "cordova.plugins.diagnostic";
const PREFERENCE_NAME = PLUGIN_ID + ".modules";
const MODULES = [
"LOCATION",
"BLUETOOTH",
"WIFI",
"CAMERA",
"NOTIFICATIONS",
"MICROPHONE",
"CONTACTS",
"CALENDAR",
"REMINDERS",
"MOTION",
"NFC",
"EXTERNAL_STORAGE",
"AIRPLANE_MODE"
];
const COMMENT_START = "<!--";
const COMMENT_END = "-->";
// Node dependencies
var path, cwd, fs;
// External dependencies
var et;
// Internal dependencies
var logger;
var projectPath, modulesPath, pluginNodePath, pluginScriptsPath, configXmlPath, pluginXmlPath, configXmlData, pluginXmlText;
/*********************
* Internal functions
*********************/
var run = function (){
var configuredModules = getSelectedModules();
logger.verbose("Modules: " + configuredModules);
readPluginXml();
enableAllModules();
if(configuredModules){
MODULES.forEach(function(module){
if(configuredModules.indexOf(module) === -1){
disableModule(module);
}
});
}
writePluginXml();
};
var handleError = function (error) {
error = PLUGIN_NAME + " - ERROR: " + error;
if(logger){
logger.error(error);
}else{
console.log(error);
console.error(error)
}
return error;
};
// Parses a given file into an elementtree object
var parseElementtreeSync = function(filename) {
var contents = fs.readFileSync(filename, 'utf-8');
if(contents) {
//Windows is the BOM. Skip the Byte Order Mark.
contents = contents.substring(contents.indexOf('<'));
}
return new et.ElementTree(et.XML(contents));
};
// Parses the config.xml into an elementtree object and stores in the config object
var getConfigXml = function() {
if(!configXmlData) {
configXmlData = parseElementtreeSync(configXmlPath);
}
return configXmlData;
};
var readPluginXml = function(){
pluginXmlText = fs.readFileSync(pluginXmlPath, 'utf-8');
};
var writePluginXml = function(){
fs.writeFileSync(pluginXmlPath, pluginXmlText, 'utf-8');
};
var getSelectedModules = function(){
var modules = null;
var preference = getConfigXml().findall("preference[@name='"+PREFERENCE_NAME+"']")[0];
if(preference){
modules = preference.attrib.value.split(' ');
}
return modules;
};
var enableAllModules = function(){
MODULES.forEach(function(module){
var commentedStartRegExp = new RegExp(getModuleStart(module)+COMMENT_START, "g");
var commentedEndRegExp = new RegExp(COMMENT_END+getModuleEnd(module), "g");
if(pluginXmlText.match(commentedStartRegExp)){
pluginXmlText = pluginXmlText.replace(commentedStartRegExp, getModuleStart(module));
pluginXmlText = pluginXmlText.replace(commentedEndRegExp, getModuleEnd(module));
}
});
};
var disableModule = function(module){
var commentedStart = getModuleStart(module)+COMMENT_START;
var commentedEnd = COMMENT_END+getModuleEnd(module);
pluginXmlText = pluginXmlText.replace(new RegExp(getModuleStart(module), "g"), commentedStart);
pluginXmlText = pluginXmlText.replace(new RegExp(getModuleEnd(module), "g"), commentedEnd);
};
var getModuleStart = function(module){
return "<!--BEGIN_MODULE "+module+"-->";
};
var getModuleEnd = function(module){
return "<!--END_MODULE "+module+"-->";
};
/**********
* Main
**********/
var main = function() {
try{
fs = require('fs');
path = require('path');
cwd = path.resolve();
pluginNodePath = cwd;
modulesPath = path.resolve(pluginNodePath, "..");
projectPath = path.resolve(modulesPath, "..");
pluginScriptsPath = path.resolve(pluginNodePath, "scripts");
logger = require(path.resolve(pluginScriptsPath, "logger.js"))(modulesPath, PLUGIN_ID);
et = require(path.resolve(modulesPath, "elementtree"));
}catch(e){
handleError("Failed to load dependencies. If using cordova@6 CLI, ensure this plugin is installed with the --fetch option or run 'npm install "+PLUGIN_ID+"': " + e.message);
}
try{
configXmlPath = path.join(projectPath, 'config.xml');
pluginXmlPath = path.join(pluginNodePath, "plugin.xml");
run();
}catch(e){
handleError(e.message);
}
};
main();