aboutsummaryrefslogtreecommitdiff
path: root/deno/tools
diff options
context:
space:
mode:
Diffstat (limited to 'deno/tools')
-rw-r--r--deno/tools/deno.json2
-rw-r--r--deno/tools/generate-geosite-rules.ts75
-rw-r--r--deno/tools/manage-vm.ts7
-rw-r--r--deno/tools/template.ts4
4 files changed, 43 insertions, 45 deletions
diff --git a/deno/tools/deno.json b/deno/tools/deno.json
index 1b2cf32..355046a 100644
--- a/deno/tools/deno.json
+++ b/deno/tools/deno.json
@@ -3,6 +3,6 @@
"tasks": {
},
"imports": {
- "mustache": "npm:mustache@^4.2.0",
+ "mustache": "npm:mustache@^4.2.0"
}
}
diff --git a/deno/tools/generate-geosite-rules.ts b/deno/tools/generate-geosite-rules.ts
index c59d34f..bfa53ba 100644
--- a/deno/tools/generate-geosite-rules.ts
+++ b/deno/tools/generate-geosite-rules.ts
@@ -1,7 +1,8 @@
-const PROXY_NAME = "node-select"
-const ATTR = "cn"
+const PROXY_NAME = "node-select";
+const ATTR = "cn";
const REPO_NAME = "domain-list-community";
-const URL = "https://github.com/v2fly/domain-list-community/archive/refs/heads/master.zip"
+const URL =
+ "https://github.com/v2fly/domain-list-community/archive/refs/heads/master.zip";
const SITES = [
"github",
"google",
@@ -39,9 +40,9 @@ const SITES = [
"ieee",
"sci-hub",
"libgen",
-]
+];
-const prefixes = ["include", "domain", "keyword", "full", "regexp"] as const
+const prefixes = ["include", "domain", "keyword", "full", "regexp"] as const;
interface Rule {
kind: (typeof prefixes)[number];
@@ -52,20 +53,20 @@ interface Rule {
type FileProvider = (name: string) => string;
function extract(starts: string[], provider: FileProvider): Rule[] {
-function parseLine(line: string): Rule {
- let kind = prefixes.find((p) => line.startsWith(p + ":"));
- if (kind != null) {
- line = line.slice(line.indexOf(":") + 1);
- } else {
- kind = "domain";
+ function parseLine(line: string): Rule {
+ let kind = prefixes.find((p) => line.startsWith(p + ":"));
+ if (kind != null) {
+ line = line.slice(line.indexOf(":") + 1);
+ } else {
+ kind = "domain";
+ }
+ const segs = line.split("@");
+ return {
+ kind,
+ value: segs[0].trim(),
+ attrs: [...segs.slice(1)].map((s) => s.trim()),
+ };
}
- const segs = line.split("@");
- return {
- kind,
- value: segs[0].trim(),
- attrs: [...segs.slice(1)].map((s) => s.trim()),
- };
-}
function parse(text: string): Rule[] {
return text
@@ -76,10 +77,10 @@ function parseLine(line: string): Rule {
.map((l) => parseLine(l));
}
- const visited = [] as string[]
- const rules = [] as Rule[]
+ const visited = [] as string[];
+ const rules = [] as Rule[];
- function add(name :string) {
+ function add(name: string) {
const text = provider(name);
for (const rule of parse(text)) {
if (rule.kind === "include") {
@@ -100,25 +101,25 @@ function parseLine(line: string): Rule {
add(start);
}
- return rules
+ return rules;
}
function toNewFormat(rules: Rule[], attr: string): [string, string] {
function toLine(rule: Rule) {
const prefixMap = {
- "domain": "DOMAIN-SUFFIX",
- "full": "DOMAIN",
- "keyword": "DOMAIN-KEYWORD",
- "regexp": "DOMAIN-REGEX",
+ domain: "DOMAIN-SUFFIX",
+ full: "DOMAIN",
+ keyword: "DOMAIN-KEYWORD",
+ regexp: "DOMAIN-REGEX",
} as const;
if (rule.kind === "include") {
- throw new Error("Include rule not parsed.")
+ throw new Error("Include rule not parsed.");
}
- return `${prefixMap[rule.kind]},${rule.value}`
+ return `${prefixMap[rule.kind]},${rule.value}`;
}
function toLines(rules: Rule[]) {
- return rules.map(r => toLine(r)).join("\n")
+ return rules.map((r) => toLine(r)).join("\n");
}
const has: Rule[] = [];
@@ -128,7 +129,6 @@ function toNewFormat(rules: Rule[], attr: string): [string, string] {
return [toLines(has), toLines(notHas)];
}
-
if (import.meta.main) {
const tmpDir = Deno.makeTempDirSync({ prefix: "geosite-rules-" });
console.log("Work dir is ", tmpDir);
@@ -150,12 +150,11 @@ if (import.meta.main) {
const provider = (name: string) =>
Deno.readTextFileSync(dataDir + "/" + name);
- const rules = extract(SITES, provider)
- const [has, notHas] = toNewFormat(rules, ATTR)
- const hasFile = tmpDir + "/has-rule"
- const notHasFile = tmpDir + "/not-has-rule"
- console.log("Write result to: " + hasFile + " , " + notHasFile)
- Deno.writeTextFileSync(hasFile, has)
- Deno.writeTextFileSync(notHasFile, notHas)
+ const rules = extract(SITES, provider);
+ const [has, notHas] = toNewFormat(rules, ATTR);
+ const hasFile = tmpDir + "/has-rule";
+ const notHasFile = tmpDir + "/not-has-rule";
+ console.log("Write result to: " + hasFile + " , " + notHasFile);
+ Deno.writeTextFileSync(hasFile, has);
+ Deno.writeTextFileSync(notHasFile, notHas);
}
-
diff --git a/deno/tools/manage-vm.ts b/deno/tools/manage-vm.ts
index a1388b1..bb985ce 100644
--- a/deno/tools/manage-vm.ts
+++ b/deno/tools/manage-vm.ts
@@ -1,9 +1,8 @@
-import os from "node:os"
+import os from "node:os";
import { join } from "@std/path";
// @ts-types="npm:@types/yargs"
import yargs from "yargs";
-
type ArchAliasMap = { [name: string]: string[] };
const arches = {
x86_64: ["x86_64", "amd64"],
@@ -13,9 +12,7 @@ type Arch = keyof typeof arches;
type GeneralArch = (typeof arches)[Arch][number];
function normalizeArch(generalName: GeneralArch): Arch {
- for (const [name, aliases] of Object.entries(
- arches as ArchAliasMap,
- )) {
+ for (const [name, aliases] of Object.entries(arches as ArchAliasMap)) {
if (aliases.includes(generalName)) return name as Arch;
}
throw Error("Unknown architecture name.");
diff --git a/deno/tools/template.ts b/deno/tools/template.ts
index 0b043a1..1b67eb8 100644
--- a/deno/tools/template.ts
+++ b/deno/tools/template.ts
@@ -73,7 +73,9 @@ export class TemplateDir {
generate(vars: Record<string, string>, generatedDir?: string) {
console.log(
- `Generating, template dir: ${this.dir}, generated dir: ${generatedDir ?? "[dry-run]"}:`,
+ `Generating, template dir: ${this.dir}, generated dir: ${
+ generatedDir ?? "[dry-run]"
+ }:`,
);
const undefinedVars = this.allNeededVars().filter((v) => !(v in vars));