-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#78 feat(access): write basic options parser
- Loading branch information
1 parent
d550698
commit 09d84cd
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { readFile, writeFile } from "fs-extra"; | ||
import { MinecraftContainer } from "../container/MinecraftContainer"; | ||
|
||
const OPTIONS_FILE = "options.txt"; | ||
|
||
export async function parseOptions( | ||
c: MinecraftContainer | ||
): Promise<Map<string, string>> { | ||
const f = c.resolvePath(OPTIONS_FILE); | ||
const d = (await readFile(f)).toString().split("\n"); | ||
const out = new Map<string, string>(); | ||
d.forEach((v) => { | ||
v = v.trim(); | ||
if (v.length === 0 || !v.includes(":")) { | ||
return; | ||
} | ||
const [k, ...p] = v.split(":"); | ||
const cv = p.join(":"); | ||
out.set(k, cv); | ||
}); | ||
return out; | ||
} | ||
|
||
// Warning: this will override! | ||
export async function buildOptions( | ||
origin: Map<string, string>, | ||
container: MinecraftContainer | ||
): Promise<void> { | ||
const f = container.resolvePath(OPTIONS_FILE); | ||
const o = []; | ||
for (const [k, v] of origin.entries()) { | ||
if (k.length > 0) { | ||
const c = k + ":" + v; // Value may be empty | ||
o.push(c); | ||
} | ||
} | ||
await writeFile(f, o.join("\n")); | ||
} |