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