forked from reshadi/jakets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.ts
67 lines (61 loc) · 1.98 KB
/
Node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import fs = require("fs");
import * as path from "path";
import {execSync} from "child_process";
import * as Jake from "./Jake";
var NodeDir = ""; //TODO: try to detect the correct path
var Node = NodeDir + "node";
let DefaultSearchPath = [process.cwd(), __dirname];
export function FindModulePath(modulePath: string, additionalLocations?: string[]): string {
let searchDirs = DefaultSearchPath;
if (additionalLocations) {
searchDirs = searchDirs.concat(additionalLocations)
}
for (let i = 0; i < searchDirs.length; ++i) {
let dir = searchDirs[i];
let fullpath = path.join(dir, "node_modules", modulePath);
if (fs.existsSync(fullpath)) {
return fullpath;
}
}
return null;
}
export function GetNodeCommand(
cmdName: string, //default command line
testCmd: string, //command to test if there is one installed locally
nodeCli: string //path to node file
) {
let localCli = path.join(process.cwd(), "node_modules", nodeCli);
let jaketsCli = path.join(__dirname, "node_modules", nodeCli);
let cmd = cmdName;
try {
if (fs.statSync(localCli)) {
cmd = Node + " " + localCli;
} else {
execSync(testCmd); //Confirms the global one exists
}
} catch (e) {
cmd = Node + " " + jaketsCli;
}
Jake.Log("Node command: " + cmd);
return cmd;
}
export function CreateExec(cmd: string) {
return function Exec(args: string | string[], callback, isSilent?: boolean) {
let argsSet: string[];
if (Array.isArray(args)) {
argsSet = args;
} else {
argsSet = [args];
}
argsSet = argsSet.map(function(arg) { return cmd + " " + arg; });
Jake.Exec(argsSet, callback);
}
}
export function CreateNodeExec(
cmdName: string, //default command line
testCmd: string, //command to test if there is one installed locally
nodeCli: string //path to node file
) {
var cmdName = GetNodeCommand(cmdName, testCmd, nodeCli);
return CreateExec(cmdName);
}