-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
denon.ts
174 lines (143 loc) · 4 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
167
168
169
170
171
172
173
174
// 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,
} from "./src/cli.ts";
import { readConfig, CompleteDenonConfig, reConfig } from "./src/config.ts";
import { parseArgs } from "./src/args.ts";
import { VERSION, BRANCH } from "./info.ts";
const logger = log.create("main");
/** 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 could be used if
* included as a module. */
if (import.meta.main) {
await log.setup({ filter: "INFO" });
const args = parseArgs(Deno.args);
// show version number.
if (BRANCH !== "master") {
logger.info(`v${VERSION}-${BRANCH}`);
} else {
logger.info(`v${VERSION}`);
}
if (args.version) Deno.exit(0);
// check compatibility
// if (!COMPAT[VERSION].includes(Deno.version.deno) && !args.upgrade) {
// logger.error(
// `Your version of denon (${VERSION}) does not support your deno version (${Deno.version.deno})`,
// );
// logger.warning(
// `Upgrade deno by running \`deno upgrade --version ${
// [...COMPAT[VERSION]].pop()
// }\``,
// );
// Deno.exit(1);
// }
await grantPermissions();
// update denon to latest release
if (args.upgrade) {
await upgrade(args.upgrade);
Deno.exit(0);
}
let config = await readConfig(args.config);
// autocomplete(config);
config.args = args;
// show help message.
if (args.help) {
printHelp();
Deno.exit(0);
}
// create configuration file.
// TODO(@qu4k): should be made interactive.
if (args.init) {
await initializeConfig(args.init);
Deno.exit(0);
}
// show all available scripts.
if (args.cmd.length === 0) {
await printAvailableScripts(config);
Deno.exit(0);
}
// const builtIn = ["run", "test", "fmt", "lint"];
const script = args.cmd[0];
// if (!config.scripts[script] && !builtIn.includes(script)) {
// const other = closest(script, Object.keys(config.scripts).concat(builtIn));
// logger.error(
// `Could not find script \`${script}\` did you mean \`${other}\`?`,
// );
// Deno.exit(1);
// }
const denon = new Denon(config);
// 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);
logger.debug("reloading config");
}
}
}
}