Retrive tscPath from typescriptServiceClient

This commit is contained in:
Qiming Zhao 2020-04-17 15:47:45 +08:00
parent 52f625c453
commit ef22359a1b
4 changed files with 24 additions and 37 deletions

View file

@ -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<TaskOptions> {
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
}

View file

@ -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)
)
}

View file

@ -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<ForkedTsServerProcess> {
if (this.servicePromise) {
return this.servicePromise
@ -260,7 +265,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient
private async startService(resendModels = false): Promise<ForkedTsServerProcess> {
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
}

View file

@ -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')
}