All files / src/core generator.ts

100% Statements 24/24
100% Branches 7/7
100% Functions 1/1
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x 1x                 8x 8x 8x 8x 8x 8x   8x 4x 4x 4x 8x 1x 1x 1x   3x 3x   3x 3x 3x 3x   3x 3x  
import { promises as fs } from "node:fs";
import { join } from "node:path";
import * as core from "@actions/core";
import bytes from "bytes";
import { type Path, glob } from "glob";
import type { ActionInputs } from "src/utils/inputs";
import color from "../utils/color";
import { truncate } from "../utils/truncate";
import { renderHTML } from "./html";
 
export async function generate(root: string, dir: Path, inputs: ActionInputs) {
	const files = await glob(join(dir.fullpath(), "*"), {
		ignore: inputs.ignore.map((i) => join(root, i)),
		dot: inputs.showHiddenFiles,
		withFileTypes: true,
	});
 
	if (files.length === 0) {
		core.debug(color.yellow(`[Skip] No targets found in: ${dir.fullpath()}`));
		return;
	}
	if (!inputs.override && files.some((file) => file.name === "index.html")) {
		core.debug(color.yellow(`[Skip] 'index.html' already exists in: ${dir.fullpath()}`));
		return;
	}
 
	core.debug(color.green("Generating 'index.html' at: ") + color.blue(dir.fullpath()));
	core.debug(`- Found ${files.length} target(s).`);
 
	const html = await renderHTML({ root, dir, files, inputs });
	await fs.writeFile(join(dir.fullpath(), "index.html"), html, "utf-8");
	core.info(color.green(`Successfully generated 'index.html' at: `) + color.blue(dir.fullpath()));
	core.info(`- File size: ${bytes(html.length)}`);
 
	core.debug(`- Generated HTML content is: ${color.magenta(truncate(html))}`);
}