Skip to content

Commit 528eb09

Browse files
committed
Find node path with "which node" command.
1 parent 7504f69 commit 528eb09

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

core/ts.core/src/ts/nodejs/NodejsProcessHelper.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
*/
1111
package ts.nodejs;
1212

13+
import java.io.BufferedReader;
1314
import java.io.File;
15+
import java.io.IOException;
16+
import java.io.InputStreamReader;
1417
import java.util.ArrayList;
1518
import java.util.List;
1619

1720
import ts.OS;
21+
import ts.utils.IOUtils;
22+
import ts.utils.StringUtils;
1823

1924
/**
2025
* Node path helper.
@@ -24,14 +29,12 @@ public class NodejsProcessHelper {
2429

2530
private static final String[] WINDOWS_NODE_PATHS = new String[] {
2631
"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" };
2933

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" };
3236

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" };
3538

3639
private NodejsProcessHelper() {
3740
}
@@ -94,7 +97,7 @@ public static File findNode(OS os) {
9497
}
9598
}
9699

97-
return null;
100+
return getNodeLocation(os);
98101
}
99102

100103
private static String getNodeFileName(OS os) {
@@ -104,4 +107,34 @@ private static String getNodeFileName(OS os) {
104107
return "node";
105108
}
106109

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+
}
107140
}

0 commit comments

Comments
 (0)