import { shared as lspCommon } from '@salesforce/lightning-lsp-common' ; import * as path from 'path' ; import { commands , ConfigurationTarget , Disposable , ExtensionContext , Uri , workspace , WorkspaceConfiguration } from 'vscode' ; import { forceLightningLwcOpen , forceLightningLwcPreview , forceLightningLwcStart , forceLightningLwcStop } from './commands' ; import { ESLINT_NODEPATH_CONFIG , LWC_EXTENSION_NAME } from './constants' ; import { createLanguageClient } from './languageClient' ; import { metaSupport } from './metasupport' ; import { DevServerService } from './service/devServerService' ; import { telemetryService } from './telemetry' ; import { activateLwcTestSupport , shouldActivateLwcTestSupport } from './testSupport' ; import { WorkspaceUtils } from './util/workspaceUtils' ; export function code2ProtocolConverter ( value ) { if ( /^win32/ . test ( process . platform ) ) { return value . toString ( ) . replace ( '%3A' , ':' ) ; } else { return value . toString ( ) ; } } function protocol2CodeConverter ( value ) { return Uri . parse ( value ) ; } export async function activate ( context ) { const extensionHRStart = process . hrtime ( ) ; console . log ( 'Activation Mode: ' + getActivationMode ( ) ) ; if ( getActivationMode ( ) === 'off' ) { console . log ( 'LWC Language Server activationMode set to off, exiting...' ) ; return ; } const extensionPackage = require ( context . asAbsolutePath ( './package.json' ) ) ; await telemetryService . initializeService ( context , LWC_EXTENSION_NAME , extensionPackage . aiKey , extensionPackage . version ) ; if ( ! workspace . workspaceFolders ) { console . log ( 'No workspace, exiting extension' ) ; return ; } const workspaceUris = [ ] ; workspace . workspaceFolders . forEach ( folder => { workspaceUris . push ( folder . uri . fsPath ) ; } ) ; const workspaceType = lspCommon . detectWorkspaceType ( workspaceUris ) ; if ( getActivationMode ( ) === 'autodetect' && ! lspCommon . isLWC ( workspaceType ) ) { console . log ( 'LWC LSP - autodetect did not find a valid project structure, exiting....' ) ; console . log ( 'WorkspaceType detected: ' + workspaceType ) ; return ; } const ourCommands = registerCommands ( context ) ; context . subscriptions . push ( ourCommands ) ; console . log ( 'Lightning Web Components Extension Activated' ) ; console . log ( 'WorkspaceType detected: ' + workspaceType ) ; const client = createLanguageClient ( context . asAbsolutePath ( path . join ( 'node_modules' , '@salesforce' , 'lwc-language-server' , 'lib' , 'server.js' ) ) ) ; context . subscriptions . push ( client . start ( ) ) ; await metaSupport . getMetaSupport ( ) ; if ( workspaceType === lspCommon . WorkspaceType . SFDX ) { const config = workspace . getConfiguration ( '' ) ; const currentNodePath = config . get < string > ( ESLINT_NODEPATH_CONFIG ) ; if ( currentNodePath && currentNodePath . includes ( LWC_EXTENSION_NAME ) ) { try { console . log ( 'Removing eslint.nodePath setting as the LWC Extension no longer manages this value' ) ; await config . update ( ESLINT_NODEPATH_CONFIG , undefined , ConfigurationTarget . Workspace ) ; } catch ( e ) { telemetryService . sendException ( 'lwc_eslint_nodepath_couldnt_be_set' , e . message ) ; } } } if ( shouldActivateLwcTestSupport ( workspaceType ) ) { activateLwcTestSupport ( context , workspaceType ) ; } WorkspaceUtils . instance . init ( context ) ; telemetryService . sendExtensionActivationEvent ( extensionHRStart ) ; } export async function deactivate ( ) { if ( DevServerService . instance . isServerHandlerRegistered ( ) ) { await DevServerService . instance . stopServer ( ) ; } console . log ( 'Lightning Web Components Extension Deactivated' ) ; telemetryService . sendExtensionDeactivationEvent ( ) ; } function getActivationMode ( ) { const config = workspace . getConfiguration ( 'salesforcedx-vscode-lightning' ) ; return config . get ( 'activationMode' ) || 'autodetect' ; } function registerCommands ( _extensionContext ) { return Disposable . from ( commands . registerCommand ( 'sfdx.force.lightning.lwc.start' , forceLightningLwcStart ) , commands . registerCommand ( 'sfdx.force.lightning.lwc.stop' , forceLightningLwcStop ) , commands . registerCommand ( 'sfdx.force.lightning.lwc.open' , forceLightningLwcOpen ) , commands . registerCommand ( 'sfdx.force.lightning.lwc.preview' , forceLightningLwcPreview ) ) ; }