Skip to content

Commit

Permalink
🪚 OmniGraph™ Prompt state (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Dec 14, 2023
1 parent 7280197 commit 905c0e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
5 changes: 3 additions & 2 deletions packages/create-lz-oapp/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";
import { render } from "ink";
import { Command } from "commander";
import { promptForConfig, promptForContinue } from "@/utilities/prompts.js";
import { promptForConfig } from "@/utilities/prompts.js";
import { Header } from "@/components/branding.js";
import { ConfigSummary } from "@/components/config.js";
import { Setup } from "@/components/setup.js";
import { Providers } from "@/components/providers.js";
import { promptToContinue } from "@layerzerolabs/io-utils";

new Command("create-lz-oapp")
.description("Create LayerZero OApp with one command")
Expand All @@ -20,7 +21,7 @@ new Command("create-lz-oapp")
render(<ConfigSummary value={config} />).unmount();

// Then we confirm we want to do this after showing the user what they have specified
const continuePlease = await promptForContinue();
const continuePlease = await promptToContinue();
if (!continuePlease) {
return;
}
Expand Down
20 changes: 1 addition & 19 deletions packages/create-lz-oapp/src/utilities/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import { EXAMPLES, PACKAGE_MANAGERS } from '@/config.js'
import prompts from 'prompts'
import { isPackageManagerAvailable } from './installation.js'
import { isDirectory } from '@layerzerolabs/io-utils'
import { handlePromptState, isDirectory } from '@layerzerolabs/io-utils'
import { resolve } from 'path'

const handlePromptState = (state: { aborted: boolean }) => {
if (state.aborted) {
// If we don't re-enable the terminal cursor before exiting
// the program, the cursor will remain hidden
process.stdout.write('\x1B[?25h')
process.stdout.write('\n')
process.exit(1)
}
}

export const promptForContinue = async () =>
prompts({
type: 'confirm',
name: 'pleasecontinue',
message: 'Would you like to continue?',
initial: true,
}).then(({ pleasecontinue }): boolean => pleasecontinue)

export const promptForConfig = () =>
prompts([
{
Expand Down
26 changes: 26 additions & 0 deletions packages/io-utils/src/stdio/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import assert from 'assert'
import prompts from 'prompts'

/**
* Helper utility to be used when raw access to prompts
* is required.
*
* This should be passed to `onState` property of the options
* to handle cases where the user sends a SIGTERM signal to the process
* (ctrl + c) usually.
*
* If not passed in, the SIGTERM will cancel the prompt but the promise
* will resolve with an invalid result and, what's more important,
* if cursor had been hidden it will be swallowed and people will need to
* restart their terminal to bring it back
*
* @param {{ aborted?: boolean}} state Prompts state object
*/
export const handlePromptState = (state: { aborted?: boolean }) => {
if (state.aborted) {
// If we don't re-enable the terminal cursor before exiting
// the program, the cursor will remain hidden
process.stdout.write('\x1B[?25h')
process.stdout.write('\n')
process.exit(1)
}
}

export const promptToContinue = async (
message: string = 'Do you want to continue?',
defaultValue = true
Expand All @@ -10,6 +35,7 @@ export const promptToContinue = async (
name: 'value',
message,
initial: defaultValue,
onState: handlePromptState,
})

assert(typeof value === 'boolean', `Invariant error: Expected a boolean response, got ${value}`)
Expand Down

0 comments on commit 905c0e2

Please sign in to comment.