Skip to content

Commit

Permalink
async startup / fixes settings.json being overwritten / fixes #1210
Browse files Browse the repository at this point in the history
  • Loading branch information
nrkruk authored and Luis Campos committed Apr 23, 2019
1 parent 78b8578 commit e3b2b30
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 30 deletions.
23 changes: 19 additions & 4 deletions packages/salesforcedx-vscode-lightning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
"typescript": "3.1.6",
"vscode": "1.1.17"
},
"extensionDependencies": [
"salesforce.salesforcedx-vscode-core"
],
"scripts": {
"vscode:prepublish": "npm prune --production",
"vscode:package": "vsce package",
Expand All @@ -64,8 +61,11 @@
"test:vscode-insiders-integration": "cross-env CODE_VERSION=insiders npm run test:vscode-integration"
},
"activationEvents": [
"onLanguage:html",
"onLanguage:javascript",
"workspaceContains:sfdx-project.json",
"workspaceContains:**/core/workspace-user.xml"
"workspaceContains:**/workspace-user.xml",
"onView:salesforce-lightning-explorer"
],
"main": "./out/src",
"contributes": {
Expand Down Expand Up @@ -133,6 +133,21 @@
"scope": "window",
"default": false,
"description": "%show_lightning_explorer_description%"
},
"salesforcedx-vscode-lightning.activationMode": {
"type": "string",
"description": "%activation_mode_description%",
"enum": [
"always",
"autodetect",
"off"
],
"enumDescription": [
"Always on",
"Only activate when a valid SFDX workspace",
"Always off"
],
"default": "autodetect"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/salesforcedx-vscode-lightning/package.nls.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"lightning_open_component_title": "SFDX: Open Lightning Component",
"lightning_explorer_title": "Lightning Explorer",
"feature_previews_title": "Salesforce Feature Previews",
"feature_previews_title": "Salesforce Lightning ",
"lightning_explorer_name": "Lightning Components",
"show_lightning_explorer_description": "Show the lightning explorer view"
"show_lightning_explorer_description": "Show the lightning explorer view",
"debug_mode_description": "Activate debug mode",
"activation_mode_description": "When the language server should be activated"
}
53 changes: 53 additions & 0 deletions packages/salesforcedx-vscode-lightning/src/dxsupport/waitForDX.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as vscode from 'vscode';

let wait: Promise<vscode.Extension<any>>;

export async function waitForDX(activate: boolean = false) {
if (!wait) {
wait = new Promise((resolve, reject) => {
// 120 seconds from now
const expires = new Date().getTime() + 1000 * 120;
const dosetup = () => {
let success = false;
try {
const coreDependency = vscode.extensions.getExtension(
'salesforce.salesforcedx-vscode-core'
);
if (coreDependency && !coreDependency.isActive && activate) {
return coreDependency.activate().then(api => {
resolve(
vscode.extensions.getExtension(
'salesforce.salesforcedx-vscode-core'
)
);
});
}
if (coreDependency && coreDependency.exports) {
success = true;
resolve(coreDependency);
}
} catch (ignore) {
// ignore
}
if (!success) {
if (new Date().getTime() > expires) {
const msg =
'salesforce.salesforcedx-vscode-core not installed or activated, some features unavailable';
console.log(msg);
reject(msg);
} else {
setTimeout(dosetup, 100);
}
}
};
setTimeout(dosetup, 100);
});
}
return wait;
}
33 changes: 16 additions & 17 deletions packages/salesforcedx-vscode-lightning/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
window,
workspace
} from 'vscode';
import * as vscode from 'vscode';
import {
LanguageClient,
LanguageClientOptions,
Expand Down Expand Up @@ -44,6 +45,17 @@ function protocol2CodeConverter(value: string): Uri {
}

export async function activate(context: ExtensionContext) {
if (
vscode.workspace
.getConfiguration('salesforcedx-vscode-lightning')
.get('activationMode') === 'off'
) {
console.log(
'Aura Components Extension deactivated - setting is turned off'
);
return;
}

console.log('Aura Components Extension Activated');
const extensionHRStart = process.hrtime();

Expand All @@ -52,8 +64,9 @@ export async function activate(context: ExtensionContext) {
);

// The debug options for the server
const debugOptions = { execArgv: ['--nolazy', '--inspect=6020'] };
// let debugOptions = { };
const debugOptions = {
execArgv: ['--nolazy', '--inspect-brk=6020']
};

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
Expand Down Expand Up @@ -140,21 +153,7 @@ export async function activate(context: ExtensionContext) {
client.start();
context.subscriptions.push(this.client);

const sfdxCoreExtension = extensions.getExtension(
'salesforce.salesforcedx-vscode-core'
);

// Telemetry
if (sfdxCoreExtension && sfdxCoreExtension.exports) {
sfdxCoreExtension.exports.telemetryService.showTelemetryMessage();

telemetryService.initializeService(
sfdxCoreExtension.exports.telemetryService.getReporter(),
sfdxCoreExtension.exports.telemetryService.isTelemetryEnabled()
);
}

telemetryService.sendExtensionActivationEvent(extensionHRStart);
telemetryService.sendExtensionActivationEvent(extensionHRStart).catch();
}

let indexingResolve: any;
Expand Down
49 changes: 45 additions & 4 deletions packages/salesforcedx-vscode-lightning/src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as util from 'util';
import * as vscode from 'vscode';
import TelemetryReporter from 'vscode-extension-telemetry';
import { telemetryService } from '.';
import { waitForDX } from '../dxsupport/waitForDX';

const EXTENSION_NAME = 'salesforcedx-vscode-lightning';

export class TelemetryService {
private static instance: TelemetryService;
private reporter: TelemetryReporter | undefined;
private isTelemetryEnabled: boolean;
private setup: Promise<TelemetryService | undefined> | undefined;

constructor() {
this.isTelemetryEnabled = false;
Expand All @@ -26,6 +30,29 @@ export class TelemetryService {
return TelemetryService.instance;
}

public async setupVSCodeTelemetry() {
// if its already set up
if (this.reporter) {
return Promise.resolve(telemetryService);
}
if (!this.setup) {
this.setup = waitForDX(true)
.then((coreDependency: vscode.Extension<any>) => {
coreDependency.exports.telemetryService.showTelemetryMessage();

telemetryService.initializeService(
coreDependency.exports.telemetryService.getReporter(),
coreDependency.exports.telemetryService.isTelemetryEnabled()
);
return telemetryService;
})
.catch(err => {
return undefined;
});
}
return this.setup;
}

public initializeService(
reporter: TelemetryReporter,
isTelemetryEnabled: boolean
Expand All @@ -34,7 +61,10 @@ export class TelemetryService {
this.reporter = reporter;
}

public sendExtensionActivationEvent(hrstart: [number, number]): void {
public async sendExtensionActivationEvent(
hrstart: [number, number]
): Promise<void> {
await this.setupVSCodeTelemetry();
if (this.reporter !== undefined && this.isTelemetryEnabled) {
const startupTime = this.getEndHRTime(hrstart);
this.reporter.sendTelemetryEvent('activationEvent', {
Expand All @@ -44,16 +74,27 @@ export class TelemetryService {
}
}

public sendExtensionDeactivationEvent(): void {
public async sendExtensionDeactivationEvent(): Promise<void> {
await this.setupVSCodeTelemetry();
if (this.reporter !== undefined && this.isTelemetryEnabled) {
this.reporter.sendTelemetryEvent('deactivationEvent', {
extensionName: EXTENSION_NAME
});
}
}

public async sendCommandEvent(commandName?: string): Promise<void> {
await this.setupVSCodeTelemetry();
if (this.reporter !== undefined && this.isTelemetryEnabled && commandName) {
this.reporter.sendTelemetryEvent('commandExecution', {
extensionName: EXTENSION_NAME,
commandName
});
}
}

private getEndHRTime(hrstart: [number, number]): string {
const hrend = process.hrtime(hrstart);
return util.format('%d%d', hrend[0], hrend[1] / 1000000);
return util.format('%ds %dms', hrend[0], hrend[1] / 1000000);
}
}
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-lightning/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "es6",
"lib": ["dom", "es2016"],
"sourceMap": true,
"declarationMap": true,
"declaration": true,
"moduleResolution": "node",
"noImplicitAny": true,
Expand Down
5 changes: 4 additions & 1 deletion packages/salesforcedx-vscode-lwc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@
"test:vscode-insiders-integration": "cross-env CODE_VERSION=insiders npm run test:vscode-integration"
},
"activationEvents": [
"onLanguage:html",
"onLanguage:javascript",
"workspaceContains:sfdx-project.json",
"workspaceContains:**/core/workspace-user.xml"
"workspaceContains:**/workspace-user.xml",
"onView:salesforce-lightning-explorer"
],
"main": "./out/src",
"contributes": {
Expand Down
42 changes: 41 additions & 1 deletion packages/salesforcedx-vscode-lwc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,41 @@ import {
TransportKind
} from 'vscode-languageclient';
import { ESLINT_NODEPATH_CONFIG, LWC_EXTENSION_NAME } from './constants';

import { WorkspaceType } from 'lightning-lsp-common/lib/shared';
import { waitForDX } from './dxsupport/waitForDX';
import { telemetryService } from './telemetry';

async function registerCommands(): Promise<vscode.Disposable | undefined> {
try {
await waitForDX(true);
const {
forceLightningLwcCreate
} = require('./commands/forceLightningLwcCreate');

// Customer-facing commands
const forceLightningLwcCreateCmd = vscode.commands.registerCommand(
'sfdx.force.lightning.lwc.create',
forceLightningLwcCreate
);

return vscode.Disposable.from(forceLightningLwcCreateCmd);
} catch (ignore) {
// ignore
return undefined;
}
}

export async function activate(context: vscode.ExtensionContext) {
if (
vscode.workspace
.getConfiguration('salesforcedx-vscode-lightning')
.get('activationMode') === 'off'
) {
console.log('LWC Extension deactivated - setting is turned off');
return;
}

const extensionHRStart = process.hrtime();
const serverModule = context.asAbsolutePath(
path.join('node_modules', 'lwc-language-server', 'lib', 'server.js')
Expand Down Expand Up @@ -50,6 +82,14 @@ export async function activate(context: vscode.ExtensionContext) {
);
}

// Commands
registerCommands()
.then(disposable => {
if (disposable) {
context.subscriptions.push(disposable);
}
})
.catch();
telemetryService.sendExtensionActivationEvent(extensionHRStart).catch();
}

Expand All @@ -72,7 +112,7 @@ function startLWCLanguageServer(
serverModule: string,
context: vscode.ExtensionContext
) {
const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
const debugOptions = { execArgv: ['--nolazy', '--inspect-brk=6009'] };
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class TelemetryService {
return Promise.resolve(telemetryService);
}
if (!this.setup) {
this.setup = waitForDX()
this.setup = waitForDX(true)
.then((coreDependency: vscode.Extension<any>) => {
coreDependency.exports.telemetryService.showTelemetryMessage();

Expand Down
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-lwc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "es6",
"lib": ["dom", "es2016"],
"sourceMap": true,
"declarationMap": true,
"declaration": true,
"moduleResolution": "node",
"noImplicitAny": true,
Expand Down

0 comments on commit e3b2b30

Please sign in to comment.