chore: initial commit for phase06

This commit is contained in:
hibna
2026-02-21 23:46:01 +03:00
parent 0941a9ba46
commit 5709d8bc10
16 changed files with 1667 additions and 15 deletions
+234
View File
@@ -0,0 +1,234 @@
import type { ConfigParser, ConfigEntry } from '@source/shared';
/**
* Parse a config file content into key-value entries based on the parser type.
*/
export function parseConfig(content: string, parser: ConfigParser): ConfigEntry[] {
switch (parser) {
case 'properties':
return parseProperties(content);
case 'json':
return parseJson(content);
case 'yaml':
return parseYaml(content);
case 'keyvalue':
return parseKeyValue(content);
default:
return [];
}
}
/**
* Serialize key-value entries back into a config file content.
*/
export function serializeConfig(
entries: ConfigEntry[],
parser: ConfigParser,
originalContent?: string,
): string {
switch (parser) {
case 'properties':
return serializeProperties(entries, originalContent);
case 'json':
return serializeJson(entries);
case 'yaml':
return serializeYaml(entries, originalContent);
case 'keyvalue':
return serializeKeyValue(entries, originalContent);
default:
return '';
}
}
// === Properties (Java .properties format) ===
function parseProperties(content: string): ConfigEntry[] {
const entries: ConfigEntry[] = [];
for (const line of content.split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('!')) continue;
const eqIndex = trimmed.indexOf('=');
if (eqIndex === -1) continue;
entries.push({
key: trimmed.substring(0, eqIndex).trim(),
value: trimmed.substring(eqIndex + 1).trim(),
});
}
return entries;
}
function serializeProperties(entries: ConfigEntry[], originalContent?: string): string {
if (!originalContent) {
return entries.map((e) => `${e.key}=${e.value}`).join('\n') + '\n';
}
const entryMap = new Map(entries.map((e) => [e.key, e.value]));
const lines = originalContent.split('\n');
const result: string[] = [];
const written = new Set<string>();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('!')) {
result.push(line);
continue;
}
const eqIndex = trimmed.indexOf('=');
if (eqIndex === -1) {
result.push(line);
continue;
}
const key = trimmed.substring(0, eqIndex).trim();
if (entryMap.has(key)) {
result.push(`${key}=${entryMap.get(key)}`);
written.add(key);
} else {
result.push(line);
}
}
// Append new keys
for (const entry of entries) {
if (!written.has(entry.key)) {
result.push(`${entry.key}=${entry.value}`);
}
}
return result.join('\n');
}
// === JSON ===
function parseJson(content: string): ConfigEntry[] {
try {
const obj = JSON.parse(content);
if (typeof obj !== 'object' || Array.isArray(obj)) return [];
return Object.entries(obj).map(([key, value]) => ({
key,
value: typeof value === 'string' ? value : JSON.stringify(value),
}));
} catch {
return [];
}
}
function serializeJson(entries: ConfigEntry[]): string {
const obj: Record<string, unknown> = {};
for (const entry of entries) {
try {
obj[entry.key] = JSON.parse(entry.value);
} catch {
obj[entry.key] = entry.value;
}
}
return JSON.stringify(obj, null, 2) + '\n';
}
// === YAML (simplified — only top-level key: value) ===
function parseYaml(content: string): ConfigEntry[] {
const entries: ConfigEntry[] = [];
for (const line of content.split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
// Only handle top-level keys (no indentation)
if (line.startsWith(' ') || line.startsWith('\t')) continue;
const colonIndex = trimmed.indexOf(':');
if (colonIndex === -1) continue;
const key = trimmed.substring(0, colonIndex).trim();
const value = trimmed.substring(colonIndex + 1).trim();
if (key) entries.push({ key, value });
}
return entries;
}
function serializeYaml(entries: ConfigEntry[], originalContent?: string): string {
if (!originalContent) {
return entries.map((e) => `${e.key}: ${e.value}`).join('\n') + '\n';
}
const entryMap = new Map(entries.map((e) => [e.key, e.value]));
const lines = originalContent.split('\n');
const result: string[] = [];
const written = new Set<string>();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#') || line.startsWith(' ') || line.startsWith('\t')) {
result.push(line);
continue;
}
const colonIndex = trimmed.indexOf(':');
if (colonIndex === -1) {
result.push(line);
continue;
}
const key = trimmed.substring(0, colonIndex).trim();
if (entryMap.has(key)) {
result.push(`${key}: ${entryMap.get(key)}`);
written.add(key);
} else {
result.push(line);
}
}
for (const entry of entries) {
if (!written.has(entry.key)) {
result.push(`${entry.key}: ${entry.value}`);
}
}
return result.join('\n');
}
// === KeyValue (Source engine cfg: `key "value"` or `key value`) ===
function parseKeyValue(content: string): ConfigEntry[] {
const entries: ConfigEntry[] = [];
for (const line of content.split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('//')) continue;
// Match: key "value" or key value
const match = trimmed.match(/^(\S+)\s+"([^"]*)"/) || trimmed.match(/^(\S+)\s+(.*)/);
if (match && match[1] && match[2] !== undefined) {
entries.push({ key: match[1], value: match[2] });
}
}
return entries;
}
function serializeKeyValue(entries: ConfigEntry[], originalContent?: string): string {
if (!originalContent) {
return entries.map((e) => `${e.key} "${e.value}"`).join('\n') + '\n';
}
const entryMap = new Map(entries.map((e) => [e.key, e.value]));
const lines = originalContent.split('\n');
const result: string[] = [];
const written = new Set<string>();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('//')) {
result.push(line);
continue;
}
const match = trimmed.match(/^(\S+)\s+/);
const matchKey = match?.[1];
if (matchKey && entryMap.has(matchKey)) {
result.push(`${matchKey} "${entryMap.get(matchKey)}"`);
written.add(matchKey);
} else {
result.push(line);
}
}
for (const entry of entries) {
if (!written.has(entry.key)) {
result.push(`${entry.key} "${entry.value}"`);
}
}
return result.join('\n');
}
+56
View File
@@ -0,0 +1,56 @@
const SPIGET_BASE = 'https://api.spiget.org/v2';
export interface SpigetResource {
id: number;
name: string;
tag: string;
icon: { url: string; data: string };
releaseDate: number;
updateDate: number;
downloads: number;
rating: { average: number; count: number };
file: { type: string; size: number; url: string };
version: { id: number };
external: boolean;
}
export interface SpigetVersion {
id: number;
name: string;
releaseDate: number;
downloads: number;
url: string;
}
export async function searchSpigetPlugins(
query: string,
page = 1,
size = 20,
): Promise<SpigetResource[]> {
const res = await fetch(
`${SPIGET_BASE}/search/resources/${encodeURIComponent(query)}?size=${size}&page=${page}&sort=-downloads`,
{ headers: { 'User-Agent': 'GamePanel/1.0' } },
);
if (!res.ok) return [];
return res.json() as Promise<SpigetResource[]>;
}
export async function getSpigetResource(id: number): Promise<SpigetResource | null> {
const res = await fetch(`${SPIGET_BASE}/resources/${id}`, {
headers: { 'User-Agent': 'GamePanel/1.0' },
});
if (!res.ok) return null;
return res.json() as Promise<SpigetResource>;
}
export async function getSpigetVersions(resourceId: number): Promise<SpigetVersion[]> {
const res = await fetch(`${SPIGET_BASE}/resources/${resourceId}/versions?sort=-releaseDate`, {
headers: { 'User-Agent': 'GamePanel/1.0' },
});
if (!res.ok) return [];
return res.json() as Promise<SpigetVersion[]>;
}
export function getSpigetDownloadUrl(resourceId: number): string {
return `${SPIGET_BASE}/resources/${resourceId}/download`;
}