UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

150 lines (149 loc) 6.75 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensurePythonWithPip = exports.ensurePython = void 0; const error_1 = __importDefault(require("../../../error")); const prompt_1 = __importDefault(require("../../../prompt")); const env_1 = require("../../../util_modules/env"); const shell_1 = require("../../../util_modules/shell"); const userConfig_1 = __importDefault(require("../../../userConfig")); const ansi_colors_1 = require("ansi-colors"); const console_1 = require("console"); function ensurePython(version, fallBackNeeded, skipHelp) { return __awaiter(this, void 0, void 0, function* () { const configKey = `python${version.replace('.', '_')}.bin`; const spawnCommand = userConfig_1.default.get(configKey); try { if (spawnCommand) { yield validateCommand(version, spawnCommand, true); return; } } catch (_a) { } return new Promise((resolve, reject) => { const child = (0, shell_1.spawn)('python' + version, ['--version'], { stdio: 'ignore' }).RAW(); child.on('error', (error) => __awaiter(this, void 0, void 0, function* () { if (env_1.isWindows) { try { yield (0, shell_1.spawn)('py -' + version, ['--version'], { stdio: 'ignore' }).ASYNC(); userConfig_1.default.set(configKey, 'py -' + version); return resolve(); } catch (_a) { } } if (fallBackNeeded) { (0, console_1.info)(`unable to locate python${version} in your system`); const ans = yield prompt_1.default.ask(prompt_1.default.question('binPath', `Please provide the binary path for python${version}:`, { type: 'input' })); yield validateCommand(version, ans.binPath, skipHelp) .then(() => { userConfig_1.default.set(`python${version.replace('.', '_')}.bin`, ans.binPath); resolve(); }) .catch((err) => { reject(err); }); } else { reject(new error_1.default(`unable to locate python${version} in your system`, { original: error, exit: 1, errorId: 'PY-2', arg: [ version, (0, ansi_colors_1.italic)((0, ansi_colors_1.bold)(`catalyst config:set ${configKey}=<binary_path>`)) ], skipHelp })); } })); child.on('exit', (code) => { if (code === 0) { userConfig_1.default.set(configKey, 'python' + version); } resolve(); }); }); }); } exports.ensurePython = ensurePython; function validateCommand(version, spawnCommand, skipHelp) { return __awaiter(this, void 0, void 0, function* () { const versionResponse = yield new Promise((resolve, reject) => { var _a; const child = (0, shell_1.spawn)(spawnCommand, ['--version'], { stdio: 'pipe' }).RAW(); const dataBuf = []; (_a = child.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (chunk) => { dataBuf.push(chunk); }); child.on('exit', (code) => { if (code === 0) { const data = Buffer.concat(dataBuf).toString(); return resolve(data); } reject(new error_1.default(`unable to locate python${version} with the given binary path`, { exit: 1, errorId: 'PY-1', arg: [version, (0, ansi_colors_1.bold)(spawnCommand)], skipHelp })); }); child.on('error', (err) => { reject(new error_1.default(`unable to locate python${version} with the given binary path`, { errorId: 'PY-1', original: err, exit: 1, arg: [version, (0, ansi_colors_1.bold)(spawnCommand)], skipHelp })); }); }); if (!versionResponse.split(' ')[1].startsWith(version)) { throw new error_1.default(`unable to locate python${version} with the given binary path`, { exit: 1, errorId: 'PY-1', arg: [version, (0, ansi_colors_1.bold)(spawnCommand)], skipHelp }); } }); } function ensurePythonWithPip(version, fallBackNeeded = false, skipHelp = false) { return __awaiter(this, void 0, void 0, function* () { yield ensurePython(version, fallBackNeeded, skipHelp); return new Promise((resolve, reject) => { const child = (0, shell_1.spawn)(userConfig_1.default.get(`python${version.replace('.', '_')}.bin`), ['-m', 'pip', '--version'], { stdio: 'ignore' }).RAW(); child.on('error', (err) => { reject(new error_1.default(`unable to ensure pip for python${version}`, { errorId: 'PIP', original: err, arg: [version], exit: 1, skipHelp })); }); child.on('exit', resolve); }); }); } exports.ensurePythonWithPip = ensurePythonWithPip;