-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimportNodeScriptPath.ts
More file actions
70 lines (58 loc) · 1.42 KB
/
importNodeScriptPath.ts
File metadata and controls
70 lines (58 loc) · 1.42 KB
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
68
69
70
import { v4 as uuidv4 } from "uuid";
import { writeFileSync } from "fs";
const TMP_DIR = "/tmp/";
const removeExtension = script => script.replace(".ts", "").replace(".js", "");
export default ({
nodeScriptPath,
extension,
mockScriptPath,
nodeCommand,
args,
debugCommand
}) => {
const tmpFile = `${TMP_DIR}/${uuidv4()}.${extension}`;
let file;
let mockScriptIncludes;
if (Array.isArray(mockScriptPath)) {
mockScriptIncludes = mockScriptPath.map(removeExtension);
} else if (typeof mockScriptPath === "string") {
mockScriptIncludes = [removeExtension(mockScriptPath)];
} else {
mockScriptIncludes = [];
}
const nodeScriptInclude = removeExtension(nodeScriptPath);
if (extension === "ts") {
file = `
${mockScriptIncludes
.map(
(script, i) =>
`
import mock${i} from '${script}';
mock${i}();
`
)
.join("\n")}
import '${nodeScriptInclude}';
`;
} else {
file = `
${mockScriptIncludes
.map(
(script, i) =>
`
const mock${i} = require('${script}');
mock${i}();
`
)
.join("\n")}
require('${nodeScriptInclude}');
`;
}
debugCommand(`File to execute: ${file}`);
writeFileSync(tmpFile, file);
const bashCommand = `${nodeCommand} "${tmpFile}" ${args}`;
return {
bashCommand,
tmpFile
};
};