Download src/utils/envParser.js from ASEM12345/anti_api: direct link, hf CLI and curl.
- Browser
- Download file 922 Bytes
-
https://huggingface.co/spaces/ASEM12345/anti_api/resolve/main/src/utils/envParser.js
- Command line
-
hf download hf://spaces/ASEM12345/anti_api/src/utils/envParser.js
-
curl -L -o envParser.js https://huggingface.co/spaces/ASEM12345/anti_api/resolve/main/src/utils/envParser.js
922 Bytes
| import fs from 'fs'; | |
| /** | |
| * 解析 .env 文件内容为对象 | |
| */ | |
| export function parseEnvFile(filePath) { | |
| const envData = {}; | |
| const content = fs.readFileSync(filePath, 'utf8'); | |
| content.split('\n').forEach(line => { | |
| line = line.trim(); | |
| if (line && !line.startsWith('#')) { | |
| const [key, ...valueParts] = line.split('='); | |
| if (key) { | |
| envData[key.trim()] = valueParts.join('=').trim(); | |
| } | |
| } | |
| }); | |
| return envData; | |
| } | |
| /** | |
| * 更新 .env 文件中的键值对 | |
| */ | |
| export function updateEnvFile(filePath, updates) { | |
| let content = fs.readFileSync(filePath, 'utf8'); | |
| Object.entries(updates).forEach(([key, value]) => { | |
| const regex = new RegExp(`^${key}=.*$`, 'm'); | |
| if (regex.test(content)) { | |
| content = content.replace(regex, `${key}=${value}`); | |
| } else { | |
| content += `\n${key}=${value}`; | |
| } | |
| }); | |
| fs.writeFileSync(filePath, content, 'utf8'); | |
| } | |