Merge pull request #37 from BytewaveMLP/fix-enoent-directory

Create settings and stats directories ad-hoc
This commit is contained in:
Erik McClure
2019-08-29 21:30:46 -07:00
committed by GitHub
2 changed files with 16 additions and 6 deletions
+11 -7
View File
@@ -1,4 +1,4 @@
import { readFileAsync, writeFileAsync } from 'fs'; import { readFileAsync, writeFileAsync, mkdirAsync } from 'fs';
import { Settings } from '../common/adminInterfaces'; import { Settings } from '../common/adminInterfaces';
import { cloneDeep } from '../common/utils'; import { cloneDeep } from '../common/utils';
import * as paths from './paths'; import * as paths from './paths';
@@ -18,13 +18,17 @@ export async function loadSettings() {
const json = await readFileAsync(settingsPath, 'utf8'); const json = await readFileAsync(settingsPath, 'utf8');
return JSON.parse(json) as Settings; return JSON.parse(json) as Settings;
} catch (e) { } catch (e) {
if (e === 'ENOENT') { if (e.code === 'ENOENT') {
writeFileAsync(settingsPath, '{}', 'utf8'); try {
return {} as Settings; await mkdirAsync(paths.pathTo('settings'));
} else { } catch (e2) {
console.log("Error reading settings file: " + e); if (e2.code !== 'EEXIST') throw e;
return {} as Settings;
} }
} else {
console.error('Error reading settings file: ' + e);
}
return cloneDeep(defaultSettings);
} }
} }
+6
View File
@@ -1,4 +1,5 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
import * as moment from 'moment'; import * as moment from 'moment';
import { compact } from 'lodash'; import { compact } from 'lodash';
import { Request } from 'express'; import { Request } from 'express';
@@ -188,6 +189,11 @@ export class StatsTracker {
} }
startStatTracking() { startStatTracking() {
if (!fs.existsSync(this.statsPath)) { if (!fs.existsSync(this.statsPath)) {
try {
fs.mkdirSync(path.dirname(this.statsPath), { recursive: true });
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}
fs.writeFileSync(this.statsPath, encodeCSV(statsHeaders), { encoding: 'utf8' }); fs.writeFileSync(this.statsPath, encodeCSV(statsHeaders), { encoding: 'utf8' });
} }