Skip to content

Commit fd9a68f

Browse files
authored
fix(fs): also substitute env variables in output path (#29)
1 parent f824ff9 commit fd9a68f

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

backend/bun/src/generators/image.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const ImageGenerator = async (
1313
json: JSONObjectImageSuccessRequest,
1414
): Promise<[NodeHTMLToImageBuffer, string]> => {
1515
// HTMLGenerator already handles font size conversion and generates the HTML
16-
const code = await HTMLGenerator(
16+
const [code] = await HTMLGenerator(
1717
json as unknown as JSONObjectHTMLSuccessRequest,
1818
);
1919

backend/bun/src/utils/file.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
import { mkdir } from "node:fs/promises";
22

3-
export const getOutputDir = async (): Promise<string> => {
3+
const replaceProcessEnv = (input: string): string => {
4+
return input.replace(/\$([A-Z_]+)/g, (_, varName) => {
5+
return process.env[varName] || "";
6+
});
7+
};
8+
9+
export const getOutputDir = async (
10+
userConfigOutDir?: string,
11+
): Promise<string> => {
12+
if (userConfigOutDir) {
13+
if (userConfigOutDir.startsWith("~")) {
14+
const homeDir = process.env.HOME || process.env.USERPROFILE || null;
15+
if (!homeDir) {
16+
throw new Error("Could not determine the user's home directory.");
17+
}
18+
userConfigOutDir = userConfigOutDir.replace("~", homeDir);
19+
}
20+
return replaceProcessEnv(userConfigOutDir);
21+
}
422
const homeDir = process.env.HOME || process.env.USERPROFILE || null;
523
if (!homeDir) {
624
throw new Error("Could not determine the user's home directory.");
@@ -20,7 +38,7 @@ export const getFullOutputPath = async (
2038
filename: string,
2139
filenamePattern: string,
2240
): Promise<string> => {
23-
if (!outputDir) outputDir = await getOutputDir();
41+
outputDir = await getOutputDir(outputDir);
2442
return `${outputDir.replace(/\/+$/, "")}/${generateFilename(
2543
filenamePattern,
2644
filename,

0 commit comments

Comments
 (0)