@controlplane/cli
Version:
Control Plane Corporation CLI
195 lines • 6.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Completer = void 0;
const _ = require("lodash");
const pathCompleter_1 = require("../../rest/pathCompleter");
const completer_1 = require("../../update/completer");
class Completer {
constructor(parsed, stack, env, profileManager) {
this.parsed = parsed;
this.stack = stack;
this.env = env;
this.profileManager = profileManager;
}
async complete() {
var _a;
if (!this.env.complete) {
// example https://www.npmjs.com/package/tabtab
return [];
}
// the current command (top of stack)
const cmd = this.currentCommand();
if (((_a = this.mainCommand()) === null || _a === void 0 ? void 0 : _a.name) === 'rest' && this.stack[2] && this.env.words <= 3) {
let verb = this.stack[2].name;
if (verb == 'edit') {
verb = 'patch';
}
return (0, pathCompleter_1.completePath)(this.env.last, verb);
}
// try options
const currentOption = this.currentOption();
if (currentOption) {
const def = this.findOption(currentOption);
if (def) {
if (def.completions) {
return this.completeChoices(def.completions);
}
else if (def.paths) {
return (0, completer_1.completeUpdatablePath)(this.env.last, def.paths);
}
else if (def.choices) {
return this.completeChoices(def.choices);
}
else if (def.boolean) {
// no point in completing true/false
}
else {
return this.completeWellKnownOptionValue(cmd, def);
}
}
}
if (this.commandChainIs('profile', 'get') || this.commandChainIs('profile', 'delete')) {
// use has run `cpln profile get`: lets complete profile names
return this.completeProfiles();
}
if (this.commandChainIs('profile', 'update') || this.commandChainIs('profile', 'set-default')) {
if (this.env.words >= 4) {
// a profile has been selected
return this.completeGeneric();
}
else {
return this.completeProfiles();
}
}
// TODO more completions
// cannot be smart at this point
return this.completeGeneric();
}
completeChoices(choices) {
return choices;
}
completeProfiles() {
return this.profileManager.list().map((p) => {
return {
name: p.name,
description: p.request.endpoint,
};
});
}
/**
* the most generic behavior: return all possible completions
*/
completeGeneric() {
const currentCmd = this.currentCommand();
// collect the args for all commands up to stack
let allOptions = [];
for (let cmd of this.stack) {
for (let opt of cmd.options) {
allOptions.push({
name: toArgName(opt.name),
description: opt.description,
});
if (opt.alias) {
for (let a of opt.alias) {
allOptions.push({
name: toArgName(a),
description: opt.description,
});
}
}
if (opt.boolean) {
// for bools the the syntax --no-OPTION is also valid
allOptions.push({
name: toArgName('no-' + opt.name),
description: opt.description,
});
}
}
}
if (this.stack.length == 1) {
// version is defined only at the root
allOptions.push({
name: '--version',
description: 'Show version number',
});
}
return _.concat(currentCmd.commands, allOptions);
}
completeWellKnownOptionValue(cmd, opt) {
if (opt.name == 'profile') {
return this.completeProfiles();
}
if (opt.name == 'endpoint' && process.env.CPLN_DEBUG) {
return [
{
name: 'http://localhost:3000',
description: 'local',
},
{
name: 'https://api.test.cpln.io',
description: 'test',
},
{
name: 'https://api.cpln.io',
description: 'prod',
},
];
}
return [
{
name: 'https://api.cpln.io',
description: '',
},
];
}
//////////////////////////////
// helpers
currentCommand() {
return this.stack[this.stack.length - 1];
}
mainCommand() {
return this.stack[1];
}
currentOption() {
const prev = this.env.prev;
if (!prev.startsWith('-')) {
return undefined;
}
return toOptName(prev);
}
getUsedOptions() {
// TODO parse env.line and collect all that look like opts (starting with --)
return new Set();
}
commandChainIs(...cmds) {
var _a;
for (let i = 0; i < cmds.length; i++) {
if (((_a = this.stack[i + 1]) === null || _a === void 0 ? void 0 : _a.name) != cmds[i]) {
return false;
}
}
return true;
}
findOption(optName) {
for (let cmd of this.stack) {
for (let opt of cmd.options) {
// try an option or any of its aliases: both --output and -o are valid and --out-put and --outPut
if (_.camelCase(opt.name) == optName || opt.name == optName || (opt.alias && opt.alias.indexOf(optName) >= 0)) {
return opt;
}
}
}
return undefined;
}
}
exports.Completer = Completer;
function toArgName(opt) {
if (opt.length == 1) {
return '-' + opt;
}
return '--' + _.kebabCase(opt);
}
function toOptName(arg) {
return _.camelCase(arg);
}
//# sourceMappingURL=completer.js.map