feat: add openFileInNpmx command for node_modules files#34
feat: add openFileInNpmx command for node_modules files#349romise merged 11 commits intonpmx-dev:mainfrom
openFileInNpmx command for node_modules files#34Conversation
This allows testing code which imports `vscode`, particularly including infrastructure for manipulating `Uri` objects.
This is a generic utility for creating a fake filesystem used by `workspace.fs`. Only `readFile` is implemented for now as that's all that's immediately needed, but other functions could be mocked just as easily when they become necessary.
This adds a new `resolvePackageRelativePath` function which takes an arbitrary file within `node_modules/` and returns its package-relative path. This is useful for generating links to specific files in npmx.dev. Given a file path, the logic for finding its associated `package.json` is the following: * Walk up its ancestor directories. * Check each for a `package.json` file. * If the file exists, and has a `name` and `version` field, use that path. * The motivation for checking `name` and `version` is to ignore "fake" `package.json` files used for configuring specific directories, such as Webpack's [`sideEffects: false`](https://webpack.js.org/guides/tree-shaking/) flag. * Calculate the relative path from the `package.json` to the original file path. For this module, we assume the file path is within a `node_modules/` directory, though this will be actually enforced earlier in the process.
This command opens a given file under `node_modules/` in npmx.dev, deep linking to its specific package version, relative file path, and line number. The command is only shown when in a `node_modules/` context and only includes a line number when opening the currently active file. There are some edge cases for opening files like: * `node_modules/.bin/foo` * `node_modules/foo` * `node_modules/@foo/bar` * `node_modules/.pnpm` Ideally these wouldn't even support the command through the `when` filter, but that regex starts to get complicated, so I didn't bite off that complexity here. A potential future feature might be "npmx: Open package" and work specifically on `node_modules/foo` and `node_modules/@foo/bar` directories, opening the package itself rather than deep-linking to any specific file. But such a command is out of scope for now.
📝 WalkthroughWalkthroughThis pull request introduces a new VSCode command 🚥 Pre-merge checks | ✅ 1✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/commands/open-file-in-npmx.ts (1)
7-13: MakefileUrioptional to match actual invocationLine 7: the handler is called from the command palette without an argument, yet the signature requires
Uri. This is misleading and can break under stricter TypeScript settings.Suggested change
-export async function openFileInNpmx(fileUri: Uri) { +export async function openFileInNpmx(fileUri?: Uri) { const textEditor = useActiveTextEditor() // If triggered from context menu, fileUri is provided. // If triggered from command palette, use active text editor. - const uri = fileUri || textEditor.value?.document.uri + const uri = fileUri ?? textEditor.value?.document.uri
9romise
left a comment
There was a problem hiding this comment.
Thank you for the creative idea! ❤️ I think this feature is really useful! I've made some changes, as described in each commit message.
openFileInNpmx command for node_modules files
This adds a new command to open the current file on npmx.dev. The intent is to be able to easily create a shareable deep link to a
node_modules/...file from within an IDE.Open.file.on.npmx.2.mp4
Individual commits provide more detail, but the general idea is to resolve the current file to its nearest
package.jsonparent, identify the package name and version, then resolve the package-relative file path and current line number. Then it resolves an npmx.dev link to that file and opens it.This currently only works for files within an NPM package. I think there's space for another "Open package on npmx.dev" command which works for the directories rather than the files, but I think the value proposition is a little lower and I didn't want to include it in scope here.
Disclaimer: I'm not terribly familiar with VSCode APIs and most of this was written by Gemini in Antigravity with some heavy hand-holding along the way. I'm fairly confident this works and is at least a reasonable implementation of the feature, but this PR might benefit from some thinking about edge cases I didn't consider.