Skip to content

Commit

Permalink
roar1
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka committed Dec 19, 2024
1 parent 9f94c7a commit a28335c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
15 changes: 10 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,13 @@ 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,
}).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.

0 comments on commit a28335c

Please sign in to comment.