OpenAsar/scripts/strip.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-02-13 21:48:29 +00:00
const { writeFileSync, readFileSync, readdirSync } = require('fs');
2022-02-06 21:18:32 +00:00
const { join } = require('path');
const stripCode = (code) => code
.replace(/(^| )\/\/.*$/gm, '')
2022-02-13 21:48:29 +00:00
.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('case ', 'case~')
.replaceAll('await ', 'await~')
.replaceAll('new ', 'new~')
.replaceAll('return ', 'return~')
.replaceAll('function ', 'function~')
.replaceAll('void ', 'void~')
.replaceAll('throw ', 'throw~')
.replaceAll('async ', 'async~')
.replaceAll('else ', 'else~')
2022-02-08 14:10:25 +00:00
.replaceAll('false', '!!0')
.replaceAll('true', '!0')
2022-02-06 21:18:32 +00:00
.replace(/((['"`])[\s\S]*?\2)|[ \n]/g, (_, g1) => g1 || '')
2022-02-13 21:48:29 +00:00
.replaceAll('~', ' ')
2022-02-06 21:18:32 +00:00
.replaceAll('? ?', '??');
2022-02-13 21:48:29 +00:00
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) => {
if (data.description) delete data.description;
return data;
2022-02-06 21:18:32 +00:00
};
2022-02-13 21:48:29 +00:00
const stripJson = (path) => writeFileSync(path, JSON.stringify(minJson(JSON.parse(readFileSync(path, 'utf8')))));
2022-02-13 21:48:29 +00:00
const tree = (dirPath) => readdirSync(dirPath).forEach((x) => {
const path = join(dirPath, x);
console.log(path);
if (x.endsWith('.js')) return stripJs(path);
if (x.endsWith('.json')) return stripJson(path);
2022-02-13 21:48:29 +00:00
if (x.endsWith('.html')) return stripHtml(path);
if (!x.includes('.')) return tree(path);
2022-02-06 21:18:32 +00:00
});
tree(join(__dirname, '..', 'src'));