1 |
|
2 | import _ from 'lodash';
|
3 | import {system, fs} from '@appium/support';
|
4 | import axios from 'axios';
|
5 | import {exec} from 'teen_process';
|
6 | import semver from 'semver';
|
7 | import findUp from 'find-up';
|
8 | import {getDefaultsForSchema, getAllArgSpecs} from './schema/schema';
|
9 |
|
10 | const npmPackage = fs.readPackageJsonFrom(__dirname);
|
11 |
|
12 | const APPIUM_VER = npmPackage.version;
|
13 | const ENGINES = (npmPackage.engines);
|
14 | const MIN_NODE_VERSION = ENGINES.node;
|
15 | const MIN_NPM_VERSION = ENGINES.npm;
|
16 |
|
17 | const GIT_META_ROOT = '.git';
|
18 | const GIT_BINARY = `git${system.isWindows() ? '.exe' : ''}`;
|
19 | const GITHUB_API = 'https://api.github.com/repos/appium/appium';
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const BUILD_INFO = {
|
25 | version: APPIUM_VER,
|
26 | };
|
27 |
|
28 | function getNodeVersion() {
|
29 | return (semver.coerce(process.version));
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | async function getNpmVersion() {
|
37 | const {stdout} = await exec(system.isWindows() ? 'npm.cmd' : 'npm', ['--version']);
|
38 | return stdout.trim();
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | async function updateBuildInfo(useGithubApiFallback = false) {
|
45 | const sha = await getGitRev(useGithubApiFallback);
|
46 | if (!sha) {
|
47 | return;
|
48 | }
|
49 | BUILD_INFO['git-sha'] = sha;
|
50 | const buildTimestamp = await getGitTimestamp(sha, useGithubApiFallback);
|
51 | if (buildTimestamp) {
|
52 | BUILD_INFO.built = buildTimestamp;
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | async function findGitRoot() {
|
64 | return await findUp(GIT_META_ROOT, {cwd: rootDir, type: 'directory'});
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | async function getGitRev(useGithubApiFallback = false) {
|
72 | const gitRoot = await findGitRoot();
|
73 | if (gitRoot) {
|
74 | try {
|
75 | const {stdout} = await exec(GIT_BINARY, ['rev-parse', 'HEAD'], {
|
76 | cwd: gitRoot,
|
77 | });
|
78 | return stdout.trim();
|
79 | } catch (ign) {}
|
80 | }
|
81 |
|
82 | if (!useGithubApiFallback) {
|
83 | return null;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 | try {
|
89 | return (
|
90 | await axios.get(`${GITHUB_API}/git/refs/tags/appium@${APPIUM_VER}`, {
|
91 | headers: {
|
92 | 'User-Agent': `Appium ${APPIUM_VER}`,
|
93 | },
|
94 | })
|
95 | ).data?.object?.sha;
|
96 | } catch (ign) {}
|
97 | return null;
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | async function getGitTimestamp(commitSha, useGithubApiFallback = false) {
|
106 | const gitRoot = await findGitRoot();
|
107 | if (gitRoot) {
|
108 | try {
|
109 | const {stdout} = await exec(GIT_BINARY, ['show', '-s', '--format=%ci', commitSha], {
|
110 | cwd: gitRoot,
|
111 | });
|
112 | return stdout.trim();
|
113 | } catch (ign) {}
|
114 | }
|
115 |
|
116 | if (!useGithubApiFallback) {
|
117 | return null;
|
118 | }
|
119 |
|
120 | try {
|
121 | return (
|
122 | await axios.get(`${GITHUB_API}/git/tags/${commitSha}`, {
|
123 | headers: {
|
124 | 'User-Agent': `Appium ${APPIUM_VER}`,
|
125 | },
|
126 | })
|
127 | ).data?.tagger?.date;
|
128 | } catch (ign) {}
|
129 | return null;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function getBuildInfo() {
|
140 | return BUILD_INFO;
|
141 | }
|
142 |
|
143 | function checkNodeOk() {
|
144 | const version = getNodeVersion();
|
145 | if (!semver.satisfies(version, MIN_NODE_VERSION)) {
|
146 | throw new Error(
|
147 | `Node version must be at least ${MIN_NODE_VERSION}; current is ${version.version}`
|
148 | );
|
149 | }
|
150 | }
|
151 |
|
152 | export async function checkNpmOk() {
|
153 | const npmVersion = await getNpmVersion();
|
154 | if (!semver.satisfies(npmVersion, MIN_NPM_VERSION)) {
|
155 | throw new Error(
|
156 | `npm version must be at least ${MIN_NPM_VERSION}; current is ${npmVersion}. Run "npm install -g npm" to upgrade.`
|
157 | );
|
158 | }
|
159 | }
|
160 |
|
161 | function warnNodeDeprecations() {
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | }
|
173 |
|
174 | async function showBuildInfo() {
|
175 | await updateBuildInfo(true);
|
176 | console.log(JSON.stringify(getBuildInfo()));
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | function getNonDefaultServerArgs(parsedArgs) {
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | const flatten = (args) => {
|
192 | const argSpecs = getAllArgSpecs();
|
193 | const flattened = _.reduce(
|
194 | [...argSpecs.values()],
|
195 | (acc, argSpec) => {
|
196 | if (_.has(args, argSpec.dest)) {
|
197 | acc[argSpec.dest] = {value: _.get(args, argSpec.dest), argSpec};
|
198 | }
|
199 | return acc;
|
200 | },
|
201 | ({})
|
202 | );
|
203 |
|
204 | return flattened;
|
205 | };
|
206 |
|
207 | const args = flatten(parsedArgs);
|
208 |
|
209 |
|
210 | const typesDiffer = (dest) =>
|
211 | typeof args[dest].value !== typeof defaultsFromSchema[dest];
|
212 |
|
213 | const defaultValueIsArray = (dest) =>
|
214 | _.isArray(defaultsFromSchema[dest]);
|
215 |
|
216 | const argsValueIsArray = (dest) => _.isArray(args[dest].value);
|
217 |
|
218 | const arraysDiffer = (dest) =>
|
219 | _.gt(_.size(_.difference(args[dest].value, defaultsFromSchema[dest])), 0);
|
220 |
|
221 | const valuesDiffer = (dest) =>
|
222 | args[dest].value !== defaultsFromSchema[dest];
|
223 |
|
224 | const defaultIsDefined = (dest) =>
|
225 | !_.isUndefined(defaultsFromSchema[dest]);
|
226 |
|
227 |
|
228 |
|
229 | const argValueNotArrayOrArraysDiffer = _.overSome([_.negate(argsValueIsArray), arraysDiffer]);
|
230 |
|
231 | const defaultValueNotArrayAndValuesDiffer = _.overEvery([
|
232 | _.negate(defaultValueIsArray),
|
233 | valuesDiffer,
|
234 | ]);
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | const isNotDefault = _.overEvery([
|
249 | defaultIsDefined,
|
250 | _.overSome([
|
251 | typesDiffer,
|
252 | _.overEvery([defaultValueIsArray, argValueNotArrayOrArraysDiffer]),
|
253 | defaultValueNotArrayAndValuesDiffer,
|
254 | ]),
|
255 | ]);
|
256 |
|
257 | const defaultsFromSchema = getDefaultsForSchema(true);
|
258 |
|
259 | return _.reduce(
|
260 | _.pickBy(args, (__, key) => isNotDefault(key)),
|
261 |
|
262 | (acc, {value, argSpec}) => _.set(acc, argSpec.dest, value),
|
263 | ({})
|
264 | );
|
265 | }
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | const compactConfig = _.partial(
|
275 | _.omitBy,
|
276 | _,
|
277 | (value, key) =>
|
278 | key === 'subcommand' || _.isUndefined(value) || (_.isObject(value) && _.isEmpty(value))
|
279 | );
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | function showConfig(nonDefaultPreConfigParsedArgs, configResult, defaults, parsedArgs) {
|
293 | console.log('Appium Configuration\n');
|
294 | console.log('from defaults:\n');
|
295 | console.dir(compactConfig(defaults));
|
296 | if (configResult.config) {
|
297 | console.log(`\nfrom config file at ${configResult.filepath}:\n`);
|
298 | console.dir(compactConfig(configResult.config));
|
299 | } else {
|
300 | console.log(`\n(no configuration file loaded)`);
|
301 | }
|
302 | const compactedNonDefaultPreConfigArgs = compactConfig(nonDefaultPreConfigParsedArgs);
|
303 | if (_.isEmpty(compactedNonDefaultPreConfigArgs)) {
|
304 | console.log(`\n(no CLI parameters provided)`);
|
305 | } else {
|
306 | console.log('\nvia CLI or function call:\n');
|
307 | console.dir(compactedNonDefaultPreConfigArgs);
|
308 | }
|
309 | console.log('\nfinal configuration:\n');
|
310 | console.dir(compactConfig(parsedArgs));
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | async function validateTmpDir(tmpDir) {
|
317 | try {
|
318 | await fs.mkdirp(tmpDir);
|
319 | } catch (e) {
|
320 | throw new Error(
|
321 | `We could not ensure that the temp dir you specified ` +
|
322 | `(${tmpDir}) exists. Please make sure it's writeable.`
|
323 | );
|
324 | }
|
325 | }
|
326 |
|
327 | const rootDir = fs.findRoot(__dirname);
|
328 |
|
329 | export {
|
330 | getBuildInfo,
|
331 | checkNodeOk,
|
332 | showBuildInfo,
|
333 | warnNodeDeprecations,
|
334 | validateTmpDir,
|
335 | getNonDefaultServerArgs,
|
336 | getGitRev,
|
337 | APPIUM_VER,
|
338 | updateBuildInfo,
|
339 | showConfig,
|
340 | rootDir,
|
341 | };
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|