Skip to content

Commit

Permalink
refactor: move cancelable and spinnerify into @solid-cli/utils
Browse files Browse the repository at this point in the history
feat: add interactive prompt for `route` subcommand
  • Loading branch information
Tommypop2 committed Jan 23, 2025
1 parent 2cfc25b commit de5fa73
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/create/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineCommand } from "citty";
import { createVanilla } from "./create-vanilla";
import * as p from "@clack/prompts";
import { cancelable, spinnerify } from "./utils/ui";
import { cancelable, spinnerify } from "@solid-cli/utils/ui";
import { createStart } from "./create-start";
import { getTemplatesList, StartTemplate, VanillaTemplate } from "./utils/constants";
import { detectPackageManager } from "@solid-cli/utils/package-manager";
Expand Down
6 changes: 6 additions & 0 deletions packages/full-solid/src/start/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRoute } from "@solid-cli/utils";
import { defineCommand } from "citty";
import * as p from "@clack/prompts";
import { green } from "picocolors";
import { cancelable } from "@solid-cli/utils/ui"
export const startCommands = defineCommand({
meta: { description: "Start-specific commands" }, subCommands: {
route: defineCommand({
Expand All @@ -13,6 +14,11 @@ export const startCommands = defineCommand({
},
},
async run({ args: { path } }) {
path ||= await cancelable(p.text({
message: "Route name", validate(value) {
if (value.length === 0) return "A route name is required"
},
}))
await createRoute(path as string);
p.log.success(`Route ${green(path as string)} successfully created!`)
},
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"types": "./types/package-manager/index.d.ts",
"import": "./dist/package-manager/index.mjs",
"require": "./dist/package-manager/index.mjs"
},
"./ui": {
"types": "./types/ui/index.d.ts",
"import": "./dist/ui/index.mjs",
"require": "./dist/ui/index.mjs"
}
},
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions packages/utils/src/ui/cancelable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { log } from "@clack/prompts";
export const cancelable = async <T = unknown>(
prompt: Promise<T | symbol>,
cancelMessage: string = "Canceled",
): Promise<T> => {
const value = await prompt;

if (typeof value === "symbol") {
log.warn(cancelMessage);
process.exit(0);
}

return value;
};
3 changes: 3 additions & 0 deletions packages/utils/src/ui/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { cancelable } from "./cancelable";
import { spinnerify } from "./spinnerify";
export { cancelable, spinnerify }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log, spinner } from "@clack/prompts";
import { spinner } from "@clack/prompts";
type SpinnerItem<T> = {
startText: string;
finishText: string;
Expand All @@ -16,18 +16,4 @@ export async function spinnerify<T>(spinners: SpinnerItem<any>[] | SpinnerItem<T
s.stop(finishText);
}
return results.length === 1 ? results[0] : results;
}

export const cancelable = async <T = unknown>(
prompt: Promise<T | symbol>,
cancelMessage: string = "Canceled",
): Promise<T> => {
const value = await prompt;

if (typeof value === "symbol") {
log.warn(cancelMessage);
process.exit(0);
}

return value;
};
}

0 comments on commit de5fa73

Please sign in to comment.