UNPKG

@tangelo/tangelo-configuration-toolkit

Version:

Tangelo Configuration Toolkit is a command-line toolkit which offers support for developing a Tangelo configuration.

44 lines (35 loc) 1.19 kB
const {NodeSSH} = require('node-ssh'); const pLimit = require('p-limit'); module.exports = class RemoteExec { #ftpConfig; #queue = []; constructor(ftpConfig) { this.#ftpConfig = ftpConfig; } add(cmd, msg) { if (!this.#queue.find(e => e[0]==cmd)) this.#queue.push([cmd, msg]); return this; } async process() { if (!this.#queue[0]) return; const sshI = new NodeSSH(); const limit = pLimit(this.#ftpConfig.parallel); await sshI.connect(this.#ftpConfig) // set up connection once .then(() => Promise.all(this.#queue.map(([cmd, msg]) => limit( () => sshI.execCommand(cmd).then(result => { if (result.stderr) _warn(result.stderr); else if (msg === 'STDOUT') _write(result.stdout); else if (msg) _info(msg === 'CMD' ? cmd : msg); }) )))) .catch(err => { if (err.code=='ECONNRESET' || err.code=='ECONNABORTED') _warn(`Server aborted connection. Retrying.`); else _warn(err); this.process(); // retry set up connection }) .finally(() => { sshI.dispose(); this.#queue = []; }); } };