kui-shell
Version:
This is the monorepo for Kui, the hybrid command-line/GUI electron-based Kubernetes tool
149 lines • 6.63 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Debug = require("debug");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const child_process_1 = require("child_process");
const i18n_1 = require("@kui-shell/core/api/i18n");
const settings_1 = require("@kui-shell/core/api/settings");
const ora_1 = require("../util/ora");
const locate_npm_1 = require("../util/locate-npm");
const strings = i18n_1.i18n('plugin-manager');
const debug = Debug('plugins/plugin-manager/cmd/install');
const usage = {
strict: 'install',
command: 'install',
breadcrumb: strings('Install plugin'),
docs: strings('install a plugin'),
example: 'plugin install <plugin>',
detailedExample: {
command: 'plugin install @kui-shell/plugin-sample',
docs: strings('a simple example plugin')
},
required: [{ name: 'plugin', docs: 'an npm module or github link' }]
};
const doInstall = (args) => __awaiter(void 0, void 0, void 0, function* () {
const { argvNoOptions, REPL } = args;
const nameWithVersion = argvNoOptions[argvNoOptions.indexOf('install') + 1];
const nameWithoutVersion = nameWithVersion.replace(/^(@?[^@]+)@.+$/, '$1');
const spinner = yield new ora_1.default().init(strings('Preparing to install', nameWithoutVersion), args);
const rootDir = settings_1.default.userDataDir();
const pluginHome = path_1.join(rootDir, 'plugins');
const targetDir = path_1.join(pluginHome, path_1.basename(nameWithoutVersion));
yield fs_extra_1.ensureDir(targetDir);
debug(`installing ${nameWithoutVersion} into pluginHome=${pluginHome} targetDir=${targetDir}`);
yield settings_1.default.exportTo(pluginHome);
const resolved = yield locate_npm_1.default();
if (!resolved) {
throw new Error('npm could not be found. Please install npm and try again');
}
const { npm } = resolved;
yield spinner.next(strings('Configuring plugin sandbox'));
yield new Promise((resolve, reject) => {
child_process_1.execFile(npm, ['init', '-y'], { cwd: pluginHome }, (error, stdout, stderr) => __awaiter(void 0, void 0, void 0, function* () {
if (error) {
console.error(error);
yield spinner.fail();
reject(error);
return;
}
if (stderr.length > 0) {
debug(stderr);
}
if (stdout.length > 0) {
debug(stdout);
}
resolve();
}));
});
yield spinner.next(strings('Installing dependencies'));
yield new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
const args = ['install', nameWithVersion, '--prod', '--no-package-lock', '--loglevel', 'info'];
debug('npm install args', args);
const sub = child_process_1.spawn(npm, args, {
cwd: pluginHome
});
if (!sub) {
yield spinner.fail();
reject(new Error(strings('Error installing dependencies')));
}
sub.stderr.on('data', (data) => __awaiter(void 0, void 0, void 0, function* () {
const error = data.toString();
if (error.indexOf('code E404') >= 0) {
sub.kill();
yield spinner.fail();
reject(new Error(strings('The plugin {0} does not exist', nameWithVersion)));
}
else if (error.indexOf('ERR') >= 0) {
yield spinner.fail();
return reject(error);
}
else {
spinner.text = error;
}
}));
sub.stdout.on('data', data => {
debug(data.toString());
});
sub.on('close', (code) => __awaiter(void 0, void 0, void 0, function* () {
debug('npm install done', code);
if (code !== 0) {
yield spinner.fail();
reject(new Error('Internal Error'));
}
else {
resolve();
}
}));
}));
yield spinner.next(strings('Updating plugin registry'), strings('Installing dependencies'));
yield REPL.qexec('plugin compile');
let commandPrefix = nameWithoutVersion.replace(/^.*plugin-(.*)$/, '$1');
try {
const pjson = yield Promise.resolve().then(() => require(path_1.join(pluginHome, 'node_modules', nameWithoutVersion, 'package.json')));
if (pjson.krew && pjson.krew.commandPrefix) {
commandPrefix = pjson.krew.commandPrefix;
}
}
catch (err) {
console.error('could not find pjson', err);
}
if (process.env.KUI_BIN_DIR && process.env.KUI_BIN_PREFIX && process.env.KUI_BIN) {
yield spinner.next(strings('Creating command-line executable'));
try {
const sourcePath = process.env.KUI_BIN;
const target = `${process.env.KUI_BIN_PREFIX}${commandPrefix}`;
debug(`creating command-line executable with sourcePath=${sourcePath} commandPrefix=${commandPrefix} target=${target} binDir=${process.env.KUI_BIN_DIR}`);
const targetPath = path_1.join(process.env.KUI_BIN_DIR, target);
yield fs_extra_1.ensureDir(process.env.KUI_BIN_DIR);
yield fs_extra_1.unlink(targetPath).catch(err => {
if (err.code !== 'ENOENT') {
throw err;
}
});
yield fs_extra_1.symlink(sourcePath, targetPath);
}
catch (err) {
yield spinner.fail();
throw err;
}
}
yield spinner.next(strings('Successfully installed. Here are your new commands:'));
yield spinner.stop();
return REPL.qexec(`plugin commands "${nameWithoutVersion}"`);
});
exports.default = (commandTree) => {
commandTree.listen('/plugin/install', doInstall, {
usage
});
};
//# sourceMappingURL=install.js.map