From 482b072d6e4df224c41124e8928d3cb2b3aa6228 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Fri, 9 Aug 2024 20:08:56 -0700 Subject: [PATCH] migrate to module scripts --- build.js | 8 ++++---- generateMetadataJsonFromScriptResources.js | 4 ++-- index.js | 12 ++++++------ package.json | 1 + verify.js | 10 +++++----- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/build.js b/build.js index 83817c65..964ad660 100644 --- a/build.js +++ b/build.js @@ -1,6 +1,6 @@ -const fs = require('fs') -const path = require('path') +import fs from 'fs' +import path from 'path' -const { readResources } = require('.') +import { readResources } from './index.js' -fs.writeFileSync(path.join(__dirname, 'dist', 'resources.json'), JSON.stringify(readResources())) +fs.writeFileSync(path.join(import.meta.dirname, 'dist', 'resources.json'), JSON.stringify(readResources())) diff --git a/generateMetadataJsonFromScriptResources.js b/generateMetadataJsonFromScriptResources.js index 7dc9e4f9..a99aa76d 100644 --- a/generateMetadataJsonFromScriptResources.js +++ b/generateMetadataJsonFromScriptResources.js @@ -4,8 +4,8 @@ * For instance - brave-fix script needs the "bf" alias */ -const fs = require("fs"); -const path = require("path"); +import fs from "fs"; +import path from "path"; const metadataJsonFile = "metadata.json"; const resourcesDir = "resources"; diff --git a/index.js b/index.js index 417066a5..37bd2c22 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,17 @@ -const fs = require('fs') -const path = require('path') +import fs from 'fs' +import path from 'path' -const metadata = require('./metadata.json') +import metadata from './metadata.json' with { type: "json" } const readResources = (() => { return metadata.map(item => ({ name: item.name, aliases: item.aliases, kind: item.kind, - content: fs.readFileSync(path.join(__dirname, 'resources', item.resourcePath)).toString('base64') + content: fs.readFileSync(path.join(import.meta.dirname, 'resources', item.resourcePath)).toString('base64') })) }) -const listCatalog = require('./filter_lists/list_catalog.json') +import listCatalog from './filter_lists/list_catalog.json' with { type: "json" } -module.exports = { listCatalog, readResources } +export { listCatalog, readResources } diff --git a/package.json b/package.json index 6da3428b..4b437bea 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "Custom resources and scriptlets used for Brave's adblocker", "main": "index.js", + "type": "module", "scripts": { "build": "node build", "test": "node verify", diff --git a/verify.js b/verify.js index d897f3cf..68d75ac1 100644 --- a/verify.js +++ b/verify.js @@ -1,9 +1,9 @@ -const { readResources, listCatalog } = require('.') +import { readResources, listCatalog } from './index.js' -const assert = require('node:assert') -const crypto = require('crypto') -const test = require('node:test') -const { Engine, FilterFormat, FilterSet } = require('adblock-rs') +import assert from 'node:assert' +import crypto from 'crypto' +import test from 'node:test' +import { Engine, FilterFormat, FilterSet } from 'adblock-rs' const getIDFromBase64PublicKey = (key) => { const hash = crypto.createHash('sha256')