From 02f711850368b355526c0863b72fc596b3e2d44d Mon Sep 17 00:00:00 2001 From: PiQuark6046 Date: Fri, 14 Jun 2024 06:12:39 +0000 Subject: [PATCH 01/15] Add sort.yml --- .github/workflows/sort.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/sort.yml diff --git a/.github/workflows/sort.yml b/.github/workflows/sort.yml new file mode 100644 index 0000000..e378589 --- /dev/null +++ b/.github/workflows/sort.yml @@ -0,0 +1,31 @@ +name: Sort HttpsExclusions + +on: + push: + branches: + - master + paths: + - "**/*.txt" + pull_request: + branches: + - master + paths: + - "**/*.txt" + +jobs: + bank: + name: Check that bank.txt is sorted + runs-on: ubuntu-latest + steps: + - name: Check out to repository + uses: actions/checkout@v4 + - name: Run sort + run: sort exclusions/banks.txt -o exclusions/banks.txt + shell: bash + - name: Remove last empty line + run: truncate -s-1 exclusions/banks.txt + shell: bash + - name: Run git diff to detect the ordering issue + run: | + ! git diff --stat | grep -E "[A-Za-z0-9-]+\.txt" + shell: bash \ No newline at end of file From 0f35754f51e0cb1f82fa72693558daacc7865d42 Mon Sep 17 00:00:00 2001 From: PiQuark6046 Date: Fri, 14 Jun 2024 09:26:51 +0000 Subject: [PATCH 02/15] comments are sorted and sort 9.4 issue --- .github/workflows/sort.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sort.yml b/.github/workflows/sort.yml index e378589..4dd649c 100644 --- a/.github/workflows/sort.yml +++ b/.github/workflows/sort.yml @@ -20,7 +20,7 @@ jobs: - name: Check out to repository uses: actions/checkout@v4 - name: Run sort - run: sort exclusions/banks.txt -o exclusions/banks.txt + run: LC_ALL=C sort exclusions/banks.txt -o exclusions/banks.tx shell: bash - name: Remove last empty line run: truncate -s-1 exclusions/banks.txt From 083c7d6ceff45214e1d82fba6df692ff4c6e5281 Mon Sep 17 00:00:00 2001 From: PiQuark6046 Date: Fri, 14 Jun 2024 09:53:35 +0000 Subject: [PATCH 03/15] fix typo --- .github/workflows/sort.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sort.yml b/.github/workflows/sort.yml index 4dd649c..acd6d04 100644 --- a/.github/workflows/sort.yml +++ b/.github/workflows/sort.yml @@ -20,7 +20,7 @@ jobs: - name: Check out to repository uses: actions/checkout@v4 - name: Run sort - run: LC_ALL=C sort exclusions/banks.txt -o exclusions/banks.tx + run: LC_ALL=C sort exclusions/banks.txt -o exclusions/banks.txt shell: bash - name: Remove last empty line run: truncate -s-1 exclusions/banks.txt From 4a48b2e18bc9305dc76ddff6ff624a612501a67d Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Fri, 14 Jun 2024 11:42:47 +0000 Subject: [PATCH 04/15] update toolchain --- .gitignore | 3 ++- package.json | 10 +++++++++- tools/banks.mts | 12 ++++++++++++ tsconfig.json | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tools/banks.mts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 4582bd3..392d8e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist -.DS_Store \ No newline at end of file +.DS_Store +node_modules \ No newline at end of file diff --git a/package.json b/package.json index 7795ae9..67414cb 100644 --- a/package.json +++ b/package.json @@ -5,5 +5,13 @@ "main": "index.js", "repository": "git@github.com:AdguardTeam/HttpsExclusions.git", "author": "AdGuard", - "license": "MIT" + "license": "MIT", + "scripts": { + "lint:banks": "tsx tools/banks.mts" + }, + "devDependencies": { + "@types/node": "^20.14.2", + "tsx": "^4.15.4", + "typescript": "^5.4.5" + } } diff --git a/tools/banks.mts b/tools/banks.mts new file mode 100644 index 0000000..3b357f9 --- /dev/null +++ b/tools/banks.mts @@ -0,0 +1,12 @@ +import * as Fs from 'node:fs' + +const Banks = Fs.readFileSync('exclusions/banks.txt').toString().split('\n') +const Sorted = Banks.toSorted((A, B) => { + if (A.startsWith('//')) { + return 0 + } + // Sort by alphabetical order + return A.localeCompare(B) +}) + +process.exit(JSON.stringify(Banks) === JSON.stringify(Sorted) ? 0 : 1) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..2441d46 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "lib": ["ES2023", "ES2023.Array", "ES2023.Collection"], + "target": "ES2022", + "moduleResolution": "NodeNext", + "removeComments": false, + "alwaysStrict": true, + "skipLibCheck": true + }, + "include": [ + "tools/**/*.mts" + ] +} \ No newline at end of file From 18c312587e27e68c317545cc581872458b2722c3 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Fri, 14 Jun 2024 11:43:07 +0000 Subject: [PATCH 05/15] update ci --- .github/workflows/sort.yml | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/.github/workflows/sort.yml b/.github/workflows/sort.yml index acd6d04..a4e19c2 100644 --- a/.github/workflows/sort.yml +++ b/.github/workflows/sort.yml @@ -17,15 +17,12 @@ jobs: name: Check that bank.txt is sorted runs-on: ubuntu-latest steps: + - name: Set up Node.js LTS + uses: actions/setup-node@v4 + with: + node-version: 'lts/*' - name: Check out to repository uses: actions/checkout@v4 - - name: Run sort - run: LC_ALL=C sort exclusions/banks.txt -o exclusions/banks.txt - shell: bash - - name: Remove last empty line - run: truncate -s-1 exclusions/banks.txt - shell: bash - - name: Run git diff to detect the ordering issue - run: | - ! git diff --stat | grep -E "[A-Za-z0-9-]+\.txt" + - name: Run lint:banks + run: npm run lint:banks shell: bash \ No newline at end of file From 978be5ff2750d5f2553cbd4da2780c9cbc31207c Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Fri, 14 Jun 2024 11:44:07 +0000 Subject: [PATCH 06/15] rename --- .github/workflows/{sort.yml => lint.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{sort.yml => lint.yml} (94%) diff --git a/.github/workflows/sort.yml b/.github/workflows/lint.yml similarity index 94% rename from .github/workflows/sort.yml rename to .github/workflows/lint.yml index a4e19c2..810a5f3 100644 --- a/.github/workflows/sort.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: Sort HttpsExclusions +name: Lint HttpsExclusions on: push: From d6b1a47c25e0ff019a3d7f434b063747d69de1c1 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 09:39:47 +0000 Subject: [PATCH 07/15] Use Nodejs 20 --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 810a5f3..4f8a118 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,7 +20,7 @@ jobs: - name: Set up Node.js LTS uses: actions/setup-node@v4 with: - node-version: 'lts/*' + node-version: '20.x' - name: Check out to repository uses: actions/checkout@v4 - name: Run lint:banks From 50fa2ee1741de0f8f27120d1a609bfd03bd1cd4e Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 09:40:43 +0000 Subject: [PATCH 08/15] follow scripthunter7 suggestion Co-authored-by: scripthunter7 --- tools/banks.mts | 77 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/tools/banks.mts b/tools/banks.mts index 3b357f9..d42c643 100644 --- a/tools/banks.mts +++ b/tools/banks.mts @@ -1,12 +1,65 @@ -import * as Fs from 'node:fs' - -const Banks = Fs.readFileSync('exclusions/banks.txt').toString().split('\n') -const Sorted = Banks.toSorted((A, B) => { - if (A.startsWith('//')) { - return 0 - } - // Sort by alphabetical order - return A.localeCompare(B) -}) - -process.exit(JSON.stringify(Banks) === JSON.stringify(Sorted) ? 0 : 1) \ No newline at end of file +import { readFile, stat } from 'node:fs/promises'; + +const RE_NL = /\r?\n/; +const COMMENT_MARKER = '//'; + +const isFileExists = async (filePath: string): Promise => { + return stat(filePath) + .then((stats) => stats.isFile()) + .catch(() => false); +}; + +const readRules = async (filePath: string): Promise => { + const fileContent = await readFile(filePath, { encoding: 'utf-8' }); + + return fileContent + .split(RE_NL) + .map((line) => line.trim()) + .filter((line) => line.length > 0 && !line.startsWith(COMMENT_MARKER)); +}; + +const isSortedAlphabetically = (arr: string[]): boolean => { + for (let i = 0; i < arr.length - 1; i += 1) { + if (arr[i] > arr[i + 1]) { + return false; + } + } + return true; +}; + +const getErrorMessage = (err: unknown): string => { + if (err instanceof Error) { + return err.message; + } + + return 'Unknown error'; +}; + +const isNonEmptyString = (value: unknown): value is string => { + return typeof value === 'string' && value.length > 0; +}; + +const main = async () => { + const filePath = process.argv[2]; + + try { + if (!isNonEmptyString(filePath)) { + throw new Error('File path is not provided'); + } + + if (!(await isFileExists(filePath))) { + throw new Error(`File ${filePath} does not exist`); + } + + const rules = await readRules(filePath); + + if (!isSortedAlphabetically(rules)) { + throw new Error(`Rules in ${filePath} is not sorted alphabetically`); + } + } catch (err) { + console.error(getErrorMessage(err)); + process.exit(1); + } +}; + +main(); \ No newline at end of file From efd3492dc720b652b96351a288be08846c18db1d Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 09:48:02 +0000 Subject: [PATCH 09/15] convert to cjs and rename --- package.json | 2 +- tools/{banks.mts => check.ts} | 2 +- tsconfig.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename tools/{banks.mts => check.ts} (96%) diff --git a/package.json b/package.json index 67414cb..9f5c5ca 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "author": "AdGuard", "license": "MIT", "scripts": { - "lint:banks": "tsx tools/banks.mts" + "lint": "tsx tools/check.ts exclusions/banks.txt" }, "devDependencies": { "@types/node": "^20.14.2", diff --git a/tools/banks.mts b/tools/check.ts similarity index 96% rename from tools/banks.mts rename to tools/check.ts index d42c643..6af7909 100644 --- a/tools/banks.mts +++ b/tools/check.ts @@ -1,4 +1,4 @@ -import { readFile, stat } from 'node:fs/promises'; +const { readFile, stat } = require('fs').promises; const RE_NL = /\r?\n/; const COMMENT_MARKER = '//'; diff --git a/tsconfig.json b/tsconfig.json index 2441d46..698a192 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,6 @@ "skipLibCheck": true }, "include": [ - "tools/**/*.mts" + "tools/**/*.ts" ] } \ No newline at end of file From 632898a999a5eee091d56548e40bfff2a016a364 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 10:04:37 +0000 Subject: [PATCH 10/15] create package-lock.json and caching --- .github/workflows/lint.yml | 3 + package-lock.json | 140 +++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 package-lock.json diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4f8a118..dce262d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -21,6 +21,9 @@ jobs: uses: actions/setup-node@v4 with: node-version: '20.x' + cache: npm + - name: Install dependencies + run: npm ci - name: Check out to repository uses: actions/checkout@v4 - name: Run lint:banks diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..3e00c5a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,140 @@ +{ + "name": "https-exclusions", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "https-exclusions", + "version": "1.0.0", + "license": "MIT", + "devDependencies": { + "@types/node": "^20.14.2", + "tsx": "^4.15.4", + "typescript": "^5.4.5" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.21.5.tgz", + "integrity": "sha512-1rYdTpyv03iycF1+BhzrzQJCdOuAOtaqHTWJZCWvijKD2N5Xu0TtVC8/+1faWqcP9iBCWOmjmhoH94dH82BxPQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@types/node": { + "version": "20.14.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.2.tgz", + "integrity": "sha512-xyu6WAMVwv6AKFLB+e/7ySZVr/0zLCzOa7rSpq6jNwpqOrUbcACDWC+53d4n2QHOnDou0fbIsg8wZu/sxrnI4Q==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/esbuild": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz", + "integrity": "sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.21.5", + "@esbuild/android-arm": "0.21.5", + "@esbuild/android-arm64": "0.21.5", + "@esbuild/android-x64": "0.21.5", + "@esbuild/darwin-arm64": "0.21.5", + "@esbuild/darwin-x64": "0.21.5", + "@esbuild/freebsd-arm64": "0.21.5", + "@esbuild/freebsd-x64": "0.21.5", + "@esbuild/linux-arm": "0.21.5", + "@esbuild/linux-arm64": "0.21.5", + "@esbuild/linux-ia32": "0.21.5", + "@esbuild/linux-loong64": "0.21.5", + "@esbuild/linux-mips64el": "0.21.5", + "@esbuild/linux-ppc64": "0.21.5", + "@esbuild/linux-riscv64": "0.21.5", + "@esbuild/linux-s390x": "0.21.5", + "@esbuild/linux-x64": "0.21.5", + "@esbuild/netbsd-x64": "0.21.5", + "@esbuild/openbsd-x64": "0.21.5", + "@esbuild/sunos-x64": "0.21.5", + "@esbuild/win32-arm64": "0.21.5", + "@esbuild/win32-ia32": "0.21.5", + "@esbuild/win32-x64": "0.21.5" + } + }, + "node_modules/get-tsconfig": { + "version": "4.7.5", + "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.7.5.tgz", + "integrity": "sha512-ZCuZCnlqNzjb4QprAzXKdpp/gh6KTxSJuw3IBsPnV/7fV4NxC9ckB+vPTt8w7fJA0TaSD7c55BR47JD6MEDyDw==", + "dev": true, + "dependencies": { + "resolve-pkg-maps": "^1.0.0" + }, + "funding": { + "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" + } + }, + "node_modules/resolve-pkg-maps": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz", + "integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==", + "dev": true, + "funding": { + "url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1" + } + }, + "node_modules/tsx": { + "version": "4.15.4", + "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.15.4.tgz", + "integrity": "sha512-d++FLCwJLrXaBFtRcqdPBzu6FiVOJ2j+UsvUZPtoTrnYtCGU5CEW7iHXtNZfA2fcRTvJFWPqA6SWBuB0GSva9w==", + "dev": true, + "dependencies": { + "esbuild": "~0.21.4", + "get-tsconfig": "^4.7.5" + }, + "bin": { + "tsx": "dist/cli.mjs" + }, + "engines": { + "node": ">=18.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + } + }, + "node_modules/typescript": { + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", + "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + } + } +} From 3732d9cee75f5c050728225ea708f01e9a2a5c37 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 10:05:39 +0000 Subject: [PATCH 11/15] Convert to cjs --- tools/{check.ts => check.cts} | 0 tsconfig.json | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tools/{check.ts => check.cts} (100%) diff --git a/tools/check.ts b/tools/check.cts similarity index 100% rename from tools/check.ts rename to tools/check.cts diff --git a/tsconfig.json b/tsconfig.json index 698a192..4ecea5e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,6 @@ "skipLibCheck": true }, "include": [ - "tools/**/*.ts" + "tools/**/*.cts" ] } \ No newline at end of file From a84691a56263a055646bc66e5d0a39d6ecfcfe47 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 10:21:07 +0000 Subject: [PATCH 12/15] make sure that tsconfig is commonjs --- tools/{check.cts => check.ts} | 0 tsconfig.json | 13 ++++++------- 2 files changed, 6 insertions(+), 7 deletions(-) rename tools/{check.cts => check.ts} (100%) diff --git a/tools/check.cts b/tools/check.ts similarity index 100% rename from tools/check.cts rename to tools/check.ts diff --git a/tsconfig.json b/tsconfig.json index 4ecea5e..fe90ec4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,13 @@ { "compilerOptions": { - "module": "NodeNext", - "lib": ["ES2023", "ES2023.Array", "ES2023.Collection"], - "target": "ES2022", - "moduleResolution": "NodeNext", - "removeComments": false, - "alwaysStrict": true, + "target": "es2016", + "module": "commonjs", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, "skipLibCheck": true }, "include": [ - "tools/**/*.cts" + "tools/**/*.ts" ] } \ No newline at end of file From cbb8012e4cefa8e8c13ea10b20b91eba7a712aae Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 10:26:01 +0000 Subject: [PATCH 13/15] fix typo --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index dce262d..f434f88 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -14,7 +14,7 @@ on: jobs: bank: - name: Check that bank.txt is sorted + name: Check that banks.txt is sorted runs-on: ubuntu-latest steps: - name: Set up Node.js LTS From d148401e81b27ce2e487200d68fef4b2a1a80832 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 10:26:36 +0000 Subject: [PATCH 14/15] use env --- .github/workflows/lint.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f434f88..0621a4f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,5 +1,8 @@ name: Lint HttpsExclusions +env: + NODE_VERSION: 20 + on: push: branches: @@ -20,7 +23,7 @@ jobs: - name: Set up Node.js LTS uses: actions/setup-node@v4 with: - node-version: '20.x' + node-version: ${{ env.NODE_VERSION }} cache: npm - name: Install dependencies run: npm ci From 4bfd1b88b93401f1b1999fcdbcd3e7b4a56c43a8 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Tue, 25 Jun 2024 10:28:52 +0000 Subject: [PATCH 15/15] modify a bit --- .github/workflows/lint.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0621a4f..f18cae1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -29,6 +29,5 @@ jobs: run: npm ci - name: Check out to repository uses: actions/checkout@v4 - - name: Run lint:banks - run: npm run lint:banks - shell: bash \ No newline at end of file + - name: Run lint + run: npm run lint \ No newline at end of file