inventory types

This commit is contained in:
Boki 2026-02-12 12:00:29 -05:00
parent cf5d944fd1
commit 3d7a8aafdf
9 changed files with 532 additions and 369 deletions

View file

@ -1,13 +1,14 @@
import { readFileSync, writeFileSync, existsSync } from 'fs';
import path from 'path';
import { logger } from '../util/logger.js';
import type { LinkMode } from '../types.js';
import type { LinkMode, PostAction } from '../types.js';
export interface SavedLink {
url: string;
name: string;
active: boolean;
mode: LinkMode;
postAction?: PostAction;
addedAt: string;
}
@ -56,14 +57,18 @@ export class ConfigStore {
const raw = readFileSync(this.filePath, 'utf-8');
const parsed = JSON.parse(raw) as Partial<SavedSettings>;
const merged = { ...DEFAULTS, ...parsed };
// Migrate old links: add name/active fields, strip /live from URLs
merged.links = merged.links.map((l: any) => ({
url: l.url.replace(/\/live\/?$/, ''),
name: l.name || '',
active: l.active !== undefined ? l.active : true,
mode: l.mode || 'live',
addedAt: l.addedAt || new Date().toISOString(),
}));
// Migrate old links: add name/active fields, strip /live from URLs, default postAction
merged.links = merged.links.map((l: any) => {
const mode: LinkMode = l.mode || 'live';
return {
url: l.url.replace(/\/live\/?$/, ''),
name: l.name || '',
active: l.active !== undefined ? l.active : true,
mode,
postAction: l.postAction || (mode === 'scrap' ? 'salvage' : 'stash'),
addedAt: l.addedAt || new Date().toISOString(),
};
});
logger.info({ path: this.filePath, linkCount: merged.links.length }, 'Loaded config.json');
return merged;
} catch (err) {
@ -88,10 +93,17 @@ export class ConfigStore {
return this.data.links;
}
addLink(url: string, name: string = '', mode: LinkMode = 'live'): void {
addLink(url: string, name: string = '', mode: LinkMode = 'live', postAction?: PostAction): void {
url = url.replace(/\/live\/?$/, '');
if (this.data.links.some((l) => l.url === url)) return;
this.data.links.push({ url, name, active: true, mode, addedAt: new Date().toISOString() });
this.data.links.push({
url,
name,
active: true,
mode,
postAction: postAction || (mode === 'scrap' ? 'salvage' : 'stash'),
addedAt: new Date().toISOString(),
});
this.save();
}
@ -108,7 +120,7 @@ export class ConfigStore {
this.save();
}
updateLinkById(id: string, updates: { name?: string; active?: boolean; mode?: LinkMode }): SavedLink | null {
updateLinkById(id: string, updates: { name?: string; active?: boolean; mode?: LinkMode; postAction?: PostAction }): SavedLink | null {
const link = this.data.links.find((l) => {
const parts = l.url.split('/');
return parts[parts.length - 1] === id;
@ -117,6 +129,7 @@ export class ConfigStore {
if (updates.name !== undefined) link.name = updates.name;
if (updates.active !== undefined) link.active = updates.active;
if (updates.mode !== undefined) link.mode = updates.mode;
if (updates.postAction !== undefined) link.postAction = updates.postAction;
this.save();
return link;
}