Skip to content

Commit

Permalink
Finish feature/3
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcastillop authored Jun 8, 2024
2 parents 18ef771 + 7274642 commit 9c7d0d9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 211 deletions.
52 changes: 40 additions & 12 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@ import { getConfig } from "@/src/utils/get-config";
import { getPackageManager } from "@/src/utils/get-package-manager";
import { handleError } from "@/src/utils/handle-error";
import { logger } from "@/src/utils/logger";
import {
fetchTree,
getItemTargetPath,
getRegistryBaseColor,
getRegistryIndex,
resolveTree,
} from "@/src/utils/registry";
import { transform } from "@/src/utils/transformers";
import chalk from "chalk";
import { Command } from "commander";
import { execa } from "execa";
import ora from "ora";
import prompts from "prompts";
import { z } from "zod";
import { downloadHook } from "../utils/downloadHook";
import { downloadHook, getAllHooksName } from "../utils/downloadHook";

const addOptionsSchema = z.object({
hooks: z.array(z.string()).optional(),
yes: z.boolean(),
overwrite: z.boolean(),
cwd: z.string(),
all: z.boolean(),
Expand All @@ -33,7 +25,6 @@ export const add = new Command()
.name("add")
.description("add a hook to your project")
.argument("[hooks...]", "the hooks to add")
.option("-y, --yes", "skip confirmation prompt.", true)
.option("-o, --overwrite", "overwrite existing files.", false)
.option(
"-c, --cwd <cwd>",
Expand All @@ -43,8 +34,45 @@ export const add = new Command()
.option("-a, --all", "add all available hooks", false)
.option("-p, --path <path>", "the path to add the hook to.")
.action(async (hooks, opts) => {
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
const options = addOptionsSchema.parse({
hooks,
...opts,
});

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

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

const allHooks = await getAllHooksName();

let selectedHooks = options.all ? allHooks : options.hooks;

if (!options.hooks?.length && !options.all && allHooks) {
const { hooks } = await prompts({
type: "multiselect",
name: "hooks",
message: "Which hooks would you like to add?",
hint: "Space to select. A to toggle all. Enter to submit.",
instructions: false,
choices: allHooks.map((entry) => ({
title: entry,
value: entry,
selected: options.all ? true : options.hooks?.includes(entry),
})),
});
selectedHooks = hooks;
}

if (!selectedHooks?.length) {
logger.warn("No hooks selected. Exiting.");
process.exit(0);
}

for (let i = 0; i < selectedHooks.length; i++) {
const hook = selectedHooks[i];
await downloadHook(hook);
}
});
36 changes: 36 additions & 0 deletions src/utils/downloadHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import fs from "fs/promises";
import fetch from "node-fetch";

interface HookList {
name: string;
path: string;
sha: string;
size: number;
url: string;
html_url: string;
git_url: string;
download_url: string;
type: string;
_links: {
self: string;
git: string;
html: string;
};
}

export const getAllHooksName = async () => {
const allHooksUrl =
"https://api.github.com/repos/novajslabs/nova.js/contents/src/hooks";

try {
const response = await fetch(allHooksUrl);

if (!response.ok) {
throw Error;
}

const hooksData = (await response.json()) as HookList[];

return hooksData.map((hook) => hook.name.replace(".ts", ""));
} catch (e) {
//console.log(`❌ ${hook} error`);
}
};

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

Expand Down
155 changes: 0 additions & 155 deletions src/utils/registry/index.ts

This file was deleted.

44 changes: 0 additions & 44 deletions src/utils/registry/schema.ts

This file was deleted.

0 comments on commit 9c7d0d9

Please sign in to comment.