Skip to content

Commit

Permalink
feat: find eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Sep 14, 2022
1 parent 055d196 commit c05f68c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
28 changes: 28 additions & 0 deletions src/eslint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import log from './log'
import { Files } from 'vscode-languageserver/node'
import { workspacePath } from './global'
import { exec } from 'child_process'

type PKG_MANAGERS = { agent: 'pnpm' | 'npm' | 'yarn', path: string }
export const resolveESLintPath = () => Files.resolve('eslint', workspacePath, workspacePath, message => { /**/ })
.catch(() => {
log('Failed to resolve local ESLint. Try Globally.')
return Promise.allSettled<PKG_MANAGERS>(
[ 'pnpm root -g', 'npm root -g', 'yarn global dir' ].map(item =>
new Promise((resolve, reject) =>
exec(item, (error, stdout) => {
if (error) {
reject(error)
return
}
resolve({
agent: item.split(' ')[0] as 'pnpm' | 'npm' | 'yarn',
path: stdout.toString().trim(),
})
}))),
).then(results => {
const agent = results.filter(({ status }) => status === 'fulfilled')[0] as PromiseFulfilledResult<PKG_MANAGERS>

return Files.resolve('eslint', agent.value.path, workspacePath, message => { /**/ })
}).catch(() => log('Failed to resolve global ESLint. Please instal ESLint first.'))
})
18 changes: 0 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import vscode from 'vscode'
import path from 'path'
import fs from 'fs'

// vscode.Task

export const getTextBylines = (startLine: number, endLine?: number) =>
vscode.window.activeTextEditor?.document.getText(
new vscode.Range(
Expand All @@ -12,22 +10,6 @@ export const getTextBylines = (startLine: number, endLine?: number) =>
),
)

/**
* Find a ESLint installation at a given root path.
* @param rootPath the root path.
* @returns the eslint installation or the unresolved command eslint.
*/
export async function findEslint(rootPath: string): Promise<string> {
const platform = process.platform
if (platform === 'win32' && await existFile(path.join(rootPath, 'node_modules', '.bin', 'eslint.cmd'))) {
return path.join('.', 'node_modules', '.bin', 'eslint.cmd')
} else if ((platform === 'linux' || platform === 'darwin') && await existFile(path.join(rootPath, 'node_modules', '.bin', 'eslint'))) {
return path.join('.', 'node_modules', '.bin', 'eslint')
} else {
return 'eslint'
}
}

function existFile(file: string): Promise<boolean> {
return new Promise<boolean>((resolve, _reject) => {
fs.stat(file, (error, stats) => {
Expand Down

0 comments on commit c05f68c

Please sign in to comment.