@controlplane/cli
Version:
Control Plane Corporation CLI
206 lines • 7.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.completeUpdatablePath = void 0;
const _ = require("lodash");
// const log: (message?: any, ...optionalParams: any[]) => void = console.log;
const log = (..._values) => { };
function completeUpdatablePath(input, paths) {
let suggestions = [];
log('@completer', 'input:', input);
// Empty input returns all paths
if (input.length < 1) {
log('@@input is empty');
return allPossiblePathsUpToParam(paths);
}
if (input.endsWith('.')) {
log('@@ends with dot');
return allAfterDot(input, paths);
}
if (input.endsWith('=')) {
log('@@ends with equal');
// all possible choice values
return allChoiceValues(input, paths);
}
if (!input.endsWith('=') && input.includes('=')) {
log('@@has an equal but not ends with it');
// value of a choice
return allChoiceValues(input, paths);
}
for (let spec of paths) {
const pathParts = spec.path.split('.');
const inputParts = input.split('.');
if (inputParts.length > pathParts.length) {
continue;
}
for (let index in inputParts) {
const inputPart = inputParts[index];
const pathPart = pathParts[index];
const pathPartIsParam = isParam(pathPart);
const isLastInputPart = Number(index) === inputParts.length - 1;
if (!isLastInputPart) {
if (pathPartIsParam) {
pathParts[index] = inputPart;
}
else if (pathPart !== inputPart) {
break;
}
continue;
}
// last part
if (pathPartIsParam) {
return [];
}
if (!pathPart.includes(inputPart)) {
break;
}
const updatedPath = pathParts.join('.');
let pathHasParam = hasParam(updatedPath);
if (pathHasParam) {
// up to the param
suggestions.push(`${updatedPath.split('<')[0]}`);
}
else {
suggestions.push(`${updatedPath}=`);
if (spec.array) {
suggestions.push(`${updatedPath}-=`);
suggestions.push(`${updatedPath}+=`);
}
}
}
}
return prepareSuggestions(suggestions);
}
exports.completeUpdatablePath = completeUpdatablePath;
function allAfterDot(input, paths) {
log('all after dot');
let suggestions = [];
const inputParts = input.split('.').filter(Boolean);
for (let spec of paths) {
log('/////////////');
log('path:', spec.path);
const pathParts = spec.path.split('.');
if (inputParts.length > pathParts.length) {
continue;
}
for (let index in inputParts) {
const inputPart = inputParts[index];
const pathPart = pathParts[index];
const pathPartIsParam = isParam(pathPart);
const isLastInputPart = Number(index) === inputParts.length - 1;
log('@index', 'inputPart:', inputPart, '| pathPart:', pathPart, '| isParam:', pathPartIsParam, '| last:', isLastInputPart);
if (pathPartIsParam && !isLastInputPart) {
log('is param and not last, updating param');
pathParts[index] = inputPart;
log('--continue');
continue;
}
if (!isLastInputPart) {
if (pathPart !== inputPart) {
log('not last and not equal');
log('--break');
break;
}
log('--continue');
continue;
}
// Last part of input, fulfill to the end of path
log('last part');
if (pathPartIsParam) {
pathParts[index] = inputPart;
}
if (pathPart !== inputPart && !pathPartIsParam) {
log('last but not equal');
log('--break');
break;
}
const updatedPath = pathParts.join('.');
if (hasParam(updatedPath)) {
// up to param
const nextPart = pathParts[Number(index) + 1];
if (isParam(nextPart)) {
break;
}
suggestions.push(`${updatedPath.split('<')[0]}`);
}
else {
suggestions.push(`${updatedPath}=`);
if (spec.array) {
suggestions.push(`${updatedPath}-=`);
suggestions.push(`${updatedPath}+=`);
}
}
}
}
return prepareSuggestions(suggestions);
}
function allChoiceValues(input, paths) {
log('/////////////////');
log('all choice values');
const inputParts = input.split('.').filter(Boolean);
for (let spec of paths) {
if (!spec.choices) {
log(spec.path, 'is not choice');
continue;
}
const pathParts = spec.path.split('.');
log('pathParts:', pathParts);
let correctPath = false;
for (let index in inputParts) {
const inputPart = inputParts[index];
const pathPart = pathParts[index];
const pathIsParam = isParam(pathPart);
const isLastInputPart = Number(index) === inputParts.length - 1;
log('inputPart:', inputPart, '| pathPart:', pathPart, '| pathIsParam:', pathIsParam, '| isLastInputPart:', isLastInputPart);
if (!isLastInputPart && pathIsParam) {
log('updating param of path with input');
pathParts[index] = inputPart;
}
if (isLastInputPart && pathPart === inputPart.split('=')[0]) {
log('last and path contains input, correct path');
correctPath = true;
}
}
if (correctPath) {
const choiceValue = [...inputParts].pop().split('=')[1] || '';
log('correct path', pathParts.join('.'));
log('choiceValue', choiceValue);
const suggestions = spec.choices.filter((c) => c.startsWith(choiceValue)).map((c) => `${pathParts.join('.')}=${c}`);
return prepareSuggestions(suggestions);
}
}
return [];
}
function allPossiblePathsUpToParam(paths) {
let suggestions = [];
for (let spec of paths) {
let path = spec.path;
let pathHasParam = hasParam(path);
if (pathHasParam) {
// up to the param
suggestions.push(`${path.split('<')[0]}`);
}
else {
suggestions.push(`${path}=`);
if (spec.array) {
suggestions.push(`${path}-=`);
suggestions.push(`${path}+=`);
}
}
continue;
}
return prepareSuggestions(suggestions);
}
function isParam(value) {
return value.startsWith('<') && value.endsWith('>');
}
function hasParam(value) {
return value.includes('<') && value.includes('>');
}
function prepareSuggestions(arr) {
const res = _.uniq(arr).sort();
if (res.length === 1) {
res.push(res[0] + ' ');
}
return res;
}
//# sourceMappingURL=completer.js.map
;