File size: 2,751 Bytes
95f4e64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node
import esrun from "./index.js";
const { argv } = process;
const nodeOptionPrefix = "--node-";
const argumentOptions = {
    "--watch": "watch",
    "-w": "watch",
    "--inspect": "inspect",
    "-i": "inspect",
    "--preserveConsole": "preserveConsole",
    "--noFileConstants": "noFileConstants",
    "--tsconfig": "tsconfig",
    "--send-code-mode": "sendCodeMode",
    "--sudo": "sudo",
};
const options = {
    watch: false,
    inspect: false,
    preserveConsole: false,
    noFileConstants: false,
    sudo: false,
    tsconfig: undefined,
    node: {},
    sendCodeMode: undefined,
};
let argsOffset = 2;
let argument;
if (argv.length < argsOffset) {
    console.log("Missing typescript input file");
    process.exit(0);
}
while ((argument = argv[argsOffset]).startsWith("--")) {
    const [command, parameters] = getCommandAndParameters(argument);
    if (command in argumentOptions) {
        options[argumentOptions[command]] = parameters;
        argsOffset++;
    }
    else if (command.startsWith(nodeOptionPrefix)) {
        options.node[command.slice(nodeOptionPrefix.length)] = parameters;
        argsOffset++;
    }
    else {
        console.log(`Unknown option ${command}`);
        process.exit(9);
    }
}
if (typeof options.tsconfig === "boolean") {
    console.log("Missing value for the '--tsconfig' parameter. Did you forget to add a \"=\"?");
    console.log("Example of valid syntax: esrun --tsconfig=/path/to/my/tsconfig.json fileToExecute.ts");
    process.exit(9);
}
const tsConfigFile = options.tsconfig instanceof Array
    ? options.tsconfig.join(",")
    : options.tsconfig;
esrun(argv[argsOffset], {
    args: argv.slice(argsOffset + 1),
    watch: options.watch,
    tsConfigFile,
    inspect: !!options.inspect,
    preserveConsole: !!options.preserveConsole,
    fileConstants: !options.noFileConstants,
    nodeOptions: options.node,
    sendCodeMode: !Array.isArray(options.sendCodeMode)
        ? undefined
        : options.sendCodeMode[0] === "cliParameters"
            ? "cliParameters"
            : options.sendCodeMode[0] === "temporaryFile"
                ? "temporaryFile"
                : undefined,
}).catch((error) => {
    console.error(error);
    process.exit(1);
});
function getCommandAndParameters(argument) {
    let colonIndex = argument.indexOf(":");
    if (colonIndex === -1)
        colonIndex = Infinity;
    let equalIndex = argument.indexOf("=");
    if (equalIndex === -1)
        equalIndex = Infinity;
    const separatorIndex = Math.min(colonIndex, equalIndex);
    if (separatorIndex === Infinity)
        return [argument, true];
    return [
        argument.slice(0, separatorIndex),
        argument.slice(separatorIndex + 1).split(","),
    ];
}