11import * as vscode from 'vscode' ;
22import * as request from 'request' ;
33import { kebabCase } from 'lodash' ;
4- import { LDFlagValue , LDFeatureStore , LDStreamProcessor } from 'ldclient -node' ;
5- import InMemoryFeatureStore = require( 'ldclient -node/feature_store' ) ;
6- import StreamProcessor = require( 'ldclient -node/streaming' ) ;
7- import Requestor = require( 'ldclient -node/requestor' ) ;
4+ import { LDFlagValue , LDFeatureStore , LDStreamProcessor } from 'launchdarkly -node-server-sdk ' ;
5+ import InMemoryFeatureStore = require( 'launchdarkly -node-server-sdk /feature_store' ) ;
6+ import StreamProcessor = require( 'launchdarkly -node-server-sdk /streaming' ) ;
7+ import Requestor = require( 'launchdarkly -node-server-sdk /requestor' ) ;
88import * as url from 'url' ;
99import opn = require( 'opn' ) ;
1010
@@ -73,7 +73,11 @@ export function generateHoverString(flag: LDFlagValue) {
7373 Default variation: ${ JSON . stringify ( flag . variations [ flag . fallthrough . variation ] ) }
7474 Off variation: ${ JSON . stringify ( flag . variations [ flag . offVariation ] ) }
7575 ${ plural ( flag . prerequisites . length , 'prerequisite' , 'prerequisites' ) }
76- ${ plural ( flag . targets . reduce ( ( acc , curr ) => acc + curr . values . length , 0 ) , 'user target' , 'user targets' ) }
76+ ${ plural (
77+ flag . targets . reduce ( ( acc , curr ) => acc + curr . values . length , 0 ) ,
78+ 'user target' ,
79+ 'user targets' ,
80+ ) }
7781 ${ plural ( flag . rules . length , 'rule' , 'rules' ) } ` ;
7882}
7983
@@ -83,15 +87,17 @@ function plural(count: number, singular: string, plural: string) {
8387
8488export function isPrecedingCharStringDelimeter ( document : vscode . TextDocument , position : vscode . Position ) {
8589 const range = document . getWordRangeAtPosition ( position , FLAG_KEY_REGEX ) ;
86-
90+ if ( ! range || ! range . start || range . start . character === 0 ) {
91+ return false ;
92+ }
8793 const c = new vscode . Range (
8894 range . start . line ,
8995 candidateTextStartLocation ( range . start . character ) ,
9096 range . start . line ,
9197 range . start . character ,
9298 ) ;
9399 const candidate = document . getText ( c ) . trim ( ) ;
94- return STRING_DELIMETERS . indexOf ( candidate ) >= 0 ;
100+ return STRING_DELIMETERS . indexOf ( candidate ) !== - 1 ;
95101}
96102
97103const candidateTextStartLocation = ( char : number ) => ( char === 1 ? 0 : char - 2 ) ;
@@ -109,30 +115,29 @@ export class LDFlagManager implements IFlagManager {
109115 constructor ( ctx : vscode . ExtensionContext , settings : IConfiguration ) {
110116 this . settings = Object . assign ( { } , settings ) ;
111117 let config = this . config ( settings ) ;
112- if ( settings . sdkKey ) {
113- this . updateProcessor = StreamProcessor ( settings . sdkKey , config , Requestor ( settings . sdkKey , config ) ) ;
114- this . start ( ) ;
115- } else {
116- vscode . window . showWarningMessage (
117- '[LaunchDarkly] sdkKey is not set. LaunchDarkly language support is unavailable.' ,
118- ) ;
118+ if ( ! settings . sdkKey ) {
119+ console . warn ( 'LaunchDarkly sdkKey is not set. Language support is unavailable.' ) ;
120+ return ;
119121 }
122+
123+ this . updateProcessor = StreamProcessor ( settings . sdkKey , config , Requestor ( settings . sdkKey , config ) ) ;
124+ this . start ( ) ;
120125 }
121126
122127 start ( ) {
123128 this . updateProcessor &&
124- this . updateProcessor . start ( function ( err ) {
129+ this . updateProcessor . start ( err => {
125130 if ( err ) {
126- console . log ( err ) ;
127- let errMsg = `[LaunchDarkly] Unexpected error retrieving flags.${
128- this . settings . baseUri != DEFAULT_BASE_URI || this . settings . streamUri != DEFAULT_STREAM_URI
129- ? ' Please make sure your configured base and stream URIs are correct'
130- : ''
131- } `;
132- vscode . window . showErrorMessage ( errMsg ) ;
133- } else {
134- process . nextTick ( function ( ) { } ) ;
131+ let errMsg ;
132+ if ( err . message ) {
133+ errMsg = `Error retrieving feature flags: ${ err . message } .` ;
134+ } else {
135+ console . error ( err ) ;
136+ errMsg = `Unexpected error retrieving flags.` ;
137+ }
138+ vscode . window . showErrorMessage ( `[LaunchDarkly] ${ errMsg } ` ) ;
135139 }
140+ process . nextTick ( function ( ) { } ) ;
136141 } ) ;
137142 }
138143
@@ -157,9 +162,9 @@ export class LDFlagManager implements IFlagManager {
157162 streamUri : settings . streamUri ,
158163 featureStore : this . store ,
159164 logger : {
160- debug : msg => {
161- console . log ( msg ) ;
162- } ,
165+ debug : console . log ,
166+ warn : console . warn ,
167+ error : console . error ,
163168 } ,
164169 userAgent : 'VSCodeExtension/' + package_json . version ,
165170 } ;
0 commit comments