This repository was archived by the owner on Mar 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.ts
More file actions
117 lines (94 loc) · 2.51 KB
/
Commands.ts
File metadata and controls
117 lines (94 loc) · 2.51 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// base code https://github.com/Caesar2011/rhinoder
import { existsSync } from "https://deno.land/std@0.74.0/fs/mod.ts";
import { readJsonSync } from "https://raw.githubusercontent.com/crewdevio/Trex/master/temp_deps/writeJson.ts";
import { red, green, yellow } from "https://deno.land/std/fmt/colors.ts";
interface runJson {
config: {
[key: string]: string;
};
files?: string[];
}
/*
* get object data from run file
*/
const data = readJsonSync("./run.json") as runJson;
let throttle = 700;
let timeout: number | null = null;
let errorTrace: string[] = [];
interface Commands {
name: string;
run: string[];
}
const commands: Array<Commands> = [];
function logMessages() {
console.clear();
console.log(green("[Commands]"));
console.log(green("[0] watching files:"));
console.info(
red(
`[1] ${
data?.files
? data?.files
.map((el) => {
console.log(" |- ", yellow(el));
return "";
})
.join("")
: "- watching all files [ .* ]"
} `
)
);
}
if (existsSync("./run.json")) {
if (!data.config) {
errorTrace.push("[Error]: the config key was not found in the run.json file");
} else {
console.log(green("commands starting..."));
setTimeout(logMessages, 1000);
const entries = Object.entries(data.config);
entries.forEach((entries) => {
commands.push({ name: entries[0], run: entries[1].split(" ") });
});
}
} else {
errorTrace.push("[Error]: run.json not found");
}
if (errorTrace.length) {
for (const error of errorTrace) {
throw new Error(red(error) + "\n").message;
}
}
const args: string[] = [];
commands.forEach(({ name }, index) => {
if (name === Deno.args[0]) {
commands[index].run.forEach((el: string) => {
args.push(el.trim());
});
}
});
let task: Deno.Process = startProcess(args);
function startProcess(args: Array<string>): Deno.Process {
if (args.length < 1) {
console.error(red("[Error]: Command not found"));
}
return Deno.run({ cmd: ["deno", "run", ...args] });
}
function runApp() {
logMessages();
task && task.close();
task = startProcess(args);
}
let files: string[] | string = data?.files
? data.files && data.files.length
? data.files
: "."
: ".";
if (import.meta.main) {
for await (const event of Deno.watchFs(files)) {
if (event.kind !== "access") {
if (timeout) clearTimeout(timeout);
console.log(yellow("reloading..."));
timeout = setTimeout(runApp, throttle);
}
}
}