10
10
*/
11
11
package ts .nodejs ;
12
12
13
+ import java .io .BufferedReader ;
13
14
import java .io .File ;
15
+ import java .io .IOException ;
16
+ import java .io .InputStreamReader ;
14
17
import java .util .ArrayList ;
15
18
import java .util .List ;
16
19
17
20
import ts .OS ;
21
+ import ts .utils .IOUtils ;
22
+ import ts .utils .StringUtils ;
18
23
19
24
/**
20
25
* Node path helper.
@@ -24,14 +29,12 @@ public class NodejsProcessHelper {
24
29
25
30
private static final String [] WINDOWS_NODE_PATHS = new String [] {
26
31
"C:/Program Files/nodejs/node.exe" .replace ('/' , File .separatorChar ),
27
- "C:/Program Files (x86)/nodejs/node.exe" .replace ('/' ,
28
- File .separatorChar ), "node" };
32
+ "C:/Program Files (x86)/nodejs/node.exe" .replace ('/' , File .separatorChar ), "node" };
29
33
30
- private static final String [] MACOS_NODE_PATHS = new String [] {
31
- "/usr/local/bin/node" , "/opt/local/bin/node" , " node" };
34
+ private static final String [] MACOS_NODE_PATHS = new String [] { "/usr/local/bin/node" , "/opt/local/bin/node" ,
35
+ "node" };
32
36
33
- private static final String [] LINUX_NODE_PATHS = new String [] {
34
- "/usr/local/bin/node" , "node" };
37
+ private static final String [] LINUX_NODE_PATHS = new String [] { "/usr/local/bin/node" , "node" };
35
38
36
39
private NodejsProcessHelper () {
37
40
}
@@ -94,7 +97,7 @@ public static File findNode(OS os) {
94
97
}
95
98
}
96
99
97
- return null ;
100
+ return getNodeLocation ( os ) ;
98
101
}
99
102
100
103
private static String getNodeFileName (OS os ) {
@@ -104,4 +107,34 @@ private static String getNodeFileName(OS os) {
104
107
return "node" ;
105
108
}
106
109
110
+ /**
111
+ * Returns the node.js location by using command "which node".
112
+ *
113
+ * @param os
114
+ * @return the node.js location by using command "which node".
115
+ */
116
+ private static File getNodeLocation (OS os ) {
117
+ String [] command = new String [] { "/bin/bash" , "-c" , "which node" };
118
+ if (os == OS .Windows ) {
119
+ command = new String [] { "cmd" , "/c" , "where node" };
120
+ } else {
121
+ command = new String [] { "/bin/bash" , "-c" , "which node" };
122
+ }
123
+ BufferedReader reader = null ;
124
+ try {
125
+ Process p = Runtime .getRuntime ().exec (command );
126
+ reader = new BufferedReader (new InputStreamReader (p .getInputStream ()));
127
+ String nodeFile = reader .readLine ();
128
+ if (StringUtils .isEmpty (nodeFile )) {
129
+ return null ;
130
+ }
131
+ File f = new File (nodeFile );
132
+ return f .exists () ? f : null ;
133
+ } catch (IOException e ) {
134
+ //e.printStackTrace();
135
+ } finally {
136
+ IOUtils .closeQuietly (reader );
137
+ }
138
+ return null ;
139
+ }
107
140
}
0 commit comments