Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

stackables/materialize-ts-function

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm codecov

Materialize typescript functions

A simple cli command to materialize the response of a typescript async function during build time.

The simplest use-case

1. Annotate your function with @materialized

import { readFile } from "fs/promises";

/**
 * @materialized
 */
async function getVersion() {
	// return anything that survives JSON.stringify()

	const pkg = await readFile("./package.json");
	const parsed = JSON.parse(pkg.toString());
	return parsed.version;
}

2. Rewrite the code during build

npx materialize-ts-function

3. End result

/**
 * @materialized
 */
async function getVersion() {
	/* __materialized__ */
	return "1.0.0";
}

Conditionally skipping materialize

Use any boolean returning expression with @if annotation

/**
 * @materialized
 * @if process.env.DEBUG !== undefined
 */
async function someComplexLogic() {
	// add business logic here
}

How it works internally

We scan the codebase for all files containing functions with the @materialized annotation and temporarily add a simple statement to the end of the file to make it executable with ts-node.

Then we capture the output and replace the annotated functions body with the serialized response from the ts-node execution.

// Temporary statements to execute the function
import { materialize } from "materialize-ts-function";

materialize(
	someComplexLogic, // <-- @materialized function
	process.env.DEBUG !== undefined // <-- @if annotation literal
)
	.then(console.log)
	.catch(console.error);

To debug one can add the same statement to the file and execute if with

ts-node ./src/original/file/location.ts

Thats it ...

... happy coding :)

About

cli to materialize response of a typescript async function during build time

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.