| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import child_process from 'node:child_process'; |
| 3 |
import process from 'node:process'; |
| 4 |
|
| 5 |
const config = {}; |
| 6 |
|
| 7 |
function init(options) { |
| 8 |
config.dest = options.dest || ""; |
| 9 |
config.source = options.source || "dist/"; |
| 10 |
config.ssh_host = process.env.RSYNC || process.env.RSYNC_DEVEL; |
| 11 |
|
| 12 |
// ensure the target directory exists |
| 13 |
if (config.ssh_host) { |
| 14 |
config.rsync_dir = process.env.RSYNC ? "/usr/local/share/cockpit/" : "~/.local/share/cockpit/"; |
| 15 |
child_process.spawnSync("ssh", [config.ssh_host, "mkdir", "-p", config.rsync_dir], { stdio: "inherit" }); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
function run(callback) { |
| 20 |
if (config.ssh_host) { |
| 21 |
const proc = child_process.spawn("rsync", ["--recursive", "--info=PROGRESS2", "--delete", |
| 22 |
config.source, config.ssh_host + ":" + config.rsync_dir + config.dest], { stdio: "inherit" }); |
| 23 |
proc.on('close', (code) => { |
| 24 |
if (code !== 0) { |
| 25 |
process.exit(1); |
| 26 |
} else { |
| 27 |
callback(); |
| 28 |
} |
| 29 |
}); |
| 30 |
} else { |
| 31 |
callback(); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
export const cockpitRsyncEsbuildPlugin = options => ({ |
| 36 |
name: 'cockpitRsyncPlugin', |
| 37 |
setup(build) { |
| 38 |
init(options || {}); |
| 39 |
build.onEnd(result => result.errors.length === 0 ? run(() => {}) : {}); |
| 40 |
}, |
| 41 |
}); |
| 42 |
|
| 43 |
export class CockpitRsyncWebpackPlugin { |
| 44 |
constructor(options) { |
| 45 |
init(options || {}); |
| 46 |
} |
| 47 |
|
| 48 |
apply(compiler) { |
| 49 |
compiler.hooks.afterEmit.tapAsync('WebpackHookPlugin', (_compilation, callback) => run(callback)); |
| 50 |
} |
| 51 |
} |