Skip to content

Commit 980d7ba

Browse files
authored
feat: multiple hook args
2 parents aab7c41 + 16b86aa commit 980d7ba

File tree

10 files changed

+70
-24
lines changed

10 files changed

+70
-24
lines changed

apps/docs/app/api/hooks/[title]/route.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import { NextResponse } from "next/server";
2+
import type { Hook } from "utils";
23
import path from "path";
34
import fs from "fs";
45

5-
type Hook = Readonly<{
6-
id: number;
7-
title: string;
8-
description: string;
9-
content: string;
10-
}>;
11-
126
const filePath = path.join(process.cwd(), "app", "hooks.json");
137

148
async function loadData(): Promise<Hook[]> {

apps/docs/app/api/hooks/route.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { NextResponse } from "next/server";
2+
import type { Hook } from "utils";
23
import path from "path";
34
import fs from "fs";
45

56
export const dynamic = "force-dynamic";
67

7-
type Hook = Readonly<{
8-
id: number;
9-
title: string;
10-
description: string;
11-
content: string;
12-
}>;
13-
148
const filePath = path.join(process.cwd(), "app", "hooks.json");
159

1610
async function loadData(): Promise<Hook[]> {

apps/docs/components/internal/hero.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function Hero() {
1717
className="group relative flex h-10 items-center gap-2 rounded-full bg-gradient-to-b from-green-900/50 to-green-700 px-6 text-lg font-medium text-green-300 transition-colors hover:text-green-200 dark:from-green-700/50 dark:to-gray-900 dark:text-green-300 dark:hover:text-green-200"
1818
>
1919
<span className="relative z-10 flex items-center gap-2">
20-
Rehooks v3 is out{" "}
20+
Rehooks v4 is out{" "}
2121
<ArrowUpRight className="transition-transform group-hover:-translate-y-0.5 group-hover:translate-x-0.5" />
2222
</span>
2323
<div className="absolute inset-0 rounded-full bg-gradient-to-r from-green-500 to-green-600 opacity-0 blur transition-opacity group-hover:opacity-20 dark:from-green-400 dark:to-green-500" />

apps/docs/content/docs/guide.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ This command will ask you to select the hooks you want to add. You can select mu
4848
npx rehooks-cli [options] [command]
4949
```
5050

51+
> Arguments are optional. You may want to leave args empty, so by running `npx rehooks-cli add` you'll access to the list of hooks.
52+
53+
```bash title="Terminal"
54+
npx rehooks-cli add useFocus useFetch useWindowSize
55+
```
56+
5157
## Options
5258

5359
These are the options available in the CLI.
@@ -62,7 +68,7 @@ These are the options available in the CLI.
6268
| Command | Description |
6369
| ----------------------- | ------------------------------------ |
6470
| `init [options] [path]` | Initialize the Rehooks configuration |
65-
| `add [options]` | Add custom hooks to your project |
71+
| `add [options] [hooks]` | Add custom hooks to your project |
6672
| `help [command]` | Displays help for a specific command |
6773

6874
### Init

apps/docs/content/docs/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Introduction
33
description: Reusable, SOLID, and maintainable React hooks that you insert directly into your project.
44
---
55

6-
# What is Rehooks?
6+
# TL;DR
77

88
Rehooks is a CLI that allows you to insert hooks directly to your project. It provides a wide range of hooks for common use cases, By using Rehooks CLI you can reduce the amount of code you need to write and focus on the core functionality of your application.
99

@@ -20,7 +20,7 @@ By using the Rehooks CLI you will able to reduce the amount of unused and duplic
2020
# Key Features
2121

2222
- 🔥 Lightweight and performant: Rehooks is written in TypeScript and bundled with `tsup` for maximum performance and compatibility.
23-
- 📦 Modular and tree-shakable: Rehooks is designed to be modular and tree-shakable, allowing you to only include the hooks you need in your application.
23+
- 📦 Tree-shakable: Rehooks is tree-shakable, allowing you to use only the hooks you need.
2424
- 🪄 Maintainable and extensible: The internal hooks are designed to be maintainable and reusable to make it easy to use based on your needs.
2525
- 📝 Well-documented: Rehooks comes with detailed documentation and examples, making it easy to understand and use.
2626
- 💪 Tested and maintained: Rehooks has a comprehensive test suite and is actively maintained by the community.

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rehooks-cli",
3-
"version": "3.8.2",
3+
"version": "4.0.0",
44
"description": "A CLI to insert hooks directly to your project.",
55
"publishConfig": {
66
"access": "public"

packages/cli/src/commands/add.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import fs from "fs";
1111
export const add = new Command()
1212
.name("add")
1313
.description("Add hooks to your project")
14+
.argument("[hooks...]", "Specify one or more hook names to add")
1415
.option("-f, --force", "Force overwrite existing hook files without prompt")
15-
.action(async (options) => {
16+
.action(async (hooks, options) => {
1617
const config = await getConfig(process.cwd());
1718

1819
if (!config) {
@@ -24,17 +25,61 @@ export const add = new Command()
2425
const shouldForceOverwrite = options.force || forceOverwrite;
2526

2627
try {
28+
if (hooks.length > 0) {
29+
for (const hook of hooks) {
30+
const hookFilePath = path.join(directory, `${hook}.ts`);
31+
32+
if (fs.existsSync(hookFilePath) && !shouldForceOverwrite) {
33+
const { overwrite } = await inquirer.prompt([
34+
{
35+
type: "confirm",
36+
name: "overwrite",
37+
message: bold(
38+
red(
39+
`${hook}.ts already exists. Do you want to overwrite it?`,
40+
),
41+
),
42+
default: false,
43+
},
44+
]);
45+
46+
if (!overwrite) {
47+
logger.info(cyan(`Skipping ${hook}.ts.`));
48+
continue;
49+
}
50+
}
51+
52+
const spinner = ora(cyan(`Adding ${hook}...`)).start();
53+
const selectedHookResponse = await axios.get(
54+
`https://rehooks.pyr33x.ir/api/hooks/${hook}`,
55+
);
56+
let { content } = selectedHookResponse.data;
57+
fs.writeFileSync(hookFilePath, content);
58+
spinner.succeed(
59+
green(`Created ${bold(hook)} hook at ${bold(hookFilePath)}.`),
60+
);
61+
}
62+
return;
63+
}
64+
2765
const fetchSpinner = ora(cyan("Fetching hooks...")).start();
2866
const response = await axios.get("https://rehooks.pyr33x.ir/api/hooks");
29-
const hooks = response.data;
67+
const hooksData = response.data;
3068
fetchSpinner.succeed(green("Done."));
3169

3270
const { selectedHooks } = await inquirer.prompt([
3371
{
3472
type: "checkbox",
3573
name: "selectedHooks",
36-
message: bold("Select hooks to add:"),
37-
choices: hooks.map((h: { title: string }) => h.title),
74+
message: bold("Pick hooks to add:"),
75+
choices: hooksData.map((h: { title: string }) => h.title),
76+
required: true,
77+
theme: {
78+
icon: {
79+
checked: green("✔"),
80+
cursor: "",
81+
},
82+
},
3883
},
3984
]);
4085

packages/cli/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async function main() {
1111
.name("rehooks")
1212
.description("A CLI to insert hooks directly to your project.")
1313
.version(
14-
packageInfo.version || "3.8.2",
14+
packageInfo.version || "4.0.0",
1515
"-v, --version",
1616
"Displays the version number",
1717
);

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./functions";
22
export * from "./constants";
3+
export * from "./types";

packages/utils/src/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type Hook = Readonly<{
2+
id: number;
3+
title: string;
4+
description: string;
5+
content: string;
6+
}>;

0 commit comments

Comments
 (0)