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",
"max_preserve_newlines": "2",
"max_preserve_newlines": 2,
"preserve_newlines": true,
"keep_array_indentation": false,
"break_chained_methods": true,
@ -11,7 +11,7 @@
"unescape_strings": false,
"jslint_happy": false,
"end_with_newline": true,
"wrap_line_length": "120",
"wrap_line_length": 120,
"indent_inner_html": false,
"comma_first": false,
"e4x": false,

View File

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

View File

@ -25,7 +25,7 @@ const loadJsonConfig = async (fn, key, req) => {
};
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(', ')}`);
}