#!/usr/bin/env node (()=>{var e={898(e,r,t){"use strict";var s=t(81).spawn,o=t(837),n=function(e){return"string"==typeof e?e.replace(/\b\s/g,"\\ "):e};e.exports=function(e,r){e=e||{};var t="win32"===((e=(p=e=o._extend({},e),["src","dest","include","exclude","excludeFirst"].forEach(function(e){var r=p[e];"string"==typeof r?p[e]=n(r):!0===Array.isArray(r)&&(p[e]=r.map(n))}),p)).platform||process.platform);if(void 0===e.src)throw Error("'src' directory is missing from options");if(void 0===e.dest)throw Error("'dest' directory is missing from options");var i=e.dest;void 0!==e.host&&(i=e.host+":"+e.dest),Array.isArray(e.src)||(e.src=[e.src]);var c=[].concat(e.src);c.push(i);var a=(e.args||[]).find(function(e){return e.match(/--chmod=/)});if(t&&!a&&c.push("--chmod=ugo=rwX"),void 0!==e.host||e.ssh){c.push("--rsh");var d="ssh";void 0!==e.port&&(d+=" -p "+e.port),void 0!==e.privateKey&&(d+=" -i "+e.privateKey),void 0!==e.sshCmdArgs&&(d+=" "+e.sshCmdArgs.join(" ")),c.push(d)}switch(!0===e.recursive&&c.push("--recursive"),!0===e.times&&c.push("--times"),(!0===e.syncDest||!0===e.deleteAll)&&(c.push("--delete"),c.push("--delete-excluded")),(!0===e.syncDestIgnoreExcl||!0===e.delete)&&c.push("--delete"),!0===e.dryRun&&(c.push("--dry-run"),c.push("--verbose")),void 0!==e.excludeFirst&&o.isArray(e.excludeFirst)&&e.excludeFirst.forEach(function(e,r){c.push("--exclude="+e)}),void 0!==e.include&&o.isArray(e.include)&&e.include.forEach(function(e,r){c.push("--include="+e)}),void 0!==e.exclude&&o.isArray(e.exclude)&&e.exclude.forEach(function(e,r){c.push("--exclude="+e)}),e.compareMode){case"sizeOnly":c.push("--size-only");break;case"checksum":c.push("--checksum")}void 0!==e.args&&o.isArray(e.args)&&(c=[...new Set([...c,...e.args])]),c=[...new Set(c)];var l=function(){},u=e.onStdout||l,h=e.onStderr||l,g="rsync ";if(c.forEach(function(e){"ssh "===e.substr(0,4)&&(e='"'+e+'"'),g+=e+" "}),g=g.trim(),e.noExec){r(null,null,null,g);return}try{var p,y,m="",f="";(y=t?s("cmd.exe",["/s","/c",'"'+g+'"'],{windowsVerbatimArguments:!0,stdio:[process.stdin,"pipe","pipe"]}):s("/bin/sh",["-c",g])).stdout.on("data",function(e){u(e),m+=e}),y.stderr.on("data",function(e){h(e),f+=e}),y.on("exit",function(e){var t=null;0!==e&&((t=Error("rsync exited with code "+e)).code=e),r(t,m,f,g)})}catch(v){r(v,null,null,g)}}},505(e,r,t){let{existsSync:s,mkdirSync:o,writeFileSync:n}=t(147),{join:i}=t(17),c=e=>{if(!e){console.warn("⚠️ [DIR] dir is not defined");return}if(s(e)){console.log(`✅ [DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`),o(e),console.log("✅ [DIR] dir created.")},a=(e,r)=>{if(r)throw Error(e);console.warn(e)},d=({dir:e,filename:r,content:t,isRequired:o,mode:d="0644"})=>{c(e);let l=i(e,r);if(s(l)){let u=`⚠️ [FILE] ${l} Required file exist.`;a(u,o);return}try{console.log(`[FILE] writing ${l} file ...`,t.length),n(l,t,{encoding:"utf8",mode:d})}catch(h){let g=`⚠️[FILE] Writing to file error. filePath: ${l}, message: ${h.message}`;a(g,o)}},l=e=>{let r=Object.keys(e),t=r.filter(r=>{let t=e[r];return t||console.error(`❌ [INPUTS] ${r} is mandatory`),t});if(t.length!==r.length)throw Error("⚠️ [INPUTS] Inputs not valid, aborting ...")},u=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,(e,r)=>r.toUpperCase());e.exports={writeToFile:d,validateRequiredInputs:l,snakeToCamel:u}},229(e,r,t){let{snakeToCamel:s}=t(505),o=process.env.GITHUB_WORKSPACE,n=process.env.REMOTE_USER||process.env.INPUT_REMOTE_USER,i={source:"",target:`/home/${n}/`,exclude:"",include:"",args:"-rlgoDzvc -i",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:`deploy_key_${n}_${Date.now()}`},c={githubWorkspace:o};["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","INCLUDE","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER"].forEach(e=>{let r=s(e.toLowerCase()),t=process.env[e]||process.env[`INPUT_${e}`]||i[r],n=void 0===t?i[r]:t,a=n;switch(r){case"source":a=n.split(" ").map(e=>`${o}/${e}`);break;case"args":a=n.split(" ");break;case"include":case"exclude":case"sshCmdArgs":a=n.split(",").map(e=>e.trim())}c[r]=a}),c.sshServer=`${c.remoteUser}@${c.remoteHost}`,c.rsyncServer=`${c.remoteUser}@${c.remoteHost}:${c.target}`,e.exports=c},976(e,r,t){let{exec:s}=t(81),{sshServer:o,githubWorkspace:n,remotePort:i}=t(229),{writeToFile:c}=t(505),a=(e,r,t)=>{r?t(Error(e)):console.warn(e)},d=async(e,r,t,d)=>new Promise((l,u)=>{let h=`local_ssh_script-${d}.sh`;try{c({dir:n,filename:h,content:e});let g=(process.env.RSYNC_STDOUT||"").substring(0,1e4);console.log(`Executing remote script: ssh -i ${r} ${o}`),s(`DEBIAN_FRONTEND=noninteractive ssh -p ${i||22} -i ${r} -o StrictHostKeyChecking=no ${o} 'RSYNC_STDOUT="${g}" bash -s' < ${h}`,(e,r="",s="")=>{if(e){let o=`⚠️ [CMD] Remote script failed: ${e.message}`;console.warn(`${o} `,r,s),a(o,t,u)}else{let n=r.substring(0,1e4);console.log("✅ [CMD] Remote script executed. \n",n,s),l(n)}})}catch(p){a(p.message,t,u)}});e.exports={remoteCmdBefore:async(e,r,t)=>d(e,r,t,"before"),remoteCmdAfter:async(e,r,t)=>d(e,r,t,"after")}},447(e,r,t){let{execSync:s}=t(81),o=t(898),n=async e=>new Promise((r,t)=>{let s=e=>{console.warn("================================================================"),console.log(e),console.warn("================================================================")};try{o(e,(e,o,n,i)=>{e?(console.error("❌ [Rsync] error: "),console.error(e),console.error("❌ [Rsync] stderr: "),console.error(n),console.error("❌️ [Rsync] stdout: "),console.error(o),console.error("❌ [Rsync] command: "),s(i),t(Error(`${e.message} ${n}`))):(console.log("⭐ [Rsync] command finished: "),s(i),r(o))})}catch(n){console.error("❌ [Rsync] command error: ",n.message,n.stack),t(n)}}),i=async()=>{try{s("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{s("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(r){throw Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${r.message}`)}},c=async({source:e,rsyncServer:r,include:t,exclude:s,remotePort:o,privateKeyPath:i,args:c,sshCmdArgs:a})=>(console.log(`[Rsync] Starting Rsync Action: ${e} to ${r}`),s&&s.length>0&&console.log(`[Rsync] excluding folders ${s}`),t&&t.length>0&&console.log(`[Rsync] including folders ${t}`),n({ssh:!0,recursive:!0,onStdout:e=>console.log(e.toString()),onStderr:e=>console.error(e.toString()),src:e,dest:r,include:t,excludeFirst:s,port:o,privateKey:i,args:c,sshCmdArgs:a})),a=async e=>{await i();let r=await c(e);return console.log("✅ [Rsync] finished.",r),process.env.RSYNC_STDOUT=`${r}`,r};e.exports={sshDeploy:a}},822(e,r,t){let{join:s}=t(17),{execSync:o}=t(81),{writeToFile:n}=t(505),i="known_hosts",c=(e="")=>{let{HOME:r}=process.env,t=s(r||"~",".ssh"),o=s(t,i);return{dir:t,filename:e,path:s(t,e),knownHostsPath:o}},a=(e,r)=>{let{dir:t,filename:s}=c(r);n({dir:t,filename:i,content:""}),console.log("✅ [SSH] known_hosts file ensured",t),n({dir:t,filename:s,content:`${e}\r `,isRequired:!0,mode:"0400"}),console.log("✅ [SSH] key added to `.ssh` dir ",t,s)},d=(e,r)=>{let{knownHostsPath:t}=c();console.log("[SSH] Adding host to `known_hosts` ....",e,t);try{o(`ssh-keyscan -p ${r||22} -H ${e} >> ${t}`,{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,t)};e.exports={getPrivateKeyPath:c,updateKnownHosts:d,addSshKey:a}},81(e){"use strict";e.exports=require("child_process")},147(e){"use strict";e.exports=require("fs")},17(e){"use strict";e.exports=require("path")},837(e){"use strict";e.exports=require("util")}},r={};function t(s){var o=r[s];if(void 0!==o)return o.exports;var n=r[s]={exports:{}},i=!0;try{e[s](n,n.exports,t),i=!1}finally{i&&delete r[s]}return n.exports}t.ab=__dirname+"/";var s={};(()=>{let{sshDeploy:e}=t(447),{remoteCmdBefore:r,remoteCmdAfter:s}=t(976),{addSshKey:o,getPrivateKeyPath:n,updateKnownHosts:i}=t(822),{validateRequiredInputs:c}=t(505),a=t(229),d=async()=>{let{source:t,remoteUser:d,remoteHost:l,remotePort:u,deployKeyName:h,sshPrivateKey:g,args:p,include:y,exclude:m,sshCmdArgs:f,scriptBefore:v,scriptAfter:R,rsyncServer:S}=a;c({sshPrivateKey:g,remoteHost:l,remoteUser:d}),o(g,h);let{path:x}=n(h);(v||R)&&i(l,u),v&&await r(v,x),await e({source:t,rsyncServer:S,include:y,exclude:m,remotePort:u,privateKeyPath:x,args:p,sshCmdArgs:f}),R&&await s(R,x)};d().then((e="")=>{console.log("✅ [DONE]",e)}).catch(e=>{console.error("❌ [ERROR]",e.message),process.exit(1)})})(),module.exports=s})();