Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-runtime-picker",
"version": "2.1.2",
"version": "2.1.3",
"main": "dist/index.js",
"exports": {
".": {
Expand Down
23 changes: 14 additions & 9 deletions src/ts-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Project, SyntaxKind } from "ts-morph";

const project = new Project({
tsConfigFilePath: "tsconfig.json",
});

export function transform(code: string, filePath: string): string {
const project = new Project({
// make sure you’ve configured TS so it can see your other files (tsconfig.json, etc.)
// otherwise type‐resolution won’t work across files
tsConfigFilePath: "tsconfig.json",
});

// Load the file if it exists, otherwise create a new one
const sourceFile = project.addSourceFileAtPathIfExists(filePath)
|| project.createSourceFile(filePath, code, { overwrite: true });

let sourceFile = project.getSourceFile(filePath);

if (!sourceFile) {
// Load the file if it exists, otherwise create a new one
sourceFile = project.createSourceFile(filePath, code, { overwrite: true });
} else {
// If the file already exists, we can replace its content
sourceFile.replaceWithText(code);
}

// Figure out what the “createPicker” import is called (alias or direct)
let createPickerAlias: string | null = null;
Expand Down