mirror of
https://github.com/easingthemes/ssh-deploy.git
synced 2026-06-05 15:13:46 +08:00
feat!: replace rsyncwrapper with local rsync module
Add src/rsync.js as a drop-in replacement for rsyncwrapper, using child_process.spawn directly. Only implements the options this project uses. Single line change in rsyncCli.js to swap the import. BREAKING CHANGE: rsyncwrapper dependency removed, rsync command is now constructed and executed via a local module using child_process.spawn. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
54
src/rsync.js
Normal file
54
src/rsync.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const escapeSpaces = (str) => (typeof str === 'string' ? str.replace(/\b\s/g, '\\ ') : str);
|
||||
|
||||
const buildRsyncCommand = ({ src, dest, excludeFirst, port, privateKey, args, sshCmdArgs }) => {
|
||||
const cmdParts = [];
|
||||
|
||||
const sources = Array.isArray(src) ? src : [src];
|
||||
cmdParts.push(...sources.map(escapeSpaces));
|
||||
cmdParts.push(escapeSpaces(dest));
|
||||
|
||||
let sshCmd = `ssh -p ${port || 22} -i ${privateKey}`;
|
||||
if (sshCmdArgs && sshCmdArgs.length > 0) {
|
||||
sshCmd += ` ${sshCmdArgs.join(' ')}`;
|
||||
}
|
||||
cmdParts.push('--rsh', `"${sshCmd}"`);
|
||||
|
||||
cmdParts.push('--recursive');
|
||||
|
||||
if (Array.isArray(excludeFirst)) {
|
||||
excludeFirst.forEach((pattern) => {
|
||||
if (pattern) cmdParts.push(`--exclude=${escapeSpaces(pattern)}`);
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(args)) {
|
||||
cmdParts.push(...args);
|
||||
}
|
||||
|
||||
return `rsync ${[...new Set(cmdParts)].join(' ')}`;
|
||||
};
|
||||
|
||||
module.exports = (options, callback) => {
|
||||
const cmd = buildRsyncCommand(options);
|
||||
const noop = () => {};
|
||||
const onStdout = options.onStdout || noop;
|
||||
const onStderr = options.onStderr || noop;
|
||||
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
const proc = spawn('/bin/sh', ['-c', cmd]);
|
||||
|
||||
proc.stdout.on('data', (data) => { onStdout(data); stdout += data; });
|
||||
proc.stderr.on('data', (data) => { onStderr(data); stderr += data; });
|
||||
|
||||
proc.on('exit', (code) => {
|
||||
let error = null;
|
||||
if (code !== 0) {
|
||||
error = new Error(`rsync exited with code ${code}`);
|
||||
error.code = code;
|
||||
}
|
||||
callback(error, stdout, stderr, cmd);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user