1212// Requirements
1313//------------------------------------------------------------------------------
1414
15- var spawn = require ( "child_process" ) . spawn ;
16- var fs = require ( "fs" ) ;
1715var path = require ( "path" ) ;
18- var chalk = require ( "chalk" ) ;
1916var debug = require ( "debug" ) ( "eslint-cli" ) ;
2017var 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+
12551debug ( "START" , process . argv ) ;
12652debug ( "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- } ) ;
0 commit comments