Path: blob/main/extensions/html-language-features/client/src/node/htmlClientMain.ts
3323 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { getNodeFileFS } from './nodeFs';6import { Disposable, ExtensionContext, l10n } from 'vscode';7import { startClient, LanguageClientConstructor, AsyncDisposable } from '../htmlClient';8import { ServerOptions, TransportKind, LanguageClientOptions, LanguageClient } from 'vscode-languageclient/node';9import * as fs from 'fs';10import TelemetryReporter from '@vscode/extension-telemetry';111213let telemetry: TelemetryReporter | undefined;14let client: AsyncDisposable | undefined;1516// this method is called when vs code is activated17export async function activate(context: ExtensionContext) {1819const clientPackageJSON = getPackageInfo(context);20telemetry = new TelemetryReporter(clientPackageJSON.aiKey);2122const serverMain = `./server/${clientPackageJSON.main.indexOf('/dist/') !== -1 ? 'dist' : 'out'}/node/htmlServerMain`;23const serverModule = context.asAbsolutePath(serverMain);2425// The debug options for the server26const debugOptions = { execArgv: ['--nolazy', '--inspect=' + (8000 + Math.round(Math.random() * 999))] };2728// If the extension is launch in debug mode the debug server options are use29// Otherwise the run options are used30const serverOptions: ServerOptions = {31run: { module: serverModule, transport: TransportKind.ipc },32debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }33};3435const newLanguageClient: LanguageClientConstructor = (id: string, name: string, clientOptions: LanguageClientOptions) => {36return new LanguageClient(id, name, serverOptions, clientOptions);37};3839const timer = {40setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable {41const handle = setTimeout(callback, ms, ...args);42return { dispose: () => clearTimeout(handle) };43}44};454647// pass the location of the localization bundle to the server48process.env['VSCODE_L10N_BUNDLE_LOCATION'] = l10n.uri?.toString() ?? '';4950client = await startClient(context, newLanguageClient, { fileFs: getNodeFileFS(), TextDecoder, telemetry, timer });51}5253export async function deactivate(): Promise<void> {54if (client) {55await client.dispose();56client = undefined;57}58}5960interface IPackageInfo {61name: string;62version: string;63aiKey: string;64main: string;65}6667function getPackageInfo(context: ExtensionContext): IPackageInfo {68const location = context.asAbsolutePath('./package.json');69try {70return JSON.parse(fs.readFileSync(location).toString());71} catch (e) {72console.log(`Problems reading ${location}: ${e}`);73return { name: '', version: '', aiKey: '', main: '' };74}75}767778