Skip to content

Commit

Permalink
Finish feature/6
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcastillop authored Jun 12, 2024
2 parents 9f08bd2 + cec009a commit 075ce24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
37 changes: 23 additions & 14 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { downloadHook, getAllHooksName } from "../utils/downloadHook";
const addOptionsSchema = z.object({
hooks: z.array(z.string()).optional(),
overwrite: z.boolean(),
cwd: z.string(),
all: z.boolean(),
path: z.string().optional(),
});
Expand All @@ -26,11 +25,6 @@ export const add = new Command()
.description("add a hook to your project")
.argument("[hooks...]", "the hooks to add")
.option("-o, --overwrite", "overwrite existing files.", false)
.option(
"-c, --cwd <cwd>",
"the working directory. defaults to the current directory.",
process.cwd()
)
.option("-a, --all", "add all available hooks", false)
.option("-p, --path <path>", "the path to add the hook to.")
.action(async (hooks, opts) => {
Expand All @@ -39,13 +33,7 @@ export const add = new Command()
...opts,
});

const cwd = path.resolve(options.cwd);

if (!existsSync(cwd)) {
logger.error(`The path ${cwd} does not exist. Please try again.`);
process.exit(1);
}

// Handle hooks selection
const allHooks = await getAllHooksName();

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

// Handle path selection
let selectedPath = options.path ?? "";

if (selectedPath === "") {
const { path } = await prompts({
type: "text",
name: "path",
message: "Where would you like to add the hooks?",
instructions: false,
});

selectedPath = path;
}

if (!existsSync(selectedPath)) {
logger.error(
`The path ${selectedPath} does not exist. Please try again.`
);
process.exit(1);
}

for (let i = 0; i < selectedHooks.length; i++) {
const hook = selectedHooks[i];
await downloadHook(hook);
await downloadHook(hook, selectedPath);
}
});
4 changes: 2 additions & 2 deletions src/utils/downloadHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getAllHooksName = async () => {
}
};

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

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

await fs.writeFile(`${hookName}.ts`, buffer);
await fs.writeFile(`${path}/${hookName}.ts`, buffer);
console.log(`✅ ${hookName} added`);
} catch (e) {
console.log(`❌ ${hookName} error`);
Expand Down

0 comments on commit 075ce24

Please sign in to comment.