aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/UtilManager.ts
blob: c1234be6c6aeb9c22ef7494920b1263e6c330701 (plain)
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
import ApiManager, { Registration } from "./ApiManager";
import { Method } from "../RouteManager";
import { exec } from 'child_process';
import { command_line } from "../ActionUtilities";
import RouteSubscriber from "../RouteSubscriber";

export default class UtilManager extends ApiManager {

    protected initialize(register: Registration): void {

        register({
            method: Method.GET,
            subscription: new RouteSubscriber("environment").add("key"),
            onValidation: ({ req, res }) => res.send(process.env[req.params.key])
        });

        register({
            method: Method.GET,
            subscription: "/pull",
            onValidation: async ({ res }) => {
                return new Promise<void>(resolve => {
                    exec('"C:\\Program Files\\Git\\git-bash.exe" -c "git pull"', err => {
                        if (err) {
                            res.send(err.message);
                            return;
                        }
                        res.redirect("/");
                        resolve();
                    });
                });
            }
        });

        register({
            method: Method.GET,
            subscription: "/buxton",
            onValidation: async ({ res }) => {
                let cwd = '../scraping/buxton';

                let onResolved = (stdout: string) => { console.log(stdout); res.redirect("/"); };
                let onRejected = (err: any) => { console.error(err.message); res.send(err); };
                let tryPython3 = () => command_line('python3 scraper.py', cwd).then(onResolved, onRejected);

                return command_line('python scraper.py', cwd).then(onResolved, tryPython3);
            },
        });

        register({
            method: Method.GET,
            subscription: "/version",
            onValidation: ({ res }) => {
                return new Promise<void>(resolve => {
                    exec('"C:\\Program Files\\Git\\bin\\git.exe" rev-parse HEAD', (err, stdout) => {
                        if (err) {
                            res.send(err.message);
                            return;
                        }
                        res.send(stdout);
                    });
                    resolve();
                });
            }
        });

    }

}