From ef22359a1baf2645fa9c21443c6c594f02ba22c2 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Fri, 17 Apr 2020 15:47:45 +0800 Subject: [PATCH] Retrive tscPath from typescriptServiceClient --- src/server/features/watchBuild.ts | 42 ++++++--------------------- src/server/languageProvider.ts | 3 +- src/server/typescriptServiceClient.ts | 12 ++++++-- src/server/utils/versionProvider.ts | 4 +++ 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/server/features/watchBuild.ts b/src/server/features/watchBuild.ts index 0704fb5..401dd37 100644 --- a/src/server/features/watchBuild.ts +++ b/src/server/features/watchBuild.ts @@ -1,10 +1,9 @@ -import { Uri, disposeAll, StatusBarItem, TaskOptions, workspace } from 'coc.nvim' +import { disposeAll, StatusBarItem, TaskOptions, Uri, workspace } from 'coc.nvim' import { CommandManager } from 'coc.nvim/lib/commands' import Task from 'coc.nvim/lib/model/task' -import fs from 'fs' import path from 'path' import { Disposable, Location } from 'vscode-languageserver-protocol' -import which from 'which' +import TypeScriptServiceClient from '../typescriptServiceClient' const countRegex = /Found\s+(\d+)\s+error/ const errorRegex = /^(.+)\((\d+),(\d+)\):\s(\w+)\sTS(\d+):\s*(.+)$/ @@ -31,7 +30,8 @@ export default class WatchProject implements Disposable { private options: TaskOptions public constructor( - commandManager: CommandManager + commandManager: CommandManager, + private client: TypeScriptServiceClient ) { this.statusItem = workspace.createStatusBarItem(1, { progress: true }) let task = this.task = workspace.createTask('TSC') @@ -115,36 +115,21 @@ export default class WatchProject implements Disposable { } public async getOptions(): Promise { - let res = await workspace.findUp(['node_modules']) - let root: string - let cmd: string - if (res) { - let file = path.join(path.dirname(res), 'node_modules/.bin/tsc') - if (fs.existsSync(file)) { - cmd = file - root = path.dirname(res) - } - } - if (!cmd) { - if (executable('tsc')) { - cmd = 'tsc' - root = workspace.cwd - } - } - if (!cmd) { + let { tscPath } = this.client + if (!tscPath) { workspace.showMessage(`Local & global tsc not found`, 'error') return } + let cmd: string let find = await workspace.findUp(['tsconfig.json']) if (!find) { workspace.showMessage('tsconfig.json not found!', 'error') return } - let configRoot = path.dirname(find) - let configPath = path.relative(root, path.join(configRoot, 'tsconfig.json')) + let root = path.dirname(find) return { cmd, - args: ['-p', configPath, '--watch', 'true', '--pretty', 'false'], + args: ['-p', 'tsconfig.json', '--watch', 'true', '--pretty', 'false'], cwd: root } } @@ -153,12 +138,3 @@ export default class WatchProject implements Disposable { disposeAll(this.disposables) } } - -function executable(command: string): boolean { - try { - which.sync(command) - } catch (e) { - return false - } - return true -} diff --git a/src/server/languageProvider.ts b/src/server/languageProvider.ts index d65f41c..ef35820 100644 --- a/src/server/languageProvider.ts +++ b/src/server/languageProvider.ts @@ -269,8 +269,9 @@ export default class LanguageProvider { } if (this.description.id == 'typescript') { + // this.client.apiVersion this.disposables.push( - new WatchBuild(commands) + new WatchBuild(commands, this.client) ) } diff --git a/src/server/typescriptServiceClient.ts b/src/server/typescriptServiceClient.ts index 83cc089..772893f 100644 --- a/src/server/typescriptServiceClient.ts +++ b/src/server/typescriptServiceClient.ts @@ -95,6 +95,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient Proto.TypesInstallerInitializationFailedEventBody >() private _apiVersion: API + private _tscPath: string private readonly disposables: Disposable[] = [] private isRestarting = false @@ -235,6 +236,10 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient return this._apiVersion } + public get tscPath(): string { + return this._tscPath + } + private service(): Thenable { if (this.servicePromise) { return this.servicePromise @@ -260,7 +265,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient private async startService(resendModels = false): Promise { const { ignoreLocalTsserver } = this.configuration - let currentVersion + let currentVersion: TypeScriptVersion if (!ignoreLocalTsserver) currentVersion = this.versionProvider.getLocalVersion() if (!currentVersion || !fs.existsSync(currentVersion.tsServerPath)) { currentVersion = this.versionProvider.getDefaultVersion() @@ -274,6 +279,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient return } this._apiVersion = currentVersion.version + this._tscPath = currentVersion.tscPath this.versionStatus.onDidChangeTypeScriptVersion(currentVersion) this.lastError = null const tsServerForkArgs = await this.getTsServerArgs() @@ -281,8 +287,8 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient const maxTsServerMemory = this._configuration.maxTsServerMemory const options = { execArgv: [ - ...(debugPort ? [`--inspect=${debugPort}`] : []), // [`--debug-brk=5859`] - ...(maxTsServerMemory ? [`--max-old-space-size=${maxTsServerMemory}`] : []), + ...(debugPort ? [`--inspect=${debugPort}`] : []), // [`--debug-brk=5859`] + ...(maxTsServerMemory ? [`--max-old-space-size=${maxTsServerMemory}`] : []), ], cwd: workspace.root } diff --git a/src/server/utils/versionProvider.ts b/src/server/utils/versionProvider.ts index 0bff15a..eac8a85 100644 --- a/src/server/utils/versionProvider.ts +++ b/src/server/utils/versionProvider.ts @@ -20,6 +20,10 @@ export class TypeScriptVersion { this._api = null } + public get tscPath(): string { + return path.resolve(this.path, '../bin/tsc') + } + public get tsServerPath(): string { return path.resolve(this.path, 'tsserver.js') }