#!/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})();