mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor: use external dependencies
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user