import * as vscode from "vscode" ; import { Command } from "./command" ; import { MultiCommand } from "./multiCommand" ; interface CommandSettings { label : string ; description : string ; interval : number ; sequence : Array < string | ComplexCommand > ; } interface CommandSettingsWithKey extends CommandSettings { command : string ; } interface CommandMap { [ key ] : CommandSettings ; } interface ComplexCommand { command : string ; args : object ; } function implementsCommandMap ( arg ) { return arg !== null && typeof arg === "object" ; } function createMultiCommand ( id , settings ) { const label = settings . label ; const description = settings . description ; const interval = settings . interval ; const sequence = settings . sequence . map ( ( command ) => { let exe ; let args ; if ( typeof command === "string" ) { exe = command ; args = null ; } else { exe = command . command ; args = command . args ; } return new Command ( exe , args ) ; } ) ; return new MultiCommand ( id , label , description , interval , sequence ) ; } let multiCommands ; function refreshUserCommands ( context ) { let configuration = vscode . workspace . getConfiguration ( "multiCommand" ) ; let commands = new Map < string , CommandSettings > ( ) ; let commandList = configuration . get < Array < CommandSettingsWithKey > | CommandMap > ( "commands" ) || [ ] ; for ( let element of context . subscriptions ) { element . dispose ( ) ; } if ( Array . isArray ( commandList ) ) { for ( let commandSettingsWithKey of commandList ) { commands . set ( commandSettingsWithKey . command , commandSettingsWithKey ) ; } } else if ( implementsCommandMap ( commandList ) ) { let commandObject = commandList as CommandMap ; Object . keys ( commandObject ) . forEach ( ( key ) => { commands . set ( key , commandObject [ key ] ) ; } ) ; } multiCommands = [ ] ; commands . forEach ( ( value , key ) => { const multiCommand = createMultiCommand ( key , value ) ; multiCommands . push ( multiCommand ) ; context . subscriptions . push ( vscode . commands . registerCommand ( key , async ( ) => { await multiCommand . execute ( ) ; } ) ) ; } ) ; } export function activate ( context ) { refreshUserCommands ( context ) ; vscode . workspace . onDidChangeConfiguration ( ( ) => { refreshUserCommands ( context ) ; } ) ; vscode . commands . registerCommand ( "extension.multiCommand.execute" , async ( args = { } ) => { try { if ( args . command ) { await vscode . commands . executeCommand ( args . command ) ; } else if ( args . sequence ) { const multiCommand = createMultiCommand ( "" , args ) ; await multiCommand . execute ( ) ; } else { await pickMultiCommand ( ) ; } } catch ( e ) { vscode . window . showErrorMessage ( `${ e . message }` ) ; } } ) ; } export function deactivate ( ) { } export async function pickMultiCommand ( ) { const picks = multiCommands . map ( ( multiCommand ) => { return { label : multiCommand . label || multiCommand . id , description : multiCommand . description || "" , multiCommand : multiCommand , } ; } ) ; const item = await vscode . window . showQuickPick ( picks , { placeHolder : `Select one of the multi commands...` , } ) ; if ( ! item ) { return ; } await item . multiCommand . execute ( ) ; }