1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | import { init as logsinkInit } from './logsink';
|
5 | import logger from './logger';
|
6 | import _ from 'lodash';
|
7 | import { server as baseServer, routeConfiguringFunction as makeRouter } from 'appium-base-driver';
|
8 | import { asyncify } from 'asyncbox';
|
9 | import { default as getParser, getDefaultArgs } from './parser';
|
10 | import { showConfig, checkNodeOk, validateServerArgs,
|
11 | warnNodeDeprecations, validateTmpDir, getNonDefaultArgs,
|
12 | getDeprecatedArgs, getGitRev, APPIUM_VER } from './config';
|
13 | import { AppiumDriver } from './appium';
|
14 | import registerNode from './grid-register';
|
15 | import { inspectObject } from './utils';
|
16 |
|
17 |
|
18 | async function preflightChecks (parser, args, throwInsteadOfExit = false) {
|
19 | try {
|
20 | checkNodeOk();
|
21 | if (args.longStacktrace) {
|
22 | require('longjohn').async_trace_limit = -1;
|
23 | }
|
24 | if (args.showConfig) {
|
25 | await showConfig();
|
26 | process.exit(0);
|
27 | }
|
28 | warnNodeDeprecations();
|
29 | validateServerArgs(parser, args);
|
30 | if (args.tmpDir) {
|
31 | await validateTmpDir(args.tmpDir);
|
32 | }
|
33 | } catch (err) {
|
34 | logger.error(err.message.red);
|
35 | if (throwInsteadOfExit) {
|
36 | throw err;
|
37 | }
|
38 |
|
39 | process.exit(1);
|
40 | }
|
41 | }
|
42 |
|
43 | function logDeprecationWarning (deprecatedArgs) {
|
44 | logger.warn('Deprecated server args:');
|
45 | for (let [arg, realArg] of _.toPairs(deprecatedArgs)) {
|
46 | logger.warn(` ${arg.red} => ${realArg}`);
|
47 | }
|
48 | }
|
49 |
|
50 | function logNonDefaultArgsWarning (args) {
|
51 | logger.info('Non-default server args:');
|
52 | inspectObject(args);
|
53 | }
|
54 |
|
55 | function logDefaultCapabilitiesWarning (caps) {
|
56 | logger.info('Default capabilities, which will be added to each request ' +
|
57 | 'unless overridden by desired capabilities:');
|
58 | inspectObject(caps);
|
59 | }
|
60 |
|
61 | async function logStartupInfo (parser, args) {
|
62 | let welcome = `Welcome to Appium v${APPIUM_VER}`;
|
63 | let appiumRev = await getGitRev();
|
64 | if (appiumRev) {
|
65 | welcome += ` (REV ${appiumRev})`;
|
66 | }
|
67 | logger.info(welcome);
|
68 |
|
69 | let showArgs = getNonDefaultArgs(parser, args);
|
70 | if (_.size(showArgs)) {
|
71 | logNonDefaultArgsWarning(showArgs);
|
72 | }
|
73 | let deprecatedArgs = getDeprecatedArgs(parser, args);
|
74 | if (_.size(deprecatedArgs)) {
|
75 | logDeprecationWarning(deprecatedArgs);
|
76 | }
|
77 | if (!_.isEmpty(args.defaultCapabilities)) {
|
78 | logDefaultCapabilitiesWarning(args.defaultCapabilities);
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | }
|
86 |
|
87 | function logServerPort (address, port) {
|
88 | let logMessage = `Appium REST http interface listener started on ` +
|
89 | `${address}:${port}`;
|
90 | logger.info(logMessage);
|
91 | }
|
92 |
|
93 | async function main (args = null) {
|
94 | let parser = getParser();
|
95 | let throwInsteadOfExit = false;
|
96 | if (args) {
|
97 |
|
98 |
|
99 | args = Object.assign({}, getDefaultArgs(), args);
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | if (args.throwInsteadOfExit) {
|
105 | throwInsteadOfExit = true;
|
106 |
|
107 | delete args.throwInsteadOfExit;
|
108 | }
|
109 | } else {
|
110 |
|
111 | args = parser.parseArgs();
|
112 | }
|
113 | await logsinkInit(args);
|
114 | await preflightChecks(parser, args, throwInsteadOfExit);
|
115 | await logStartupInfo(parser, args);
|
116 | let appiumDriver = new AppiumDriver(args);
|
117 | let routeConfiguringFunction = makeRouter(appiumDriver);
|
118 | let server = await baseServer({
|
119 | routeConfiguringFunction,
|
120 | port: args.port,
|
121 | hostname: args.address,
|
122 | allowCors: args.allowCors,
|
123 | basePath: args.basePath,
|
124 | });
|
125 | if (args.allowCors) {
|
126 | logger.warn('You have enabled CORS requests from any host. Be careful not ' +
|
127 | 'to visit sites which could maliciously try to start Appium ' +
|
128 | 'sessions on your machine');
|
129 | }
|
130 | appiumDriver.server = server;
|
131 | try {
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | if (args.nodeconfig !== null) {
|
137 | await registerNode(args.nodeconfig, args.address, args.port);
|
138 | }
|
139 | } catch (err) {
|
140 | await server.close();
|
141 | throw err;
|
142 | }
|
143 |
|
144 | process.once('SIGINT', async function onSigint () {
|
145 | logger.info(`Received SIGINT - shutting down`);
|
146 | await server.close();
|
147 | });
|
148 |
|
149 | process.once('SIGTERM', async function onSigterm () {
|
150 | logger.info(`Received SIGTERM - shutting down`);
|
151 | await server.close();
|
152 | });
|
153 |
|
154 | logServerPort(args.address, args.port);
|
155 |
|
156 | return server;
|
157 | }
|
158 |
|
159 | if (require.main === module) {
|
160 | asyncify(main);
|
161 | }
|
162 |
|
163 | export { main };
|