-
Notifications
You must be signed in to change notification settings - Fork 1
/
vl.ts
executable file
·82 lines (72 loc) · 2.06 KB
/
vl.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
#!/usr/bin/env deno run --allow-read
//
// Copyright 2022 Joona Piirainen
// MIT License
//
import { Colors, fs, path, process, url } from "./deps.ts";
import "./globals.ts";
import "./globals.d.ts";
const main = async () => {
try {
if (["--version", "-v"].includes(Deno.args[0])) {
printVersionInfo();
return (process.exitCode = 0);
}
const fArg = Deno.args.find((a) => !a.startsWith("--"));
// no <script> argument provided
if (fArg === "-" || fArg == null) {
printUsage();
return (process.exitCode = 2);
}
await runScript(fArg);
} catch (e) {
console.error(e);
}
};
const writeAndImport = async (
source: string,
filePath: string,
origin = filePath,
) => {
await Deno.writeTextFile(filePath, source);
const wait = runScript(filePath, origin);
await Deno.remove(filePath);
await wait;
};
const runScript = async (filePath: string, origin = filePath) => {
const ext = path.extname(filePath);
if (ext == "") {
const tempFileName = fs.existsSync(filePath)
? `${path.basename(filePath)}-${Math.random().toString(36).substr(2)}.mjs`
: `${path.basename(filePath)}.mjs`;
return await writeAndImport(
await Deno.readTextFile(filePath),
path.join(path.dirname(filePath), tempFileName),
origin,
);
}
const __fileName = path.resolve(origin);
const __dirName = path.dirname(__fileName);
Object.assign(globalThis, { __fileName, __dirName });
await import(url.pathToFileURL(filePath).toString());
};
const printVersionInfo = () =>
console.log(`
Version info:
Violet version: 1.0
Deno version: ${Deno.version.deno}
V8 version: ${Deno.version.v8}
TypeScript version: ${Deno.version.typescript}
`);
const printUsage = () =>
console.log(`
${Colors.bgBrightMagenta(Colors.black(" Violet "))}
Usage:
vl [options] <script>
Options:
--quiet : don't echo commands
--shell=<path> : custom shell binary
--prefix=<command> : prefix all commands
--version, -v : print version info
`);
await main();