From 79f1b01506df70e851198673bdafcbf03ba49548 Mon Sep 17 00:00:00 2001 From: Aaron Suen Date: Sat, 27 Feb 2021 11:23:33 -0500 Subject: [PATCH] Code cleanup --- .jsbeautifyrc | 6 +- .jshintrc | 1 - TODO | 9 --- cdbscreens.js | 196 +++++++++++++++++++++++++------------------------- index.js | 2 +- 5 files changed, 102 insertions(+), 112 deletions(-) delete mode 100644 TODO diff --git a/.jsbeautifyrc b/.jsbeautifyrc index 6c63c4b..29faf71 100644 --- a/.jsbeautifyrc +++ b/.jsbeautifyrc @@ -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, diff --git a/.jshintrc b/.jshintrc index 63e9dc3..5dc12e2 100644 --- a/.jshintrc +++ b/.jshintrc @@ -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.) diff --git a/TODO b/TODO deleted file mode 100644 index 5c8b4e0..0000000 --- a/TODO +++ /dev/null @@ -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. - ------------------------------------------------------------------------- diff --git a/cdbscreens.js b/cdbscreens.js index 37ec9c3..d8731db 100644 --- a/cdbscreens.js +++ b/cdbscreens.js @@ -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; diff --git a/index.js b/index.js index 34a9ac8..b37c03e 100644 --- a/index.js +++ b/index.js @@ -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(', ')}`); }