-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(readme.md): update docs, including how to start with next.js
- Loading branch information
Jack Hopkins
committed
Feb 2, 2024
1 parent
a5b3696
commit 9460802
Showing
3 changed files
with
93 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env node | ||
|
||
import ts from "typescript" | ||
import fs from "fs" | ||
import path from "path" | ||
import {glob} from "glob" | ||
import tanukiTransformer, {PatchFunctionCompiler} from "./lib/src/tanukiTransformer.js"; | ||
|
||
function getTsConfig() { | ||
const configFile = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json'); | ||
if (!configFile) { | ||
throw new Error('Could not find a valid "tsconfig.json".'); | ||
} | ||
|
||
const configFileContents = ts.readConfigFile(configFile, ts.sys.readFile); | ||
return ts.parseJsonConfigFileContent(configFileContents.config, ts.sys, path.dirname(configFile)); | ||
} | ||
|
||
function applyTransformer(transformerFactory, fileNames, compilerOptions, tsInstance) { | ||
const program = ts.createProgram(fileNames, compilerOptions); | ||
// Create the compiler instance and clear the file once here | ||
const compiler = new PatchFunctionCompiler(program, tsInstance); | ||
compiler.clearFile(); | ||
fileNames.forEach(fileName => { | ||
const sourceFile = program.getSourceFile(fileName); | ||
const transformed = ts.transform( | ||
sourceFile, | ||
[transformerFactory(program, tsInstance, compilerOptions, compiler)]) | ||
const printer = ts.createPrinter(); | ||
const result = printer.printFile(transformed.transformed[0]); | ||
|
||
fs.writeFileSync(fileName, result); | ||
}); | ||
} | ||
|
||
function tanukiTransformerFactory(program, tsInstance, compilerOptions, compiler) { | ||
return tanukiTransformer(program, {}, { ts: tsInstance, compiler }); | ||
} | ||
console.log(`Current directory: ${process.cwd()}`); | ||
// Using glob.sync to list TypeScript files synchronously | ||
const files = glob.sync(["**/*.ts", "**/*.tsx"], { ignore: ["node_modules/**", "**/*.d.ts"] }); | ||
|
||
const tsConfig = getTsConfig(); | ||
|
||
// Apply the transformer | ||
const tsInstance = ts; | ||
applyTransformer(tanukiTransformerFactory, files, tsConfig.options, tsInstance); |