Skip to content

Commit

Permalink
refactor: eslint --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaslanjaka committed May 11, 2023
1 parent f185ad8 commit e3218df
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 48 deletions.
5 changes: 4 additions & 1 deletion src/curl/download-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ const tmp = join(__dirname, 'tmp');
if (!existsSync(tmp)) mkdirSync(tmp, { recursive: true });

// download to dir
const toDir = downloadImage('https://avatars.githubusercontent.com/u/32372333?v=4&s=160', tmp);
const _toDir = downloadImage(
'https://avatars.githubusercontent.com/u/32372333?v=4&s=160',
tmp
);
4 changes: 3 additions & 1 deletion src/markdown/error-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export default class ErrorMarkdown {
const frame = e.stack.split('\n')[2]; // change to 3 for grandparent func
//const lineNumber = frame.split(':').reverse()[1];
//const functionName = frame.split(' ')[5];
this.filelog = join(process.cwd(), 'tmp', 'errors', md5(hash ? hash : toUnix(frame))) + '.md';
this.filelog =
join(process.cwd(), 'tmp', 'errors', md5(hash ? hash : toUnix(frame))) +
'.md';
this.message = 'error messages log at ' + this.filelog;

if (typeof obj == 'object') {
Expand Down
6 changes: 5 additions & 1 deletion src/markdown/toHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { root } from '../types/_config';
import { renderBodyMarkdown } from './toHtml';
import { parsePost } from './transformPosts';

const postPath = join(root, 'src-posts', '/2022/05/fully-lazy-loaded-adsense.md');
const postPath = join(
root,
'src-posts',
'/2022/05/fully-lazy-loaded-adsense.md'
);
const parse = parsePost(postPath, postPath, false);
const render = renderBodyMarkdown(parse, true);
export { postPath, parse, render };
6 changes: 3 additions & 3 deletions src/node/array-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class array_iterator {
if (item) {
return {
value: item,
done: false,
done: false
};
}
return { done: true };
Expand All @@ -32,11 +32,11 @@ export class array_iterator {
if (item) {
return {
value: item,
done: false,
done: false
};
}
return { done: true };
},
}
};
}
}
25 changes: 20 additions & 5 deletions src/node/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { TypedEmitter } from 'tiny-typed-emitter';
import { toUnix } from 'upath';
import { DynamicObject } from '../types';
import './cache-serialize';
import { cacheDir, existsSync, join, mkdirSync, read, resolve, write } from './filemanager';
import {
cacheDir,
existsSync,
join,
mkdirSync,
read,
resolve,
write
} from './filemanager';
import { json_encode } from './JSON';
import logger from './logger';
import { md5, md5FileSync } from './md5-file';
Expand Down Expand Up @@ -91,7 +99,8 @@ export default class CacheFile extends TypedEmitter<CacheFileEvent> {
const stack = new Error().stack.split('at')[2];
hash = md5(stack);
}
if (!existsSync(CacheFile.options.folder)) mkdirSync(CacheFile.options.folder);
if (!existsSync(CacheFile.options.folder))
mkdirSync(CacheFile.options.folder);
this.dbFile = join(CacheFile.options.folder, 'db-' + hash);
if (!existsSync(this.dbFile)) write(this.dbFile, {});
let db = read(this.dbFile, 'utf-8');
Expand Down Expand Up @@ -141,7 +150,8 @@ export default class CacheFile extends TypedEmitter<CacheFileEvent> {
// if key is long text
if (key.length > 32) {
// search post id
const regex = /id:.*([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/gm;
const regex =
/id:.*([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/gm;
const m = regex.exec(key);
if (m && typeof m[1] == 'string') return m[1];
// return first 32 byte text
Expand All @@ -154,7 +164,8 @@ export default class CacheFile extends TypedEmitter<CacheFileEvent> {
* @param key
* @returns
*/
locateKey = (key: string) => join(CacheFile.options.folder, this.currentHash, md5(this.resolveKey(key)));
locateKey = (key: string) =>
join(CacheFile.options.folder, this.currentHash, md5(this.resolveKey(key)));
dump(key?: string) {
if (key) {
return {
Expand All @@ -175,7 +186,11 @@ export default class CacheFile extends TypedEmitter<CacheFileEvent> {

// save cache on process exit
scheduler.add('writeCacheFile-' + this.currentHash, () => {
logger.log(chalk.magentaBright(self.currentHash), 'saved cache', self.dbFile);
logger.log(
chalk.magentaBright(self.currentHash),
'saved cache',
self.dbFile
);
write(self.dbFile, json_encode(self.md5Cache));
});
if (value) write(locationCache, json_encode(value));
Expand Down
2 changes: 1 addition & 1 deletion src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const logger = {
},
profileEnd: function (label?: string): void {
return console.profileEnd(label);
},
}
};
type newConsole = typeof logger &
Console & {
Expand Down
6 changes: 5 additions & 1 deletion src/node/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ class process {
* @param options
* @param callback
*/
static doProcess(lockfile: string, options: { verbose: boolean } | any, callback: any) {
static doProcess(
lockfile: string,
options: { verbose: boolean } | any,
callback: any
) {
if (typeof options.verbose == 'boolean') {
this.verbose = options.verbose;
}
Expand Down
9 changes: 6 additions & 3 deletions src/node/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export function bindProcessExit(key: string, fn: () => void): void {
*/
function exitHandler(options: { cleanup: any; exit: any }, exitCode: any) {
Object.keys(fns).forEach((key) => {
if (scheduler.verbose) logger.log(logname, `executing function key: ${key}`);
if (scheduler.verbose)
logger.log(logname, `executing function key: ${key}`);
fns[key]();
});
if (options.cleanup && scheduler.verbose) logger.log(logname, `clean exit(${exitCode})`);
if (options.cleanup && scheduler.verbose)
logger.log(logname, `clean exit(${exitCode})`);
if (options.exit) process.exit();
}

Expand Down Expand Up @@ -122,7 +124,8 @@ class scheduler {
functions[key]();
if (deleteAfter) delete functions[key];
} else {
if (scheduler.verbose) logger.error(`function with key: ${key} is not function`);
if (scheduler.verbose)
logger.error(`function with key: ${key} is not function`);
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/node/slugify/replacements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const overridableReplacements = [
['&', ' and '],
['🦄', ' unicorn '],
['♥', ' love '],
['♥', ' love ']
];

export default overridableReplacements;
20 changes: 17 additions & 3 deletions src/node/spawner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// noinspection DuplicatedCode

import { ChildProcess, ChildProcessWithoutNullStreams, spawn, SpawnOptions } from 'child_process';
import {
ChildProcess,
ChildProcessWithoutNullStreams,
spawn,
SpawnOptions
} from 'child_process';
import process from 'process';
import scheduler from './scheduler';

Expand All @@ -15,7 +20,12 @@ class spawner {
* @param callback callback for children process
*/
// eslint-disable-next-line no-unused-vars
static spawn(command: string, args?: string[], opt: SpawnOptions = {}, callback?: (path: ChildProcess) => any) {
static spawn(
command: string,
args?: string[],
opt: SpawnOptions = {},
callback?: (path: ChildProcess) => any
) {
const defaultOption: SpawnOptions = { stdio: 'pipe', detached: false };
if (['npm', 'ts-node', 'tsc', 'npx', 'hexo'].includes(command)) {
command = /^win/.test(process.platform) ? `${command}.cmd` : command;
Expand Down Expand Up @@ -62,7 +72,11 @@ class spawner {
* Kill all ChildProcessWithoutNullStreams[]
*/
static children_kill() {
console.log('killing', spawner.children.length, spawner.children.length > 1 ? 'child processes' : 'child process');
console.log(
'killing',
spawner.children.length,
spawner.children.length > 1 ? 'child processes' : 'child process'
);

for (let i = 0; i < spawner.children.length; i++) {
const child = spawner.children[i];
Expand Down
2 changes: 1 addition & 1 deletion src/node/transliterate/replacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,7 @@ const replacements = [
['🆆', 'W'],
['🆇', 'X'],
['🆈', 'Y'],
['🆉', 'Z'],
['🆉', 'Z']
];

export default replacements;
5 changes: 4 additions & 1 deletion src/node/truncate-utf8-bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ function truncate(getLength, string, byteLength) {
codePoint = string.charCodeAt(i);
segment = string[i];

if (isHighSurrogate(codePoint) && isLowSurrogate(string.charCodeAt(i + 1))) {
if (
isHighSurrogate(codePoint) &&
isLowSurrogate(string.charCodeAt(i + 1))
) {
i += 1;
segment += string[i];
}
Expand Down
63 changes: 40 additions & 23 deletions src/translator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const cookieJarFile = path.join(__dirname, '/../../build/cookiejar.txt');

interface CurlOpt {
method: 'GET' | 'POST' | 'HEAD' | 'PATCH' | 'OPTION';
callback: (status: number, data: string | Buffer, headers: Buffer | HeaderInfo[], curlInstance: Curl) => void;
callback: (
status: number,
data: string | Buffer,
headers: Buffer | HeaderInfo[],
curlInstance: Curl
) => void;
}

class Translator {
Expand All @@ -33,44 +38,50 @@ class Translator {
const parseUrl = new URL(url);
const self = this;
this.request(
`https://translate.google.com/gen204?proxye=website&client=webapp&sl=${this.sl}&tl=${
this.tl
}&hl=en&u=${encodeURIComponent(url)}`
`https://translate.google.com/gen204?proxye=website&client=webapp&sl=${
this.sl
}&tl=${this.tl}&hl=en&u=${encodeURIComponent(url)}`
);

this.request(
`https://translate.google.com/translate?hl=en&sl=${this.sl}&tl=${this.tl}&u=${encodeURIComponent(url)}&sandbox=1`
`https://translate.google.com/translate?hl=en&sl=${this.sl}&tl=${
this.tl
}&u=${encodeURIComponent(url)}&sandbox=1`
);

this.request(
`https://translate.googleusercontent.com/translate_p?hl=en&sl=${this.sl}&tl=${this.tl}&u=${decodeURIComponent(
`https://translate.googleusercontent.com/translate_p?hl=en&sl=${
this.sl
}&tl=${this.tl}&u=${decodeURIComponent(
url
)}&depth=1&rurl=translate.google.com&sp=nmt4&pto=aue,ajax,boq&usg=ALkJrhgAAAAAYMQSbrcRl2o4a3kZsbz6V0Mz1jPOGzly`
);

this.request(
`https://translate.google.com/website?depth=1&hl=en&pto=aue,ajax,boq&rurl=translate.google.com&sl=${
this.sl
}&sp=nmt4&tl=${this.tl}&u=${decodeURIComponent(url)}&usg=ALkJrhgP3S6k0r9M1L0I0usu2YoSrco1KQ`
}&sp=nmt4&tl=${this.tl}&u=${decodeURIComponent(
url
)}&usg=ALkJrhgP3S6k0r9M1L0I0usu2YoSrco1KQ`
);

const parse = new URL(url);
this.request(
`https://${parse.host.replace(/\./, '-')}.translate.goog${parseUrl.pathname}?_x_tr_sl=${this.sl}&_x_tr_tl=${
this.tl
}&_x_tr_hl=en&_x_tr_pto=ajax`
`https://${parse.host.replace(/\./, '-')}.translate.goog${
parseUrl.pathname
}?_x_tr_sl=${this.sl}&_x_tr_tl=${this.tl}&_x_tr_hl=en&_x_tr_pto=ajax`
);

this.request(
`https://translate.google.com/translate_un?sl=${this.sl}&tl=${this.tl}&u=${decodeURIComponent(
url
)}&usg=ALkJrhg-dpAhmINQHidHIs0byhWyENzuSA`
`https://translate.google.com/translate_un?sl=${this.sl}&tl=${
this.tl
}&u=${decodeURIComponent(url)}&usg=ALkJrhg-dpAhmINQHidHIs0byhWyENzuSA`
);

this.request(
`http://translate.google.com/translate?depth=1&nv=1&rurl=translate.google.com&sl=${this.sl}&sp=nmt4&tl=${
this.tl
}&u=${encodeURI(url)}`,
`http://translate.google.com/translate?depth=1&nv=1&rurl=translate.google.com&sl=${
this.sl
}&sp=nmt4&tl=${this.tl}&u=${encodeURI(url)}`,
function (_statusCode, data, _headers, _curlInstance) {
self.result = data;
if (typeof callback == 'function') {
Expand All @@ -92,12 +103,15 @@ class Translator {
dom = new JSDOM(data);
const hyperlinks = dom.window.document.getElementsByTagName('a');
if (hyperlinks.length > 0) {
self.request(hyperlinks.item(0).href, function (_status, data, _headers, _curlInstance) {
self.result = data;
if (typeof callback == 'function') {
callback(String(data));
self.request(
hyperlinks.item(0).href,
function (_status, data, _headers, _curlInstance) {
self.result = data;
if (typeof callback == 'function') {
callback(String(data));
}
}
});
);
}
});
}
Expand All @@ -108,7 +122,8 @@ class Translator {
extractTranslated(html: string) {
const dom = new JSDOM(html);
// fix hyperlinks
const hyperlinks: HTMLCollectionOf<HTMLAnchorElement> = dom.window.document.getElementsByTagName('a');
const hyperlinks: HTMLCollectionOf<HTMLAnchorElement> =
dom.window.document.getElementsByTagName('a');
for (let i = 0; i < hyperlinks.length; i++) {
const hyperlink = hyperlinks.item(i);
const href = new URL(hyperlink.href);
Expand All @@ -129,7 +144,9 @@ class Translator {
private capture(parentHtml: string) {
const dom = new JSDOM(parentHtml);
const script = dom.window.document.createElement('script');
script.innerHTML = String(fs.readFileSync(path.join(__dirname, 'translate-capture.js')));
script.innerHTML = String(
fs.readFileSync(path.join(__dirname, 'translate-capture.js'))
);
dom.window.document.body.appendChild(script);
return dom.serialize();
}
Expand Down
4 changes: 3 additions & 1 deletion src/validator/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ async function copyDir(src, dest) {
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);

entry.isDirectory() ? await copyDir(srcPath, destPath) : await fs.copyFile(srcPath, destPath);
entry.isDirectory()
? await copyDir(srcPath, destPath)
: await fs.copyFile(srcPath, destPath);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/validator/post_callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function (content, headers) {

//https://cdn.rawgit.com/dimaslanjaka/Web-Manajemen/master/Animasi/text-animasi.html
//replace old cdn.rawgit.com to github page
content = content.replace(new RegExp('https://cdn.rawgit.com/dimaslanjaka', 'm'), 'https://www.webmanajemen.com/');
content = content.replace(
new RegExp('https://cdn.rawgit.com/dimaslanjaka', 'm'),
'https://www.webmanajemen.com/'
);
return content;
};
5 changes: 4 additions & 1 deletion src/validator/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const root = path.join(__dirname, '/../');
const docs = path.join(root, 'docs');

// if public_dir/index.html empty, throw
const tests = [path.join(docs, 'index.html'), path.join(docs, 'The Legend Of Neverland/Quiz.html')];
const tests = [
path.join(docs, 'index.html'),
path.join(docs, 'The Legend Of Neverland/Quiz.html')
];
tests.forEach(function (test) {
if (fs.existsSync(test)) {
const sizes_byte = fs.statSync(test).size;
Expand Down

0 comments on commit e3218df

Please # to comment.