use workspaceFolder for project root
This commit is contained in:
parent
4671415826
commit
f29a6f424b
4 changed files with 26 additions and 38 deletions
|
@ -1,15 +1,13 @@
|
||||||
import { ChildProcess, spawn } from 'child_process'
|
import { disposeAll, StatusBarItem, TaskOptions, workspace } from 'coc.nvim'
|
||||||
import { disposeAll, StatusBarItem, workspace, TaskOptions } from 'coc.nvim'
|
import { CommandManager } from 'coc.nvim/lib/commands'
|
||||||
import { Command, CommandManager } from 'coc.nvim/lib/commands'
|
import Task from 'coc.nvim/lib/model/task'
|
||||||
import findUp from 'find-up'
|
import findUp from 'find-up'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import readline from 'readline'
|
|
||||||
import { Disposable, Location } from 'vscode-languageserver-protocol'
|
import { Disposable, Location } from 'vscode-languageserver-protocol'
|
||||||
import Uri from 'vscode-uri'
|
import Uri from 'vscode-uri'
|
||||||
import which from 'which'
|
import which from 'which'
|
||||||
import { resolveRoot } from '../utils/fs'
|
import { resolveRoot } from '../utils/fs'
|
||||||
import Task from 'coc.nvim/lib/model/task'
|
|
||||||
|
|
||||||
const TSC = './node_modules/.bin/tsc'
|
const TSC = './node_modules/.bin/tsc'
|
||||||
const countRegex = /Found\s+(\d+)\s+error/
|
const countRegex = /Found\s+(\d+)\s+error/
|
||||||
|
@ -126,13 +124,7 @@ export default class WatchProject implements Disposable {
|
||||||
let docs = workspace.documents
|
let docs = workspace.documents
|
||||||
let idx = docs.findIndex(doc => doc.uri.indexOf(TSC) !== -1)
|
let idx = docs.findIndex(doc => doc.uri.indexOf(TSC) !== -1)
|
||||||
if (idx !== -1) return
|
if (idx !== -1) return
|
||||||
let doc = workspace.getDocument(workspace.bufnr)
|
let cwd = workspace.cwd
|
||||||
let cwd: string
|
|
||||||
if (doc && doc.schema == 'file') {
|
|
||||||
cwd = path.dirname(Uri.parse(doc.uri).fsPath)
|
|
||||||
} else {
|
|
||||||
cwd = workspace.cwd
|
|
||||||
}
|
|
||||||
let res = findUp.sync(['node_modules'], { cwd })
|
let res = findUp.sync(['node_modules'], { cwd })
|
||||||
let cmd: string
|
let cmd: string
|
||||||
let root: string
|
let root: string
|
||||||
|
@ -178,11 +170,3 @@ function executable(command: string): boolean {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function wait(ms: number): Promise<any> {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
setTimeout(() => {
|
|
||||||
resolve()
|
|
||||||
}, ms)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ export interface ITypeScriptServiceClient {
|
||||||
onTypesInstallerInitializationFailed: Event<Proto.TypesInstallerInitializationFailedEventBody>
|
onTypesInstallerInitializationFailed: Event<Proto.TypesInstallerInitializationFailedEventBody>
|
||||||
readonly logger: Logger
|
readonly logger: Logger
|
||||||
|
|
||||||
getProjectRootPath(uri: string): string
|
getProjectRootPath(uri: string): string | null
|
||||||
normalizePath(resource: Uri): string | null
|
normalizePath(resource: Uri): string | null
|
||||||
asUrl(filepath: string): Uri
|
asUrl(filepath: string): Uri
|
||||||
toPath(uri: string): string
|
toPath(uri: string): string
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
import cp from 'child_process'
|
import cp from 'child_process'
|
||||||
import findUp from 'find-up'
|
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import os from 'os'
|
import os from 'os'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
@ -791,20 +790,22 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
public getProjectRootPath(uri: string): string {
|
public getProjectRootPath(uri: string): string | null {
|
||||||
|
let root = workspace.cwd
|
||||||
let u = Uri.parse(uri)
|
let u = Uri.parse(uri)
|
||||||
let { cwd } = workspace
|
if (u.scheme == 'file') {
|
||||||
if (u.scheme != 'file') return cwd
|
let folder = workspace.getWorkspaceFolder(uri)
|
||||||
if (u.fsPath.startsWith(cwd) && cwd != os.homedir()) {
|
if (folder) {
|
||||||
let files = fs.readdirSync(cwd)
|
root = Uri.parse(folder.uri).fsPath
|
||||||
if (files.indexOf('tsconfig.json') !== -1
|
} else {
|
||||||
|| files.indexOf('jsconfig.json') !== -1
|
let filepath = Uri.parse(uri).fsPath
|
||||||
|| files.indexOf('package.json') !== -1) {
|
if (!filepath.startsWith(root)) {
|
||||||
return cwd
|
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 {
|
public configurePlugin(pluginName: string, configuration: {}): any {
|
||||||
|
|
|
@ -30,10 +30,9 @@ function getManager(uri: string): string {
|
||||||
return res.endsWith('yarn.lock') ? 'yarn' : 'npm'
|
return res.endsWith('yarn.lock') ? 'yarn' : 'npm'
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRoot(uri: string): string {
|
function getRoot(): string | null {
|
||||||
let dir = path.dirname(Uri.parse(uri).fsPath)
|
let res = findUp.sync(['package.json'], { cwd: workspace.cwd })
|
||||||
let res = findUp.sync(['package.json'], { cwd: dir })
|
if (!res) return null
|
||||||
if (!res) return dir
|
|
||||||
return path.dirname(res)
|
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> {
|
export async function installModules(uri: string, names: string[]): Promise<void> {
|
||||||
names = distinct(names)
|
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 arr = names.concat(names.map(s => `@types/${s}`))
|
||||||
let statusItem = workspace.createStatusBarItem(99, { progress: true })
|
let statusItem = workspace.createStatusBarItem(99, { progress: true })
|
||||||
statusItem.text = `Checking module ${arr.join(' ')}`
|
statusItem.text = `Checking module ${arr.join(' ')}`
|
||||||
|
|
Loading…
Add table
Reference in a new issue