This commit is contained in:
bipproduction
2025-02-16 16:01:12 +08:00
parent 4636502886
commit fc8a675e05
8 changed files with 304 additions and 14 deletions

View File

@@ -1,18 +1,21 @@
"use client";
import { Center, Container, Stack, Text } from "@mantine/core";
import { useRef } from "react";
import FlipOnScroll from "./FlipScroll";
const name = process.env.NEXT_PUBLIC_NAME;
function Content5() {
const ref = useRef(null);
return (
<Stack ref={ref}>
ini name
{name && <Text>{name}</Text>}
<Container w={{ base: "100%", md: "80%" }} p={"xl"} h={720}>
<Center>
<Text fz={"3.4rem"}>CONTENT 5</Text>
</Center>
<FlipOnScroll />
</Container>
</Stack>
);

53
src/lib/EnvStringParse.ts Normal file
View File

@@ -0,0 +1,53 @@
type EnvVariable = { key: string; value: string };
class EnvStringParser {
/**
* Parses an environment string into a key-value object.
* @param envString - The environment string to parse.
* @param env - Optional custom environment variables (defaults to `process.env` in Node.js).
* @returns A Record<string, string> containing parsed environment variables.
*/
static parse(envString: string, env: Record<string, string | undefined> = process.env): Record<string, string> {
const envVars: EnvVariable[] = [];
// Split the string into lines
const lines = envString.split(/\r?\n/); // Handle both \n and \r\n line endings
for (const line of lines) {
const trimmedLine = line.trim();
// Skip comments and empty lines
if (!trimmedLine || trimmedLine.startsWith("#")) continue;
// Match key-value pairs with support for quoted values
const match = trimmedLine.match(/^([\w.-]+)=(?:"([^"]*)"|'([^']*)'|([^#\s]*))/);
if (!match) {
console.warn(`Skipping invalid line: ${trimmedLine}`);
continue;
}
const key = match[1];
let value = match[2] || match[3] || match[4] || ""; // Handle double quotes, single quotes, or unquoted values
// Resolve environment variable placeholders like ${VAR_NAME}
value = value.replace(/\$\{(\w+)\}/g, (_, varName) => {
if (env[varName]) {
return env[varName]!;
} else {
console.warn(`Environment variable ${varName} is not defined`);
return "";
}
});
envVars.push({ key, value });
}
// Convert array of EnvVariable objects into a Record<string, string>
const envObj: Record<string, string> = {};
for (const { key, value } of envVars) {
envObj[key] = value;
}
return envObj;
}
}
export default EnvStringParser;