Create settings and stats directories ad-hoc

This commit is contained in:
Eliot Partridge
2019-08-29 22:57:21 -05:00
parent 6cdd4f0826
commit aaee2dc8b7
2 changed files with 16 additions and 6 deletions
+10 -6
View File
@@ -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);
}
}
+6
View File
@@ -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' });
}