forked from denosaurs/denon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
denon.ts
166 lines (138 loc) · 3.44 KB
/
denon.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2020-present the denosaurs team. All rights reserved. MIT license.
import { log } from "./deps.ts";
import { Watcher, FileEvent } from "./src/watcher.ts";
import { Runner } from "./src/runner.ts";
import { Daemon } from "./src/daemon.ts";
import {
printAvailableScripts,
printHelp,
initializeConfig,
grantPermissions,
upgrade,
autocomplete,
} from "./src/cli.ts";
import {
readConfig,
CompleteDenonConfig,
reConfig,
} from "./src/config.ts";
import { parseArgs } from "./src/args.ts";
import { setupLog } from "./src/log.ts";
export const VERSION = "v2.2.0";
export const BRANCH = "master";
/**
* Events you can listen to when creating a `denon`
* instance as module:
* ```typescript
* const denon = new Denon(config);
* for await (let event of denon.run(script)) {
* // event handling here
* }
* ```
*/
export declare type DenonEventType =
| "start"
| "reload"
| "crash"
| "success"
| "exit";
export declare type DenonEvent =
| DenonStartEvent
| DenonReloadEvent
| DenonCrashEvent
| DenonSuccessEvent
| DenonExitEvent;
export declare interface DenonStartEvent {
type: "start";
}
export declare interface DenonReloadEvent {
type: "reload";
change: FileEvent[];
}
export declare interface DenonCrashEvent {
type: "crash";
status: Deno.ProcessStatus;
}
export declare interface DenonSuccessEvent {
type: "success";
status: Deno.ProcessStatus;
}
export declare interface DenonExitEvent {
type: "exit";
}
/**
* Denon instance.
* Holds loaded configuration and handles creation
* of daemons with the `start(script)` method.
*/
export class Denon {
watcher: Watcher;
runner: Runner;
constructor(public config: CompleteDenonConfig) {
this.watcher = new Watcher(config.watcher);
this.runner = new Runner(config, config.args ? config.args.cmd : []);
}
run(script: string): AsyncIterable<DenonEvent> {
return new Daemon(this, script);
}
}
/**
* CLI starts here,
* other than the awesome `denon` cli this is an
* example on how the library should be used if
* included as a module.
*/
if (import.meta.main) {
await setupLog();
await grantPermissions();
const args = parseArgs(Deno.args);
let config = await readConfig(args.config);
await setupLog(config.logger);
autocomplete(config);
config.args = args;
// show help message.
if (args.help) {
printHelp(VERSION);
Deno.exit(0);
}
// show version number.
log.info(VERSION);
if (args.version) Deno.exit(0);
// update denon to latest release
if (args.upgrade) {
await upgrade(args.upgrade);
Deno.exit(0);
}
// create configuration file.
// TODO: should be made interactive.
if (args.init) {
await initializeConfig(args.init);
Deno.exit(0);
}
// show all available scripts.
if (args.cmd.length === 0) {
printAvailableScripts(config);
Deno.exit(0);
}
const script = args.cmd[0];
const denon = new Denon(config);
if (config.logger.fullscreen) console.clear();
if (config.watcher.match) {
log.info(`watching path(s): ${config.watcher.match.join(" ")}`);
}
if (config.watcher.exts) {
log.info(`watching extensions: ${config.watcher.exts.join(",")}`);
}
// TODO(qu4k): events
for await (let event of denon.run(script)) {
if (event.type === "reload") {
if (
event.change.some((_) =>
reConfig.test(_.path) && _.path === config.configPath
)
) {
config = await readConfig(args.config);
}
}
}
}