feat!: replace rsyncwrapper with direct child_process.spawn

Remove the outdated rsyncwrapper dependency (unmaintained, uses
deprecated util._extend) and replace with a built-in buildRsyncCommand()
+ spawn implementation. Zero runtime dependencies.

BREAKING CHANGE: rsyncwrapper dependency removed, rsync command is now
constructed and executed directly via child_process.spawn.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dragan Filipovic
2026-04-02 22:50:21 +02:00
parent 2644554bce
commit b11fb7f911
4 changed files with 91 additions and 58 deletions

View File

@@ -1,35 +1,90 @@
const { execSync } = require('child_process');
const nodeRsync = require('rsyncwrapper');
const { execSync, spawn } = require('child_process');
const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
const logCMD = (cmd) => {
const escapeSpaces = (str) => (typeof str === 'string' ? str.replace(/\b\s/g, '\\ ') : str);
const buildRsyncCommand = ({ src, dest, excludeFirst, port, privateKey, args, sshCmdArgs }) => {
const cmdParts = [];
// Sources and destination (with space escaping)
const sources = (Array.isArray(src) ? src : [src]).map(escapeSpaces);
cmdParts.push(...sources);
cmdParts.push(escapeSpaces(dest));
// SSH transport
let sshCmd = `ssh -p ${port || 22} -i ${privateKey}`;
if (sshCmdArgs && sshCmdArgs.length > 0) {
sshCmd += ` ${sshCmdArgs.join(' ')}`;
}
cmdParts.push('--rsh', `"${sshCmd}"`);
// Recursive
cmdParts.push('--recursive');
// Exclude-first patterns
if (excludeFirst && excludeFirst.length > 0) {
excludeFirst.forEach((pattern) => {
if (pattern) cmdParts.push(`--exclude=${escapeSpaces(pattern)}`);
});
}
// User-provided args
if (args && args.length > 0) {
cmdParts.push(...args);
}
// Deduplicate while preserving order
const dedupedParts = [...new Set(cmdParts)];
return `rsync ${dedupedParts.join(' ')}`;
};
const runRsync = async (config) => new Promise((resolve, reject) => {
const cmd = buildRsyncCommand(config);
const logCMD = (c) => {
console.warn('================================================================');
console.log(cmd);
console.log(c);
console.warn('================================================================');
};
try {
nodeRsync(config, (error, stdout, stderr, cmd) => {
if (error) {
console.error('❌ [Rsync] error: ');
console.error(error);
console.error('❌ [Rsync] stderr: ');
console.error(stderr);
console.error('❌️ [Rsync] stdout: ');
console.error(stdout);
console.error('❌ [Rsync] command: ');
logCMD(cmd);
reject(new Error(`${error.message}\n\n${stderr}`));
} else {
console.log('⭐ [Rsync] command finished: ');
logCMD(cmd);
resolve(stdout);
}
});
} catch (error) {
let stdout = '';
let stderr = '';
const proc = spawn('/bin/sh', ['-c', cmd]);
proc.stdout.on('data', (data) => {
const str = data.toString();
console.log(str);
stdout += str;
});
proc.stderr.on('data', (data) => {
const str = data.toString();
console.error(str);
stderr += str;
});
proc.on('exit', (code) => {
if (code !== 0) {
const error = new Error(`rsync exited with code ${code}`);
error.code = code;
console.error('❌ [Rsync] error: ');
console.error(error);
console.error('❌ [Rsync] stderr: ');
console.error(stderr);
console.error('❌️ [Rsync] stdout: ');
console.error(stdout);
console.error('❌ [Rsync] command: ');
logCMD(cmd);
reject(new Error(`${error.message}\n\n${stderr}`));
} else {
console.log('⭐ [Rsync] command finished: ');
logCMD(cmd);
resolve(stdout);
}
});
proc.on('error', (error) => {
console.error('❌ [Rsync] command error: ', error.message, error.stack);
reject(error);
}
});
});
const validateRsync = async () => {
@@ -57,17 +112,8 @@ const rsyncCli = async ({
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
if (exclude && exclude.length > 0) console.log(`[Rsync] excluding folders ${exclude}`);
const defaultOptions = {
ssh: true,
recursive: true,
onStdout: (data) => console.log(data.toString()),
onStderr: (data) => console.error(data.toString())
};
// RSYNC COMMAND
/* eslint-disable object-property-newline */
return nodeRsyncPromise({
...defaultOptions,
return runRsync({
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
privateKey: privateKeyPath, args, sshCmdArgs
});