@controlplane/cli
Version:
Control Plane Corporation CLI
116 lines • 3.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompletionCommand = void 0;
const fs = require("fs");
const command_1 = require("./command");
const completer_1 = require("./completion/completer");
const cache_generator_1 = require("./completion/cache-generator");
/**
* Parses completion environment from process.env
*/
function parseEnv(env) {
const line = env.COMP_LINE || '';
const point = Number(env.COMP_POINT) || 0;
const words = Number(env.COMP_CWORD) || 0;
const parts = line.split(' ');
const last = parts[parts.length - 1] || '';
const prev = parts[parts.length - 2] || '';
const partial = line.slice(0, point);
const lastPartial = partial.split(' ').pop() || '';
return {
complete: true,
words,
point,
line,
partial,
last,
lastPartial,
prev,
};
}
/**
* Logs completions to stdout in the correct format for shell completion
* Format: name:description (colon-separated, colons in description escaped)
* Shell scripts parse with ${line%%:*} to extract name
*/
function logCompletions(completions) {
for (const item of completions) {
if (typeof item === 'string') {
console.log(item);
}
else {
// Output just the name - descriptions are parsed by shell scripts
// but can cause issues with special characters (colons in URLs, etc.)
console.log(item.name);
}
}
}
function dump(obj) {
if (process.env.CPLN_DEBUG) {
const path = '/tmp/complete-debug.json';
try {
fs.writeFileSync(path, JSON.stringify(obj, undefined, 2));
}
catch (_a) { }
}
}
class CompletionCommand extends command_1.Command {
constructor() {
super(...arguments);
this.command = 'completion';
this.describe = false;
}
builder(yargs) {
return yargs;
}
async handle(args) {
// Regenerate cache if stale (version mismatch or missing)
cache_generator_1.CacheGenerator.regenerateIfStale(args);
// get a parser without handlers
// @ts-ignore
const parser = args._parser();
// get the line as string[]
// ignore the first 4 elements because:
// node main.js cpln completion --
const array = process.argv.slice(5);
// force the model to be populated:
// handler are disable and all error reporting is turned off - we just want the parser to do 1 pass
let currentArgs;
try {
parser.showHelpOnFail(false);
parser.exitProcess(false);
parser.fail(() => { });
parser.strict(false);
currentArgs = parser.parse(array);
}
catch (e) {
// console.log(e);
}
// get the model from the spying instance
const stack = parser['_command_stack'];
const env = parseEnv(process.env);
await this.complete(args, stack, env);
}
async complete(args, stack, env) {
const c = new completer_1.Completer(args, stack, env, this.env.profileManager);
try {
const completions = await c.complete();
dump({
env: env,
args: args,
stack: stack,
completions: completions,
});
logCompletions(completions);
}
catch (e) {
dump({
env: env,
stack: stack,
error: e,
});
}
}
}
exports.CompletionCommand = CompletionCommand;
//# sourceMappingURL=complete.js.map