Bobbygllh
Bobbygllh

Reputation: 132

NPM Package with VSCode Snippets

I was wondering if there is anyway to potentially set up a NPM Package to contain VSCode snippets and have them be automatically availble for users to utilize (ie. not having to drag/drop them into their VSCode snippets folder or something like that. I know that it is possible to create a VSCode Extension that contains snippets and users can download the extension and the snippets are automatically added. I have tried to replicate that functionality with an NPM Package but have been largely unsuccessful. I did build an extension and that works by have an "Contributes" key in the package.json which seemingly exports the snippets contained in the extension into the main snippets folder.

Categories Package.JSON

I tried replicating this w/ an NPM Package but VSCode doesn't seem to register it in the same way. Any ideas on how this might be accomplished? All the devs are familiar with NPM and that is how we distribute packages to the team so i'm just trying to keep things centralized for management purposes.

Upvotes: 0

Views: 462

Answers (1)

user128511
user128511

Reputation:

Maybe not the answer you were looking for but if you add a postinstall script to your package as in

{
  "name": "my-vscode-extension",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "node postinstall.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

It will run the script after installing the npm package. So, you could issue whatever commands needed to install the extension into VSCode. The VSCode docs say

code --install-extension <extension-vsix-path>

will install an extension from the command line

Apparently you need to make a vsix file so put that in your npm package.

Note: If you did this with a public npm package I suspect you'd get a very bad reputation for hacking people's vscode/system. You shouldn't install things into other people's editors or apps via npm. But, if it's just for you or some company wide private extension on a private npm server then it's probably fine.

Upvotes: 1

Related Questions