[Scripts > Strip] Strip HTML

This commit is contained in:
Ducko 2022-02-13 21:48:29 +00:00
parent c02460a7c2
commit c9d2d435fe

View file

@ -1,34 +1,41 @@
const fs = require('fs'); const { writeFileSync, readFileSync, readdirSync } = require('fs');
const { join } = require('path'); const { join } = require('path');
const stripCode = (code) => code const stripCode = (code) => code
.replace(/(^| )\/\/.*$/gm, '') .replace(/(^| )\/\/.*$/gm, '')
.replaceAll('const ', 'const#') .replaceAll('const ', 'const~')
.replaceAll('let ', 'let#') .replaceAll('let ', 'let~')
.replaceAll('var ', 'var#') .replaceAll('var ', 'var~')
.replaceAll('class ', 'class#') .replaceAll('class ', 'class~')
.replaceAll('get ', 'get#') .replaceAll('get ', 'get~')
.replaceAll('delete ', 'delete#') .replaceAll('delete ', 'delete~')
.replaceAll(' extends ', '#extends#') .replaceAll(' extends ', '~extends~')
.replaceAll('typeof ', 'typeof#') .replaceAll('typeof ', 'typeof~')
.replaceAll(' of ', '#of#') .replaceAll(' of ', '~of~')
.replaceAll(' in ', '#in#') .replaceAll(' in ', '~in~')
.replaceAll('case ', 'case#') .replaceAll('case ', 'case~')
.replaceAll('await ', 'await#') .replaceAll('await ', 'await~')
.replaceAll('new ', 'new#') .replaceAll('new ', 'new~')
.replaceAll('return ', 'return#') .replaceAll('return ', 'return~')
.replaceAll('function ', 'function#') .replaceAll('function ', 'function~')
.replaceAll('void ', 'void#') .replaceAll('void ', 'void~')
.replaceAll('throw ', 'throw#') .replaceAll('throw ', 'throw~')
.replaceAll('async ', 'async#') .replaceAll('async ', 'async~')
.replaceAll('else ', 'else#') .replaceAll('else ', 'else~')
.replaceAll('false', '!!0') .replaceAll('false', '!!0')
.replaceAll('true', '!0') .replaceAll('true', '!0')
.replace(/((['"`])[\s\S]*?\2)|[ \n]/g, (_, g1) => g1 || '') .replace(/((['"`])[\s\S]*?\2)|[ \n]/g, (_, g1) => g1 || '')
.replaceAll('#', ' ') .replaceAll('~', ' ')
.replaceAll('? ?', '??'); .replaceAll('? ?', '??');
const stripJs = (jsPath) => fs.writeFileSync(jsPath, stripCode(fs.readFileSync(jsPath, 'utf8'))); const fixHtml = (code) => code
.replaceAll(' loop', '~loop')
.replaceAll(' autoplay', '~autoplay')
.replaceAll(' src', '~src')
.replaceAll(' id', '~id');
const stripJs = (path) => writeFileSync(path, stripCode(readFileSync(path, 'utf8')));
const stripHtml = (path) => writeFileSync(path, stripCode(fixHtml(readFileSync(path, 'utf8'))));
const minJson = (data) => { const minJson = (data) => {
if (data.description) delete data.description; if (data.description) delete data.description;
@ -36,14 +43,15 @@ const minJson = (data) => {
return data; return data;
}; };
const stripJson = (path) => fs.writeFileSync(path, JSON.stringify(minJson(JSON.parse(fs.readFileSync(path, 'utf8'))))); const stripJson = (path) => writeFileSync(path, JSON.stringify(minJson(JSON.parse(readFileSync(path, 'utf8')))));
const tree = (dirPath) => fs.readdirSync(dirPath).forEach((x) => { const tree = (dirPath) => readdirSync(dirPath).forEach((x) => {
const path = join(dirPath, x); const path = join(dirPath, x);
console.log(path); console.log(path);
if (x.endsWith('.js')) return stripJs(path); if (x.endsWith('.js')) return stripJs(path);
if (x.endsWith('.json')) return stripJson(path); if (x.endsWith('.json')) return stripJson(path);
if (x.endsWith('.html')) return stripHtml(path);
if (!x.includes('.')) return tree(path); if (!x.includes('.')) return tree(path);
}); });