From 07e50cd47df5ce1f7186a404df9c7a5eee658922 Mon Sep 17 00:00:00 2001 From: Oj Date: Sun, 6 Feb 2022 21:18:32 +0000 Subject: [PATCH] [Scripts > Strip] Rewrite --- .github/workflows/nightly.yml | 2 +- scripts/strip.js | 38 +++++++++++++++++++++++++++++++++++ scripts/strip.sh | 4 ---- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 scripts/strip.js delete mode 100644 scripts/strip.sh diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index c378378..8006e07 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -28,7 +28,7 @@ jobs: npm i -g asar bash scripts/injectPolyfills.sh sed -i -e "s/nightly/nightly-$(git rev-parse HEAD | cut -c 1-7)/" src/index.js - bash scripts/strip.sh + node scripts/strip.js npx asar pack src app.asar - name: Upload artifact diff --git a/scripts/strip.js b/scripts/strip.js new file mode 100644 index 0000000..1561a5f --- /dev/null +++ b/scripts/strip.js @@ -0,0 +1,38 @@ +const fs = require('fs'); +const { join } = require('path'); + +const stripCode = (code) => code + .replace(/(^| )\/\/.*$/gm, '') + .replaceAll('const ', 'const#') + .replaceAll('let ', 'let#') + .replaceAll('var ', 'var#') + .replaceAll('class ', 'class#') + .replaceAll('get ', 'get#') + .replaceAll('delete ', 'delete#') + .replaceAll(' extends ', '#extends#') + .replaceAll('typeof ', 'typeof#') + .replaceAll(' of ', '#of#') + .replaceAll(' in ', '#in#') + .replaceAll('await ', 'await#') + .replaceAll('new ', 'new#') + .replaceAll('return ', 'return#') + .replaceAll('function ', 'function#') + .replaceAll('void ', 'void#') + .replaceAll('throw ', 'throw#') + .replaceAll('async ', 'async#') + .replaceAll('else ', 'else#') + .replace(/((['"`])[\s\S]*?\2)|[ \n]/g, (_, g1) => g1 || '') + .replaceAll('#', ' ') + .replaceAll('? ?', '??'); + +const stripFile = (jsPath) => { + console.log(jsPath); + fs.writeFileSync(jsPath, stripCode(fs.readFileSync(jsPath, 'utf8'))); +}; + +const tree = (dirPath) => fs.readdirSync(dirPath).forEach((x) => { + if (x.endsWith('.js')) return stripFile(join(dirPath, x)); + if (!x.includes('.')) return tree(join(dirPath, x)); +}); + +tree(join(__dirname, '..', 'src')); \ No newline at end of file diff --git a/scripts/strip.sh b/scripts/strip.sh deleted file mode 100644 index bd49dfe..0000000 --- a/scripts/strip.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -find src -type f -name "*.js" -exec sed -i -E 's@[[:blank:]]*([^:]//).*@@;T;/./!d' {} + # Remove // comments (ignore URLs) -find src -type f -name "*.js" -exec sed -i '/^[[:space:]]*$/d' {} + # Remove whitespace \ No newline at end of file