diff --git a/src/server/features/watchBuild.ts b/src/server/features/watchBuild.ts index 2d8dea7..a1663c2 100644 --- a/src/server/features/watchBuild.ts +++ b/src/server/features/watchBuild.ts @@ -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 { - return new Promise(resolve => { - setTimeout(() => { - resolve() - }, ms) - }) -} diff --git a/src/server/typescriptService.ts b/src/server/typescriptService.ts index a5a55ae..f612f67 100644 --- a/src/server/typescriptService.ts +++ b/src/server/typescriptService.ts @@ -76,7 +76,7 @@ export interface ITypeScriptServiceClient { onTypesInstallerInitializationFailed: Event 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 diff --git a/src/server/typescriptServiceClient.ts b/src/server/typescriptServiceClient.ts index bb06b33..655f53e 100644 --- a/src/server/typescriptServiceClient.ts +++ b/src/server/typescriptServiceClient.ts @@ -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 { diff --git a/src/server/utils/modules.ts b/src/server/utils/modules.ts index 4453ca2..9c99c95 100644 --- a/src/server/utils/modules.ts +++ b/src/server/utils/modules.ts @@ -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(array: T[], keyFn?: (t: T) => string): T[] { export async function installModules(uri: string, names: string[]): Promise { 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(' ')}`