This commit is contained in:
Rizky
2024-04-25 15:32:14 +07:00
parent 9771281430
commit 98333a82a8
4 changed files with 104 additions and 25 deletions
+1
View File
@@ -32,6 +32,7 @@ type PrasiServer = {
mode: "dev" | "prod";
handle: (req: Request) => Promise<Response>;
index: { head: string[]; body: string[]; render: () => string };
prasi: { page_id?: string };
}) => Promise<Response>;
init?: (arg: { port: number }) => Promise<void>;
};
+66
View File
@@ -0,0 +1,66 @@
/** We don't normalize anything, so it is just strings and strings. */
export type Data = Record<string, string>;
/** We typecast the value as a string so that it is compatible with envfiles. */
export type Input = Record<string, any>;
// perhaps in the future we can use @bevry/json's toJSON and parseJSON and JSON.stringify to support more advanced types
function removeQuotes(str: string) {
// Check if the string starts and ends with single or double quotes
if (
(str.startsWith('"') && str.endsWith('"')) ||
(str.startsWith("'") && str.endsWith("'"))
) {
// Remove the quotes
return str.slice(1, -1);
}
// If the string is not wrapped in quotes, return it as is
return str;
}
/** Parse an envfile string. */
export function parse(src: string): Data {
const result: Data = {};
const lines = splitInLines(src);
for (const line of lines) {
const match = line.match(/^([^=:#]+?)[=:]((.|\n)*)/);
if (match) {
const key = match[1].trim();
const value = removeQuotes(match[2].trim());
result[key] = value;
}
}
return result;
}
/** Turn an object into an envfile string. */
export function stringify(obj: Input): string {
let result = "";
for (const [key, value] of Object.entries(obj)) {
if (key) {
const line = `${key}=${jsonValueToEnv(value)}`;
result += line + "\n";
}
}
return result;
}
function splitInLines(src: string): string[] {
return src
.replace(/("[\s\S]*?")/g, (_m, cg) => {
return cg.replace(/\n/g, "%%LINE-BREAK%%");
})
.split("\n")
.filter((i) => Boolean(i.trim()))
.map((i) => i.replace(/%%LINE-BREAK%%/g, "\n"));
}
function jsonValueToEnv(value: any): string {
let processedValue = String(value);
processedValue = processedValue.replace(/\n/g, "\\n");
processedValue = processedValue.includes("\\n")
? `"${processedValue}"`
: processedValue;
return processedValue;
}