Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 0e3cf19

Browse files
author
steelbrain
committed
⬆️ Use the new helper modules in worker
1 parent 3d1227f commit 0e3cf19

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

src/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function getIgnoresFile(fileDir) {
4646
return Path.dirname(find(fileDir, '.eslintignore'))
4747
}
4848

49-
export function getCliFromDirectory(path) {
49+
export function getEslintFromDirectory(path) {
5050
try {
5151
return require(Path.join(path, 'lib', 'cli.js'))
5252
} catch (e) {

src/worker.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,53 @@
22
// Note: 'use babel' doesn't work in forked processes
33
process.title = 'linter-eslint helper'
44

5-
const CP = require('childprocess-promise')
6-
const execFileSync = require('child_process').execFileSync
7-
const Path = require('path')
5+
import Path from 'path'
6+
import {execFileSync} from 'child_process'
7+
import CP from 'childprocess-promise'
8+
import resolveEnv from 'resolve-end'
9+
import * as Helpers from './helpers'
810

9-
const resolveEnv = require('resolve-env')
10-
const Helpers = require('./es5-helpers')
11-
12-
const findEslintDir = Helpers.findEslintDir
13-
const find = Helpers.find
14-
const determineConfigFile = Helpers.determineConfigFile
15-
const getEslintCli = Helpers.getEslintCli
1611
const Communication = new CP()
1712

18-
// closed-over module-scope variables
19-
let eslintPath = null
20-
let eslint = null
13+
let eslint
14+
let lastEslintDirectory
2115

22-
Communication.on('JOB', function(job) {
23-
const params = job.Message
24-
const modulesPath = find(params.fileDir, 'node_modules')
25-
const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore'))
26-
let configFile = null
16+
Communication.on('JOB', function(Job) {
2717
global.__LINTER_RESPONSE = []
2818

29-
// Check for config file and determine whether to bail out
30-
configFile = determineConfigFile(params)
19+
const params = Job.Message
20+
const modulesPath = Helpers.getModulesDirectory(params.fileDir)
21+
const ignoreFile = Helpers.getIgnoresFile(params.fileDir)
22+
const configFile = Helpers.getEslintConfig(params.fileDir)
23+
const eslintDirectory = Helpers.getEslintDirectory(params, modulesPath)
3124

3225
if (params.canDisable && configFile === null) {
33-
job.Response = []
34-
return
26+
return Job.Response = []
27+
}
28+
29+
if (eslintDirectory !== lastEslintDirectory) {
30+
lastEslintDirectory = eslintDirectory
31+
eslint = Helpers.getEslintFromDirectory(eslintDirectory)
3532
}
3633

3734
if (modulesPath) {
3835
process.env.NODE_PATH = modulesPath
3936
} else process.env.NODE_PATH = ''
4037
require('module').Module._initPaths()
4138

42-
// Determine which eslint instance to use
43-
const eslintNewPath = findEslintDir(params)
44-
if (eslintNewPath !== eslintPath) {
45-
eslint = getEslintCli(eslintNewPath)
46-
eslintPath = eslintNewPath
47-
}
48-
49-
job.Response = new Promise(function(resolve) {
39+
Job.Response = new Promise(function(resolve) {
5040
let filePath
51-
if (eslintignoreDir) {
52-
filePath = Path.relative(eslintignoreDir, params.filePath)
53-
process.chdir(eslintignoreDir)
41+
if (ignoreFile) {
42+
filePath = Path.relative(ignoreFile, params.filePath)
43+
process.chdir(ignoreFile)
5444
} else {
5545
filePath = Path.basename(params.filePath)
5646
process.chdir(params.fileDir)
5747
}
48+
5849
const argv = [
5950
process.execPath,
60-
eslintPath,
51+
eslintDirectory,
6152
'--stdin',
6253
'--format',
6354
Path.join(__dirname, 'reporter.js')
@@ -77,6 +68,7 @@ Communication.on('JOB', function(job) {
7768
}
7869
argv.push('--stdin-filename', filePath)
7970
process.argv = argv
71+
8072
eslint.execute(process.argv, params.contents)
8173
resolve(global.__LINTER_RESPONSE)
8274
})

0 commit comments

Comments
 (0)