Skip to content

Commit

Permalink
Merge pull request #184 from Armadillidiid/feat/delete-script-after-exec
Browse files Browse the repository at this point in the history
Feature: Delete Script After Execution
  • Loading branch information
easingthemes authored Jul 24, 2024
2 parents 01a39e3 + b82eced commit b99511b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { existsSync, mkdirSync, writeFileSync } = require('fs');
const { existsSync, mkdirSync, writeFileSync, unlink } = require('fs');
const { join } = require('path');

const validateDir = (dir) => {
Expand Down Expand Up @@ -45,6 +45,29 @@ const writeToFile = ({ dir, filename, content, isRequired, mode = '0644' }) => {
}
};

const deleteFile = ({ dir, filename, isRequired }) => {
validateDir(dir);
const filePath = join(dir, filename);

if (existsSync(filePath)) {
const message = `⚠️ [FILE] ${filePath} Required file exist.`;
handleError(message, isRequired);
return;
}

try {
console.log(`[FILE] Deleting ${filePath} file ...`);
unlink(filePath, (error) => {
if (error) {
throw new Error(error);
}
});
} catch (error) {
const message = `⚠️[FILE] Deleting file error. filePath: ${filePath}, message: ${error.message}`;
handleError(message, isRequired);
}
};

const validateRequiredInputs = (inputs) => {
const inputKeys = Object.keys(inputs);
const validInputs = inputKeys.filter((inputKey) => {
Expand All @@ -66,6 +89,7 @@ const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.t

module.exports = {
writeToFile,
deleteFile,
validateRequiredInputs,
snakeToCamel
};
4 changes: 3 additions & 1 deletion src/remoteCmd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { exec } = require('child_process');
const crypto = require('crypto');
const { sshServer, githubWorkspace, remotePort } = require('./inputs');
const { writeToFile } = require('./helpers');
const { writeToFile, deleteFile } = require('./helpers');

const handleError = (message, isRequired, callback) => {
if (isRequired) {
Expand Down Expand Up @@ -30,6 +30,8 @@ const remoteCmd = async (content, privateKeyPath, isRequired, label) => new Prom
} else {
const limited = data.substring(0, dataLimit);
console.log('✅ [CMD] Remote script executed. \n', limited, stderr);
deleteFile({ dir: githubWorkspace, filename });
console.log('✅ [FILE] Script file deleted.');
resolve(limited);
}
}
Expand Down

0 comments on commit b99511b

Please sign in to comment.