forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcordova.js
More file actions
249 lines (206 loc) · 6.93 KB
/
cordova.js
File metadata and controls
249 lines (206 loc) · 6.93 KB
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
var Task = require('./task').Task,
IonicStats = require('./stats').IonicStats,
fs = require('fs'),
Q = require('q'),
argv = require('optimist').argv,
path = require('path'),
exec = require('child_process').exec,
colors = require('colors');
var IonicTask = function() {};
IonicTask.prototype = new Task();
IonicTask.prototype.run = function(ionic) {
this.ionic = ionic;
var self = this;
var cmdName = process.argv[2].toLowerCase();
var q;
this.isLiveReload = ((cmdName == 'run' || cmdName == 'emulate') && (argv.livereload || argv['live-reload'] || argv.l));
if(this.isLiveReload) {
q = self.setupLiveReload();
} else {
// ensure the content node was set back to its original
q = self.ionic.setConfigXml({
resetContent: true,
errorWhenNotFound: false
});
}
q.then(function(){
self.runCordova(cmdName);
})
};
IonicTask.prototype.runCordova = function(cmdName) {
var self = this;
var cmdArgs = (process.argv.length > 3 ? process.argv.slice(3) : []);
var cmdArg, x, y;
// backwards compatibility prior to fully wrapping cordova cmds
if(cmdName == 'platform') {
// `ionic platform <PLATFORM>` used to actually run `ionic platform add <PLATFORM>`
// if a cordova platform cmd isn't the cmd then automatically insert `add`
var hasCordovaCmd = false;
var validCommands = 'add remove rm list ls update up check'.split(' ');
for(x=0; x<cmdArgs.length; x++) {
cmdArg = cmdArgs[x].toLowerCase();
for(y=0; y<validCommands.length; y++) {
if(cmdArg == validCommands[y]) {
hasCordovaCmd = true;
break;
}
}
}
if(!hasCordovaCmd) {
cmdArgs.unshift('add');
}
}
this.addHooks();
cmdArgs.unshift(cmdName);
// clean out any cmds that may confuse cordova
var cleanArgs = [];
var port = argv.port || argv.p || '';
var liveReloadPort = argv.livereloadport || argv['livereload-port'] || argv.i || '';
var ignoreCmds = '--livereload -l --consolelogs -c --serverlogs -s --port -p --livereload-port -i'.split(' ');
var isValdCmd;
for(x=0; x<cmdArgs.length; x++) {
cmdArg = cmdArgs[x];
if(port && cmdArg == port) continue;
if(liveReloadPort && cmdArg == liveReloadPort) continue;
isValdCmd = true;
for(y=0; y<ignoreCmds.length; y++) {
if(cmdArg == ignoreCmds[y]) {
isValdCmd = false;
break;
}
}
if(isValdCmd) {
// make sure --target= has double quotes around it (process.argv removes them)
if(cmdArg.indexOf('--target=') === 0 && cmdArg.indexOf('"') === -1) {
cmdArg = cmdArg.replace('--target=', '--target="') + '"';
}
cleanArgs.push(cmdArg);
}
}
var cordovaProcess = exec('cordova ' + cleanArgs.join(' '));
cordovaProcess.stdout.on('data', function (data) {
process.stdout.write(data);
});
cordovaProcess.stderr.on('data', function (data) {
if(data) {
process.stderr.write(data.toString().error.bold);
}
});
if(self.isLiveReload) {
cordovaProcess.on('exit', function(){
setTimeout(function(){
// set it back to the original src after a few seconds
self.ionic.setConfigXml({
resetContent: true,
errorWhenNotFound: true
});
}, 5000);
});
process.on('exit', function(){
// verify it was set back
self.ionic.setConfigXml({
resetContent: true,
errorWhenNotFound: false
});
});
var readLine = require("readline");
if(process.platform === "win32") {
var rl = readLine.createInterface ({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function (){
process.emit("SIGINT");
});
}
process.on("SIGINT", function(){
process.exit();
});
}
IonicStats.t();
};
IonicTask.prototype.setupLiveReload = function() {
var d = Q.defer();
console.log('Setup Live Reload'.green.bold);
var self = this;
var serve = new require('./serve');
var serveTask = new serve.IonicTask();
serveTask.ionic = this.ionic;
serveTask.isPlatformServe = true;
serveTask.loadSettings(function(){
serveTask.runLivereload = true;
serveTask.launchBrowser = false;
serveTask.start(self.ionic);
self.ionic.setConfigXml({
devServer: serveTask.devServer
}).then(function(){
d.resolve();
});
});
return d.promise;
};
IonicTask.prototype.addHooks = function() {
// Add hooks which this Ionic project doesn't already have
// note: hook scripts must be executable!
if( !fs.existsSync(path.join('www')) ) {
// don't both doing any of this if they aren't
// in the correct working directory, which would have `www`
return;
}
// loop through all the hook directories added to the ionic-cli
var cliHooksPath = path.join(__filename, '../../hooks');
fs.readdir(cliHooksPath, function(err, files){
if(err) return;
for(var x=0; x<files.length; x++) {
if(files[x].indexOf('.') > -1) continue;
addCliHookDirectory( path.join(cliHooksPath, files[x]), files[x] );
}
});
function addCliHookDirectory(cliHookPath, hookDirectoryName) {
fs.readdir(cliHookPath, function(err, files){
// loop through each of the scripts in the ionic-cli hook directory
if(err) return;
for(var x=0; x<files.length; x++) {
var hookFilename = files[x];
if(hookFilename.indexOf('.js') === -1) return;
// check if this hook script has already been added to this ionic project
var projectHookPath = path.join('hooks', hookDirectoryName, hookFilename);
addHookScript(cliHookPath, hookDirectoryName, hookFilename);
}
});
}
function addHookScript(cliHookPath, hookDirectoryName, hookFilename) {
// add the root hooks directory if the project doesn't have it
try {
var projectHookPath = path.join('hooks');
if( !fs.existsSync(projectHookPath) ) {
fs.mkdirSync(projectHookPath);
}
// add the hook directory (ie: after_prepare) if the project doesn't have it
projectHookPath = path.join(projectHookPath, hookDirectoryName);
if( !fs.existsSync(projectHookPath) ) {
fs.mkdirSync(projectHookPath);
}
var projectScript = path.join(projectHookPath, hookFilename);
if( !fs.existsSync(projectScript) ) {
// copy the hook script to the project
try {
var cliScript = path.join(cliHookPath, hookFilename);
fs.writeFileSync(projectScript, fs.readFileSync(cliScript));
} catch(e) {
console.log( ('addCliHookDirectory fs.createReadStream: ' + e).error );
return;
}
}
// make the script file executable
try {
fs.chmodSync(projectScript, '755');
} catch(e) {
console.log( ('addCliHookDirectory fs.chmodSync: ' + e).error );
}
} catch(e) {
console.log('Error adding hook script ' + hookDirectoryName + '/' + hookFilename + ', ' + e);
}
}
};
exports.IonicTask = IonicTask;