-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathappLaunchHelper.js
163 lines (148 loc) · 5.47 KB
/
appLaunchHelper.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
const path = require('path');
const chalk = require('chalk');
const { execSync } = require('child_process');
const chromeLauncher = require('chrome-launcher');
const regExp = require('./regexp.js');
const util = require('./util.js');
const { logger } = require('./logger');
const PACKAGE_BASE_PATH = path.join(util.WITS_BASE_PATH, '../', 'container');
const WITS_PACKAGE = 'WITs.wgt';
const EMULATOR_IP = '0.0.0.0';
module.exports = {
installPackage: (deviceInfo, hostAppName) => {
const deviceName = deviceInfo.deviceName;
const appInstallPath = deviceInfo.appInstallPath;
const WGT_FILE_PUSH_COMMAND = `${
util.TOOLS_SDB_PATH
} -s ${deviceName} push "${path.join(
PACKAGE_BASE_PATH,
WITS_PACKAGE
)}" "${appInstallPath}"`;
const APP_INSTALL_COMMAND = `${util.TOOLS_SDB_PATH} -s ${deviceName} shell 0 vd_appinstall ${hostAppName} ${appInstallPath}${WITS_PACKAGE}`;
const pushResult = execSync(WGT_FILE_PUSH_COMMAND, {
encoding: 'utf-8',
stdio: 'pipe'
});
util.displayOutput(pushResult);
const installResult = execSync(APP_INSTALL_COMMAND, {
encoding: 'utf-8',
stdio: 'pipe'
});
util.displayOutput(installResult);
if (installResult.includes('failed[')) {
logger.error(chalk.red(`\nFailed to install Wits`));
util.exit();
}
},
unInstallPackage: (deviceName, hostAppName) => {
const APP_UNINSTALL_COMMAND = `${util.TOOLS_SDB_PATH} -s ${deviceName} shell 0 vd_appuninstall ${hostAppName}`;
const result = execSync(APP_UNINSTALL_COMMAND, {
encoding: 'utf-8',
stdio: 'pipe'
});
util.displayOutput(result);
if (result.includes('failed[')) {
logger.warn(`\n[warning] Failed to uninstall Wits`);
}
},
launchApp: (deviceName, hostAppId) => {
const APP_LAUNCH_COMMAND = `${util.TOOLS_SDB_PATH} -s ${deviceName} shell 0 was_execute ${hostAppId}`;
const result = execSync(APP_LAUNCH_COMMAND, {
encoding: 'utf-8',
stdio: 'pipe'
});
util.displayOutput(result);
if (result === null || result.includes('failed[')) {
throw new Error(
'Failed to launchApp. Please check the application is already installed on the Target.'
);
}
},
launchDebugMode: (deviceName, hostAppId, deviceIpAddress) => {
const APP_LAUNCH_DEBUG_MODE_COMMAND = `${util.TOOLS_SDB_PATH} -s ${deviceName} shell 0 debug ${hostAppId}`;
const APP_LAUNCH_DEBUG_MODE_COMMAND_TIMEOUT = `${APP_LAUNCH_DEBUG_MODE_COMMAND} 300`;
const result =
execSync(APP_LAUNCH_DEBUG_MODE_COMMAND, {
encoding: 'utf-8',
stdio: 'pipe'
}) ||
execSync(APP_LAUNCH_DEBUG_MODE_COMMAND_TIMEOUT, {
encoding: 'utf-8',
stdio: 'pipe'
});
util.displayOutput(result);
if (result === null || result.includes('failed')) {
throw new Error(
'Failed to launchDebugMode. Please check the application is already installed on the Target.'
);
}
const debugPort = result
.match(regExp.DEBUG_PORT)[0]
.match(regExp.NUMBER_WORD)[0];
let debugIP = '';
if (deviceIpAddress === EMULATOR_IP) {
const LOCAL_HOST = '127.0.0.1';
setPortForward(deviceName, debugPort);
debugIP = LOCAL_HOST;
} else {
debugIP = deviceIpAddress;
}
try {
launchChrome(debugIP + ':' + debugPort);
} catch (e) {
logger.log(
`Please install a Chrome browser or input ${debugIP}:${debugPort} into the address bar of the chromium based browser. ${e}`
);
}
},
terminateApp: (deviceName, hostAppId) => {
const APP_TERMINATE_COMMAND = `${util.TOOLS_SDB_PATH} -s ${deviceName} shell 0 was_kill ${hostAppId}`;
const result = execSync(APP_TERMINATE_COMMAND, {
encoding: 'utf-8',
stdio: 'pipe'
});
util.displayOutput(result);
}
};
function setPortForward(deviceName, port) {
const LOCAL_HOST = '127.0.0.1';
const removeResult = execSync(
`${util.TOOLS_SDB_PATH} -s ${deviceName} forward --remove tcp:${port}`,
{
encoding: 'utf-8',
stdio: 'pipe'
}
);
util.displayOutput(removeResult);
const tcpResult = execSync(
`${util.TOOLS_SDB_PATH} -s ${deviceName} forward tcp:${port} tcp:${port}`,
{
encoding: 'utf-8',
stdio: 'pipe'
}
);
util.displayOutput(tcpResult);
try {
launchChrome(LOCAL_HOST + ':' + port);
} catch (e) {
logger.log(
`Please install a Chrome browser or input ${LOCAL_HOST}:${port} into the address bar of the chromium based browser. ${e}`
);
}
}
function launchChrome(url) {
chromeLauncher
.launch({
startingUrl: url,
chromeFlags: [
'--disable-web-security',
'--enable-blink-features=ShadowDOMV0,CustomElementsV0,HTMLImports'
]
})
.then(chrome => {
logger.log(`Chrome debugging port running on ${chrome.port}`);
})
.catch(e => {
logger.log(chalk.red(`Please install a Chrome browser. ${e}`));
});
}