File size: 2,225 Bytes
95f4e64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Runner from "./Runner.js";
import { watch } from "chokidar";
import { posix } from "path";
function debounce(func, wait) {
    let timeout = null;
    return function (...parameters) {
        if (timeout)
            clearTimeout(timeout);
        else
            func(...parameters);
        timeout = setTimeout(() => (timeout = null), wait);
    };
}
export default class Watcher extends Runner {
    constructor(input, options) {
        super(input, options);
        this.watcher = null;
        this.watched = [];
        this.watched =
            options?.watch instanceof Array
                ? options.watch.map((glob) => posix.resolve(glob))
                : [];
    }
    async run() {
        try {
            console.clear();
            await this.build();
            this.execute();
            this.watch();
        }
        catch (error) { }
    }
    async rerun() {
        if (!this.watcher)
            throw "Cannot re-run before a first run";
        if (this.childProcess) {
            this.childProcess?.kill("SIGINT");
            this.childProcess = undefined;
        }
        await this.rebuild();
        // we update the list of watched files
        if (this.buildOutput) {
            this.watch();
        }
        await this.execute();
    }
    async rebuild() {
        if (!this.preserveConsole) {
            console.clear();
        }
        this.dependencies.length = 0;
        if (this.buildOutput) {
            try {
                this.buildOutput = await this.buildContext?.rebuild();
                this.outputCode = this.getOutputCode();
                this.dependencies = this.retrieveDependencies();
            }
            catch (error) {
                this.buildOutput = undefined;
                this.outputCode = "";
            }
        }
        else {
            await this.build();
        }
    }
    watch() {
        void this.watcher?.close();
        this.watcher = watch([
            ...this.dependencies,
            "package.json",
            ...this.watched,
        ]);
        this.watcher.on("change", debounce(this.rerun.bind(this), 300));
        this.watcher.on("unlink", debounce(this.rerun.bind(this), 300));
    }
}