Refactor: use external dependencies

This commit is contained in:
SukkaW 2022-04-22 08:43:30 +08:00
parent 159e516078
commit 31e9f31e37
3 changed files with 14 additions and 41 deletions

View File

@ -2,6 +2,8 @@ const fs = require('fs');
const { promises: fsPromises } = fs;
const pathFn = require('path');
const table = require('table');
const listDir = require('@sukka/listdir');
const { green, yellow } = require('picocolors');
const PRESET_MITM_HOSTNAMES = [
'*baidu.com',
@ -158,13 +160,6 @@ const PRESET_MITM_HOSTNAMES = [
})();
/** Util function */
function green(...args) {
return `\u001b[32m${args.join(' ')}\u001b[0m`;
}
function yellow(...args) {
return `\u001b[33m${args.join(' ')}\u001b[0m`;
}
function parseDomain(input) {
try {
const url = new URL(`https://${input}`);
@ -195,37 +190,3 @@ function escapeRegExp(string = '') {
? string.replace(reRegExpChar, '\\$&')
: string;
}
function listDir(path, options) {
const results = [];
options = Object.assign({ ignoreHidden: true, ignorePattern: null }, options);
return listDirWalker(path, results, '', options).then(() => results);
}
function listDirWalker(path, results, parent, options) {
const promises = [];
return readAndFilterDir(path, options).then(items => {
items.forEach(item => {
const currentPath = pathFn.join(parent, item.name);
if (item.isDirectory()) {
promises.push(listDirWalker(pathFn.join(path, item.name), results, currentPath, options));
}
else {
results.push(currentPath);
}
});
}).then(() => Promise.all(promises));
}
function readAndFilterDir(path, options) {
const { ignoreHidden = true, ignorePattern } = options;
return fs.promises.readdir(path, Object.assign(Object.assign({}, options), { withFileTypes: true }))
.then(results => {
if (ignoreHidden) {
results = results.filter(({ name }) => !name.startsWith('.'));
}
if (ignorePattern) {
results = results.filter(({ name }) => !ignorePattern.test(name));
}
return results;
});
}

11
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"@sukka/listdir": "^0.2.0",
"picocolors": "^1.0.0",
"piscina": "^3.1.0",
"table": "^6.7.3",
"undici": "^5.0.0"
@ -189,6 +190,11 @@
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
},
"node_modules/picocolors": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",
"integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ=="
},
"node_modules/piscina": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/piscina/-/piscina-3.1.0.tgz",
@ -420,6 +426,11 @@
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
},
"picocolors": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",
"integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ=="
},
"piscina": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/piscina/-/piscina-3.1.0.tgz",

View File

@ -15,6 +15,7 @@
"license": "ISC",
"dependencies": {
"@sukka/listdir": "^0.2.0",
"picocolors": "^1.0.0",
"piscina": "^3.1.0",
"table": "^6.7.3",
"undici": "^5.0.0"