Skip to content

Commit 920c136

Browse files
docs(build): Add comprehensive JSDoc documentation and implement Echo logging system
Added extensive JSDoc documentation to core build modules and functions to improve developer experience and code clarity. This update includes detailed descriptions, parameter explanations, and usage examples for Build.ts, Exec.ts, File.ts, JSON.ts, Merge.ts, Entry.ts, and related interfaces. Implemented a new `Echo` function and corresponding interface to control command output verbosity based on log levels. The `Exec` function was updated to accept an optional `Echo` callback, enabling filtered output for child processes. The `Build` function now utilizes this `Echo` function to stream `tsc` and `tsc-alias` output. Introduced `Level` type and variable definitions, which utilize ESBuild's `LogLevel` to manage build verbosity. Additionally, created `Source/Type/Level.ts` and `Source/Variable/Level.ts` to support the new logging infrastructure.
1 parent a2620c4 commit 920c136

File tree

17 files changed

+673
-10
lines changed

17 files changed

+673
-10
lines changed

Source/Class/Build.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@
33
/**
44
* @module Build
55
*
6+
* CLI command for running the build process using Commander.js.
7+
*
8+
* This module defines a command-line interface for the build system. It provides
9+
* options for specifying files to build, custom ESBuild/TypeScript configurations,
10+
* and watch mode for continuous rebuilding.
11+
*
12+
* @example
13+
* Basic usage - build all TypeScript files:
14+
* ```bash
15+
* node Class/Build.js "Source/**\/*.ts"
16+
* ```
17+
*
18+
* @example
19+
* Build with custom ESBuild configuration:
20+
* ```bash
21+
* node Class/Build.js "Source/**\/*.ts" --ESBuild ./config/esbuild.js
22+
* ```
23+
*
24+
* @example
25+
* Build with custom TypeScript configuration:
26+
* ```bash
27+
* node Class/Build.js "Source/**\/*.ts" --TypeScript ./tsconfig.build.json
28+
* ```
29+
*
30+
* @example
31+
* Enable watch mode for continuous builds:
32+
* ```bash
33+
* node Class/Build.js "Source/**\/*.ts" --Watch
34+
* ```
35+
*
36+
* @example
37+
* Combine options:
38+
* ```bash
39+
* node Class/Build.js "Index.ts" "Source/**\/*.ts" -E ./Config/ESBuild.js -T ./Config/TS.json --Watch
40+
* ```
641
*/
742
export default new (await import("commander")).Command()
843
.name("Build")

Source/Function/Build.ts

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,87 @@
11
import type { BuildOptions } from "esbuild";
22

3+
import Echo from "../Function/Echo.js";
34
import type Interface from "../Interface/Build.js";
45
import type Set from "../Interface/Build/Set.js";
56

67
/**
78
* @module Build
89
*
10+
* Main build function that processes files and performs builds using ESBuild and TypeScript.
11+
*
12+
* This function handles the complete build pipeline including:
13+
* - Expanding file patterns (globbing)
14+
* - Loading ESBuild configuration
15+
* - Applying custom ESBuild presets
16+
* - Running TypeScript compilation
17+
* - Running tsc-alias for path aliases
18+
* - Watching for changes (optional)
19+
*
20+
* @param File - An array of file patterns to build. Patterns can include wildcards (*, **).
21+
* @param Option - Optional configuration object.
22+
* @param Option.ESBuild - Path to a custom ESBuild configuration file or function.
23+
* @param Option.TypeScript - Path to TypeScript configuration file (defaults to "tsconfig.json").
24+
* @param Option.Watch - Enable watch mode to rebuild on file changes.
25+
* @param Option.Exclude - Array of file patterns to exclude from the build.
26+
*
27+
* @returns Promise<void>
28+
*
29+
* @example
30+
* Basic build:
31+
* ```typescript
32+
* import Build from "./Function/Build.js";
33+
*
34+
* await Build(["Source/**\/*.ts"]);
35+
* ```
36+
*
37+
* @example
38+
* Build with options:
39+
* ```typescript
40+
* await Build(
41+
* ["Source/**\/*.ts"],
42+
* {
43+
* ESBuild: "./Config/ESBuild.js",
44+
* TypeScript: "./tsconfig.build.json",
45+
* Watch: true,
46+
* Exclude: ["Source/**\/*.test.ts"],
47+
* }
48+
* );
49+
* ```
50+
*
51+
* @example
52+
* Custom ESBuild configuration:
53+
* ```typescript
54+
* // Config/ESBuild.js - returns BuildOptions
55+
* export default {
56+
* minify: true,
57+
* target: "es2020",
58+
* };
59+
* ```
60+
*
61+
* @example
62+
* Custom ESBuild configuration function:
63+
* ```typescript
64+
* // Config/ESBuild.js - returns function
65+
* import type BuildSetInterface from "../Interface/Build/Set.js";
66+
* import type { BuildOptions } from "esbuild";
67+
*
68+
* export default async (current: BuildOptions) => {
69+
* return {
70+
* ...current,
71+
* plugins: [...(current.plugins ?? []), myPlugin],
72+
* };
73+
* };
74+
* ```
75+
*
76+
* @example
77+
* Using the Pipe for dependency order:
78+
* ```typescript
79+
* import { Pipe } from "./Function/Build.js";
80+
*
81+
* // Pipe is populated with files in dependency order
82+
* await Build(["Index.ts", "Source/**\/*.ts"]);
83+
* console.log(Pipe); // ["Source/Module1.ts", "Source/Module2.ts", "Index.ts"]
84+
* ```
985
*/
1086
export default (async (...[File, Option]) => {
1187
for (const _File of File) {
@@ -81,9 +157,12 @@ export default (async (...[File, Option]) => {
81157
}
82158

83159
try {
84-
await Exec(`tsc -p ${Configuration.tsconfig}`);
160+
await Exec(`tsc -p ${Configuration.tsconfig}`, Echo);
85161

86-
await Exec(`tsc-alias -f -p ${Configuration.tsconfig}`);
162+
await Exec(
163+
`tsc-alias -f -p ${Configuration.tsconfig}`,
164+
Echo,
165+
);
87166
} catch (_Error) {
88167
console.error(_Error);
89168
}

Source/Function/Echo.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { Echo } from "../Interface/Echo.js";
2+
import { Level } from "../Variable/Level.js";
3+
4+
/**
5+
* Echo callback function that filters output based on the Level setting.
6+
*
7+
* This function is used to control how output from child processes (stdout/stderr)
8+
* is displayed. It provides a mechanism to filter or suppress output based on the
9+
* current log level setting.
10+
*
11+
* @param Data - The output line data (trimmed whitespace)
12+
* @param IsError - Optional boolean flag indicating if this is stderr output (true)
13+
* or stdout output (false/undefined)
14+
*
15+
* @returns Promise<void>
16+
*
17+
* @example
18+
* Default usage (respects Level variable):
19+
* ```typescript
20+
* import Echo from "./Function/Echo.js";
21+
*
22+
* await Echo("Build started...");
23+
* await Echo("Error occurred", true);
24+
* ```
25+
*
26+
* @example
27+
* Custom echo function:
28+
* ```typescript
29+
* import type { Echo } from "./Interface/Echo.js";
30+
* import { Level } from "./Variable/Level.js";
31+
*
32+
* const customEcho: Echo = async (Data: string, IsError?: boolean): Promise<void> => {
33+
* if (Level === "silent") {
34+
* return;
35+
* }
36+
* console.log(Data);
37+
* };
38+
* ```
39+
*/
40+
const Echo = async (Data: string, _IsError?: boolean): Promise<void> => {
41+
// Suppress all output when Level is "silent"
42+
if (Level === "silent") {
43+
return;
44+
}
45+
46+
console.log(Data);
47+
};
48+
49+
export default Echo satisfies Echo as Echo;

Source/Function/Entry.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ import type { BuildOptions } from "esbuild";
33
/**
44
* @module Entry
55
*
6+
* Function that filters entry points from build options based on exclusion patterns.
7+
*
8+
* This function removes entry points that match patterns in the `From` array.
9+
* It supports various entry point formats: strings, objects with `in` property, and records.
10+
*
11+
* @param Current - The current ESBuild build options containing entryPoints.
12+
*
13+
* @param From - An array of file patterns to exclude from entry points.
14+
*
15+
* @returns The filtered array of entry points.
16+
*
17+
* @example
18+
* Filtering common test files:
19+
* ```typescript
20+
* import Entry from "./Function/Entry.js";
21+
* import { Exclude } from "./Function/Exclude.js";
22+
*
23+
* const options: BuildOptions = {
24+
* entryPoints: ["src/index.ts", "src/test.ts", "src/util.ts"],
25+
* };
26+
*
27+
* const filtered = Entry(options, ["**\/*.test.ts"]);
28+
* // Result: ["src/index.ts", "src/util.ts"]
29+
* ```
30+
*
31+
* @example
32+
* With object entry points:
33+
* ```typescript
34+
* const options: BuildOptions = {
35+
* entryPoints: [
36+
* { in: "src/main/index.ts", out: "main" },
37+
* { in: "src/test/index.ts", out: "test" },
38+
* ],
39+
* };
40+
*
41+
* const filtered = Entry(options, ["**\/test\/**"]);
42+
* // Result: [{ in: "src/main/index.ts", out: "main" }]
43+
* ```
644
*/
745
export default (Current: BuildOptions, From: string[]) => {
846
let _Filtered = [];

Source/Function/Exec.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
11
import type Interface from "../Interface/Exec.js";
22

3+
export const { default: EchoFn } = await import("../Function/Echo.js");
4+
35
/**
46
* @module Exec
57
*
8+
* Function that executes a shell command and streams its output through an echo function.
9+
*
10+
* This function spawns a child process to execute the given command and streams
11+
* the stdout and stderr through the provided echo callback function. This allows
12+
* for custom handling of command output, including filtering by log level.
13+
*
14+
* @param Command - The shell command to execute.
15+
* @param Echo - Optional callback function for handling stdout/stderr output.
16+
* If false, output is suppressed. If a function, it receives the output data.
17+
* Defaults to the default Echo function which respects the Level setting.
18+
*
19+
* @returns Promise<void>
20+
*
21+
* @example
22+
* Basic execution with default echo:
23+
* ```typescript
24+
* import Exec from "./Function/Exec.js";
25+
*
26+
* await Exec("npm run build");
27+
* ```
28+
*
29+
* @example
30+
* Suppressing output:
31+
* ```typescript
32+
* await Exec("npm run test", false);
33+
* ```
34+
*
35+
* @example
36+
* Custom echo function:
37+
* ```typescript
38+
* import type { Echo } from "../Interface/Echo.js";
39+
*
40+
* const myEcho: Echo = async (data, isError) => {
41+
* if (isError) {
42+
* console.error(`[ERROR] ${data}`);
43+
* } else {
44+
* console.log(`[INFO] ${data}`);
45+
* }
46+
* };
47+
*
48+
* await Exec("npm run build", myEcho);
49+
* ```
50+
*
51+
* @example
52+
* Multiple commands:
53+
* ```typescript
54+
* await Exec("npm install");
55+
* await Exec("npm run build");
56+
* await Exec("npm run test");
57+
* ```
658
*/
7-
export default (async (
8-
...[Command, Echo = async (Return) => console.log(Return)]
9-
) => {
59+
export default (async (...[Command, Echo = EchoFn]) => {
1060
try {
1161
const { stdout, stderr } = (await import("child_process")).exec(
1262
Command,

Source/Function/File.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@ import type Interface from "../Interface/File.js";
33
/**
44
* @module File
55
*
6+
* Function that processes and loads files, with special TypeScript transpilation support.
7+
*
8+
* This function processes files based on their extension:
9+
* - For TypeScript (.ts) files: transpiles to JavaScript using TypeScript compiler and writes the .js file
10+
* - For all files: imports and returns the default export of the module
11+
*
12+
* @param Path - The file path to process.
13+
*
14+
* @returns Promise<any> The default export of the processed module.
15+
*
16+
* @example
17+
* Loading a TypeScript configuration file:
18+
* ```typescript
19+
* import File from "./Function/File.js";
20+
*
21+
* const esbuildConfig = await File("./ESBuild.ts");
22+
* // Returns the default export of the transpiled ESBuild.js
23+
* ```
24+
*
25+
* @example
26+
* Loading a JSON configuration (will be imported as-is):
27+
* ```typescript
28+
* const config = await File("./config.json");
29+
* ```
30+
*
31+
* @example
32+
* Dynamic configuration loading:
33+
* ```typescript
34+
* async function loadConfig<Option>(path: string): Promise<Option> {
35+
* return (await File(path)) as Option;
36+
* }
37+
* ```
638
*/
739
export default (async (...[Path]) => {
840
if (Path.split(".").pop() === "ts") {

Source/Function/JSON.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ import type Interface from "../Interface/JSON.js";
33
/**
44
* @module JSON
55
*
6+
* Function that reads and parses JSON files.
7+
*
8+
* This function reads a file from a specified directory and parses it as JSON.
9+
* It is commonly used for loading configuration files (package.json, tsconfig.json, etc.).
10+
*
11+
* @param File - The name or path of the JSON file to read.
12+
*
13+
* @param From - An optional directory path from which to read the file.
14+
* If not provided, uses the current directory (".").
15+
*
16+
* @returns Promise<any> The parsed JSON content.
17+
*
18+
* @example
19+
* Reading package.json:
20+
* ```typescript
21+
* import JSON from "./Function/JSON.js";
22+
*
23+
* const packageJson = await JSON("package.json");
24+
* console.log(packageJson.version);
25+
* ```
26+
*
27+
* @example
28+
* Reading from a specific directory:
29+
* ```typescript
30+
* const tsConfig = await JSON("tsconfig.json", "./Source");
31+
* ```
32+
*
33+
* @example
34+
* Using inline with TypeScript configuration:
35+
* ```typescript
36+
* import JSON from "./Function/JSON.js";
37+
* import { dirname, fileURLToPath } from "node:url";
38+
*
39+
* const config = await JSON(
40+
* "../../tsconfig.json",
41+
* dirname(fileURLToPath(import.meta.url))
42+
* );
43+
* ```
644
*/
745
export default (async (...[File, From]) =>
846
JSON.parse(

0 commit comments

Comments
 (0)