UNPKG

1.79 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/* eslint no-console:0 */
4
5/*
6 * Small tool, launching and monitoring ios-web-kit-proxy, and relaunching
7 * on predefined errors.
8 *
9 * Usage:
10 * ./bin/ios-webkit-debug-proxy-launcher.js [args]
11 * args: ios-webkit-debug-proxy args (they will be passed over)
12 *
13 * Example:
14 * ./bin/ios-webkit-debug-proxy-launcher.js -c <UDID>:27753 -d
15 *
16 * Note:
17 * For iOS8.1 try this first:
18 * brew install --HEAD ideviceinstaller
19 */
20
21'use strict';
22
23const spawn = require('child_process').spawn;
24const _ = require('lodash');
25
26const args = process.argv.slice(2);
27
28const RESTART_ON_MESSAGES = [
29 'Invalid message _rpc_applicationUpdated',
30 'Invalid message _rpc_applicationSentListing'];
31
32const PROXY_CMD = 'ios_webkit_debug_proxy';
33let proxy;
34
35const handleKillProcess = function (exitCode) {
36 console.log('\nKilling proxy process!');
37 proxy.kill('SIGTERM');
38 process.exit((exitCode || 0));
39};
40
41const 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
68process.on('SIGINT', handleKillProcess);
69process.on('SIGTERM', handleKillProcess);
70
71startProxy();