-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49.js
More file actions
96 lines (85 loc) · 2.95 KB
/
Copy path49.js
File metadata and controls
96 lines (85 loc) · 2.95 KB
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
import B from 'bluebird';
import path from 'path';
import _inquirer from 'inquirer';
import log from '../lib/logger';
import authorize from 'authorize-ios';
import { fs, system } from 'appium-support';
import { exec } from 'teen_process';
// rename to make more sense
const authorizeIos = authorize;
const pkgRoot = process.env.NO_PRECOMPILE ?
path.resolve(__dirname, '..') : path.resolve(__dirname, '..', '..');
const ok = (message) => { return {ok: true, optional: false, message}; };
const nok = (message) => { return {ok: false, optional: false, message}; };
const okOptional = (message) => { return {ok: true, optional: true, message}; };
const nokOptional = (message) => { return {ok: false, optional: true, message}; };
const inquirer = {
prompt: B.promisify(function (question, cb) { // eslint-disable-line promise/prefer-await-to-callbacks
_inquirer.prompt(question, function (resp) { cb(null, resp); }); // eslint-disable-line promise/prefer-await-to-callbacks
})
};
function configureBinaryLog (opts) {
let actualLog = log.unwrap().log;
log.unwrap().log = function (level, prefix, msg) {
let l = this.levels[level];
if (l < this.levels[this.level]) return; // eslint-disable-line curly
actualLog(level, prefix, msg);
};
log.level = opts.debug ? 'debug' : 'info';
}
/**
* Return an executable path of cmd
*
* @param {string} cmd Standard output by command
* @return {?string} The full path of cmd. `null` if the cmd is not found.
*/
async function resolveExecutablePath (cmd) {
let executablePath;
try {
executablePath = await fs.which(cmd);
if (executablePath && await fs.exists(executablePath)) {
return executablePath;
}
} catch (err) {
if ((/not found/gi).test(err.message)) {
log.debug(err);
} else {
log.warn(err);
}
}
log.debug(`No executable path of '${cmd}'.`);
if (executablePath) {
log.debug(`Does '${executablePath}' exist?`);
}
return null;
}
/**
* @typedef {Object} NpmPackageInfo
* @property {string} version - version
* @property {string} path - A path to npm root
*/
/**
* Returns the path and version of given package name
* @param {string} packageName A package name to get path and version data
* @return {?NpmPackageInfo}
*/
async function getNpmPackageInfo (packageName) {
const npmPath = await resolveExecutablePath(`npm${system.isWindows() ? `.cmd` : ''}`);
if (!npmPath) {
return nokOptional(`'npm' binary not found in PATH: ${process.env.PATH}`);
}
let pJson = {};
try {
const {stdout} = await exec(npmPath, ['list', '-g', '-l', '-j', packageName]);
pJson = JSON.parse(stdout);
} catch (err) {
log.debug(err);
return null;
}
if (pJson.dependencies && pJson.dependencies[packageName]) {
return {version: pJson.dependencies[packageName].version, path: pJson.path};
}
return null;
}
export { pkgRoot, ok, nok, okOptional, nokOptional, inquirer, configureBinaryLog,
authorizeIos, resolveExecutablePath, getNpmPackageInfo};