Merge pull request #184 from Armadillidiid/feat/delete-script-after-exec

Feature: Delete Script After Execution
This commit is contained in:
Dragan Filipović 2024-07-24 18:55:06 +02:00 committed by GitHub
commit b99511bf85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -1,4 +1,4 @@
const { existsSync, mkdirSync, writeFileSync } = require('fs'); const { existsSync, mkdirSync, writeFileSync, unlink } = require('fs');
const { join } = require('path'); const { join } = require('path');
const validateDir = (dir) => { const validateDir = (dir) => {
@ -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 validateRequiredInputs = (inputs) => {
const inputKeys = Object.keys(inputs); const inputKeys = Object.keys(inputs);
const validInputs = inputKeys.filter((inputKey) => { const validInputs = inputKeys.filter((inputKey) => {
@ -66,6 +89,7 @@ const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.t
module.exports = { module.exports = {
writeToFile, writeToFile,
deleteFile,
validateRequiredInputs, validateRequiredInputs,
snakeToCamel snakeToCamel
}; };

View File

@ -1,7 +1,7 @@
const { exec } = require('child_process'); const { exec } = require('child_process');
const crypto = require('crypto'); const crypto = require('crypto');
const { sshServer, githubWorkspace, remotePort } = require('./inputs'); const { sshServer, githubWorkspace, remotePort } = require('./inputs');
const { writeToFile } = require('./helpers'); const { writeToFile, deleteFile } = require('./helpers');
const handleError = (message, isRequired, callback) => { const handleError = (message, isRequired, callback) => {
if (isRequired) { if (isRequired) {
@ -30,6 +30,8 @@ const remoteCmd = async (content, privateKeyPath, isRequired, label) => new Prom
} else { } else {
const limited = data.substring(0, dataLimit); const limited = data.substring(0, dataLimit);
console.log('✅ [CMD] Remote script executed. \n', limited, stderr); console.log('✅ [CMD] Remote script executed. \n', limited, stderr);
deleteFile({ dir: githubWorkspace, filename });
console.log('✅ [FILE] Script file deleted.');
resolve(limited); resolve(limited);
} }
} }