[Snow Team] config-fixer: auto-commit work
This commit is contained in:
@@ -1,98 +1,113 @@
|
||||
import fs from 'node:fs'
|
||||
import process from 'node:process'
|
||||
import fs from "node:fs";
|
||||
import process from "node:process";
|
||||
|
||||
export function loadEnvFiles(filePaths: string[]): void {
|
||||
for (const filePath of filePaths) {
|
||||
loadEnvFile(filePath)
|
||||
loadEnvFile(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
function loadEnvFile(filePath: string): void {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
const rawText = fs.readFileSync(filePath, 'utf8')
|
||||
const lines = rawText.split(/\r?\n/)
|
||||
const rawText = fs.readFileSync(filePath, "utf8");
|
||||
const lines = rawText.split(/\r?\n/);
|
||||
|
||||
for (const rawLine of lines) {
|
||||
const line = rawLine.trim()
|
||||
const line = rawLine.trim();
|
||||
|
||||
if (!line || line.startsWith('#')) {
|
||||
continue
|
||||
if (!line || line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const separatorIndex = line.indexOf('=')
|
||||
const separatorIndex = line.indexOf("=");
|
||||
if (separatorIndex <= 0) {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = line.slice(0, separatorIndex).trim()
|
||||
const key = line.slice(0, separatorIndex).trim();
|
||||
if (!key || key in process.env) {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
process.env[key] = parseEnvValue(line.slice(separatorIndex + 1))
|
||||
process.env[key] = parseEnvValue(line.slice(separatorIndex + 1));
|
||||
}
|
||||
}
|
||||
|
||||
function parseEnvValue(rawValue: string): string {
|
||||
const value = String(rawValue || '').trim()
|
||||
const value = String(rawValue || "").trim();
|
||||
|
||||
if (!value) {
|
||||
return ''
|
||||
return "";
|
||||
}
|
||||
|
||||
const quote = value[0]
|
||||
const quote = value[0];
|
||||
if ((quote === '"' || quote === "'") && value.endsWith(quote)) {
|
||||
return value.slice(1, -1)
|
||||
return value.slice(1, -1);
|
||||
}
|
||||
|
||||
return value
|
||||
return value;
|
||||
}
|
||||
|
||||
export function parseBoolean(rawValue: unknown): boolean | null {
|
||||
const normalized = String(rawValue || '')
|
||||
const normalized = String(rawValue || "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.toLowerCase();
|
||||
|
||||
if (!normalized) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
if (['1', 'true', 'yes', 'on'].includes(normalized)) {
|
||||
return true
|
||||
if (["1", "true", "yes", "on"].includes(normalized)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (['0', 'false', 'no', 'off'].includes(normalized)) {
|
||||
return false
|
||||
if (["0", "false", "no", "off"].includes(normalized)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
export function parseInteger(rawValue: unknown): number | null {
|
||||
const normalized = String(rawValue || '').trim()
|
||||
const normalized = String(rawValue || "").trim();
|
||||
|
||||
if (!normalized) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = Number(normalized)
|
||||
return Number.isFinite(parsed) ? parsed : null
|
||||
const parsed = Number(normalized);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
export function parseJsonArray<T = unknown>(rawValue: unknown): T[] | null {
|
||||
const normalized = String(rawValue || '').trim()
|
||||
const normalized = String(rawValue || "").trim();
|
||||
|
||||
if (!normalized) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(normalized)
|
||||
return Array.isArray(parsed) ? parsed as T[] : null
|
||||
const parsed = JSON.parse(normalized);
|
||||
return Array.isArray(parsed) ? (parsed as T[]) : null;
|
||||
} catch {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseCommaSeparatedStrings(rawValue: unknown): string[] | null {
|
||||
const normalized = String(rawValue || "").trim();
|
||||
|
||||
if (!normalized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const items = normalized
|
||||
.split(",")
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item.length > 0);
|
||||
|
||||
return items.length > 0 ? items : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user