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

Function saveFile

src/shared/preferences/PreferencesFile.ts:89–123  ·  view source on GitHub ↗
(filePath: string, data: AppPreferencesData, state: BackState)

Source from the content-addressed store, hash-verified

87 }
88
89 export async function saveFile(filePath: string, data: AppPreferencesData, state: BackState): Promise<void> {
90 const json = stringifyJsonDataFile(data);
91 if (json.length === 0) {
92 log.error('PreferencesFile', 'Serialized preferences string is empty, skipping write.');
93 return;
94 }
95 const temp = await getTempFilename();
96 const encoded = new TextEncoder().encode(json);
97 await fs.promises.writeFile(temp, encoded);
98 // Check: was it written correctly?
99 let stat = await fs.promises.stat(temp);
100 let count = 0;
101 while (stat.size !== encoded.length) {
102 if (count > 3) {
103 log.error('PreferencesFile', 'Repeated failure to write preferences.');
104 return fs.promises.unlink(temp);
105 }
106 await fs.promises.writeFile(temp, json);
107 stat = await fs.promises.stat(temp);
108 count++;
109 }
110 try {
111 await fs.promises.rename(temp, filePath);
112 } catch {
113 try {
114 await fs.promises.copyFile(temp, filePath);
115 await fs.promises.unlink(temp);
116 } catch (err) {
117 log.error('Launcher', 'PARTICULARLY BAD ERROR: Failed to save temp file for Prefs correctly, requeued');
118 state.prefsQueue.push(() => {
119 PreferencesFile.saveFile(path.join(state.config.flashpointPath, PREFERENCES_FILENAME), state.preferences, state);
120 });
121 }
122 }
123 }
124}

Callers 1

readOrCreateFileFunction · 0.70

Calls 3

stringifyJsonDataFileFunction · 0.90
getTempFilenameFunction · 0.90
pushMethod · 0.65

Tested by

no test coverage detected