Code cleanup

master
Aaron Suen 2021-02-27 11:23:33 -05:00
parent 8362e41b0e
commit 79f1b01506
5 changed files with 102 additions and 112 deletions

View File

@ -1,7 +1,7 @@
{ {
"indent_size": "1", "indent_size": 1,
"indent_char": "\t", "indent_char": "\t",
"max_preserve_newlines": "2", "max_preserve_newlines": 2,
"preserve_newlines": true, "preserve_newlines": true,
"keep_array_indentation": false, "keep_array_indentation": false,
"break_chained_methods": true, "break_chained_methods": true,
@ -11,7 +11,7 @@
"unescape_strings": false, "unescape_strings": false,
"jslint_happy": false, "jslint_happy": false,
"end_with_newline": true, "end_with_newline": true,
"wrap_line_length": "120", "wrap_line_length": 120,
"indent_inner_html": false, "indent_inner_html": false,
"comma_first": false, "comma_first": false,
"e4x": false, "e4x": false,

View File

@ -3,7 +3,6 @@
// See http://jshint.com/docs/ for more details // See http://jshint.com/docs/ for more details
"maxerr" : 50, // {int} Maximum error before stopping "maxerr" : 50, // {int} Maximum error before stopping
"esversion" : 6,
// Enforcing // Enforcing
"bitwise" : false, // true: Prohibit bitwise operators (&, |, ^, etc.) "bitwise" : false, // true: Prohibit bitwise operators (&, |, ^, etc.)

9
TODO
View File

@ -1,9 +0,0 @@
------------------------------------------------------------------------
- Webhook support.
- Produce JSON appropriate for Discord.
- POST it to an appropriate URL.
- Allow custom JSON object spec.
- Allow it to be piped to a custom script.
------------------------------------------------------------------------

View File

@ -1,106 +1,106 @@
'use strict'; // 'use strict';
const util = require('util'); // const util = require('util');
const fs = require('fs'); // const fs = require('fs');
const crypto = require('crypto'); // const crypto = require('crypto');
const config = require('./config'); // const config = require('./config');
const cdblib = require('./cdblib'); // const cdblib = require('./cdblib');
async function cdbscreens() { // async function cdbscreens() {
let screens = config.screenshots; // let screens = config.screenshots;
if(typeof screens === 'string' || screens instanceof String) // if(typeof screens === 'string' || screens instanceof String)
screens = screens.split(','); // screens = screens.split(',');
if(!Array.isArray(screens)) // if(!Array.isArray(screens))
screens = [screens]; // screens = [screens];
const loading = []; // const loading = [];
screens = 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(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, // loading.push(new Promise((r, j) => fs.readFile(x.name,
(e, d) => e ? j(e) : r(x.data = d)))); // (e, d) => e ? j(e) : r(x.data = d))));
return x; // return x;
}); // });
await Promise.all(loading); // await Promise.all(loading);
screens.forEach(x => x.id = crypto.createHash('sha256') // screens.forEach(x => x.id = crypto.createHash('sha256')
.update(x.data) // .update(x.data)
.digest('hex')); // .digest('hex'));
const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/'; // const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/';
await cdblib.login(); // await cdblib.login();
console.log('checking existing screenshots...'); // console.log('checking existing screenshots...');
const $ = await cdblib.fetch(uri); // const $ = await cdblib.fetch(uri);
const exist = []; // const exist = [];
$('.screenshot_list a') // $('.screenshot_list a')
.each((i, e) => { // .each((i, e) => {
const href = $(e) // const href = $(e)
.attr('href'); // .attr('href');
if(/screenshots\/new/.test(href)) // if(/screenshots\/new/.test(href))
return; // return;
exist.push({ // exist.push({
id: $('img', e) // id: $('img', e)
.attr('alt'), // .attr('alt'),
key: href.match(/screenshots\/(\d+)\/edit/)[1] // key: href.match(/screenshots\/(\d+)\/edit/)[1]
}); // });
}); // });
let dirty; // let dirty;
let epos = 0; // let epos = 0;
let spos = 0; // let spos = 0;
const deletes = []; // const deletes = [];
while(epos < exist.length && spos < screens.length) { // while(epos < exist.length && spos < screens.length) {
if(exist[epos].id === screens[spos].id) // if(exist[epos].id === screens[spos].id)
spos++; // spos++;
else // else
deletes.push(exist[epos].key); // deletes.push(exist[epos].key);
epos++; // epos++;
} // }
while(epos < exist.length) { // while(epos < exist.length) {
deletes.push(exist[epos].key); // deletes.push(exist[epos].key);
epos++; // epos++;
} // }
while(spos < screens.length) { // while(spos < screens.length) {
dirty = true; // dirty = true;
const scr = screens[spos]; // const scr = screens[spos];
console.log(`uploading ${scr.name || scr.id}...`); // console.log(`uploading ${scr.name || scr.id}...`);
const newuri = uri + 'screenshots/new/'; // const newuri = uri + 'screenshots/new/';
const $$ = await cdblib.fetch(newuri); // const $$ = await cdblib.fetch(newuri);
const fields = cdblib.getfields($$); // const fields = cdblib.getfields($$);
fields.title = scr.id; // fields.title = scr.id;
fields.fileUpload = { // fields.fileUpload = {
buffer: scr.data, // buffer: scr.data,
filename: scr.name || scr.id, // filename: scr.name || scr.id,
content_type: 'application/octet-stream' // content_type: 'application/octet-stream'
}; // };
if(config.dryrun) { // if(config.dryrun) {
console.log('dry run; not uploading'); // console.log('dry run; not uploading');
console.log(util.inspect(fields)); // console.log(util.inspect(fields));
} else // } else
await cdblib.fetch(newuri, 'post', fields, { multipart: true }); // await cdblib.fetch(newuri, 'post', fields, { multipart: true });
spos++ // spos++
} // }
for(let i = 0; i < deletes.length; i++) { // for(let i = 0; i < deletes.length; i++) {
const key = deletes[i]; // const key = deletes[i];
dirty = true; // dirty = true;
console.log(`deleting screenshot ${key}...`) // console.log(`deleting screenshot ${key}...`)
const edituri = uri + 'screenshots/' + key + '/edit/'; // const edituri = uri + 'screenshots/' + key + '/edit/';
const $$ = await cdblib.fetch(edituri); // const $$ = await cdblib.fetch(edituri);
const fields = cdblib.getfields($$); // const fields = cdblib.getfields($$);
fields.delete = 'y'; // fields.delete = 'y';
delete fields.approved; // delete fields.approved;
if(config.dryrun) { // if(config.dryrun) {
console.log('dry run; not deleting'); // console.log('dry run; not deleting');
console.log(util.inspect(fields)); // console.log(util.inspect(fields));
} else // } else
await cdblib.fetch(edituri, 'post', fields, { multipart: true }); // await cdblib.fetch(edituri, 'post', fields, { multipart: true });
} // }
console.log('screenshots synced'); // console.log('screenshots synced');
return dirty; // return dirty;
} // }
module.exports = cdbscreens; // module.exports = cdbscreens;

View File

@ -25,7 +25,7 @@ const loadJsonConfig = async (fn, key, req) => {
}; };
function missingConfig(...opts) { function missingConfig(...opts) {
return new Error(`missing/invalid option${opts.length == 1 ? '' : 's'}: ${ return new Error(`missing/invalid option${opts.length === 1 ? '' : 's'}: ${
opts.map(x => `--${x}=...`).join(', ')}`); opts.map(x => `--${x}=...`).join(', ')}`);
} }