Skip to content

Commit

Permalink
chore: grules v0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Sep 27, 2024
1 parent 5dd77b6 commit 6aada79
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 99 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.1",
"tinybench": "^2.9.0",
"typescript": ">=5.5.4"
"typescript": ">=5.6.2"
},
"repository": {
"type": "git",
Expand Down
124 changes: 59 additions & 65 deletions src/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-await-in-loop, require-unicode-regexp */

const escapeRegExp = /["&'<=>]/g;

const escapeFunction = (string) => {
Expand Down Expand Up @@ -35,29 +37,23 @@ const escapeFunction = (string) => {
};

/**
* The `html` function is designed to tag template literals and automatically escape their expressions.
* @param {TemplateStringsArray} literals Tagged template literals.
* @param {...any} expressions Expressions to interpolate.
* @returns {string} The processed HTML string.
* @param {TemplateStringsArray} literals literals
* @param {...any} expressions expressions
* @returns {string} string
*/
export const html = (literals, ...expressions) => {
let accumulator = "";

for (let i = 0; i !== expressions.length; ++i) {
let literal = literals.raw[i];
let string =
typeof expressions[i] === "string"
? expressions[i]
: expressions[i] == null
? ""
: Array.isArray(expressions[i])
? expressions[i].join("")
: `${expressions[i]}`;
let string = Array.isArray(expressions[i])
? expressions[i].join("")
: String(expressions[i] ?? "");

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else {
string &&= escapeFunction(string);
string = escapeFunction(string);
}

accumulator += literal + string;
Expand All @@ -67,11 +63,10 @@ export const html = (literals, ...expressions) => {
};

/**
* The `htmlGenerator` function acts as the generator version of the `html` function.
* @param {TemplateStringsArray} literals Tagged template literals.
* @param {...any} expressions Expressions to interpolate.
* @yields Processed HTML strings.
* @returns {Generator<string, void, void>} The HTML generator.
* @param {TemplateStringsArray} literals literals
* @param {...any} expressions expressions
* @yields {string} string
* @returns {Generator<string, void, void>} Generator<string, void, void>
*/
export const htmlGenerator = function* (literals, ...expressions) {
for (let i = 0; i !== expressions.length; ++i) {
Expand Down Expand Up @@ -106,50 +101,50 @@ export const htmlGenerator = function* (literals, ...expressions) {

if (expression[Symbol.iterator]) {
for (expression of expression) {
if (typeof expression === "string") {
string = expression;
} else {
if (expression == null) {
continue;
}

string = `${expression}`;
if (expression == null) {
continue;
}

if (string) {
if (!isRaw) {
string = escapeFunction(string);
}
string = String(expression);

if (!string) {
continue;
}

yield string;
if (!isRaw) {
string = escapeFunction(string);
}

yield string;
}

continue;
}

string = `${expression}`;
string = String(expression);
}

if (string) {
if (!isRaw) {
string = escapeFunction(string);
}
if (!string) {
continue;
}

yield string;
if (!isRaw) {
string = escapeFunction(string);
}

yield string;
}

continue;
}

string = `${expression}`;
string = String(expression);
}

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else {
string &&= escapeFunction(string);
string = escapeFunction(string);
}

if (literal || string) {
Expand All @@ -163,11 +158,10 @@ export const htmlGenerator = function* (literals, ...expressions) {
};

/**
* This version of HTML generator should be preferred for asynchronous and streaming use cases.
* @param {TemplateStringsArray} literals Tagged template literals.
* @param {...any} expressions Expressions to interpolate.
* @yields Processed HTML strings.
* @returns {AsyncGenerator<string, void, void>} The HTML generator.
* @param {TemplateStringsArray} literals literals
* @param {...any} expressions expressions
* @yields {string} string
* @returns {AsyncGenerator<string, void, void>} AsyncGenerator<string, void, void>
*/
export const htmlAsyncGenerator = async function* (literals, ...expressions) {
for (let i = 0; i !== expressions.length; ++i) {
Expand Down Expand Up @@ -205,50 +199,50 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
expression[Symbol.asyncIterator]
) {
for await (expression of expression) {
if (typeof expression === "string") {
string = expression;
} else {
if (expression == null) {
continue;
}

string = `${expression}`;
if (expression == null) {
continue;
}

if (string) {
if (!isRaw) {
string = escapeFunction(string);
}
string = String(expression);

yield string;
if (!string) {
continue;
}

if (!isRaw) {
string = escapeFunction(string);
}

yield string;
}

continue;
}

string = `${expression}`;
string = String(expression);
}

if (string) {
if (!isRaw) {
string = escapeFunction(string);
}
if (!string) {
continue;
}

yield string;
if (!isRaw) {
string = escapeFunction(string);
}

yield string;
}

continue;
}

string = `${expression}`;
string = String(expression);
}

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else {
string &&= escapeFunction(string);
string = escapeFunction(string);
}

if (literal || string) {
Expand Down
4 changes: 2 additions & 2 deletions src/includeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { readFileSync } from "node:fs";
const cache = new Map();

/**
* @param {string} path The path to the file to render.
* @returns {string} The content of the file.
* @param {string} path path
* @returns {string} string
*/
export const includeFile = (path) => {
let file = cache.get(path);
Expand Down
Loading

0 comments on commit 6aada79

Please sign in to comment.