Skip to content

Commit 075ce24

Browse files
authored
Finish feature/6
2 parents 9f08bd2 + cec009a commit 075ce24

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/commands/add.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { downloadHook, getAllHooksName } from "../utils/downloadHook";
1616
const addOptionsSchema = z.object({
1717
hooks: z.array(z.string()).optional(),
1818
overwrite: z.boolean(),
19-
cwd: z.string(),
2019
all: z.boolean(),
2120
path: z.string().optional(),
2221
});
@@ -26,11 +25,6 @@ export const add = new Command()
2625
.description("add a hook to your project")
2726
.argument("[hooks...]", "the hooks to add")
2827
.option("-o, --overwrite", "overwrite existing files.", false)
29-
.option(
30-
"-c, --cwd <cwd>",
31-
"the working directory. defaults to the current directory.",
32-
process.cwd()
33-
)
3428
.option("-a, --all", "add all available hooks", false)
3529
.option("-p, --path <path>", "the path to add the hook to.")
3630
.action(async (hooks, opts) => {
@@ -39,13 +33,7 @@ export const add = new Command()
3933
...opts,
4034
});
4135

42-
const cwd = path.resolve(options.cwd);
43-
44-
if (!existsSync(cwd)) {
45-
logger.error(`The path ${cwd} does not exist. Please try again.`);
46-
process.exit(1);
47-
}
48-
36+
// Handle hooks selection
4937
const allHooks = await getAllHooksName();
5038

5139
let selectedHooks = options.all ? allHooks : options.hooks;
@@ -71,8 +59,29 @@ export const add = new Command()
7159
process.exit(0);
7260
}
7361

62+
// Handle path selection
63+
let selectedPath = options.path ?? "";
64+
65+
if (selectedPath === "") {
66+
const { path } = await prompts({
67+
type: "text",
68+
name: "path",
69+
message: "Where would you like to add the hooks?",
70+
instructions: false,
71+
});
72+
73+
selectedPath = path;
74+
}
75+
76+
if (!existsSync(selectedPath)) {
77+
logger.error(
78+
`The path ${selectedPath} does not exist. Please try again.`
79+
);
80+
process.exit(1);
81+
}
82+
7483
for (let i = 0; i < selectedHooks.length; i++) {
7584
const hook = selectedHooks[i];
76-
await downloadHook(hook);
85+
await downloadHook(hook, selectedPath);
7786
}
7887
});

src/utils/downloadHook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const getAllHooksName = async () => {
3737
}
3838
};
3939

40-
export const downloadHook = async (hookName: string) => {
40+
export const downloadHook = async (hookName: string, path: string) => {
4141
const hookUrl = `https://raw.githubusercontent.com/novajslabs/nova.js/main/src/hooks/${hookName}.ts`;
4242

4343
try {
@@ -50,7 +50,7 @@ export const downloadHook = async (hookName: string) => {
5050
const arrayBuffer = await response.arrayBuffer();
5151
const buffer = Buffer.from(arrayBuffer);
5252

53-
await fs.writeFile(`${hookName}.ts`, buffer);
53+
await fs.writeFile(`${path}/${hookName}.ts`, buffer);
5454
console.log(`✅ ${hookName} added`);
5555
} catch (e) {
5656
console.log(`❌ ${hookName} error`);

0 commit comments

Comments
 (0)