I built a VSCode extension that consumes an external API. I need it to lunch a command to fetch some data every time a user opens VSCode.
I have read the documentation, but I don't find an answer.
Thank you for your time!
On the extension's package.json you have the activationEvents setting. This setting establishes WHEN your extension will be activated. You can read about the different options in the official docs, but let me tell you that one of the options is the star operator *. Your extension will activate when VSCode is opened.
To do this, from an open terminal or command prompt, navigate to your project folder and type code .: Note: Users on macOS must first run a command ( Shell Command: Install 'code' command in PATH) to add VS Code executable to the PATH environment variable. Read the macOS setup guide for help.
Can be invoked using the Command Palette. Can be invoked using a keybinding. Can be invoked through the VS Code UI, such as through the editor title bar. Is intended as an API for other extensions to consume.
The vscode.commands.executeCommand API programmatically executes a command. This lets you use VS Code's built-in functionality, and build on extensions such as VS Code's built-in Git and Markdown extensions. The editor.action.addCommentLine command, for example, comments the currently selected lines in the active text editor:
I had a misconception about how an extension's life cycle worked. Finally, I've been able to solve my problem. Here is how:
On the extension's package.json
you have the activationEvents
setting. This setting establishes WHEN your extension will be activated. You can read about the different options in the official docs, but let me tell you that one of the options is the star operator *
.
If you set this in you package.json
:
"activationEvents": [
"*"
],
Your extension will activate when VSCode is opened.
What happens when your extension is activated? It will fire the activate
function, defined by default in the extension.ts
/extension.js
file.
There, you can put the code for, for example, launching a specific Command
, or creating a specific Tree View
.
Hope it helps.
"activationEvents": [ "onStartupFinished" ]
This is much better then "*", it means the same thing besides it won't distract startup of your VS Code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With