I hate nodejs.

This commit is contained in:
bytedream 2022-06-14 23:39:26 +02:00
parent a9a8609cb8
commit 7a69ac83e0
3 changed files with 45 additions and 10 deletions

View File

@ -3,13 +3,14 @@
"module": "esnext", "module": "esnext",
"target": "es2019", "target": "es2019",
"removeComments": true, "removeComments": true,
"esModuleInterop": true,
"lib": [ "lib": [
"dom", "dom",
"es5", "es5",
"scripthost", "scripthost",
"es2015.collection", "es2015.collection",
"es2015.promise" "es2015.promise"
] ],
}, },
"exclude": [ "exclude": [
"../node_modules" "../node_modules"

View File

@ -9,22 +9,55 @@ const sass = require('node-sass')
const sassPluginNodeImport = require('node-sass-package-importer') const sassPluginNodeImport = require('node-sass-package-importer')
const typescript = require('typescript') const typescript = require('typescript')
// because of javascript magic, require and import cannot be used at the same time async function build_manifest() {
/*async function build_manifest() {
const manifest = JSON.parse(await fs.readFileSync('src/manifest.json')) const manifest = JSON.parse(await fs.readFileSync('src/manifest.json'))
const manifestMatches = [] manifest['version'] = process.env.npm_package_version
// because nodejs is nodejs, the simple commented out code below cannot be used.
// thus, the following bloated regexes must be used
/*const manifestMatches = []
for (const m of matches) { for (const m of matches) {
for (const domain of m.domains) { for (const domain of m.domains) {
manifestMatches.push(`*://*.${domain}/*`) manifestMatches.push(`*://*.${domain}/*`)
} }
} }
manifest['content_scripts']['matches'] = manifestMatches*/
manifest['content_scripts']['matches'] = manifestMatches manifest['content_scripts']['matches'] = []
await fs.writeFileSync('src/manifest.json', JSON.stringify(manifest)) const matchesRegex = new RegExp(/export\s+const\s+matches\s+=\s+(?<matches>\[.*?])/gms)
}*/ const matchesClassesRegex = new RegExp(/new\s+(?<class>\w+)\(\)/gms)
const matchTs = fs.readFileSync('src/match/match.ts')
const jsMatches = matchesRegex.exec(matchTs).groups.matches
let m
while ((m = matchesClassesRegex.exec(jsMatches))) {
if (m.index === matchesClassesRegex.lastIndex) {
matchesClassesRegex.lastIndex++
}
if (m.groups.class !== undefined) {
const classDomainsRegex = new RegExp('class\\s+' + m.groups.class + '.*?domains\\s*=\\s*(?<domains>\\[.*?])', 'gms')
let mm
while ((mm = classDomainsRegex.exec(matchTs))) {
if (mm.index === classDomainsRegex.lastIndex) {
classDomainsRegex.lastIndex++
}
if (mm.groups.domains !== undefined) {
const matches = []
for (const domain of JSON.parse(mm.groups.domains.replaceAll('\'', '"', -1))) {
matches.push(`*://*.${domain}/*`)
}
manifest['content_scripts'][0]['matches'] = manifest['content_scripts'][0]['matches'].concat(matches)
}
}
}
}
await fs.writeFileSync('src/manifest.json', JSON.stringify(manifest, null, 2))
}
async function build_misc() { async function build_misc() {
const files = { const files = {
@ -101,6 +134,7 @@ async function build_js() {
} }
async function build() { async function build() {
await build_manifest()
await build_misc() await build_misc()
await build_html() await build_html()
await build_css() await build_css()

View File

@ -1,6 +1,6 @@
const fs = require('fs') import * as fs from "fs";
const path = require('path') import * as path from "path";
const yazl = require('yazl') import * as yazl from "yazl";
function walkDirectory(dir, callback) { function walkDirectory(dir, callback) {
for (const file of fs.readdirSync(dir)) { for (const file of fs.readdirSync(dir)) {