nope/lib/cli/repl.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-01-17 14:39:48 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-07-27 15:45:00
* @modify date 2021-07-27 15:45:00
* @desc [description]
*/
import run from "./runNopeBackend";
import { start } from "repl";
/**
* Starts an interactive console with the loaded dispatcher.
*/
export async function repl(
additionalArguments: {
help: string;
type: "string" | "number";
name: string | string;
defaultValue?: any;
}[] = []
) {
2022-01-17 17:06:10 +00:00
const loader = await run(
additionalArguments,
{
// skipLoadingConfig: true
},
true
);
2022-01-17 14:39:48 +00:00
const interactiveConsole = start({});
// Assing the context
2022-01-17 17:06:10 +00:00
interactiveConsole.context.dispatcher = loader.dispatcher;
interactiveConsole.context.loader = loader;
2022-01-17 14:39:48 +00:00
// Promise, that will be finished on exiting the interactive console.
const promise = new Promise((resolve) => {
interactiveConsole.once("exit", resolve);
});
await promise;
2022-01-17 17:06:10 +00:00
await loader.dispatcher.dispose();
2022-01-17 14:39:48 +00:00
}
// If requested As Main => Perform the Operation.
if (require.main === module) {
repl().catch(console.error);
}