2020-04-11 16:29:20 +02:00
|
|
|
const { existsSync, mkdirSync, writeFileSync } = require('fs');
|
2023-01-02 01:02:44 +01:00
|
|
|
const { join } = require('path');
|
2020-04-11 16:29:20 +02:00
|
|
|
|
|
|
|
const validateDir = (dir) => {
|
2023-01-02 01:02:44 +01:00
|
|
|
if (existsSync(dir)) {
|
2020-04-11 16:29:20 +02:00
|
|
|
console.log(`[SSH] ${dir} dir exist`);
|
2023-01-02 01:02:44 +01:00
|
|
|
return;
|
2020-04-11 16:29:20 +02:00
|
|
|
}
|
2023-01-02 01:02:44 +01:00
|
|
|
|
|
|
|
console.log(`[SSH] Creating ${dir} dir in workspace root`);
|
|
|
|
mkdirSync(dir);
|
|
|
|
console.log('✅ [SSH] dir created.');
|
2020-04-11 16:29:20 +02:00
|
|
|
};
|
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
const writeToFile = ({ dir, filename, content, isRequired }) => {
|
|
|
|
validateDir(dir);
|
|
|
|
const filePath = join(dir, filename);
|
|
|
|
|
|
|
|
if (existsSync(filePath)) {
|
|
|
|
console.log(`[FILE] ${filePath} file exist`);
|
|
|
|
if (isRequired) {
|
|
|
|
throw new Error(`⚠️ [FILE] ${filePath} Required file exist, aborting ...`);
|
2020-04-11 16:29:20 +02:00
|
|
|
}
|
2023-01-02 01:02:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
writeFileSync(filePath, content, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
mode: 0o600
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`⚠️[FILE] Writing to file error. filePath: ${filePath}, message: ${e.message}`);
|
2020-04-11 16:29:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
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());
|
|
|
|
|
2020-04-11 16:29:20 +02:00
|
|
|
module.exports = {
|
2023-01-02 01:02:44 +01:00
|
|
|
writeToFile,
|
|
|
|
validateRequiredInputs,
|
|
|
|
snakeToCamel
|
2020-04-11 16:29:20 +02:00
|
|
|
};
|