Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make our Y/N prompt a toggle, and make default answer yes for non-destructive questions #646

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions client/packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "./src/util/packageManager.js";
import { pathExists, readJsonFile } from "./src/util/fs.js";
import prettier from "prettier";
import toggle from "./src/toggle.js";

const execAsync = promisify(exec);

Expand Down Expand Up @@ -460,7 +461,10 @@ async function handleEnvFile(pkgAndAuthInfo, appId) {
console.log(
`If we set ${chalk.green("`" + envName + "`")}, we can remember the app that you chose for all future commands.`,
);
const ok = await promptOk("Want us to create this env file for you?");
const ok = await promptOk(
"Want us to create this env file for you?",
/*defaultAnswer=*/ true,
);
if (!ok) {
console.log(
`No .env file created. You can always set ${chalk.green("`" + envName + "`")} later. \n`,
Expand Down Expand Up @@ -517,6 +521,7 @@ async function login(options) {

const ok = await promptOk(
`This will open instantdb.com in your browser, OK to proceed?`,
/*defaultAnswer=*/ true,
);

if (!ok) return;
Expand Down Expand Up @@ -632,6 +637,7 @@ async function promptImportAppOrCreateApp() {
if (!apps.length) {
const ok = await promptOk(
"You don't have any apps. Want to create a new one?",
/*defaultAnswer=*/ true,
);
if (!ok) return { ok: false };
return await promptCreateApp();
Expand Down Expand Up @@ -1288,14 +1294,19 @@ function prettyPrintJSONErr(data) {
}
}

async function promptOk(message) {
async function promptOk(message, defaultAnswer = false) {
const options = program.opts();

if (options.yes) return true;

return await confirm({
return await toggle({
message,
default: false,
default: defaultAnswer,
theme: {
style: {
highlight: (x) => chalk.underline.blue(x),
answer: (x) => chalk.underline.blue(x),
}
}
}).catch(() => false);
}

Expand Down
4 changes: 3 additions & 1 deletion client/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"instant-cli": "bin/index.js"
},
"dependencies": {
"@inquirer/prompts": "^5.3.8",
"@inquirer/prompts": "5.3.8",
"@inquirer/core": "9.0.10",
"ansi-escapes": "4.3.2",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"dotenv": "^16.3.1",
Expand Down
53 changes: 53 additions & 0 deletions client/packages/cli/src/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// From:
// https://github.com/skarahoda/inquirer-toggle
import {
isDownKey,
isUpKey,
useKeypress,
useState,
isEnterKey,
} from "@inquirer/core";
import { createPrompt, usePrefix, makeTheme } from "@inquirer/core";
import ansiEscapes from "ansi-escapes";

function isLeftKey(key) {
return key.name === "left";
}

function isRightKey(key) {
return key.name === "right";
}

export default createPrompt((config, done) => {
const theme = makeTheme({ active: "yes", inactive: "no" }, config.theme);
const prefix = usePrefix({ theme });
const [value, setValue] = useState(config.default ?? false);
const [isDone, setIsDone] = useState(false);

useKeypress((key) => {
if (isEnterKey(key)) {
setIsDone(true);
done(value);
} else if (
isLeftKey(key) ||
isRightKey(key) ||
isUpKey(key) ||
isDownKey(key)
) {
setValue(!value);
}
});
const message = theme.style.message(config.message);

if (isDone) {
return `${prefix} ${message} ${theme.style.answer(value ? theme.active : theme.inactive)}`;
}

const activeMessage = value
? theme.style.highlight(theme.active)
: theme.active;
const inactiveMessage = value
? theme.inactive
: theme.style.highlight(theme.inactive);
return `${prefix} ${message} ${inactiveMessage} / ${activeMessage}${ansiEscapes.cursorHide}`;
});
8 changes: 7 additions & 1 deletion client/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading