summaryrefslogtreecommitdiffstats
path: root/pkg/lib/cockpit-rsync-plugin.js
blob: bb26be322f59360c10de16dbc3aa2ddb5202864c (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
import child_process from "child_process";

const config = {};

function init(options) {
    config.dest = options.dest || "";
    config.source = options.source || "dist/";
    config.ssh_host = process.env.RSYNC || process.env.RSYNC_DEVEL;

    // ensure the target directory exists
    if (config.ssh_host) {
        config.rsync_dir = process.env.RSYNC ? "/usr/local/share/cockpit/" : "~/.local/share/cockpit/";
        child_process.spawnSync("ssh", [config.ssh_host, "mkdir", "-p", config.rsync_dir], { stdio: "inherit" });
    }
}

function run(callback) {
    if (config.ssh_host) {
        const proc = child_process.spawn("rsync", ["--recursive", "--info=PROGRESS2", "--delete",
            config.source, config.ssh_host + ":" + config.rsync_dir + config.dest], { stdio: "inherit" });
        proc.on('close', (code) => {
            if (code !== 0) {
                process.exit(1);
            } else {
                callback();
            }
        });
    } else {
        callback();
    }
}

export const cockpitRsyncEsbuildPlugin = options => ({
    name: 'cockpitRsyncPlugin',
    setup(build) {
        init(options || {});
        build.onEnd(result => result.errors.length === 0 ? run(() => {}) : {});
    },
});

export class CockpitRsyncWebpackPlugin {
    constructor(options) {
        init(options || {});
    }

    apply(compiler) {
        compiler.hooks.afterEmit.tapAsync('WebpackHookPlugin', (_compilation, callback) => run(callback));
    }
}