1

I'm working on a VS Code extension that auto-configures a number of items.

What I'm not clear on after reading this link: Is it possible to call command between extensions in VSCode?

Is the correct way to call another command within the same extension in my TypeScript code.

Ex. hit CTRL+Shift+P that activates and runs my main user-facing command, it runs through appropriate logic, etc.

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import fs = require('fs');
import path = require('path');
const ini = require('ini');

export function activate(context: vscode.ExtensionContext) {

    // install check name to match package.json
    const myCommand = 'extension.myCommandName';

    // command handler
    const myCommandHandler = async (name?: 'MyCommand: Do Things') => {
        // Check if a local path exists
        function checkDirectorySync(directory: fs.PathLike) {
            try {
                fs.statSync(directory);
                return true;
            } catch (e) {
                return false;
            }
        }

        let isDirectoryValid = checkDirectorySync('C:\\Users\myusername\someFolder');

        // now call another command to launch an input box
        if (isDirectoryValid === false) {
            var secondCommand = vscode.extensions.getExtension('extension.MyOtherCommand');
            // is the ext loaded and ready?
            if (secondCommand.isActive == false) {
                secondCommand.activate().then(
                    function () {
                        console.log("Extension activated");
                        vscode.extensions.findCommand();
                        vscode.commands.executeCommand("extension.MyOtherCommand");
                    },
                    function () {
                        console.log("Extension activation failed");
                    }
                );
            } else {
                vscode.commands.executeCommand("extension.MyOtherCommand");
            }
        }
    };

    // push directory check command to command registry
    context.subscriptions.push(vscode.commands.registerCommand(myCommand, myCommandHandler));
}

// this method is called when your extension is deactivated
export function deactivate() { }

Just seeking to understand with this simple example if this is the correct approach. The other post seemed a little confusing.

Any quick advice greatly appreciated

trebleCode
  • 2,134
  • 19
  • 34
  • What is confusing about the other post? And what's the problem with your code (or is working fine as-is)? – Gama11 Feb 14 '19 at 19:32
  • Without trying to make a call to another command, my code runs fine. In looking through the examples for showing input boxes, it looks like the MS example is using a wrapper class around the `window.showInputBox()` built-method that adds some example extensibility. I'm realizing I'll probably have to use their example wrapper and customize it to my needs. It looks like if I wrap that functionality into a standalone command, I should be able to call it in my "master" command. Just alot of API bits to dig through – trebleCode Feb 14 '19 at 19:44
  • @Gama11 The other confusing part is that the other post shows calling a .xml file as the extension. I'm confused how that object is getting its `.isActive` property? – trebleCode Feb 14 '19 at 20:09
  • Maybe you should check out what `getExtension()` does in the API docs - that code does not "call a .xml file". https://code.visualstudio.com/api/references/vscode-api#extensions.getExtension – Gama11 Feb 14 '19 at 22:00
  • Cheers for that. I'll have to hack around with this a few different ways. Appreciate the pointer – trebleCode Feb 14 '19 at 22:14

0 Answers0