Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add error handling #23

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-ravens-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hanabi.rest/cli": patch
---

add error handling
2 changes: 1 addition & 1 deletion src/helpers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getFiles(version_id: string) {
handleResponseStatus(res.status);

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const filesData: any = await res.json();
const filesData = await res.json() as Record<string, string|undefined>;

return {
readme: filesData["guide.md"],
Expand Down
36 changes: 28 additions & 8 deletions templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import fs from "fs-extra";
import { type PackageJson } from "type-fest";
import { extractImportsFromSource } from "@/src/helpers/ast";
import prompts from "prompts";
import { fileURLToPath } from 'url';
import { fileURLToPath } from "url";

export const installTemplate = async ({
root,
Expand All @@ -24,33 +24,54 @@ export const installTemplate = async ({
appId: string;
}) => {
console.info(`Using ${packageManager}`);
console.info()
console.info();

const { readme, route, migrations, seed, source } = await getFiles(appId);

if (!readme || !route || !migrations || !source) {
throw new Error(
"Failed to fetch files. Your API may be in the process of being generated or may have failed to generate."
);
}

fs.mkdirSync(root, { recursive: true });

const fileCopyError = (err: NodeJS.ErrnoException | null | undefined) => {
if (err) {
console.error("Failed to copy files. Please check permissions.", err);
process.exit(1);
}
}
};

fs.copy(
path.join(path.dirname(fileURLToPath(import.meta.url)), "templates", "workers", "wrangler.toml"),
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"templates",
"workers",
"wrangler.toml"
),
path.join(root, "wrangler.toml"),
fileCopyError
);

fs.copy(
path.join(path.dirname(fileURLToPath(import.meta.url)), "templates", "workers", "tsconfig.json"),
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"templates",
"workers",
"tsconfig.json"
),
path.join(root, "tsconfig.json"),
fileCopyError
);

fs.copy(
path.join(path.dirname(fileURLToPath(import.meta.url)), "templates", "workers", "gitignore"),
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"templates",
"workers",
"gitignore"
),
path.join(root, ".gitignore"),
fileCopyError
);
Expand All @@ -63,7 +84,7 @@ export const installTemplate = async ({
// Create the files
fs.writeFileSync(path.join(root, "README.md"), readme);
fs.writeFileSync(path.join(root, "migrations", "schema.sql"), migrations);
fs.writeFileSync(path.join(root, "seed.sql"), seed);
seed && fs.writeFileSync(path.join(root, "seed.sql"), seed);
fs.writeFileSync(path.join(root, "route.md"), route);
fs.writeFileSync(path.join(root, "src", "index.ts"), source);

Expand Down Expand Up @@ -119,7 +140,6 @@ export const installTemplate = async ({
console.info("Skip...");
return;
}

}

await install(packageManager, skipCodePackage ? [] : dependencies);
Expand Down
Loading