'use strict' ; import * as path from 'path' ; import * as fs from 'fs' ; import traverseWebpack from './traverseWebpack' ; import { window , workspace , ExtensionContext , WorkspaceConfiguration , Disposable } from 'vscode' ; import { LanguageClient , LanguageClientOptions , ServerOptions , TransportKind , CancellationToken , Middleware , DidChangeConfigurationNotification , ConfigurationParams } from 'vscode-languageclient' ; interface ReactEdSettings { maxNumberOfProblems : number ; } let client ; namespace Configuration { let configurationListener ; export function computeConfiguration ( params , _token , _next ) { if ( ! params . items ) { return null ; } let result = [ ] ; for ( let item of params . items ) { if ( item . section ) { result . push ( null ) ; continue ; } let config ; if ( item . scopeUri ) { config = workspace . getConfiguration ( 'reacted' , client . protocol2CodeConverter . asUri ( item . scopeUri ) ) ; } else { config = workspace . getConfiguration ( 'reacted' ) ; } result . push ( { maxNumberOfProblems : config . get ( 'maxNumberOfProblems' ) } ) ; } return result ; } export function initialize ( ) { configurationListener = workspace . onDidChangeConfiguration ( ( ) => { client . sendNotification ( DidChangeConfigurationNotification . type , { settings : null } ) ; } ) ; } export function dispose ( ) { if ( configurationListener ) { configurationListener . dispose ( ) ; } } } export function activate ( context ) { window . showInformationMessage ( 'Started ReactEd' ) ; function pathExists ( p ) { try { fs . accessSync ( p ) ; } catch ( err ) { return false ; } return true ; } /**** Parsing through the webpack config file for bundle location ****/ const WebpackPath = path . join ( workspace . rootPath , 'webpack.config.js' ) ; window . showInformationMessage ( WebpackPath ) ; if ( pathExists ( WebpackPath ) ) { let content = fs . readFileSync ( WebpackPath , 'utf-8' ) ; const filepathReg = /.*path:.*'|.*path:.*"/g ; const filenameReg = /.*filename:.*'|.*filename:.*"/g ; let filepathLine = content . match ( filepathReg ) ; let filenameLine = content . match ( filenameReg ) ; let filesReg = /'.*'|".*"/g ; let filepath = filepathLine [ 0 ] . match ( filesReg ) ; let filename = filenameLine [ 0 ] . match ( filesReg ) ; let bundle = path . join ( workspace . rootPath , filepath [ 0 ] . slice ( 1 , filepath [ 0 ] . length - 1 ) , filename [ 0 ] . slice ( 1 , filename [ 0 ] . length - 1 ) ) ; /****if found bundle then traversing the bundle file ****/ if ( pathExists ( bundle ) ) { window . showInformationMessage ( 'Bundle Found..Starting Parse' ) ; let traverse = new traverseWebpack ( ) ; traverse . grepWithFs ( bundle ) ; let debounce = false ; fs . watch ( bundle , { encoding : 'buffer' } , ( event , filename ) => { if ( event === 'change' && ! debounce && filename ) { debounce = true ; setTimeout ( ( ) => { debounce = false ; } , 5000 ) ; traverse . grepWithFs ( bundle ) ; } } ) ; } else { window . showInformationMessage ( 'Unable to find webpack bundle' ) ; } } let serverModule = context . asAbsolutePath ( path . join ( 'server' , 'server.js' ) ) ; let debugOptions = { execArgv : [ "--nolazy" , "--inspect=6009" ] } ; let serverOptions = { run : { module : serverModule , transport : TransportKind . ipc } , debug : { module : serverModule , transport : TransportKind . ipc , options : debugOptions } } ; let middleware = { workspace : { configuration : Configuration . computeConfiguration } } ; let clientOptions = { documentSelector : [ { scheme : 'file' , language : 'javascript' } , { scheme : 'file' , language : 'javascriptreact' } ] , synchronize : { fileEvents : workspace . createFileSystemWatcher ( '**/.clientrc' ) } , middleware : middleware } ; client = new LanguageClient ( 'languageServerExample' , 'Language Server Example' , serverOptions , clientOptions ) ; client . start ( ) ; } export function deactivate ( ) { if ( ! client ) { return undefined ; } Configuration . dispose ( ) ; return client . stop ( ) ; }