zcatalyst-cli
Version:
Command Line Tool for CATALYST
157 lines (156 loc) • 7.21 kB
JavaScript
;
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.ensurePython = ensurePython;
exports.ensurePythonWithPip = ensurePythonWithPip;
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");
const utils_1 = require("../../../util_modules/fs/utils");
const runtime_store_1 = __importDefault(require("../../../runtime-store"));
const logger_1 = require("../../../util_modules/logger");
function ensurePython(version, fallBackNeeded, skipHelp) {
return __awaiter(this, void 0, void 0, function* () {
const configKey = `python${version.replace('.', '_')}.bin`;
const spawnCommand = env_1.isExtension
? runtime_store_1.default.get(`executables.${configKey}`)
: userConfig_1.default.get(configKey);
if (spawnCommand) {
try {
yield validateCommand(version, spawnCommand, true);
return getPythonExecPath(spawnCommand);
}
catch (er) {
throw er;
}
}
try {
const fallBackSpawnCommand = 'python' + version;
yield validateCommand(version, fallBackSpawnCommand, true);
return getPythonExecPath(fallBackSpawnCommand);
}
catch (err) {
if (env_1.isWindows) {
try {
const winPyLauncher = 'py -' + version;
yield (0, shell_1.spawn)(winPyLauncher, ['--version'], {
stdio: 'ignore'
}).ASYNC();
userConfig_1.default.set(configKey, winPyLauncher);
yield validateCommand(version, winPyLauncher, true);
return getPythonExecPath(winPyLauncher);
}
catch (er) {
(0, logger_1.debug)('Unable to locate PyLauncher in win: ' + er);
}
}
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);
userConfig_1.default.set(`python${version.replace('.', '_')}.bin`, ans.binPath);
return getPythonExecPath(ans.binPath);
}
const { errorId, config } = env_1.isExtension
? { errorId: 'PY-2-EXT', config: 'zcatalyst.python_3_9' }
: { errorId: 'PY-2', config: `catalyst config:set ${configKey}=<binary_path>` };
throw new error_1.default(`unable to locate python${version} in your system`, {
exit: 1,
errorId: errorId,
arg: [version, (0, ansi_colors_1.italic)((0, ansi_colors_1.bold)(config))],
skipHelp
});
}
});
}
function validateCommand(version, spawnCommand, skipHelp) {
return __awaiter(this, void 0, void 0, function* () {
spawnCommand = (0, utils_1.untildify)(spawnCommand);
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 getPythonExecPath(spawnCommand) {
return __awaiter(this, void 0, void 0, function* () {
spawnCommand = (0, utils_1.untildify)(spawnCommand);
const pyVChild = (0, shell_1.spawn)(spawnCommand, ['-c', 'import sys;print(sys.executable)'], {
stdio: 'pipe'
}).SYNC();
return pyVChild.stdout.toString().trim();
});
}
function ensurePythonWithPip(version_1) {
return __awaiter(this, arguments, void 0, function* (version, fallBackNeeded = false, skipHelp = false) {
const pyBin = yield ensurePython(version, fallBackNeeded, skipHelp);
yield new Promise((resolve, reject) => {
const child = (0, shell_1.spawn)(pyBin, ['-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);
});
return pyBin;
});
}