fix(FN-1952): use fusion storage for pi config
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { readFile, writeFile, access, mkdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
const SCRIPTS_FILE = join(homedir(), ".pi", "fusion", "scripts.json");
|
||||
function projectScriptsFile(projectDir: string): string {
|
||||
return join(resolve(projectDir), ".fusion", "scripts.json");
|
||||
}
|
||||
|
||||
function legacyProjectScriptsFile(projectDir: string): string {
|
||||
return join(resolve(projectDir), ".pi", "fusion", "scripts.json");
|
||||
}
|
||||
|
||||
interface ScriptsData {
|
||||
scripts: Record<string, string>;
|
||||
@@ -11,17 +16,28 @@ interface ScriptsData {
|
||||
class ScriptStore {
|
||||
private scripts: Record<string, string> = {};
|
||||
private filePath: string;
|
||||
private legacyFilePath?: string;
|
||||
|
||||
constructor(filePath: string) {
|
||||
constructor(filePath: string, legacyFilePath?: string) {
|
||||
this.filePath = filePath;
|
||||
this.legacyFilePath = legacyFilePath;
|
||||
}
|
||||
|
||||
async load(): Promise<void> {
|
||||
const paths = this.legacyFilePath ? [this.filePath, this.legacyFilePath] : [this.filePath];
|
||||
try {
|
||||
await access(this.filePath);
|
||||
const content = await readFile(this.filePath, "utf-8");
|
||||
const data = JSON.parse(content) as ScriptsData;
|
||||
this.scripts = data.scripts || {};
|
||||
for (const path of paths) {
|
||||
try {
|
||||
await access(path);
|
||||
const content = await readFile(path, "utf-8");
|
||||
const data = JSON.parse(content) as ScriptsData;
|
||||
this.scripts = data.scripts || {};
|
||||
return;
|
||||
} catch {
|
||||
// Try the next candidate.
|
||||
}
|
||||
}
|
||||
this.scripts = {};
|
||||
} catch {
|
||||
// File doesn't exist or is invalid - start with empty scripts
|
||||
this.scripts = {};
|
||||
@@ -57,18 +73,22 @@ class ScriptStore {
|
||||
}
|
||||
}
|
||||
|
||||
let storeInstance: ScriptStore | null = null;
|
||||
const storeInstances = new Map<string, ScriptStore>();
|
||||
|
||||
export async function loadScriptStore(): Promise<ScriptStore> {
|
||||
if (!storeInstance) {
|
||||
storeInstance = new ScriptStore(SCRIPTS_FILE);
|
||||
await storeInstance.load();
|
||||
export async function loadScriptStore(projectDir: string): Promise<ScriptStore> {
|
||||
const scriptsFile = projectScriptsFile(projectDir);
|
||||
let store = storeInstances.get(scriptsFile);
|
||||
if (!store) {
|
||||
store = new ScriptStore(scriptsFile, legacyProjectScriptsFile(projectDir));
|
||||
storeInstances.set(scriptsFile, store);
|
||||
await store.load();
|
||||
}
|
||||
return storeInstance;
|
||||
return store;
|
||||
}
|
||||
|
||||
export function resetScriptStore(): void {
|
||||
storeInstance = null;
|
||||
storeInstances.clear();
|
||||
}
|
||||
|
||||
export { projectScriptsFile, legacyProjectScriptsFile };
|
||||
export type { ScriptStore };
|
||||
|
||||
Reference in New Issue
Block a user