Skip to content

Commit

Permalink
chore: simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Sep 27, 2024
1 parent 5dd77b6 commit 82ad351
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 103 deletions.
8 changes: 0 additions & 8 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Gürgün Dayıoğlu
Copyright (c) 2023 Gürgün Dayıoğlu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-unused-vars */

import { html } from "../src/index.js";
import { Bench } from "tinybench";
import { writeFileSync } from "node:fs";
import { Buffer } from "node:buffer";

let result = "";

const bench = new Bench({ time: 500 });

bench.add("simple HTML formatting", () => {
Expand Down Expand Up @@ -112,7 +114,7 @@ await bench.warmup();
await bench.run();

const table = bench.table();
console.table(table);
globalThis.console.table(table);

writeFileSync(
"bench/results.json",
Expand Down
6 changes: 0 additions & 6 deletions bin/example/.eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion bin/example/assets/script.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.warn("Hello World!");
globalThis.console.warn("Hello World!");
4 changes: 2 additions & 2 deletions bin/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"build": "ghtml --roots=assets/ --refs=routes/ --prefix=/p/assets/"
},
"dependencies": {
"@fastify/static": "^7.0.1",
"fastify": "^4.26.1",
"@fastify/static": "^8.0.1",
"fastify": "^5.0.0",
"ghtml": "file:../../"
}
}
2 changes: 2 additions & 0 deletions bin/example/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable n/no-missing-import, require-await */

import { html } from "ghtml";

export default async (fastify) => {
Expand Down
6 changes: 4 additions & 2 deletions bin/example/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable n/no-missing-import */

import Fastify from "fastify";

const fastify = Fastify();

// Plugins
await fastify.register(import("@fastify/static"), {
root: new URL("assets/", import.meta.url).pathname,
root: new globalThis.URL("assets/", import.meta.url).pathname,
prefix: "/p/assets/",
wildcard: false,
index: false,
Expand All @@ -20,5 +22,5 @@ fastify.listen({ port: 5050 }, (err, address) => {
throw err;
}

console.warn(`Server listening at ${address}`);
globalThis.console.warn(`Server listening at ${address}`);
});
16 changes: 9 additions & 7 deletions bin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const parseArguments = (args) => {
}

if (!roots || !refs) {
console.error(
globalThis.console.error(
'Usage: npx ghtml --roots="base/path/to/scan/assets/1/,base/path/to/scan/assets/2/" --refs="views/path/to/append/hashes/1/,views/path/to/append/hashes/2/" [--prefix="/optional/prefix/"]',
);
process.exit(1);
Expand All @@ -31,20 +31,22 @@ const main = async () => {
const { roots, refs, prefix } = parseArguments(process.argv.slice(2));

try {
console.warn(`Generating hashes and updating file paths...`);
console.warn(`Scanning files in: ${roots}`);
console.warn(`Updating files in: ${refs}`);
console.warn(`Using prefix: ${prefix}`);
globalThis.console.warn(`Generating hashes and updating file paths...`);
globalThis.console.warn(`Scanning files in: ${roots}`);
globalThis.console.warn(`Updating files in: ${refs}`);
globalThis.console.warn(`Using prefix: ${prefix}`);

await generateHashesAndReplace({
roots,
refs,
prefix,
});

console.warn("Hash generation and file updates completed successfully.");
globalThis.console.warn(
"Hash generation and file updates completed successfully.",
);
} catch (error) {
console.error(`Error occurred: ${error.message}`);
globalThis.console.error(`Error occurred: ${error.message}`);
process.exit(1);
}
};
Expand Down
6 changes: 4 additions & 2 deletions bin/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-await-in-loop */

import { Glob } from "glob";
import { createHash } from "node:crypto";
import { readFile, writeFile } from "node:fs/promises";
Expand Down Expand Up @@ -44,12 +46,12 @@ const updateFilePathsWithHashes = async (
for (const [path, hash] of fileHashes) {
const fullPath = prefix + path;
const escapedPath = fullPath.replace(
/[$()*+.?[\\\]^{|}]/g,
/[$()*+.?[\\\]^{|}]/gu,
String.raw`\$&`,
);
const regex = new RegExp(
`(?<path>${escapedPath})(\\?(?<queryString>[^#"'\`]*))?`,
"g",
"gu",
);

content = content.replace(
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import grules from "grules";

export default [...grules];
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"c8": "^10.1.2",
"grules": "^0.23.3",
"grules": "^0.24.2",
"tinybench": "^2.9.0",
"typescript": ">=5.5.4"
"typescript": ">=5.6.2"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 82ad351

Please sign in to comment.