-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnx.ts
82 lines (70 loc) · 1.54 KB
/
dnx.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Require:
* Deno v1.0.0-rc1
* Install:
* deno install -f --unstable --allow-run --allow-env --allow-read --allow-write https://raw.githubusercontent.com/axetroy/deno_dnx/master/dnx.ts
* Usage:
* dnx run --allow-net --allow-read https://deno.land/std@v1.0.0-rc1/http/file_server.ts
*/
import { join } from "https://deno.land/std@v1.0.0-rc1/path/mod.ts";
import { ensureDir } from "https://deno.land/std@v1.0.0-rc1/fs/ensure_dir.ts";
import {
signal,
Disposable,
} from "https://deno.land/std@v1.0.0-rc1/signal/mod.ts";
const {
run,
execPath,
env,
cwd,
args,
exit,
makeTempDirSync,
removeSync,
dir,
} = Deno;
const dnxCacheDir = join(dir("cache") as string, "dnx");
await ensureDir(dnxCacheDir);
const tempDir = makeTempDirSync({
dir: dnxCacheDir,
});
const ps = run({
cwd: cwd(),
cmd: [execPath()].concat(args),
env: {
...env.toObject(),
DENO_DIR: tempDir,
},
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
const signalListener = signal(
Deno.Signal.SIGUSR1,
Deno.Signal.SIGUSR2,
Deno.Signal.SIGINT,
);
const disposables: Disposable[] = [
signalListener,
{
dispose: () => {
ps.stdin?.close();
ps.stdout?.close();
ps.stderr?.close();
ps.close();
},
},
];
(async () => {
for await (const _ of signalListener) {
removeSync(tempDir, { recursive: true });
ps.kill(1);
exit(1);
}
});
const status = await ps.status();
for (const entry of disposables) {
entry.dispose();
}
removeSync(tempDir, { recursive: true });
exit(status.code);