Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Add more doc for terra-core #33

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion terra-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
// export * from "./app_root_path";
export * from './path_resolver';
export * from './testing_utils';

/**
* Abstract representation of a AST node in terra.
* This interface serves as a base for node types and can be extended to
* represent various node-specific details and functionalities within terra.
*/
export interface TerraNode {}

/**
* Class representing the result of a parsing operation.
* Contains an array of `TerraNode` objects.
*/
export class ParseResult {
nodes: TerraNode[] = [];
}

/**
* Represents the context in which terra operates.
* Includes various directory paths and configuration flags.
*/
export class TerraContext {
/**
* Constructs a new `TerraContext` instance.
* @param buildDir The build directory path.
* @param configDir The configuration directory path.
* @param outputDir The output directory path.
* @param clean Flag indicating whether to clean the build directory.
* @param verbose Flag for verbose logging.
*/
constructor(
public readonly buildDir: string = '',
public readonly configDir: string = '',
Expand All @@ -18,17 +38,39 @@ export class TerraContext {
) {}
}

/**
* Type definition for a Parser function in terra.
* Transforms source code into AST nodes (TerraNode).
* This `Parser` can be part of a chain, where each `Parser` has the ability
* to modify the results of its predecessor before passing them to the next `Parser`.
*
* @param terraContext The `TerraContext` instance, providing operational context.
* @param args Additional arguments or data for parsing.
* @param parseResult An optional initial `ParseResult` from a previous Parser in the chain.
* @returns A new or modified `ParseResult` to be used by subsequent Parsers.
*/
export type Parser = (
terraContext: TerraContext,
args: any,
parseResult?: ParseResult
) => ParseResult | undefined;

/** Interface representing the result of a rendering operation. */
export interface RenderResult {
file_name: string;
file_content: string;
}

/**
* Type definition for a Renderer function in Terra.
* Processes the results obtained from a Parser.
* Use this Renderer to generate code or other forms of output based on the parsed AST nodes (TerraNode).
*
* @param terraContext The `TerraContext` instance, providing the operational context.
* @param args Additional arguments relevant to the rendering process.
* @param parseResult The `ParseResult` instance containing the Parser's output.
* @returns An array of `RenderResult` objects, representing the rendered output.
*/
export type Renderer = (
terraContext: TerraContext,
args: any,
Expand Down
23 changes: 21 additions & 2 deletions terra-core/src/path_resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import fs from 'fs';

import os from 'os';
import path from 'path';

/**
* Resolve the schema: `<module_name>:<path>`, or absolute path, or relative path
* Resolves a given path to its absolute form.
* It can resolve paths in various formats: module schemas (`<module_name>:<path>`), absolute paths, or relative paths.
* If the path is not absolute and `prefixDirIfNotAbsolute` is provided, the path is resolved relative to this directory.
*
* @param p The path to resolve, which can be in the format of a module schema, absolute, or relative path.
* @param prefixDirIfNotAbsolute The directory to use as a prefix for non-absolute paths. Defaults to an empty string.
* @returns The resolved absolute path.
*/
export function resolvePath(
p: string,
Expand Down Expand Up @@ -34,6 +39,13 @@ export function resolvePath(
return path.resolve(localPathInModule);
}

/**
* Resolves the path to a specified module.
* This function assumes the module exists in the `node_modules` directory relative to the current package's `package.json`.
*
* @param module The name of the module to resolve.
* @returns The resolved path of the module.
*/
// TODO(littlegnal): Use `require.resolve(`${module}/package.json`)` to require the module's path
export function resolveModulePath(module: string): string {
let currentPackageJsonPath = process.env.npm_package_json;
Expand All @@ -49,6 +61,13 @@ export function resolveModulePath(module: string): string {
return path.resolve(parserPackageDir);
}

/**
* Requires a module and returns its exported content.
* The module is expected to be located in the `node_modules` directory relative to the current package's `package.json`.
*
* @param module The name of the module to require.
* @returns The exported content of the required module.
*/
// TODO(littlegnal): Use `require(`${module}/package.json`)` to require the module
export function requireModule(module: string): any {
let currentPackageJsonPath = process.env.npm_package_json;
Expand Down
2 changes: 1 addition & 1 deletion terra-core/src/testing_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ export function visibleForTesting(module: any, ...func: Function[]): void {
}
}
}
}
}
Loading