Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Commit

Permalink
Fix a bug with empty domain groups and missing files (#31)
Browse files Browse the repository at this point in the history
* Fix a bug with empty domain groups, follow-up #29

* Fix bug with empty domain groups and missing files
  • Loading branch information
Chan Chak Shing authored May 11, 2020
1 parent 3708fe2 commit 3be287e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 8 additions & 5 deletions script/libs/splitDomainsByTlds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
*/
function splitDomainsByTlds(domains) {
let retval = [domains];
for (let tldCounts = 1; tldCounts < 2; ++tldCounts) {
for (let tldCounts = 1; tldCounts < 3; ++tldCounts) {
let tlds = domains.map((domain) =>
domain.split(".").reverse().slice(0, tldCounts).reverse().join(".")
);

// remove possible duplicates
tlds = Array.from(new Set(tlds));
let tmp = tlds
.map((tld) =>
domains.filter((domain) => domain.endsWith(`.${tld}`) || domain === tld)
)
.filter((arr) => arr.length > 0);

if (tlds.length > 1) {
retval = tlds.map((tld) =>
domains.filter((domain) => domain.endsWith(`.${tld}`))
);
if (tmp.length > 1) {
retval = tmp;
break;
}
}
Expand Down
13 changes: 6 additions & 7 deletions script/ruleset-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ if (process.argv.length <= 2) {
}

// create a backup of the input FILE if it does not exist
// path.normalize mess up files with underscore in their names
const filename = process.argv[2];
const filename = path.normalize(process.argv[2]);
const backupFilename = filename + ".bak";
if (!fs.existsSync(backupFilename)) {
fs.copyFileSync(filename, backupFilename);
Expand Down Expand Up @@ -64,11 +63,11 @@ for (const domains of domainGroups) {
const command = rawCommandParts.filter((part) => part != null).join(" ");
child_process.execSync(command);

// remove the original file if neccessary
if (domainGroups.length > 1) {
fs.unlinkSync(filename);
}

// remove temporary file
fs.unlinkSync(tmpFilename);
}

// remove the original file if neccessary
if (domainGroups.length > 1) {
fs.unlinkSync(filename);
}

0 comments on commit 3be287e

Please sign in to comment.