DefinitelyTyped/types/tabtab/tabtab-tests.ts
Kamontat Chantrachirathumrong 8b982e49c5
Update test define file
2018-11-25 14:33:34 +01:00

84 lines
2.1 KiB
TypeScript

/// <reference types="node" />
import { install, log, TabtabEnv, uninstall, parseEnv } from "tabtab";
const opts = require("minimist")(process.argv.slice(2), {
string: ["foo", "bar"],
boolean: ["help", "version", "loglevel"]
});
const args = opts._;
const completion = (env: TabtabEnv) => {
if (!env.complete) return;
// Write your completions there
if (env.prev === "foo") {
return log(["is", "this", "the", "real", "life"]);
}
if (env.prev === "bar") {
return log(["is", "this", "just", "fantasy"]);
}
if (env.prev === "--loglevel") {
return log(["error", "warn", "info", "notice", "verbose"]);
}
return log([
"--help",
"--version",
"--loglevel",
"foo",
"bar",
"install-completion",
"completion",
"someCommand:someCommand is some kind of command with a description",
{
name: "someOtherCommand:hey",
description: 'You must add a description for items with ":" in them'
},
"anotherOne"
]);
};
const run = (): Promise<void> => {
const cmd = args[0];
// Write your CLI there
// Here we install for the program `tabtab-test` (this file), with
// completer being the same program. Sometimes, you want to complete
// another program that's where the `completer` option might come handy.
if (cmd === "install-completion") {
return install({
name: "tabtab-test",
completer: "tabtab-test"
});
}
if (cmd === "uninstall-completion") {
// Here we uninstall for the program `tabtab-test` (this file).
return uninstall({
name: "tabtab-test"
});
}
// The completion command is added automatically by tabtab when the program
// is completed.
if (cmd === "completion") {
return new Promise<void>(res => {
const env = parseEnv(process.env);
completion(env);
res();
});
}
return new Promise<void>((_, rej) => rej());
};
run().catch(e => {
console.error(e);
});