-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommands.js
executable file
·393 lines (302 loc) · 11 KB
/
commands.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
var utils = require('./lib/utils'),
spawn = require('child_process').spawn,
exec = require('child_process').exec,
fs = require('fs'),
plist = require('plist'),
parser = new(require('xml2js')).Parser({
trim: true,
normalize: true,
explicitArray: false
});
var PROFILES_DIR = process.env['HOME'] + '/Library/MobileDevice/Provisioning Profiles/';
// var help = {
// 'i5, iphone5': 'Run project in iphone 5 simulator - iPhone (Retina 4-inch).',
// 'i4, iphone4': 'Run project in iphone 4 simulator - iPhone (Retina 3.5-inch).',
// 'i3, iphone3': 'Run project in iphone 3 simulator - iPhone.',
// 'di, deployios': 'Deploy to device without using iTunes :)',
// 'ri, reloadios': 'Just RELOAD the app in simulator - '+'Only the changes in JS files will have effect!'.yellow,
// 'ci, cleaniphone':'Clean the project and start fresh.',
// 'deli, deleteiphone':'Delete the aplication from the simulator'
// };
var help = {
'i5': 'Run project in iphone 5 simulator - iPhone (Retina 4-inch).',
'i4': 'Run project in iphone 4 simulator - iPhone (Retina 3.5-inch).',
'i3': 'Run project in iphone 3 simulator - iPhone.\n',
'ipadr' : 'Run project in ipad simulator - iPad (retina)',
'ipad' : 'Run project in ipad simulator - iPad\n',
'di': 'Deploy to device without using iTunes :)',
'di -i': 'Deploy to device the last build without recompiling - usefull if you switch devices\n',
'ri': 'Hot RELOAD the app in simulator - ' + 'Only the changes in JS files will have effect!'.yellow,
'c, clean': 'Clean the iOs project and start fresh.'
};
function getLastSession(params, callback) {
fs.exists(__dirname + '/session.json', function(exists) {
if (exists) {
fs.readFile(__dirname + '/session.json', function(err, data) {
if (!err && data) {
try {
var last_session = JSON.parse(data);
if (last_session && last_session) {
getTiapp(function(tiapp) {
callback(null, last_session);
});
}
}
catch(err) {
callback(err);
}
} else {
callback(err);
}
});
} else {
callback('no_session');
}
});
}
function execute(params, callback) {
getTiapp(function(tiapp) {
var prc = spawn('titanium', params);
prc.stdout.setEncoding('utf8');
prc.stdout.pipe(process.stdout);
prc.stderr.pipe(process.stderr);
var gotit = false;
prc.stdout.on('data', function(data) {
if (!gotit) {
gotit = true;
var running_app = exec('ps -eo comm|grep ' + tiapp.name + '.app|grep -v \'grep\'', function(error, stdout, stderr) {
if (error || !stdout) {
gotit = false;
} else {
getLastSession(params, function(err, res) {
var res = res || {};
res[tiapp.name] = res[tiapp.name] || {};
res[tiapp.name].app = stdout.trim()
fs.writeFile(__dirname + '/session.json', JSON.stringify(res), function(err) {
if (err) throw err;
utils.message('Session saved!', 'success');
});
})
}
});
}
});
prc.on('close', function(code) { !! callback && callback();
});
});
};
function install(params, callback) {
var prc = spawn(__dirname + '/bin/fruitstrap', params);
prc.stdout.setEncoding('utf8');
prc.stdout.pipe(process.stdout);
prc.stderr.pipe(process.stderr);
prc.on('close', function(code) { !! callback && callback();
});
};
function getIosEnv(callback) {
var prc = spawn('titanium', ['info', '-t', 'ios', '-o', 'json']);
prc.stdout.setEncoding('utf8');
var buf = '';
prc.stdout.on('data', function(data) {
buf = buf + data.toString();
});
prc.on('close', function(code) {
var data = JSON.parse(buf);
var out = {};
Object.keys(data.xcode).forEach(function(kc) {
if (data.xcode[kc].selected) {
out.sim = data.xcode[kc].sims.reverse();
out.device = data.xcode[kc].sdks.reverse();
}
});
!! callback && callback(out);
});
};
function tailAppLog(tiapp) {
var running_app = exec('ps -eo comm|grep ' + tiapp.name + '.app|grep -v \'grep\'', function(error, stdout, stderr) {
if (error || !stdout) {
utils.message('Cannot find the running app', error);
return;
}
else {
var log_file = stdout.replace(tiapp.name + '.app/' + tiapp.name + '\n', 'Documents/' + tiapp.guid + '.log').trim();
var tail = spawn('tail', ['-f', log_file]);
tail.stdout.setEncoding('utf8');
// tail.stdout.pipe(process.stdout);
tail.stdout.on('data', function(data) {
utils.logWatcher(error, data);
});
tail.stderr.on('data', function(data) {
utils.logWatcher(error, data);
});
tail.on('close', function(code) {
utils.message('The logger exited with the code: ' + code, 'warn');
});
}
});
};
function getTiapp(callback) {
fs.readFile(process.cwd() + '/tiapp.xml', 'utf-8', function(err, xml) {
if (err !== null) {
utils.message("\nIt seems that tiapp.xml it's not correct.\nPlease review it for possible XML errors.\n", 'error');
return;
}
parser.parseString(xml, function(err, tiapp) {
if (err !== null) {
utils.message("\nIt seems that tiapp.xml it's not correct.\nPlease review it for possible XML errors.\n", 'error');
return;
} !! callback && callback(tiapp['ti:app']);
});
});
};
function getProfiles(id, callback) {
var profiles = [];
fs.readdir(PROFILES_DIR, function(err, files) {
if (err !== null) {
callback(true, 'Error searching for provisioning profiles');
return;
}
var found_profile = false;
for (var i = 0; i < files.length; i++) {
if (files[i] !== '.DS_Store') {
var xml = fs.readFileSync(PROFILES_DIR + files[i], 'utf8'),
parsed_plist = plist.parseStringSync(xml.substring(xml.indexOf('<?xml'), xml.indexOf('</plist>') + 8)),
PlistAppIdPrefix = parsed_plist.ApplicationIdentifierPrefix,
PlistAppId = parsed_plist.Entitlements['application-identifier'],
PlistProfile = PlistAppId.replace(PlistAppIdPrefix + '.', '').replace('.*', '');
if (id.indexOf(PlistProfile) >= 0) {
if (new Date() > new Date(parsed_plist.ExpirationDate)) {
console.log(('Found an EXPIRED matching profile: ' + parsed_plist.Name.inverse + ' => ' + PlistAppId.inverse));
console.log('Trying to find another profile...');
} else {
console.log(('Found a VALID matching profile: "' + parsed_plist.Name.inverse + '" => ' + PlistAppId.inverse + '\nTrying with this one...\n\n').green);
callback(null, files[i].substring(0, files[i].indexOf('.')));
found_profile = true;
break;
}
}
}
}
!found_profile && callback('no_profile');
});
}
module.exports = {
help: function() {
var list = [];
for (var k in help) {
list.push(' ' + utils.rpad(k, 15) + help[k].grey);
}
var prompt = ["Available commands:".yellow.bold, "", list.join("\n"), ""];
console.log(prompt.join("\n"));
},
clean: function(tiapp) {
execute(['clean'], function() {
utils.message('STI done.');
});
},
ipad: function(tiapp){
execute(['build', '-p', 'ios', '-D', 'development', '-Y', 'ipad'], function() {
utils.message('Simulator should be started now...');
});
},
ipadr: function(tiapp){
execute(['build', '-p', 'ios', '-D', 'development', '-Y', 'ipad', '--retina'], function() {
utils.message('Simulator should be started now...');
});
},
i5: function(tiapp) {
execute(['build', '-p', 'ios', '-D', 'development', '--retina', '--tall'], function() {
utils.message('Simulator should be started now...');
});
},
i4: function(tiapp) {
execute(['build', '-p', 'ios', '-D', 'development', '--retina'], function() {
utils.message('Simulator should be started now...');
});
},
i3: function(tiapp) {
execute(['build', '-p', 'ios', '-D', 'development'], function() {
utils.message('Simulator should be started now...');
});
},
di: function(tiapp) {
var options = ['build', '-p', 'ios', '-T', 'device', '-b'];
getProfiles(tiapp.id, function(err, profile_id) {
if (!err && profile_id) {
execute(options.concat(['-P', profile_id]), function() {
utils.message('Trying to install on device...');
var app_file = process.cwd() + '/build/iphone/build/Debug-iphoneos/' + tiapp.name + '.app';
fs.exists(app_file, function(exists) {
if (exists) {
install([app_file], function() {
process.exit();
});
}
});
});
} else {
utils.message('It seems we could not found a valid profile for this app.\n\tBe sure that you have a valid profile for '+tiapp.id.inverse, 'error');
}
});
},
ri: function(tiapp) {
var running_app = exec('ps -eo comm|grep ' + tiapp.name + '.app|grep -v \'grep\'', function(error, stdout, stderr) {
var app = stdout.replace(/.app\/(.*)/, '.app').trim();
if (app) {
fs.exists(app, function(exists) {
if (exists) {
utils.message('Trying to reload app...\n\tOk, app running, restarting...');
var killInstruments = spawn('killall', ['instruments']);
fs.unlink('/tmp/sti.trace');
setTimeout(function() {
var reload = spawn('instruments', ['-D', '/tmp/sti.trace', '-t', '/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate', app], {
detached: true
});
exec('osascript -e \'application "iPhone Simulator" activate\'');
setTimeout(function() {
tailAppLog(tiapp);
},
2000);
},
500);
} else {
utils.message('It seems the app doesn\'t exist anymore in the current simulator.\n\tYou will have to run a new build command (sti with i3,i4 or i5)', 'error');
}
});
} else {
utils.message('App not running, trying to restart...', 'warn');
getLastSession(tiapp, function(err, session) {
if (!err && session && session[tiapp.name] && session[tiapp.name].app) {
fs.exists(session[tiapp.name].app, function(exists) {
if (exists) {
var reload = spawn('instruments', ['-D', '/tmp/sti.trace', '-t', '/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate', session[tiapp.name].app], {
detached: true
});
var interval = setInterval(function() {
exec('ps -eo comm|grep "' + session[tiapp.name].app + '"|grep -v \'grep\'', function(error, stdout, stderr) {
if (stdout.trim() == session[tiapp.name].app) {
setTimeout(function() {
tailAppLog(tiapp);
},
1000);
clearInterval(interval);
exec('osascript -e \'application "iPhone Simulator" activate\'');
}
});
},
1000);
} else {
utils.message('It seems the app doesn\'t exist anymore in the current simulator.\n\tYou will have to run a new build command (sti with i3,i4 or i5)', 'error');
delete session[tiapp.name];
fs.writeFile(__dirname + '/session.json', JSON.stringify(session), function(err) {});
}
})
} else {
utils.message('Cannot restart app, run a build command (sti with i3, i4 or i5)', 'error');
}
})
}
});
},
getTiapp: getTiapp
};