env-toolkit
Version:
This is a package that can generate or compare env files for your project.
47 lines (46 loc) • 2.19 kB
JavaScript
import chalk from 'chalk';
import { args } from './argv.js';
import { updateOrCompareEnv } from './compare-env.js';
import { findEnvVars } from './find-env.js';
import { convertPrefixesToRegExp, createEnvFile, getOutputFilePath, printTable } from './utils.js';
const { blue, green, redBright } = chalk;
(function main() {
try {
const { print, directory, exclude, name, output, overwrite, prefixes, compare, update } = args;
const prefixesRegExp = convertPrefixesToRegExp(prefixes);
const excludedDirectories = JSON.parse(exclude);
const variablesFound = findEnvVars(directory, excludedDirectories, prefixesRegExp);
const variablesList = Array.from(variablesFound).sort();
console.log(blue('Scanned directory:'), green(directory));
console.log(blue('Excluded folders:'), green(excludedDirectories.filter((folder) => folder !== directory)));
if ((variablesFound === null || variablesFound === void 0 ? void 0 : variablesFound.size) > 0) {
console.log(blue('Total environment variables found:'), green(variablesFound.size));
}
if (compare || update) {
updateOrCompareEnv(directory, name, variablesFound, compare, update);
}
else {
if (!(variablesFound === null || variablesFound === void 0 ? void 0 : variablesFound.size)) {
console.log(redBright('No environment variables found in the directory.'));
return;
}
if (print) {
printTable(variablesList);
}
else {
const outputFilePath = getOutputFilePath(output, name, overwrite);
createEnvFile(variablesList, outputFilePath);
console.log(blue('Output file:'), green(outputFilePath));
}
}
}
catch (err) {
if (err.message.includes('Unexpected token')) {
console.log('Invalid json array provided. Please provide a valid JSON array.');
}
else {
console.error('Error generating your file:\n', err === null || err === void 0 ? void 0 : err.message);
}
}
})();