askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
44 lines • 2.11 kB
JavaScript
import { createRuntime } from "./core.js";
import { ui } from "./ui.js";
/**
* Factory function for creating standalone ask functions with custom configurations
* All createAsk functions work the same way - they create their own runtime context and execute flows
*/
export function createAsk(config) {
return async (flow, opts) => {
// Store the custom root container globally so the UI can access it
const originalRootContainer = globalThis.__customRootContainer;
const originalRootContainerProps = globalThis
.__customRootContainerProps;
// Set the custom root container if provided in options, otherwise use component from config
if (opts && typeof opts === "object" && "rootContainer" in opts) {
globalThis.__customRootContainer = opts.rootContainer;
globalThis.__customRootContainerProps =
opts.rootContainerProps || {};
}
else if (config.component) {
// Use the component from createAsk config as the default root container
globalThis.__customRootContainer = config.component;
globalThis.__customRootContainerProps = {};
}
// Force cleanup of any existing UI instance to ensure fresh render with new container
ui.cleanup?.();
try {
// Create a runtime with the standard UI (which will now use our custom root container)
const runtime = createRuntime(ui);
// Register onCancel callback if provided in options
if (opts && typeof opts === "object" && "onCancel" in opts && opts.onCancel) {
runtime.registerCancelCallback(opts.onCancel);
}
// Execute the flow with the custom runtime
return await runtime.executeFlow(flow);
}
finally {
// Restore the original root container
globalThis.__customRootContainer = originalRootContainer;
globalThis.__customRootContainerProps =
originalRootContainerProps;
}
};
}
//# sourceMappingURL=ask-factory.js.map