Leandro
Leandro

Reputation: 1522

How do I run an extension command on startup on VSCode?

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!

Upvotes: 16

Views: 6510

Answers (2)

David Melkumyan
David Melkumyan

Reputation: 427

"activationEvents": [ "onStartupFinished" ]

This is much better then "*", it means the same thing besides it won't distract startup of your VS Code

Upvotes: 31

Leandro
Leandro

Reputation: 1522

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.

Upvotes: 18

Related Questions