resolve tsc from workspaceFolder

This commit is contained in:
chemzqm 2019-04-19 18:27:37 +08:00
parent fe5c4d84dd
commit 8f7bf42a36
3 changed files with 7 additions and 44 deletions

View file

@ -7,9 +7,7 @@ import path from 'path'
import { Disposable, Location } from 'vscode-languageserver-protocol'
import Uri from 'vscode-uri'
import which from 'which'
import { resolveRoot } from '../utils/fs'
const TSC = './node_modules/.bin/tsc'
const countRegex = /Found\s+(\d+)\s+error/
const errorRegex = /^(.+)\((\d+),(\d+)\):\s(\w+)\sTS(\d+):\s*(.+)$/
@ -31,7 +29,6 @@ export default class WatchProject implements Disposable {
public static readonly id: string = 'tsserver.watchBuild'
public static readonly startTexts: string[] = ['Starting compilation in watch mode', 'Starting incremental compilation']
private statusItem: StatusBarItem
private isRunning = false
private task: Task
private options: TaskOptions
@ -69,6 +66,7 @@ export default class WatchProject implements Disposable {
private async check(): Promise<void> {
let running = await this.task.running
if (running) {
this.options = this.getOptions()
this.statusItem.isProgress = false
this.statusItem.text = '?'
this.statusItem.show()
@ -82,8 +80,6 @@ export default class WatchProject implements Disposable {
}
private onStop(): void {
let { nvim } = workspace
this.isRunning = false
this.statusItem.hide()
}
@ -121,13 +117,10 @@ export default class WatchProject implements Disposable {
}
public getOptions(): TaskOptions {
let docs = workspace.documents
let idx = docs.findIndex(doc => doc.uri.indexOf(TSC) !== -1)
if (idx !== -1) return
let cwd = workspace.cwd
let res = findUp.sync(['node_modules'], { cwd })
let cmd: string
let res = findUp.sync(['node_modules'], { cwd: workspace.root })
let root: string
let cmd: string
// let root: string
if (!res) {
if (executable('tsc')) {
cmd = 'tsc'
@ -144,11 +137,12 @@ export default class WatchProject implements Disposable {
workspace.showMessage(`Local & global tsc not found`, 'error')
return
}
let configRoot = resolveRoot(cwd, ['tsconfig.json'])
if (!configRoot) {
let find = findUp.sync(['tsconfig.json'], { cwd: root })
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'))
return {
cmd,

View file

@ -1,30 +0,0 @@
import path from 'path'
import os from 'os'
import fs from 'fs'
export function getParentDirs(fullpath: string): string[] {
let obj = path.parse(fullpath)
if (!obj || !obj.root) return []
let res = []
let p = path.dirname(fullpath)
while (p && p !== obj.root) {
res.push(p)
p = path.dirname(p)
}
return res
}
export function resolveRoot(cwd: string, subs: string[], home?: string): string | null {
home = home || os.homedir()
let { root } = path.parse(cwd)
let paths = getParentDirs(cwd)
paths.unshift(cwd)
for (let p of paths) {
if (p == home || p == root) return null
for (let sub of subs) {
let d = path.join(p, sub)
if (fs.existsSync(d)) return path.dirname(d)
}
}
return root
}

View file

@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import fs from 'fs'
import path from 'path'
import { getParentDirs } from './fs'
import { workspace, Uri } from 'coc.nvim'
import API from './api'
import { TypeScriptServiceConfiguration } from './configuration'