MCPcopy Create free account
hub / github.com/FlashpointProject/launcher / readOrCreateFile

Function readOrCreateFile

src/shared/preferences/PreferencesFile.ts:40–71  ·  view source on GitHub ↗
(filePath: string, state: BackState, flashpointPath: string, onError?: (error: string) => void)

Source from the content-addressed store, hash-verified

38 * @param onError Called for each error that occurs while parsing.
39 */
40 export async function readOrCreateFile(filePath: string, state: BackState, flashpointPath: string, onError?: (error: string) => void): Promise<AppPreferencesData> {
41 // Try to get the data from the file
42 const data = await readFile(filePath, flashpointPath, onError)
43 .catch((e) => {
44 if (e.code !== 'ENOENT') {
45 if (onError) { onError(e); }
46 throw e;
47 }
48 return undefined;
49 });
50 // If doesn't exist, set data to default and save it to a new file
51 if (!data) {
52 const overridePath = path.join(flashpointPath, '.preferences.defaults.json');
53 console.log('Checking for prefs override at ' + overridePath);
54 try {
55 await fs.promises.copyFile(overridePath, filePath);
56 console.log('Copied default preferences (override)');
57 // File copied, try loading again
58 return readOrCreateFile(filePath, state, flashpointPath, onError);
59 } catch (err) {
60 console.log(err);
61 // Failed to copy overrides, use defaults
62 const defaultPrefs = deepCopy(defaultPreferencesData);
63 await saveFile(filePath, defaultPrefs, state)
64 .catch(() => console.error('Failed to save default preferences file!'));
65 console.log('Copied default preferences');
66 return defaultPrefs;
67 }
68 }
69 // Return
70 return data;
71 }
72
73 export async function readFile(filePath: string, fpPath: string, onError?: (error: string) => void): Promise<AppPreferencesData> {
74 let defaultPrefs = deepCopy(defaultPreferencesData);

Callers

nothing calls this directly

Calls 4

deepCopyFunction · 0.90
readFileFunction · 0.70
saveFileFunction · 0.70
onErrorFunction · 0.50

Tested by

no test coverage detected