/
cli.ts
executable file
·65 lines (57 loc) · 1.61 KB
/
cli.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
#!/usr/bin/env -S deno run --unstable -A
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// The script for easily running the Node.js script with Deno using Node.JS
// compatibility library (std/node).
import { createRequire } from "./module.ts";
import "./global.ts";
function usage() {
console.log(`Usage: node [options] <script.js> [arguments]
Runs Node.js script with Deno using Node.js compatibility library.
Options:
-h, --help print node command line options
`);
}
const opts = {
help: false,
};
const args = [...Deno.args];
let arg: string | undefined = undefined;
let script: string | undefined = undefined;
// We don't use std/flags to parse the cli options because we need to separate
// options for node/main.ts and the options for the node.js script.
// deno-lint-ignore no-cond-assign
while (arg = args.shift()) {
if (arg === "-h" || arg === "--help") {
opts.help = true;
continue;
} else if (arg.startsWith("-")) {
// Ignores unknown option
continue;
} else {
// The first non-option is the main Node.js script
script = arg;
// Breaks here to keep the rest of args which will be passed to
// the main Node.js script
break;
}
}
if (opts.help) {
usage();
Deno.exit(0);
}
if (!script) {
usage();
Deno.exit(1);
}
// deno-lint-ignore no-undef
process.argv = ["", script, ...args];
// deno-lint-ignore no-undef
Object.defineProperty(process.argv, "0", {
get() {
return Deno.execPath();
},
});
const require = createRequire(Deno.cwd());
const path = require("path");
const resolved = path.resolve(script);
require(resolved);