UNPKG

4.96 kBJavaScriptView Raw
1#!/usr/bin/env node
2// transpile:main
3
4import { init as logsinkInit } from './logsink';
5import logger from './logger'; // logger needs to remain first of imports
6import _ from 'lodash';
7import { server as baseServer, routeConfiguringFunction as makeRouter } from 'appium-base-driver';
8import { asyncify } from 'asyncbox';
9import { default as getParser, getDefaultArgs } from './parser';
10import { showConfig, checkNodeOk, validateServerArgs,
11 warnNodeDeprecations, validateTmpDir, getNonDefaultArgs,
12 getDeprecatedArgs, getGitRev, APPIUM_VER } from './config';
13import { AppiumDriver } from './appium';
14import registerNode from './grid-register';
15import { inspectObject } from './utils';
16
17
18async 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
43function 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
50function logNonDefaultArgsWarning (args) {
51 logger.info('Non-default server args:');
52 inspectObject(args);
53}
54
55function 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
61async 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 // TODO: bring back loglevel reporting below once logger is flushed out
81 // logger.info('Console LogLevel: ' + logger.transports.console.level);
82 // if (logger.transports.file) {
83 // logger.info('File LogLevel: ' + logger.transports.file.level);
84 // }
85}
86
87function logServerPort (address, port) {
88 let logMessage = `Appium REST http interface listener started on ` +
89 `${address}:${port}`;
90 logger.info(logMessage);
91}
92
93async function main (args = null) {
94 let parser = getParser();
95 let throwInsteadOfExit = false;
96 if (args) {
97 // a containing package passed in their own args, let's fill them out
98 // with defaults
99 args = Object.assign({}, getDefaultArgs(), args);
100
101 // if we have a containing package instead of running as a CLI process,
102 // that package might not appreciate us calling 'process.exit' willy-
103 // nilly, so give it the option to have us throw instead of exit
104 if (args.throwInsteadOfExit) {
105 throwInsteadOfExit = true;
106 // but remove it since it's not a real server arg per se
107 delete args.throwInsteadOfExit;
108 }
109 } else {
110 // otherwise parse from CLI
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 // TODO prelaunch if args.launch is set
133 // TODO: startAlertSocket(server, appiumServer);
134
135 // configure as node on grid, if necessary
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
159if (require.main === module) {
160 asyncify(main);
161}
162
163export { main };