UNPKG

hardhat

Version:

Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

82 lines 3.33 kB
import { createInterface } from "node:readline"; import { styleText } from "node:util"; import { assertHardhatInvariant } from "@nomicfoundation/hardhat-errors"; import { AsyncMutex } from "@nomicfoundation/hardhat-utils/synchronization"; export class UserInterruptionManagerImplementation { #hooks; #mutex = new AsyncMutex(); constructor(hooks) { this.#hooks = hooks; } async displayMessage(interruptor, message) { return await this.#mutex.exclusiveRun(async () => { return await this.#hooks.runHandlerChain("userInterruptions", "displayMessage", [interruptor, message], defaultDisplayMessage); }); } async requestInput(interruptor, inputDescription) { return await this.#mutex.exclusiveRun(async () => { return await this.#hooks.runHandlerChain("userInterruptions", "requestInput", [interruptor, inputDescription], defaultRequestInput); }); } async requestSecretInput(interruptor, inputDescription) { return await this.#mutex.exclusiveRun(async () => { return await this.#hooks.runHandlerChain("userInterruptions", "requestSecretInput", [interruptor, inputDescription], defaultRequestSecretInput); }); } async uninterrupted(f) { return await this.#mutex.exclusiveRun(f); } } async function defaultDisplayMessage(_context, interruptor, message) { console.log(styleText("blue", `[${interruptor}]`) + ` ${message}`); } async function defaultRequestInput(_context, interruptor, inputDescription) { const rl = createInterface({ input: process.stdin, output: process.stdout, }); return await new Promise((resolve) => { rl.question(styleText("blue", `[${interruptor}]`) + ` ${inputDescription}: `, (answer) => { resolve(answer); rl.close(); }); }); } async function defaultRequestSecretInput(_context, interruptor, inputDescription) { const rl = createInterface({ input: process.stdin, output: process.stdout, }); /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We need to access a private property of the readline interface. */ const rlAsAny = rl; let initialMessage; rlAsAny._writeToOutput = (out) => { if (initialMessage === undefined || out.length !== 1) { if (initialMessage === undefined) { initialMessage = out; } assertHardhatInvariant(rlAsAny.output !== undefined, "Expected readline output to be defined"); // We show the initial message as is if (out.startsWith(initialMessage)) { rlAsAny.output.write(initialMessage); out = out.slice(initialMessage.length); } else if (out.trim() === "") { rlAsAny.output.write(out); out = ""; } } // We show the rest of the chars as "*" for (const _ of out) { rlAsAny.output.write("*"); } }; return await new Promise((resolve) => { rl.question(styleText("blue", `[${interruptor}]`) + ` ${inputDescription}: `, (answer) => { resolve(answer); rl.close(); }); }); } //# sourceMappingURL=user-interruptions.js.map