Fix optional JSON configs
Missing files are allowed, invalid JSON is not. Also refactor some filesystem handling.master
parent
0029e9a3cf
commit
aa7dce4247
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const fs = require('fs');
|
const fsx = require('fs-extra');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
const mylog = require('./mylog');
|
const mylog = require('./mylog');
|
||||||
|
@ -8,18 +8,15 @@ const cdbfetch = require('./cdbfetch');
|
||||||
|
|
||||||
async function cdbscreens() {
|
async function cdbscreens() {
|
||||||
let screens = config.screenshots;
|
let screens = config.screenshots;
|
||||||
const loading = [];
|
screens = await Promise.all(screens
|
||||||
screens = screens
|
|
||||||
.map(x => (typeof x === 'string' || x instanceof String) ? { name: x } : x)
|
.map(x => (typeof x === 'string' || x instanceof String) ? { name: x } : x)
|
||||||
.map(x => {
|
.map(async x => {
|
||||||
if(x.data)
|
if(x.data)
|
||||||
x.data = Buffer.from(x.data);
|
x.data = Buffer.from(x.data);
|
||||||
else
|
else
|
||||||
loading.push(new Promise((r, j) => fs.readFile(x.name,
|
x.data = await fsx.readFile(x.name);
|
||||||
(e, d) => e ? j(e) : r(x.data = d))));
|
|
||||||
return x;
|
return x;
|
||||||
});
|
}));
|
||||||
await Promise.all(loading);
|
|
||||||
screens.forEach(x => x.hash = crypto.createHash('sha256')
|
screens.forEach(x => x.hash = crypto.createHash('sha256')
|
||||||
.update(x.data)
|
.update(x.data)
|
||||||
.digest('base64')
|
.digest('base64')
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const fs = require('fs');
|
const fsx = require('fs-extra');
|
||||||
const util = require('util');
|
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
const mylog = require('./mylog');
|
const mylog = require('./mylog');
|
||||||
const spawn = require('./spawn');
|
const spawn = require('./spawn');
|
||||||
|
|
||||||
const readFile = util.promisify(fs.readFile);
|
|
||||||
|
|
||||||
function luaser(obj) {
|
function luaser(obj) {
|
||||||
if(Array.isArray(obj))
|
if(Array.isArray(obj))
|
||||||
return `{${obj.map(luaser).join(',')}}`;
|
return `{${obj.map(luaser).join(',')}}`;
|
||||||
|
@ -30,7 +27,7 @@ function luaser(obj) {
|
||||||
|
|
||||||
module.exports = async cwd => {
|
module.exports = async cwd => {
|
||||||
const luahook = `config = ${luaser(config)}\n\n` +
|
const luahook = `config = ${luaser(config)}\n\n` +
|
||||||
(await readFile('luahook.lua'))
|
(await fsx.readFile('luahook.lua'))
|
||||||
.toString();
|
.toString();
|
||||||
|
|
||||||
mylog('loading lua metadata...');
|
mylog('loading lua metadata...');
|
||||||
|
|
13
index.js
13
index.js
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
process.on('unhandledRejection', e => { throw e; });
|
process.on('unhandledRejection', e => { throw e; });
|
||||||
|
|
||||||
const fs = require('fs');
|
const fsx = require('fs-extra');
|
||||||
const util = require('util');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
const gitmirror = require('./gitmirror');
|
const gitmirror = require('./gitmirror');
|
||||||
|
@ -13,13 +12,12 @@ const cdbedit = require('./cdbedit');
|
||||||
const cdbrelease = require('./cdbrelease');
|
const cdbrelease = require('./cdbrelease');
|
||||||
const cdbscreens = require('./cdbscreens');
|
const cdbscreens = require('./cdbscreens');
|
||||||
|
|
||||||
const readFile = util.promisify(fs.readFile);
|
const loadJsonConfig = async (fn, key) => {
|
||||||
const loadJsonConfig = async (fn, key, req) => {
|
|
||||||
try {
|
try {
|
||||||
const conf = await readFile(fn);
|
const conf = await fsx.readFile(fn);
|
||||||
config.set(key, JSON.parse(conf.toString()));
|
config.set(key, JSON.parse(conf.toString()));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if(req) throw e;
|
if(e.code !== 'ENOENT') throw e;
|
||||||
console.warn(e.message || e);
|
console.warn(e.message || e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -39,8 +37,7 @@ function missingConfig(...opts) {
|
||||||
await gitexport(true, async full => {
|
await gitexport(true, async full => {
|
||||||
if(config.cdbjson)
|
if(config.cdbjson)
|
||||||
await loadJsonConfig(
|
await loadJsonConfig(
|
||||||
path.join(full, config.cdbjsonpath),
|
path.join(full, config.cdbjsonpath), 'gitjson');
|
||||||
'gitjson', true);
|
|
||||||
if(config.cdblua)
|
if(config.cdblua)
|
||||||
await gitlua(full);
|
await gitlua(full);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue