-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
39 lines (37 loc) · 1003 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import boxen from "boxen";
import { z } from "zod";
export function announce(message: string, title?: string) {
console.log(
boxen(message, {
padding: 1,
margin: 3,
title: title || "Stagehand",
})
);
}
/**
* Get an environment variable and throw an error if it's not found
* @param name - The name of the environment variable
* @returns The value of the environment variable
*/
export function getEnvVar(name: string, required = true): string | undefined {
const value = process.env[name];
if (!value && required) {
throw new Error(`${name} not found in environment variables`);
}
return value;
}
/**
* Validate a Zod schema against some data
* @param schema - The Zod schema to validate against
* @param data - The data to validate
* @returns Whether the data is valid against the schema
*/
export function validateZodSchema(schema: z.ZodTypeAny, data: unknown) {
try {
schema.parse(data);
return true;
} catch {
return false;
}
}