diff --git a/dist/index.js b/dist/index.js index 6234f45..f42f78e 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -(()=>{var e={505:(e,s,o)=>{const{existsSync:r,mkdirSync:t,writeFileSync:n,unlink:c}=o(147);const{join:i}=o(17);const validateDir=e=>{if(!e){console.warn("⚠️ [DIR] dir is not defined");return}if(r(e)){console.log(`✅ [DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`);t(e);console.log("✅ [DIR] dir created.")};const handleError=(e,s)=>{if(s){throw new Error(e)}console.warn(e)};const writeToFile=({dir:e,filename:s,content:o,isRequired:t,mode:c="0644"})=>{validateDir(e);const a=i(e,s);if(r(a)){const e=`⚠️ [FILE] ${a} Required file exist.`;handleError(e,t);return}try{console.log(`[FILE] writing ${a} file ...`,o.length);n(a,o,{encoding:"utf8",mode:c})}catch(e){const s=`⚠️[FILE] Writing to file error. filePath: ${a}, message: ${e.message}`;handleError(s,t)}};const deleteFile=({dir:e,filename:s,isRequired:o})=>{validateDir(e);const t=i(e,s);if(r(t)){const e=`⚠️ [FILE] ${t} Required file exist.`;handleError(e,o);return}try{console.log(`[FILE] Deleting ${t} file ...`);c(t,(e=>{if(e){throw new Error(e)}}))}catch(e){const s=`⚠️[FILE] Deleting file error. filePath: ${t}, message: ${e.message}`;handleError(s,o)}};const validateRequiredInputs=e=>{const s=Object.keys(e);const o=s.filter((s=>{const o=e[s];if(!o){console.error(`❌ [INPUTS] ${s} is mandatory`)}return o}));if(o.length!==s.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,s)=>s.toUpperCase()));e.exports={writeToFile:writeToFile,deleteFile:deleteFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,s,o)=>{const{snakeToCamel:r}=o(505);const t=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER","SCRIPT_BEFORE_REQUIRED","SCRIPT_AFTER_REQUIRED"];const n=process.env.GITHUB_WORKSPACE;const c=process.env.REMOTE_USER||process.env.INPUT_REMOTE_USER;const i={source:"",target:`/home/${c}/`,exclude:"",args:"-rlgoDzvc -i",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:`deploy_key_${c}_${Date.now()}`};const a={githubWorkspace:n};t.forEach((e=>{const s=r(e.toLowerCase());const o=process.env[e]||process.env[`INPUT_${e}`]||i[s];const t=o===undefined?i[s]:o;let c=t;switch(s){case"source":c=t.split(" ").map((e=>`${n}/${e}`));break;case"args":c=t.split(" ");break;case"exclude":case"sshCmdArgs":c=t.split(",").map((e=>e.trim()));break}a[s]=c}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,s,o)=>{const{exec:r}=o(81);const t=o(113);const{sshServer:n,githubWorkspace:c,remotePort:i}=o(229);const{writeToFile:a,deleteFile:l}=o(505);const handleError=(e,s,o)=>{if(s){o(new Error(e))}else{console.warn(e)}};const remoteCmd=async(e,s,o,d)=>new Promise(((p,g)=>{const u=t.randomUUID();const h=`local_ssh_script-${d}-${u}.sh`;try{a({dir:c,filename:h,content:e});const t=1e4;const d=(process.env.RSYNC_STDOUT||"").substring(0,t);console.log(`Executing remote script: ssh -i ${s} ${n}`);r(`DEBIAN_FRONTEND=noninteractive ssh -p ${i||22} -i ${s} -o StrictHostKeyChecking=no ${n} 'RSYNC_STDOUT="${d}" bash -s' < ${h}`,((e,s="",r="")=>{if(e){const t=`⚠️ [CMD] Remote script failed: ${e.message}`;console.warn(`${t} \n`,s,r);handleError(t,o,g)}else{const e=s.substring(0,t);console.log("✅ [CMD] Remote script executed. \n",e,r);l({dir:c,filename:h});console.log("✅ [FILE] Script file deleted.");p(e)}}))}catch(e){handleError(e.message,o,g)}}));e.exports={remoteCmdBefore:async(e,s,o)=>remoteCmd(e,s,o,"before"),remoteCmdAfter:async(e,s,o)=>remoteCmd(e,s,o,"after")}},447:(e,s,o)=>{const{execSync:r,spawn:t}=o(81);const escapeSpaces=e=>typeof e==="string"?e.replace(/\b\s/g,"\\ "):e;const buildRsyncCommand=({src:e,dest:s,excludeFirst:o,port:r,privateKey:t,args:n,sshCmdArgs:c})=>{const i=[];const a=(Array.isArray(e)?e:[e]).map(escapeSpaces);i.push(...a);i.push(escapeSpaces(s));let l=`ssh -p ${r||22} -i ${t}`;if(c&&c.length>0){l+=` ${c.join(" ")}`}i.push("--rsh",`"${l}"`);i.push("--recursive");if(o&&o.length>0){o.forEach((e=>{if(e)i.push(`--exclude=${escapeSpaces(e)}`)}))}if(n&&n.length>0){i.push(...n)}const d=[...new Set(i)];return`rsync ${d.join(" ")}`};const runRsync=async e=>new Promise(((s,o)=>{const r=buildRsyncCommand(e);const logCMD=e=>{console.warn("================================================================");console.log(e);console.warn("================================================================")};let n="";let c="";const i=t("/bin/sh",["-c",r]);i.stdout.on("data",(e=>{const s=e.toString();console.log(s);n+=s}));i.stderr.on("data",(e=>{const s=e.toString();console.error(s);c+=s}));i.on("exit",(e=>{if(e!==0){const s=new Error(`rsync exited with code ${e}`);s.code=e;console.error("❌ [Rsync] error: ");console.error(s);console.error("❌ [Rsync] stderr: ");console.error(c);console.error("❌️ [Rsync] stdout: ");console.error(n);console.error("❌ [Rsync] command: ");logCMD(r);o(new Error(`${s.message}\n\n${c}`))}else{console.log("⭐ [Rsync] command finished: ");logCMD(r);s(n)}}));i.on("error",(e=>{console.error("❌ [Rsync] command error: ",e.message,e.stack);o(e)}))}));const validateRsync=async()=>{try{r("rsync --version",{stdio:"inherit"});console.log("✅️ [CLI] Rsync exists");return}catch(e){console.warn("⚠️ [CLI] Rsync doesn't exists",e.message)}console.log('[CLI] Start rsync installation with "apt-get" \n');try{r("sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n")}catch(e){throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`)}};const rsyncCli=async({source:e,rsyncServer:s,exclude:o,remotePort:r,privateKeyPath:t,args:n,sshCmdArgs:c})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${s}`);if(o&&o.length>0)console.log(`[Rsync] excluding folders ${o}`);return runRsync({src:e,dest:s,excludeFirst:o,port:r,privateKey:t,args:n,sshCmdArgs:c})};const sshDeploy=async e=>{await validateRsync();const s=await rsyncCli(e);console.log("✅ [Rsync] finished.",s);process.env.RSYNC_STDOUT=`${s}`;return s};e.exports={sshDeploy:sshDeploy}},822:(e,s,o)=>{const{join:r}=o(17);const{execSync:t}=o(81);const{EOL:n}=o(37);const{writeToFile:c}=o(505);const i="known_hosts";const getPrivateKeyPath=(e="")=>{const{HOME:s}=process.env;const o=r(s||"~",".ssh");const t=r(o,i);return{dir:o,filename:e,path:r(o,e),knownHostsPath:t}};const addSshKey=(e,s)=>{const{dir:o,filename:r}=getPrivateKeyPath(s);c({dir:o,filename:i,content:""});console.log("✅ [SSH] known_hosts file ensured",o);c({dir:o,filename:r,content:`${e}${n}`,isRequired:true,mode:"0400"});console.log("✅ [SSH] key added to `.ssh` dir ",o,r)};const updateKnownHosts=(e,s)=>{const{knownHostsPath:o}=getPrivateKeyPath();console.log("[SSH] Adding host to `known_hosts` ....",e,o);try{t(`ssh-keyscan -p ${s||22} -H ${e} >> ${o}`,{stdio:"inherit"})}catch(s){console.error("❌ [SSH] Adding host to `known_hosts` ERROR",e,s.message)}console.log("✅ [SSH] Adding host to `known_hosts` DONE",e,o)};e.exports={getPrivateKeyPath:getPrivateKeyPath,updateKnownHosts:updateKnownHosts,addSshKey:addSshKey}},81:e=>{"use strict";e.exports=require("child_process")},113:e=>{"use strict";e.exports=require("crypto")},147:e=>{"use strict";e.exports=require("fs")},37:e=>{"use strict";e.exports=require("os")},17:e=>{"use strict";e.exports=require("path")}};var s={};function __nccwpck_require__(o){var r=s[o];if(r!==undefined){return r.exports}var t=s[o]={exports:{}};var n=true;try{e[o](t,t.exports,__nccwpck_require__);n=false}finally{if(n)delete s[o]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var o={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:s,remoteCmdAfter:o}=__nccwpck_require__(976);const{addSshKey:r,getPrivateKeyPath:t,updateKnownHosts:n}=__nccwpck_require__(822);const{validateRequiredInputs:c}=__nccwpck_require__(505);const i=__nccwpck_require__(229);const run=async()=>{const{source:a,remoteUser:l,remoteHost:d,remotePort:p,deployKeyName:g,sshPrivateKey:u,args:h,exclude:_,sshCmdArgs:m,scriptBefore:y,scriptBeforeRequired:f,scriptAfter:R,scriptAfterRequired:E,rsyncServer:w}=i;c({sshPrivateKey:u,remoteHost:d,remoteUser:l});r(u,g);const{path:S}=t(g);if(y||R){n(d,p)}if(y){await s(y,S,f)}await e({source:a,rsyncServer:w,exclude:_,remotePort:p,privateKeyPath:S,args:h,sshCmdArgs:m});if(R){await o(R,S,E)}};run().then(((e="")=>{console.log("✅ [DONE]",e)})).catch((e=>{console.error("❌ [ERROR]",e.message);process.exit(1)}))})();module.exports=o})(); \ No newline at end of file +(()=>{var e={505:(e,s,o)=>{const{existsSync:r,mkdirSync:t,writeFileSync:n,unlink:c}=o(147);const{join:i}=o(17);const validateDir=e=>{if(!e){console.warn("⚠️ [DIR] dir is not defined");return}if(r(e)){console.log(`✅ [DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`);t(e);console.log("✅ [DIR] dir created.")};const handleError=(e,s)=>{if(s){throw new Error(e)}console.warn(e)};const writeToFile=({dir:e,filename:s,content:o,isRequired:t,mode:c="0644"})=>{validateDir(e);const a=i(e,s);if(r(a)){const e=`⚠️ [FILE] ${a} Required file exist.`;handleError(e,t);return}try{console.log(`[FILE] writing ${a} file ...`,o.length);n(a,o,{encoding:"utf8",mode:c})}catch(e){const s=`⚠️[FILE] Writing to file error. filePath: ${a}, message: ${e.message}`;handleError(s,t)}};const deleteFile=({dir:e,filename:s,isRequired:o})=>{validateDir(e);const t=i(e,s);if(r(t)){const e=`⚠️ [FILE] ${t} Required file exist.`;handleError(e,o);return}try{console.log(`[FILE] Deleting ${t} file ...`);c(t,(e=>{if(e){throw new Error(e)}}))}catch(e){const s=`⚠️[FILE] Deleting file error. filePath: ${t}, message: ${e.message}`;handleError(s,o)}};const validateRequiredInputs=e=>{const s=Object.keys(e);const o=s.filter((s=>{const o=e[s];if(!o){console.error(`❌ [INPUTS] ${s} is mandatory`)}return o}));if(o.length!==s.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,s)=>s.toUpperCase()));e.exports={writeToFile:writeToFile,deleteFile:deleteFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,s,o)=>{const{snakeToCamel:r}=o(505);const t=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER","SCRIPT_BEFORE_REQUIRED","SCRIPT_AFTER_REQUIRED"];const n=process.env.GITHUB_WORKSPACE;const c=process.env.REMOTE_USER||process.env.INPUT_REMOTE_USER;const i={source:"",target:`/home/${c}/`,exclude:"",args:"-rlgoDzvc -i",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:`deploy_key_${c}_${Date.now()}`};const a={githubWorkspace:n};t.forEach((e=>{const s=r(e.toLowerCase());const o=process.env[e]||process.env[`INPUT_${e}`]||i[s];const t=o===undefined?i[s]:o;let c=t;switch(s){case"source":c=t.split(" ").map((e=>`${n}/${e}`));break;case"args":c=t.split(" ");break;case"exclude":case"sshCmdArgs":c=t.split(",").map((e=>e.trim()));break}a[s]=c}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,s,o)=>{const{exec:r}=o(81);const t=o(113);const{sshServer:n,githubWorkspace:c,remotePort:i}=o(229);const{writeToFile:a,deleteFile:l}=o(505);const handleError=(e,s,o)=>{if(s){o(new Error(e))}else{console.warn(e)}};const remoteCmd=async(e,s,o,d)=>new Promise(((p,u)=>{const g=t.randomUUID();const h=`local_ssh_script-${d}-${g}.sh`;try{a({dir:c,filename:h,content:e});const t=1e4;const d=(process.env.RSYNC_STDOUT||"").substring(0,t);console.log(`Executing remote script: ssh -i ${s} ${n}`);r(`DEBIAN_FRONTEND=noninteractive ssh -p ${i||22} -i ${s} -o StrictHostKeyChecking=no ${n} 'RSYNC_STDOUT="${d}" bash -s' < ${h}`,((e,s="",r="")=>{if(e){const t=`⚠️ [CMD] Remote script failed: ${e.message}`;console.warn(`${t} \n`,s,r);handleError(t,o,u)}else{const e=s.substring(0,t);console.log("✅ [CMD] Remote script executed. \n",e,r);l({dir:c,filename:h});console.log("✅ [FILE] Script file deleted.");p(e)}}))}catch(e){handleError(e.message,o,u)}}));e.exports={remoteCmdBefore:async(e,s,o)=>remoteCmd(e,s,o,"before"),remoteCmdAfter:async(e,s,o)=>remoteCmd(e,s,o,"after")}},58:(e,s,o)=>{const{spawn:r}=o(81);const escapeSpaces=e=>typeof e==="string"?e.replace(/\b\s/g,"\\ "):e;const buildRsyncCommand=({src:e,dest:s,excludeFirst:o,port:r,privateKey:t,args:n,sshCmdArgs:c})=>{const i=[];const a=Array.isArray(e)?e:[e];i.push(...a.map(escapeSpaces));i.push(escapeSpaces(s));let l=`ssh -p ${r||22} -i ${t}`;if(c&&c.length>0){l+=` ${c.join(" ")}`}i.push("--rsh",`"${l}"`);i.push("--recursive");if(Array.isArray(o)){o.forEach((e=>{if(e)i.push(`--exclude=${escapeSpaces(e)}`)}))}if(Array.isArray(n)){i.push(...n)}return`rsync ${[...new Set(i)].join(" ")}`};e.exports=(e,s)=>{const o=buildRsyncCommand(e);const noop=()=>{};const t=e.onStdout||noop;const n=e.onStderr||noop;let c="";let i="";const a=r("/bin/sh",["-c",o]);a.stdout.on("data",(e=>{t(e);c+=e}));a.stderr.on("data",(e=>{n(e);i+=e}));a.on("exit",(e=>{let r=null;if(e!==0){r=new Error(`rsync exited with code ${e}`);r.code=e}s(r,c,i,o)}))}},447:(e,s,o)=>{const{execSync:r}=o(81);const t=o(58);const nodeRsyncPromise=async e=>new Promise(((s,o)=>{const logCMD=e=>{console.warn("================================================================");console.log(e);console.warn("================================================================")};try{t(e,((e,r,t,n)=>{if(e){console.error("❌ [Rsync] error: ");console.error(e);console.error("❌ [Rsync] stderr: ");console.error(t);console.error("❌️ [Rsync] stdout: ");console.error(r);console.error("❌ [Rsync] command: ");logCMD(n);o(new Error(`${e.message}\n\n${t}`))}else{console.log("⭐ [Rsync] command finished: ");logCMD(n);s(r)}}))}catch(e){console.error("❌ [Rsync] command error: ",e.message,e.stack);o(e)}}));const validateRsync=async()=>{try{r("rsync --version",{stdio:"inherit"});console.log("✅️ [CLI] Rsync exists");return}catch(e){console.warn("⚠️ [CLI] Rsync doesn't exists",e.message)}console.log('[CLI] Start rsync installation with "apt-get" \n');try{r("sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n")}catch(e){throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`)}};const rsyncCli=async({source:e,rsyncServer:s,exclude:o,remotePort:r,privateKeyPath:t,args:n,sshCmdArgs:c})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${s}`);if(o&&o.length>0)console.log(`[Rsync] excluding folders ${o}`);const i={ssh:true,recursive:true,onStdout:e=>console.log(e.toString()),onStderr:e=>console.error(e.toString())};return nodeRsyncPromise({...i,src:e,dest:s,excludeFirst:o,port:r,privateKey:t,args:n,sshCmdArgs:c})};const sshDeploy=async e=>{await validateRsync();const s=await rsyncCli(e);console.log("✅ [Rsync] finished.",s);process.env.RSYNC_STDOUT=`${s}`;return s};e.exports={sshDeploy:sshDeploy}},822:(e,s,o)=>{const{join:r}=o(17);const{execSync:t}=o(81);const{EOL:n}=o(37);const{writeToFile:c}=o(505);const i="known_hosts";const getPrivateKeyPath=(e="")=>{const{HOME:s}=process.env;const o=r(s||"~",".ssh");const t=r(o,i);return{dir:o,filename:e,path:r(o,e),knownHostsPath:t}};const addSshKey=(e,s)=>{const{dir:o,filename:r}=getPrivateKeyPath(s);c({dir:o,filename:i,content:""});console.log("✅ [SSH] known_hosts file ensured",o);c({dir:o,filename:r,content:`${e}${n}`,isRequired:true,mode:"0400"});console.log("✅ [SSH] key added to `.ssh` dir ",o,r)};const updateKnownHosts=(e,s)=>{const{knownHostsPath:o}=getPrivateKeyPath();console.log("[SSH] Adding host to `known_hosts` ....",e,o);try{t(`ssh-keyscan -p ${s||22} -H ${e} >> ${o}`,{stdio:"inherit"})}catch(s){console.error("❌ [SSH] Adding host to `known_hosts` ERROR",e,s.message)}console.log("✅ [SSH] Adding host to `known_hosts` DONE",e,o)};e.exports={getPrivateKeyPath:getPrivateKeyPath,updateKnownHosts:updateKnownHosts,addSshKey:addSshKey}},81:e=>{"use strict";e.exports=require("child_process")},113:e=>{"use strict";e.exports=require("crypto")},147:e=>{"use strict";e.exports=require("fs")},37:e=>{"use strict";e.exports=require("os")},17:e=>{"use strict";e.exports=require("path")}};var s={};function __nccwpck_require__(o){var r=s[o];if(r!==undefined){return r.exports}var t=s[o]={exports:{}};var n=true;try{e[o](t,t.exports,__nccwpck_require__);n=false}finally{if(n)delete s[o]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var o={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:s,remoteCmdAfter:o}=__nccwpck_require__(976);const{addSshKey:r,getPrivateKeyPath:t,updateKnownHosts:n}=__nccwpck_require__(822);const{validateRequiredInputs:c}=__nccwpck_require__(505);const i=__nccwpck_require__(229);const run=async()=>{const{source:a,remoteUser:l,remoteHost:d,remotePort:p,deployKeyName:u,sshPrivateKey:g,args:h,exclude:_,sshCmdArgs:y,scriptBefore:m,scriptBeforeRequired:f,scriptAfter:R,scriptAfterRequired:E,rsyncServer:S}=i;c({sshPrivateKey:g,remoteHost:d,remoteUser:l});r(g,u);const{path:w}=t(u);if(m||R){n(d,p)}if(m){await s(m,w,f)}await e({source:a,rsyncServer:S,exclude:_,remotePort:p,privateKeyPath:w,args:h,sshCmdArgs:y});if(R){await o(R,w,E)}};run().then(((e="")=>{console.log("✅ [DONE]",e)})).catch((e=>{console.error("❌ [ERROR]",e.message);process.exit(1)}))})();module.exports=o})(); \ No newline at end of file diff --git a/src/rsync.js b/src/rsync.js new file mode 100644 index 0000000..2f0f2ea --- /dev/null +++ b/src/rsync.js @@ -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); + }); +}; diff --git a/src/rsyncCli.js b/src/rsyncCli.js index ca9c641..07033a2 100644 --- a/src/rsyncCli.js +++ b/src/rsyncCli.js @@ -1,90 +1,35 @@ -const { execSync, spawn } = require('child_process'); +const { execSync } = require('child_process'); +const nodeRsync = require('./rsync'); -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) => { +const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => { + const logCMD = (cmd) => { console.warn('================================================================'); - console.log(c); + console.log(cmd); console.warn('================================================================'); }; - 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) => { + 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) { console.error('❌ [Rsync] command error: ', error.message, error.stack); reject(error); - }); + } }); const validateRsync = async () => { @@ -112,8 +57,17 @@ 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 runRsync({ + return nodeRsyncPromise({ + ...defaultOptions, src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort, privateKey: privateKeyPath, args, sshCmdArgs });