Skip to content
This repository was archived by the owner on Nov 22, 2021. It is now read-only.

Commit 7904e44

Browse files
committed
Fix: Refactoring and Stop spawn.
No longer `eslint-cli` does not spawn a child process to run a local ESLint. It runs that in own process merely.
1 parent b2087cd commit 7904e44

File tree

3 files changed

+88
-99
lines changed

3 files changed

+88
-99
lines changed

bin/eslint-cli.js

Lines changed: 13 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -12,100 +12,24 @@
1212
// Requirements
1313
//------------------------------------------------------------------------------
1414

15-
var spawn = require("child_process").spawn;
16-
var fs = require("fs");
1715
var path = require("path");
18-
var chalk = require("chalk");
1916
var debug = require("debug")("eslint-cli");
2017
var resolve = require("resolve").sync;
2118

22-
var cwd = process.cwd();
23-
2419
//------------------------------------------------------------------------------
2520
// Helpers
2621
//------------------------------------------------------------------------------
2722

28-
/**
29-
* Checks whether or not the file which is at given path exists.
30-
*
31-
* @param {string} filePath - A file path to check.
32-
* @returns {boolean} `true` if the file which is at given path exists.
33-
*/
34-
function exists(filePath) {
35-
try {
36-
return fs.statSync(filePath).isFile();
37-
}
38-
catch (err) {
39-
return false;
40-
}
41-
}
42-
43-
/**
44-
* Executes a given JavaScript file with the same arguments as this.
45-
*
46-
* @param {string} filePath - A file path to execute.
47-
* @returns {child_process.ChildProcess|null} A child process or null.
48-
*/
49-
function execute(filePath) {
50-
var command = process.argv[0];
51-
var args = process.argv.slice(1);
52-
args[0] = filePath;
53-
54-
debug("EXECUTE", command, args);
55-
56-
try {
57-
return spawn(command, args, {stdio: "inherit"});
58-
}
59-
catch (err) {
60-
debug("FAILED TO EXECUTE", err.stack);
61-
return null;
62-
}
63-
}
64-
65-
/**
66-
* Finds and tries executing "./bin/eslint.js".
67-
*
68-
* This is useful to ESLint contributors.
69-
* ESLint's repository has "./bin/eslint.js".
70-
*
71-
* @returns {child_process.ChildProcess|null} A child process or null.
72-
*/
73-
function tryExecutingBinEslintJs() {
74-
var dir = cwd;
75-
var prevDir = dir;
76-
do {
77-
var binPath = path.join(dir, "bin", "eslint.js");
78-
if (exists(binPath)) {
79-
debug("FOUND", binPath);
80-
return execute(binPath);
81-
}
82-
debug("NOT FOUND", binPath);
83-
84-
// Finish if package.json is found.
85-
if (exists(path.join(dir, "package.json"))) {
86-
break;
87-
}
88-
89-
// Go to next.
90-
prevDir = dir;
91-
dir = path.resolve(dir, "..");
92-
}
93-
while (dir !== prevDir);
94-
95-
debug("NOT FOUND", "\"./bin/eslint.js\"");
96-
return null;
97-
}
98-
9923
/**
10024
* Finds and tries executing a local eslint module.
10125
*
102-
* @returns {child_process.ChildProcess|null} A child process or null.
26+
* @param {string} basedir - A path of the directory that it starts searching.
27+
* @returns {string|null} The path of a local eslint module.
10328
*/
104-
function tryExecutingESLint() {
29+
function getLocalEslint(basedir) {
10530
var indexPath = null;
10631
try {
107-
indexPath = resolve("eslint", {basedir: cwd});
108-
debug("FOUND", indexPath);
32+
indexPath = resolve("eslint", {basedir: basedir});
10933
}
11034
catch (err) {
11135
debug("NOT FOUND", "\"eslint\"");
@@ -114,33 +38,27 @@ function tryExecutingESLint() {
11438

11539
var binPath = path.resolve(path.dirname(indexPath), "..", "bin", "eslint.js");
11640
debug("FOUND", binPath);
117-
return execute(binPath);
41+
return binPath;
11842
}
11943

12044
//------------------------------------------------------------------------------
12145
// Main
12246
//------------------------------------------------------------------------------
12347
/* eslint-disable no-process, no-console */
12448

49+
var cwd = process.cwd();
50+
12551
debug("START", process.argv);
12652
debug("ROOT", cwd);
12753

128-
var task = tryExecutingBinEslintJs() || tryExecutingESLint();
129-
130-
if (task == null) {
131-
console.error(chalk.red.bold(
54+
var binPath = getLocalEslint(cwd) || require("../lib/get-bin-eslint-js")(cwd);
55+
if (binPath != null) {
56+
require(binPath);
57+
}
58+
else {
59+
console.error(require("chalk").red.bold(
13260
"Cannot find local ESLint!\n" +
13361
"Please install ESLint by `npm install eslint --save-dev`.\n"
13462
));
13563
process.exit(1);
13664
}
137-
138-
debug("PID", task.pid);
139-
140-
task.on("exit", function(exitCode) {
141-
debug("EXIT", exitCode);
142-
process.exit(exitCode);
143-
});
144-
task.on("error", function(error) {
145-
debug("ERROR", error.stack);
146-
});

lib/get-bin-eslint-js.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2015 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
7+
"use strict";
8+
9+
//------------------------------------------------------------------------------
10+
// Requirements
11+
//------------------------------------------------------------------------------
12+
13+
var fs = require("fs");
14+
var path = require("path");
15+
var debug = require("debug")("eslint-cli");
16+
17+
//------------------------------------------------------------------------------
18+
// Helpers
19+
//------------------------------------------------------------------------------
20+
21+
/**
22+
* Checks whether or not the file which is at given path exists.
23+
*
24+
* @param {string} filePath - A file path to check.
25+
* @returns {boolean} `true` if the file which is at given path exists.
26+
*/
27+
function exists(filePath) {
28+
try {
29+
return fs.statSync(filePath).isFile();
30+
}
31+
catch (err) {
32+
return false;
33+
}
34+
}
35+
36+
//------------------------------------------------------------------------------
37+
// Public Interface
38+
//------------------------------------------------------------------------------
39+
40+
/**
41+
* Finds and tries executing "./bin/eslint.js".
42+
*
43+
* This is useful to ESLint contributors.
44+
* ESLint's repository has "./bin/eslint.js".
45+
*
46+
* @param {string} basedir - A path of the directory that it starts searching.
47+
* @returns {string|null} The path of "./bin/eslint.js"
48+
*/
49+
module.exports = function getBinEslintJs(basedir) {
50+
var dir = basedir;
51+
var prevDir = dir;
52+
do {
53+
var binPath = path.join(dir, "bin", "eslint.js");
54+
if (exists(binPath)) {
55+
debug("FOUND", binPath);
56+
return binPath;
57+
}
58+
debug("NOT FOUND", binPath);
59+
60+
// Finish if package.json is found.
61+
if (exists(path.join(dir, "package.json"))) {
62+
break;
63+
}
64+
65+
// Go to next.
66+
prevDir = dir;
67+
dir = path.resolve(dir, "..");
68+
}
69+
while (dir !== prevDir);
70+
71+
debug("NOT FOUND", "\"./bin/eslint.js\"");
72+
return null;
73+
};

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
22
"name": "eslint-cli",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "The local eslint executor.",
5-
"files": [
6-
"bin"
7-
],
5+
"files": ["bin", "lib"],
86
"bin": {
97
"eslint": "bin/eslint-cli.js",
108
"eslint-cli": "bin/eslint-cli.js"

0 commit comments

Comments
 (0)