Skip to content

Commit ca74ef8

Browse files
author
ido
committed
make code reuseable
1 parent 5e04d38 commit ca74ef8

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

intellij-common/src/com/wix/nodejs/NodeFinder.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
import java.util.regex.Pattern;
1717

1818
public final class NodeFinder {
19-
public static final String RT_BASE_NAME = SystemInfo.isWindows ? "rt.cmd" : "rt";
19+
// public static final String RT_BASE_NAME = SystemInfo.isWindows ? "rt.cmd" : "rt";
2020
private static final Pattern NVM_NODE_DIR_NAME_PATTERN = Pattern.compile("^v?(\\d+)\\.(\\d+)\\.(\\d+)$");
2121
public static final String NODE_MODULES = "node_modules";
22-
public static final String DEFAULT_RT_BIN = SystemInfo.isWindows ? "node_modules\\.bin\\rt.cmd" : "node_modules/react-templates/bin/rt.js";
2322

2423
// TODO figure out a way to automatically get this path or add it to config
2524
// should read from /usr/local/lib/node_modules/eslint/lib/rules
@@ -29,16 +28,20 @@ public final class NodeFinder {
2928
private NodeFinder() {
3029
}
3130

31+
public static String getBinName(String baseBinName) {
32+
return SystemInfo.isWindows ? baseBinName + ".cmd" : baseBinName;
33+
}
34+
3235
// List infos = ContainerUtil.newArrayList();
3336
// NodeModuleSearchUtil.findModulesWithName(infos, "eslint", project.getBaseDir(), null, false);
3437

35-
@Nullable
36-
public static File findInterpreterInPath() {
37-
return PathEnvironmentVariableUtil.findInPath(RT_BASE_NAME);
38-
}
38+
// @Nullable
39+
// public static File findInterpreterInPath() {
40+
// return PathEnvironmentVariableUtil.findInPath(RT_BASE_NAME);
41+
// }
3942

4043
@NotNull
41-
public static List<File> listPossibleRTExe(String exeFileName) {
44+
public static List<File> searchNodeModulesBin(String exeFileName) {
4245
Set<File> interpreters = ContainerUtil.newLinkedHashSet();
4346
List<File> fromPath = PathEnvironmentVariableUtil.findAllExeFilesInPath(exeFileName);
4447
List<File> nvmInterpreters = listNodeInterpretersFromNvm(exeFileName);
@@ -51,11 +54,35 @@ public static List<File> listPossibleRTExe(String exeFileName) {
5154
return ContainerUtil.newArrayList(interpreters);
5255
}
5356

57+
@NotNull
58+
public static List<File> searchAllScopesForBin(File projectRoot, String exeFileName) {
59+
// List<File> nodeModules = searchProjectNodeModules(projectRoot);
60+
List<File> globalJscsBin = searchNodeModulesBin(exeFileName);
61+
File file = resolvePath(projectRoot, NODE_MODULES, ".bin", exeFileName);
62+
if (file.exists()) {
63+
globalJscsBin.add(file);
64+
}
65+
66+
// if (SystemInfo.isWindows) {
67+
// File file = resolvePath(projectRoot, NODE_MODULES, ".bin", exeFileName);
68+
// if (file.exists()) {
69+
// globalJscsBin.add(file);
70+
// }
71+
// } else {
72+
// File file = resolvePath(projectRoot, NODE_MODULES, ".bin", exeFileName);
73+
// if (file.exists()) {
74+
// globalJscsBin.add(file);
75+
// }
76+
// }
77+
// globalJscsBin.addAll(nodeModules);
78+
return globalJscsBin;
79+
}
80+
5481
// @NotNull
5582
// public static List<File> searchForRTBin(File projectRoot) {
5683
//// List<File> nodeModules = searchProjectNodeModules(projectRoot);
5784
// List<File> globalRTBin = listPossibleRTExe(exeFileName);
58-
//
85+
5986
// if (SystemInfo.isWindows) {
6087
// File file = resolvePath(projectRoot, NODE_MODULES, ".bin", "rt.cmd");
6188
// if (file.exists()) {

intellij-common/src/com/wix/utils/FileUtils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,48 @@ public static VirtualFile findFileByPath(VirtualFile path, String valuePath) {
274274
}
275275
return file;
276276
}
277+
278+
279+
public static String join(String file, String ext) {
280+
return file + '/' + ext;
281+
}
282+
283+
public static String removeExt(String file, String ext) {
284+
if (file.endsWith(ext)) {
285+
return file.replace(ext, "");
286+
}
287+
return file;
288+
}
289+
290+
public static String relativePath(VirtualFile root, VirtualFile file) {
291+
// get project relative path
292+
return file.getPath().substring(root.getPath().length() + 1);
293+
}
294+
295+
public static String getNormalizedPath(int doubleDotCount, String[] pathsOfPath) {
296+
StringBuilder newValuePath = new StringBuilder();
297+
for (int i = 0; i < pathsOfPath.length - doubleDotCount; i++) {
298+
if (0 != i) {
299+
newValuePath.append('/');
300+
}
301+
newValuePath.append(pathsOfPath[i]);
302+
}
303+
return newValuePath.toString();
304+
}
305+
306+
public static int getDoubleDotCount(String valuePath) {
307+
int doubleDotCount = (valuePath.length() - valuePath.replaceAll("\\.\\.", "").length()) / 2;
308+
boolean doubleDotCountTrues = false;
309+
310+
while (!doubleDotCountTrues && 0 != doubleDotCount) {
311+
if (valuePath.startsWith(StringUtil.repeat("../", doubleDotCount))) {
312+
doubleDotCountTrues = true;
313+
} else if (valuePath.startsWith(StringUtil.repeat("../", doubleDotCount - 1) + "..")) {
314+
doubleDotCountTrues = true;
315+
} else {
316+
doubleDotCount--;
317+
}
318+
}
319+
return doubleDotCount;
320+
}
277321
}

0 commit comments

Comments
 (0)