diff --git a/src/ts/server/settings.ts b/src/ts/server/settings.ts index e234561..507403a 100644 --- a/src/ts/server/settings.ts +++ b/src/ts/server/settings.ts @@ -1,4 +1,4 @@ -import { readFileAsync, writeFileAsync } from 'fs'; +import { readFileAsync, writeFileAsync, mkdirAsync } from 'fs'; import { Settings } from '../common/adminInterfaces'; import { cloneDeep } from '../common/utils'; import * as paths from './paths'; @@ -18,13 +18,17 @@ export async function loadSettings() { const json = await readFileAsync(settingsPath, 'utf8'); return JSON.parse(json) as Settings; } catch (e) { - if (e === 'ENOENT') { - writeFileAsync(settingsPath, '{}', 'utf8'); - return {} as Settings; + if (e.code === 'ENOENT') { + try { + await mkdirAsync(paths.pathTo('settings')); + } catch (e2) { + if (e2.code !== 'EEXIST') console.error('Failed to create settings directory: ' + e2); + } } else { - console.log("Error reading settings file: " + e); - return {} as Settings; + console.error('Error reading settings file: ' + e); } + + return cloneDeep(defaultSettings); } } diff --git a/src/ts/server/stats.ts b/src/ts/server/stats.ts index 9a93a95..0b282e6 100644 --- a/src/ts/server/stats.ts +++ b/src/ts/server/stats.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as path from 'path'; import * as moment from 'moment'; import { compact } from 'lodash'; import { Request } from 'express'; @@ -188,6 +189,11 @@ export class StatsTracker { } startStatTracking() { if (!fs.existsSync(this.statsPath)) { + try { + fs.mkdirSync(path.dirname(this.statsPath), { recursive: true }); + } catch (e) { + if (e.code !== 'EEXIST') console.error('Failed to create stats directory: ' + e); + } fs.writeFileSync(this.statsPath, encodeCSV(statsHeaders), { encoding: 'utf8' }); }