Skip to content

Commit

Permalink
fix(utils): Load typescript files relative to process current working…
Browse files Browse the repository at this point in the history
… directory (#79)

* feat(deps): update typescript to v5.3.3

* chore(git): Added coverage directory to gitignore

* fix(utils): Load typescript files relative to process current working directory
  • Loading branch information
lluiscab authored Apr 29, 2024
1 parent 02868b8 commit f9932a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ yarn-error.log*
.idea

.DS_Store
coverage/
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,19 @@ export function getFilePkg(program: ts.Program, filename: string) {
* @returns File package for further manipulations
*/
export function parseFile(basePath: string, filename: string): ParsedFilePkg {
/**
* Parameter `filename` is the filepath relative to `basePath`, but the Program
* created by `ts.createProgram` will be relative to `process.cwd()`.
*
* We have to modify `filename` and make sure it is relative to `
* process.cwd`, otherwise `program.getSourceFile` in `getFilePkg` will return undefined
*/
const cwd = process.cwd()
const filenameRelativeCWD = path.relative(cwd, path.join(basePath, filename))

const options = getTsCompilerOptions(basePath, true)
const program = ts.createProgram([filename], options)
return getFilePkg(program, filename)
const program = ts.createProgram([filenameRelativeCWD], options)
return getFilePkg(program, filenameRelativeCWD)
}

/**
Expand Down

0 comments on commit f9932a4

Please sign in to comment.