replace find-up

This commit is contained in:
chemzqm 2019-06-13 22:52:57 +08:00
parent 0d84cdf870
commit 3e925b9d7c
3 changed files with 11 additions and 16 deletions

View file

@ -470,7 +470,6 @@
"@chemzqm/tsconfig": "^0.0.3", "@chemzqm/tsconfig": "^0.0.3",
"@chemzqm/tslint-config": "^1.0.18", "@chemzqm/tslint-config": "^1.0.18",
"@types/fast-diff": "^1.2.0", "@types/fast-diff": "^1.2.0",
"@types/find-up": "^2.1.1",
"@types/node": "^12.0.7", "@types/node": "^12.0.7",
"coc.nvim": "^0.0.69", "coc.nvim": "^0.0.69",
"rimraf": "^2.6.3", "rimraf": "^2.6.3",
@ -478,7 +477,6 @@
}, },
"dependencies": { "dependencies": {
"fast-diff": "^1.2.0", "fast-diff": "^1.2.0",
"find-up": "^4.0.0",
"semver": "^6.1.1", "semver": "^6.1.1",
"tslib": "^1.9.3", "tslib": "^1.9.3",
"typescript": "3.5.1", "typescript": "3.5.1",

View file

@ -1,7 +1,6 @@
import { Uri, disposeAll, StatusBarItem, TaskOptions, workspace } from 'coc.nvim' import { Uri, disposeAll, StatusBarItem, TaskOptions, workspace } from 'coc.nvim'
import { CommandManager } from 'coc.nvim/lib/commands' import { CommandManager } from 'coc.nvim/lib/commands'
import Task from 'coc.nvim/lib/model/task' import Task from 'coc.nvim/lib/model/task'
import findUp from 'find-up'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { Disposable, Location } from 'vscode-languageserver-protocol' import { Disposable, Location } from 'vscode-languageserver-protocol'
@ -37,7 +36,7 @@ export default class WatchProject implements Disposable {
this.statusItem = workspace.createStatusBarItem(1, { progress: true }) this.statusItem = workspace.createStatusBarItem(1, { progress: true })
let task = this.task = workspace.createTask('TSC') let task = this.task = workspace.createTask('TSC')
this.disposables.push(commandManager.registerCommand(WatchProject.id, async () => { this.disposables.push(commandManager.registerCommand(WatchProject.id, async () => {
let opts = this.options = this.getOptions() let opts = this.options = await this.getOptions()
await this.start(opts) await this.start(opts)
})) }))
task.onExit(code => { task.onExit(code => {
@ -65,7 +64,7 @@ export default class WatchProject implements Disposable {
private async check(): Promise<void> { private async check(): Promise<void> {
let running = await this.task.running let running = await this.task.running
if (running) { if (running) {
this.options = this.getOptions() this.options = await this.getOptions()
this.statusItem.isProgress = false this.statusItem.isProgress = false
this.statusItem.text = '?' this.statusItem.text = '?'
this.statusItem.show() this.statusItem.show()
@ -115,8 +114,8 @@ export default class WatchProject implements Disposable {
} }
} }
public getOptions(): TaskOptions { public async getOptions(): Promise<TaskOptions> {
let res = findUp.sync(['node_modules'], { cwd: workspace.root }) let res = await workspace.findUp(['node_modules'])
let root: string let root: string
let cmd: string let cmd: string
// let root: string // let root: string
@ -136,7 +135,7 @@ export default class WatchProject implements Disposable {
workspace.showMessage(`Local & global tsc not found`, 'error') workspace.showMessage(`Local & global tsc not found`, 'error')
return return
} }
let find = findUp.sync(['tsconfig.json'], { cwd: root }) let find = await workspace.findUp(['tsconfig.json'])
if (!find) { if (!find) {
workspace.showMessage('tsconfig.json not found!', 'error') workspace.showMessage('tsconfig.json not found!', 'error')
return return

View file

@ -1,7 +1,6 @@
import { exec } from 'child_process' import { exec } from 'child_process'
import path from 'path' import path from 'path'
import { Uri, workspace } from 'coc.nvim' import { Uri, workspace } from 'coc.nvim'
import findUp from 'find-up'
export function runCommand(cmd: string, cwd: string, timeout?: number): Promise<string> { export function runCommand(cmd: string, cwd: string, timeout?: number): Promise<string> {
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
@ -22,15 +21,14 @@ export function runCommand(cmd: string, cwd: string, timeout?: number): Promise<
}) })
} }
function getManager(uri: string): string { async function getManager(): Promise<string> {
let dir = path.dirname(Uri.parse(uri).fsPath) let res = await workspace.findUp(['yarn.lock', 'package-lock.json'])
let res = findUp.sync(['yarn.lock', 'package-lock.json'], { cwd: dir })
if (!res) return 'yarn' if (!res) return 'yarn'
return res.endsWith('yarn.lock') ? 'yarn' : 'npm' return res.endsWith('yarn.lock') ? 'yarn' : 'npm'
} }
function getRoot(): string | null { async function getRoot(): Promise<string | null> {
let res = findUp.sync(['package.json'], { cwd: workspace.cwd }) let res = await workspace.findUp(['package.json'])
if (!res) return null if (!res) return null
return path.dirname(res) return path.dirname(res)
} }
@ -74,7 +72,7 @@ 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() let root = await getRoot()
if (!root) { if (!root) {
workspace.showMessage(`package.json not found from cwd: ${workspace.cwd}`, 'error') workspace.showMessage(`package.json not found from cwd: ${workspace.cwd}`, 'error')
return return
@ -88,7 +86,7 @@ export async function installModules(uri: string, names: string[]): Promise<void
return exists ? name : null return exists ? name : null
}) })
})) }))
let manager = getManager(uri) let manager = await getManager()
exists = exists.filter(s => s != null) exists = exists.filter(s => s != null)
if (!exists.length) return if (!exists.length) return
let devs = exists.filter(s => s.startsWith('@types')) let devs = exists.filter(s => s.startsWith('@types'))