@tmustier/pi-nes 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,8 +58,8 @@ Config is stored at `~/.pi/nes/config.json`. Use `/nes config` for guided setup.
58
58
 
59
59
  ```json
60
60
  {
61
- "romDir": "~/roms/nes",
62
- "saveDir": "~/.pi/nes/saves",
61
+ "romDir": "/roms/nes",
62
+ "saveDir": "/roms/nes/saves",
63
63
  "renderer": "image",
64
64
  "pixelScale": 1.2,
65
65
  "keybindings": {
@@ -79,8 +79,8 @@ Config is stored at `~/.pi/nes/config.json`. Use `/nes config` for guided setup.
79
79
 
80
80
  | Option | Default | Description |
81
81
  |--------|---------|-------------|
82
- | `romDir` | `~/.pi/nes/roms` | Where to look for ROM files |
83
- | `saveDir` | `~/.pi/nes/saves` | Where to store battery saves |
82
+ | `romDir` | `/roms/nes` | Where to look for ROM files |
83
+ | `saveDir` | `/roms/nes/saves` | Where to store battery saves (defaults to `<romDir>/saves`) |
84
84
  | `renderer` | `"image"` | `"image"` (Kitty graphics) or `"text"` (ANSI) |
85
85
  | `pixelScale` | `1.2` | Display scale (0.5–4.0) |
86
86
 
@@ -15,9 +15,15 @@ export interface NesConfig {
15
15
  keybindings: InputMapping;
16
16
  }
17
17
 
18
+ const DEFAULT_ROM_DIR = path.join(path.sep, "roms", "nes");
19
+
20
+ function defaultSaveDir(romDir: string): string {
21
+ return path.join(romDir, "saves");
22
+ }
23
+
18
24
  export const DEFAULT_CONFIG: NesConfig = {
19
- romDir: path.join(path.sep, "roms", "nes"),
20
- saveDir: path.join(os.homedir(), ".pi", "nes", "saves"),
25
+ romDir: DEFAULT_ROM_DIR,
26
+ saveDir: defaultSaveDir(DEFAULT_ROM_DIR),
21
27
  enableAudio: false,
22
28
  renderer: "image",
23
29
  pixelScale: 1.2,
@@ -39,15 +45,18 @@ export function getConfigPath(): string {
39
45
 
40
46
  export function normalizeConfig(raw: unknown): NesConfig {
41
47
  const parsed = typeof raw === "object" && raw !== null ? (raw as RawConfig) : {};
48
+ const romDir =
49
+ typeof parsed.romDir === "string" && parsed.romDir.length > 0
50
+ ? normalizePath(parsed.romDir, DEFAULT_CONFIG.romDir)
51
+ : DEFAULT_CONFIG.romDir;
52
+ const saveDirFallback = defaultSaveDir(romDir);
53
+ const saveDir =
54
+ typeof parsed.saveDir === "string" && parsed.saveDir.length > 0
55
+ ? normalizePath(parsed.saveDir, saveDirFallback)
56
+ : saveDirFallback;
42
57
  return {
43
- romDir:
44
- typeof parsed.romDir === "string" && parsed.romDir.length > 0
45
- ? normalizePath(parsed.romDir, DEFAULT_CONFIG.romDir)
46
- : DEFAULT_CONFIG.romDir,
47
- saveDir:
48
- typeof parsed.saveDir === "string" && parsed.saveDir.length > 0
49
- ? normalizePath(parsed.saveDir, DEFAULT_CONFIG.saveDir)
50
- : DEFAULT_CONFIG.saveDir,
58
+ romDir,
59
+ saveDir,
51
60
  enableAudio: typeof parsed.enableAudio === "boolean" ? parsed.enableAudio : DEFAULT_CONFIG.enableAudio,
52
61
  renderer: parsed.renderer === "text" ? "text" : DEFAULT_CONFIG.renderer,
53
62
  pixelScale: normalizePixelScale(parsed.pixelScale),
@@ -61,12 +70,15 @@ export function formatConfig(config: NesConfig): string {
61
70
 
62
71
  export async function loadConfig(): Promise<NesConfig> {
63
72
  const configPath = getConfigPath();
73
+ let config: NesConfig;
64
74
  try {
65
75
  const raw = await fs.readFile(configPath, "utf8");
66
- return normalizeConfig(JSON.parse(raw));
76
+ config = normalizeConfig(JSON.parse(raw));
67
77
  } catch {
68
- return DEFAULT_CONFIG;
78
+ config = DEFAULT_CONFIG;
69
79
  }
80
+ await ensureDirectory(config.saveDir);
81
+ return config;
70
82
  }
71
83
 
72
84
  export async function configExists(): Promise<boolean> {
@@ -84,6 +96,14 @@ export async function saveConfig(config: NesConfig): Promise<void> {
84
96
  await fs.writeFile(configPath, formatConfig(config));
85
97
  }
86
98
 
99
+ async function ensureDirectory(dirPath: string): Promise<void> {
100
+ try {
101
+ await fs.mkdir(dirPath, { recursive: true });
102
+ } catch {
103
+ // ignore
104
+ }
105
+ }
106
+
87
107
  function normalizePixelScale(raw: unknown): number {
88
108
  if (typeof raw !== "number" || Number.isNaN(raw)) {
89
109
  return DEFAULT_CONFIG.pixelScale;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmustier/pi-nes",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "NES emulator extension for pi",
5
5
  "keywords": [
6
6
  "pi-package",
package/spec.md CHANGED
@@ -60,12 +60,10 @@ pi-nes/
60
60
  - Use `isKeyRelease()` for clean key‑up events.
61
61
 
62
62
  ## Saves
63
- - Store SRAM at `~/.pi/nes/saves/<rom-name>.sav`.
63
+ - Store SRAM at `<saveDir>/<rom-name>.sav` (default `/roms/nes/saves`).
64
64
  - Load SRAM on ROM start.
65
65
  - Persist on exit and periodically (e.g., every 5–10 seconds).
66
66
 
67
- Note: the native core does not currently expose SRAM for persistence.
68
-
69
67
  ## Configuration
70
68
  - `~/.pi/nes/config.json` with:
71
69
  - `romDir`
@@ -91,4 +89,4 @@ Note: audio output is currently disabled; setting `enableAudio` will show a warn
91
89
  - Default ROM dir: `/roms/nes` (configurable).
92
90
  - Default core: `native`.
93
91
  - Default pixel scale: `1.2`.
94
- - Default save dir: `~/.pi/nes/saves` (configurable).
92
+ - Default save dir: `/roms/nes/saves` (configurable).