Skip to content
Open
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
39 changes: 29 additions & 10 deletions src/lib/cf/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,49 @@ interface WranglerConfig {
kv_namespaces: Binding[];
}

export const checkForDir = async (path: string, dir: string): Promise<boolean> => {
export const checkForDir = async (
path: string,
dir: string,
): Promise<boolean> => {
// who tf designed an api that returns undefined on success
return await access(path + "/" + dir).then(() => true).catch(() => false);
}
return await access(path + "/" + dir)
.then(() => true)
.catch(() => false);
};

export const readDir = async (path: string, dir: string): Promise<string[]> => {
return await readdir(path + "/" + dir);
}
};

export const getBindingsByType = async (path: string, bindingType: keyof WranglerConfig): Promise<Binding[]> => {
const jsoncToJson = (jsonc: string): string => {
// Remove comments (both // and /* */)
let json = jsonc.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
(m, g) => (g ? "" : m),
);
// Remove trailing commas before } or ]
json = json.replace(/,(\s*[}\]])/g, "$1");
return json;
};

export const getBindingsByType = async (
path: string,
bindingType: keyof WranglerConfig,
): Promise<Binding[]> => {
const wranglerConfig = Bun.file(`${path}/wrangler.jsonc`);
const text = await wranglerConfig.text();
const jsonString = text.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m);
const jsonString = jsoncToJson(text);
const json: WranglerConfig = JSON.parse(jsonString);

return json[bindingType as keyof WranglerConfig];
}
};

export const getWranglerConfig = async (path: string): Promise<any | null> => {
const wranglerConfig = Bun.file(`${path}/wrangler.jsonc`);
if (!await wranglerConfig.exists()) {
if (!(await wranglerConfig.exists())) {
return null;
}
const text = await wranglerConfig.text();
const jsonString = text.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m);
const jsonString = jsoncToJson(text);
return JSON.parse(jsonString);
}
};