ec9347f8c6
* feat: Add SSH remote script support - before and after rsync * fix: remove __dirname * feat: add sshCmdArgs option * Add promise instead of callback * fix: improve logs * fix: Add simple command exists instead of a plugin * add non interactive install * feat: add onStderr and onStdout logs * Improve reject messages * feat: Add RSYNC_STDOUT env variable * emoji updates * fix: update workflow actions
72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
const { existsSync, mkdirSync, writeFileSync } = require('fs');
|
|
const { join } = require('path');
|
|
|
|
const validateDir = (dir) => {
|
|
if (!dir) {
|
|
console.warn('⚠️ [DIR] dir is not defined');
|
|
return;
|
|
}
|
|
if (existsSync(dir)) {
|
|
console.log(`✅ [DIR] ${dir} dir exist`);
|
|
return;
|
|
}
|
|
|
|
console.log(`[DIR] Creating ${dir} dir in workspace root`);
|
|
mkdirSync(dir);
|
|
console.log('✅ [DIR] dir created.');
|
|
};
|
|
|
|
const handleError = (message, isRequired) => {
|
|
if (isRequired) {
|
|
throw new Error(message);
|
|
}
|
|
console.warn(message);
|
|
};
|
|
|
|
const writeToFile = ({ dir, filename, content, isRequired, mode = '0644' }) => {
|
|
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] writing ${filePath} file ...`, content.length);
|
|
writeFileSync(filePath, content, {
|
|
encoding: 'utf8',
|
|
mode
|
|
});
|
|
} catch (error) {
|
|
const message = `⚠️[FILE] Writing to file error. filePath: ${filePath}, message: ${error.message}`;
|
|
handleError(message, isRequired);
|
|
}
|
|
};
|
|
|
|
const validateRequiredInputs = (inputs) => {
|
|
const inputKeys = Object.keys(inputs);
|
|
const validInputs = inputKeys.filter((inputKey) => {
|
|
const inputValue = inputs[inputKey];
|
|
|
|
if (!inputValue) {
|
|
console.error(`❌ [INPUTS] ${inputKey} is mandatory`);
|
|
}
|
|
|
|
return inputValue;
|
|
});
|
|
|
|
if (validInputs.length !== inputKeys.length) {
|
|
throw new Error('⚠️ [INPUTS] Inputs not valid, aborting ...');
|
|
}
|
|
};
|
|
|
|
const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
|
|
|
|
module.exports = {
|
|
writeToFile,
|
|
validateRequiredInputs,
|
|
snakeToCamel
|
|
};
|