1/*@internal*/ 2namespace ts.server { 3 declare const addEventListener: any; 4 declare const removeEventListener: any; 5 function findArgumentStringArray(argName: string): readonly string[] { 6 const arg = findArgument(argName); 7 if (arg === undefined) { 8 return emptyArray; 9 } 10 return arg.split(",").filter(name => name !== ""); 11 } 12 13 export function getLogLevel(level: string | undefined) { 14 if (level) { 15 const l = level.toLowerCase(); 16 for (const name in LogLevel) { 17 if (isNaN(+name) && l === name.toLowerCase()) { 18 return <LogLevel><any>LogLevel[name]; 19 } 20 } 21 } 22 return undefined; 23 } 24 25 export interface StartInput { 26 args: readonly string[]; 27 logger: Logger; 28 cancellationToken: ServerCancellationToken; 29 serverMode: LanguageServiceMode | undefined; 30 unknownServerMode?: string; 31 startSession: (option: StartSessionOptions, logger: Logger, cancellationToken: ServerCancellationToken) => void; 32 } 33 function start({ args, logger, cancellationToken, serverMode, unknownServerMode, startSession: startServer }: StartInput, platform: string) { 34 const syntaxOnly = hasArgument("--syntaxOnly"); 35 36 logger.info(`Starting TS Server`); 37 logger.info(`Version: ${version}`); 38 logger.info(`Arguments: ${args.join(" ")}`); 39 logger.info(`Platform: ${platform} NodeVersion: ${getNodeMajorVersion()} CaseSensitive: ${sys.useCaseSensitiveFileNames}`); 40 logger.info(`ServerMode: ${serverMode} syntaxOnly: ${syntaxOnly} hasUnknownServerMode: ${unknownServerMode}`); 41 42 setStackTraceLimit(); 43 44 if (Debug.isDebugging) { 45 Debug.enableDebugInfo(); 46 } 47 48 if (sys.tryEnableSourceMapsForHost && /^development$/i.test(sys.getEnvironmentVariable("NODE_ENV"))) { 49 sys.tryEnableSourceMapsForHost(); 50 } 51 52 // Overwrites the current console messages to instead write to 53 // the log. This is so that language service plugins which use 54 // console.log don't break the message passing between tsserver 55 // and the client 56 console.log = (...args) => logger.msg(args.length === 1 ? args[0] : args.join(", "), Msg.Info); 57 console.warn = (...args) => logger.msg(args.length === 1 ? args[0] : args.join(", "), Msg.Err); 58 console.error = (...args) => logger.msg(args.length === 1 ? args[0] : args.join(", "), Msg.Err); 59 60 startServer( 61 { 62 globalPlugins: findArgumentStringArray("--globalPlugins"), 63 pluginProbeLocations: findArgumentStringArray("--pluginProbeLocations"), 64 allowLocalPluginLoads: hasArgument("--allowLocalPluginLoads"), 65 useSingleInferredProject: hasArgument("--useSingleInferredProject"), 66 useInferredProjectPerProjectRoot: hasArgument("--useInferredProjectPerProjectRoot"), 67 suppressDiagnosticEvents: hasArgument("--suppressDiagnosticEvents"), 68 noGetErrOnBackgroundUpdate: hasArgument("--noGetErrOnBackgroundUpdate"), 69 syntaxOnly, 70 serverMode 71 }, 72 logger, 73 cancellationToken 74 ); 75 } 76 77 setStackTraceLimit(); 78 // Cannot check process var directory in webworker so has to be typeof check here 79 if (typeof process !== "undefined") { 80 start(initializeNodeSystem(), require("os").platform()); 81 } 82 else { 83 // Get args from first message 84 const listener = (e: any) => { 85 removeEventListener("message", listener); 86 const args = e.data; 87 start(initializeWebSystem(args), "web"); 88 }; 89 addEventListener("message", listener); 90 } 91} 92