Fix optional JSON configs

Missing files are allowed, invalid JSON is not.

Also refactor some filesystem handling.
master
Aaron Suen 2021-02-28 08:27:05 -05:00
parent 0029e9a3cf
commit aa7dce4247
3 changed files with 12 additions and 21 deletions

View File

@ -1,6 +1,6 @@
'use strict';
const fs = require('fs');
const fsx = require('fs-extra');
const crypto = require('crypto');
const config = require('./config');
const mylog = require('./mylog');
@ -8,18 +8,15 @@ const cdbfetch = require('./cdbfetch');
async function cdbscreens() {
let screens = config.screenshots;
const loading = [];
screens = screens
screens = await Promise.all(screens
.map(x => (typeof x === 'string' || x instanceof String) ? { name: x } : x)
.map(x => {
.map(async x => {
if(x.data)
x.data = Buffer.from(x.data);
else
loading.push(new Promise((r, j) => fs.readFile(x.name,
(e, d) => e ? j(e) : r(x.data = d))));
x.data = await fsx.readFile(x.name);
return x;
});
await Promise.all(loading);
}));
screens.forEach(x => x.hash = crypto.createHash('sha256')
.update(x.data)
.digest('base64')

View File

@ -1,13 +1,10 @@
'use strict';
const fs = require('fs');
const util = require('util');
const fsx = require('fs-extra');
const config = require('./config');
const mylog = require('./mylog');
const spawn = require('./spawn');
const readFile = util.promisify(fs.readFile);
function luaser(obj) {
if(Array.isArray(obj))
return `{${obj.map(luaser).join(',')}}`;
@ -30,7 +27,7 @@ function luaser(obj) {
module.exports = async cwd => {
const luahook = `config = ${luaser(config)}\n\n` +
(await readFile('luahook.lua'))
(await fsx.readFile('luahook.lua'))
.toString();
mylog('loading lua metadata...');

View File

@ -2,8 +2,7 @@
process.on('unhandledRejection', e => { throw e; });
const fs = require('fs');
const util = require('util');
const fsx = require('fs-extra');
const path = require('path');
const config = require('./config');
const gitmirror = require('./gitmirror');
@ -13,13 +12,12 @@ const cdbedit = require('./cdbedit');
const cdbrelease = require('./cdbrelease');
const cdbscreens = require('./cdbscreens');
const readFile = util.promisify(fs.readFile);
const loadJsonConfig = async (fn, key, req) => {
const loadJsonConfig = async (fn, key) => {
try {
const conf = await readFile(fn);
const conf = await fsx.readFile(fn);
config.set(key, JSON.parse(conf.toString()));
} catch (e) {
if(req) throw e;
if(e.code !== 'ENOENT') throw e;
console.warn(e.message || e);
}
};
@ -39,8 +37,7 @@ function missingConfig(...opts) {
await gitexport(true, async full => {
if(config.cdbjson)
await loadJsonConfig(
path.join(full, config.cdbjsonpath),
'gitjson', true);
path.join(full, config.cdbjsonpath), 'gitjson');
if(config.cdblua)
await gitlua(full);
});