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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 'use strict'; const semver = require('semver'); const os = require('os'); const path = require('path'); const fs = require('fs'); const logger = require('../logger'); const launcher = require('./launcher'); const notListed = 'not listed'; const _getVersion = (cmd, result, options, father) => { let regexp; if (options.listedIn) { regexp = `${options.key ? options.key : cmd}${options.regexp !== undefined ? options.regexp : father.regexp}`; } else { regexp = options.regexp; } let version = result.output ? result.output : result.stderr; if (regexp) { const match = version.match(regexp); if (match && match.length > 1) { version = match[1].replace(os.EOL, ''); } else { version = options.listedIn ? notListed : version.substring(0, version.length > 20 ? 20 : version.length); } } version = version ? version.replace(os.EOL, '') : 'none'; return version; }; const _cachedExec = (command, noCache) => { if (!global.pisco_executions) { global.pisco_executions = {}; } let result = noCache ? false : global.pisco_executions[command]; if (!result) { const cmds = command.split(' '); global.pisco_executions[command] = launcher.execute(cmds[0], cmds.slice(1), {mute: true, stdio: ['ignore', 'pipe', 'pipe']}) .then((result) => { global.pisco_executions[command] = result; return result; }); return global.pisco_executions[command]; } else if (!result.then) { result = Promise.resolve(result); } return result; }; const _sh = (cmd, options, father, noCache) => { if (options.listedIn) { if (father && father.list) { logger.trace('Getting list for', cmd); return _cachedExec(father.list, noCache); } else { return Promise.reject({error: `There is no definition for listing in ${options.listedIn}`}); } } else { let option = options.option ? options.option : '-v'; if (options.uncheckable) { return { output: 'uncheckable' }; } else { return _cachedExec(`${cmd} ${option}`, noCache); } } }; const _check = (cmd, options, result, father) => new Promise((ok, ko) => { let out = {version: options.version ? options.version : 'any', uncheckable : options.uncheckable}; if (result.status < 0) { out.error = `'${cmd}' is not found!!`; out.data = result.output; } else if (result.status >= 0) { const actual = _getVersion(cmd, result, options, father); if (options.version || actual === notListed) { if (semver.valid(actual) && semver.satisfies(actual, options.version)) { out.message = ['#green', cmd, '(', actual, ') is installed ...', '#green', 'OK']; } else { out.error = `not satisfied by: '${actual}'`; } } else { out.message = ['#green', cmd, '(', actual, ') is installed ...', '#green', 'OK']; } } if (out.error && !options.uncheckable) { ko(out); } else { ok(out); } logger.info.apply(logger, ['#cyan', cmd, '(', out.version, ') is required -> '].concat(out.message ? out.message : [out.error, '...', options.uncheckable ? '#green' : '#red', options.uncheckable ? 'OK (uncheckable)' : 'ERROR!'])); }); module.exports = { sh: _sh, check: _check }; |