use workspaceFolder for project root

This commit is contained in:
chemzqm 2019-04-19 17:42:55 +08:00
parent 4671415826
commit f29a6f424b
4 changed files with 26 additions and 38 deletions

View file

@ -1,15 +1,13 @@
import { ChildProcess, spawn } from 'child_process'
import { disposeAll, StatusBarItem, workspace, TaskOptions } from 'coc.nvim'
import { Command, CommandManager } from 'coc.nvim/lib/commands'
import { disposeAll, StatusBarItem, TaskOptions, workspace } from 'coc.nvim'
import { CommandManager } from 'coc.nvim/lib/commands'
import Task from 'coc.nvim/lib/model/task'
import findUp from 'find-up'
import fs from 'fs'
import path from 'path'
import readline from 'readline'
import { Disposable, Location } from 'vscode-languageserver-protocol'
import Uri from 'vscode-uri'
import which from 'which'
import { resolveRoot } from '../utils/fs'
import Task from 'coc.nvim/lib/model/task'
const TSC = './node_modules/.bin/tsc'
const countRegex = /Found\s+(\d+)\s+error/
@ -126,13 +124,7 @@ export default class WatchProject implements Disposable {
let docs = workspace.documents
let idx = docs.findIndex(doc => doc.uri.indexOf(TSC) !== -1)
if (idx !== -1) return
let doc = workspace.getDocument(workspace.bufnr)
let cwd: string
if (doc && doc.schema == 'file') {
cwd = path.dirname(Uri.parse(doc.uri).fsPath)
} else {
cwd = workspace.cwd
}
let cwd = workspace.cwd
let res = findUp.sync(['node_modules'], { cwd })
let cmd: string
let root: string
@ -178,11 +170,3 @@ function executable(command: string): boolean {
}
return true
}
function wait(ms: number): Promise<any> {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, ms)
})
}

View file

@ -76,7 +76,7 @@ export interface ITypeScriptServiceClient {
onTypesInstallerInitializationFailed: Event<Proto.TypesInstallerInitializationFailedEventBody>
readonly logger: Logger
getProjectRootPath(uri: string): string
getProjectRootPath(uri: string): string | null
normalizePath(resource: Uri): string | null
asUrl(filepath: string): Uri
toPath(uri: string): string

View file

@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import cp from 'child_process'
import findUp from 'find-up'
import fs from 'fs'
import os from 'os'
import path from 'path'
@ -791,20 +790,22 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient
return args
}
public getProjectRootPath(uri: string): string {
public getProjectRootPath(uri: string): string | null {
let root = workspace.cwd
let u = Uri.parse(uri)
let { cwd } = workspace
if (u.scheme != 'file') return cwd
if (u.fsPath.startsWith(cwd) && cwd != os.homedir()) {
let files = fs.readdirSync(cwd)
if (files.indexOf('tsconfig.json') !== -1
|| files.indexOf('jsconfig.json') !== -1
|| files.indexOf('package.json') !== -1) {
return cwd
if (u.scheme == 'file') {
let folder = workspace.getWorkspaceFolder(uri)
if (folder) {
root = Uri.parse(folder.uri).fsPath
} else {
let filepath = Uri.parse(uri).fsPath
if (!filepath.startsWith(root)) {
root = path.dirname(filepath)
}
}
}
let res = findUp.sync(['tsconfig.json', 'jsconfig.json'], { cwd: path.dirname(u.fsPath) })
return res ? path.dirname(res) : workspace.cwd
if (root == os.homedir()) return null
return root
}
public configurePlugin(pluginName: string, configuration: {}): any {

View file

@ -30,10 +30,9 @@ function getManager(uri: string): string {
return res.endsWith('yarn.lock') ? 'yarn' : 'npm'
}
function getRoot(uri: string): string {
let dir = path.dirname(Uri.parse(uri).fsPath)
let res = findUp.sync(['package.json'], { cwd: dir })
if (!res) return dir
function getRoot(): string | null {
let res = findUp.sync(['package.json'], { cwd: workspace.cwd })
if (!res) return null
return path.dirname(res)
}
@ -76,7 +75,11 @@ export function distinct<T>(array: T[], keyFn?: (t: T) => string): T[] {
export async function installModules(uri: string, names: string[]): Promise<void> {
names = distinct(names)
let root = getRoot(uri)
let root = getRoot()
if (!root) {
workspace.showMessage(`package.json not found from cwd: ${workspace.cwd}`, 'error')
return
}
let arr = names.concat(names.map(s => `@types/${s}`))
let statusItem = workspace.createStatusBarItem(99, { progress: true })
statusItem.text = `Checking module ${arr.join(' ')}`