Skip to content

Commit

Permalink
Create env2yaml.cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
joanfabregat committed Mar 26, 2024
1 parent 63a43dc commit 4db1dae
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions env2yaml.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs');
const readline = require('readline');

// Command-line argument handling
const [,, inputFile, outputFile] = process.argv;

if (!inputFile || !outputFile) {
console.error("Usage: node script.js input.env output.yaml");
process.exit(1);
}

// Check input file existence
if (!fs.existsSync(inputFile)) {
console.error(`Input file does not exist: ${inputFile}`);
process.exit(1);
}

// Function to process a line from the .env file
function processEnvLine(line) {
if (line.startsWith('#') || line.trim() === '') {
return line; // Comment or empty line
}

const [key, value] = line.split('=');
if (!key) {
console.error(`Invalid .env syntax in line: ${line}`)
process.exit(1);
}

// YAML value formatting
let yamlValue;
if (value === '') {
yamlValue = "''";
} else if (!isNaN(value)) { // Check if it's a number
yamlValue = value;
} else if (value.startsWith('"') && value.endsWith('"') ||
value.startsWith("'") && value.endsWith("'")
) {
yamlValue = value; // Already has quotes
} else {
yamlValue = `'${value}'`; // Add quotes for strings
}

return `${key}: ${yamlValue}`;
}

// Main conversion logic
const rl = readline.createInterface({ input: fs.createReadStream(inputFile) });
let outputData = '';

rl.on('line', (line) => {
outputData += processEnvLine(line) + '\n';
});

rl.on('close', () => {
fs.writeFileSync(outputFile, outputData);
console.log('Conversion complete!');
});

0 comments on commit 4db1dae

Please sign in to comment.