1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | 'use strict';
|
22 |
|
23 | const spawn = require('child_process').spawn;
|
24 | const _ = require('lodash');
|
25 |
|
26 | const args = process.argv.slice(2);
|
27 |
|
28 | const RESTART_ON_MESSAGES = [
|
29 | 'Invalid message _rpc_applicationUpdated',
|
30 | 'Invalid message _rpc_applicationSentListing'];
|
31 |
|
32 | const PROXY_CMD = 'ios_webkit_debug_proxy';
|
33 | let proxy;
|
34 |
|
35 | const handleKillProcess = function (exitCode) {
|
36 | console.log('\nKilling proxy process!');
|
37 | proxy.kill('SIGTERM');
|
38 | process.exit((exitCode || 0));
|
39 | };
|
40 |
|
41 | const startProxy = function () {
|
42 | console.log(`RUNNING: ${PROXY_CMD} ${args.join(' ')}`);
|
43 |
|
44 | proxy = spawn(PROXY_CMD, args);
|
45 |
|
46 | proxy.stdout.on('data', function onStdout (data) {
|
47 | console.log(`stdout: ${data}`);
|
48 | });
|
49 |
|
50 | proxy.stderr.on('data', function onStderr (data) {
|
51 | console.log(`stderr: ${data}`);
|
52 | const restartMessage = _(RESTART_ON_MESSAGES).find(function findMessage (message) {
|
53 | return ('' + data).indexOf(message) >= 0;
|
54 | });
|
55 | if (restartMessage) {
|
56 | console.log(`Detected error message: ${restartMessage}`);
|
57 | console.log('Killing proxy!');
|
58 | proxy.kill('SIGTERM');
|
59 | process.nextTick(startProxy);
|
60 | }
|
61 | });
|
62 |
|
63 | proxy.on('close', function onClose (code) {
|
64 | console.log(`Child process exited with code ${code}`);
|
65 | });
|
66 | };
|
67 |
|
68 | process.on('SIGINT', handleKillProcess);
|
69 | process.on('SIGTERM', handleKillProcess);
|
70 |
|
71 | startProxy();
|