opticdev / optic

OpenAPI linting, diffing and testing. Optic helps prevent breaking changes, publish accurate documentation and improve the design of your APIs.
https://useoptic.com
MIT License
1.35k stars 79 forks source link

replace __dirname in favor of process.cwd in diff command web flag #2856

Closed prochafilho closed 3 weeks ago

prochafilho commented 3 weeks ago

🍗 Description

What does this PR do? Anything folks should know?

A bit of context: to facilitate the consumption of @useoptic/optic, we package it using pkg to make it an executable.

If I run the diff command using native TypeScript compilers such as ts-node, I have no issue because the final HTML file resolves to a local file in my filesystem, for example, file:///Users/xxxxxxx/work/tools/tool-api-standards/node_modules/@useoptic/optic/web/build/index.html#W0cZF0U

But because we package using pkg, it prefixes the local file path with snapshot, so it becomes: file:///snapshot/tool-api-standards/node_modules/@useoptic/optic/web/build/index.html#W0cZF0U

During packaging process pkg collects project files and places them into executable. It is called a snapshot. At run time the packaged application has access to snapshot filesystem where all that files reside. Packaged files have /snapshot/ prefix in their paths (or C:\snapshot\ in Windows). If you used pkg /path/app.js command line, then filename value will be likely /snapshot/path/app.js at run time. dirname will be /snapshot/path as well.

I think it is caused by maybeChangelogUrl found in the following line in combination with our use of pkg to make executables: maybeChangelogUrl

pkg recommends to use process.cwd() instead of __dirname

On the other hand, to access a real file system at run time (pick up a user’s external javascript plugin, JSON configuration or even get a list of the user’s directory) you should take process.cwd() or path.dirname(process.execPath)

One can assert the following is true: (process.execPath) === __dirname === true

đź“š References

Links to relevant docs (Notion, Twist, GH issues, etc.), if applicable. https://www.npmjs.com/package/pkg#snapshot-filesystem https://www.npmjs.com/package/ts-node

đź‘ą QA

How can other humans verify that this PR is correct?

When running diff with the --web flag, it should open a browser window with the results.