piral-cli-dotenv
Version:
Extends the Piral CLI command options to automatically include dotenv files.
48 lines • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const dotenv_1 = require("dotenv");
const plugin = (cli) => {
// these commands are supported
const commands = [
'build-piral',
'debug-piral',
'validate-piral',
'build-pilet',
'debug-pilet',
'publish-pilet',
'validate-pilet',
];
commands.forEach((command) => {
cli.withFlags(command, (flags) => flags.string('env').describe('env', 'Sets the environment variables.').default('env', undefined));
cli.beforeCommand(command, (args) => {
const e = args.env;
if (typeof e === 'string') {
if (e.indexOf('=') !== -1) {
// the next one is actually quite dangerous,
// but we'll do it anyway for the moment ...
// (unfortunately dotenv does not help us here)
const lines = e.split(';').join('\n');
const values = (0, dotenv_1.parse)(lines);
Object.keys(values).forEach((key) => (process.env[key] = values[key]));
}
else {
const paths = [e, `.env.${e}`, `${e}.env`];
for (const path of paths) {
if ((0, fs_1.existsSync)(path)) {
(0, dotenv_1.config)({
path,
});
break;
}
}
}
}
else {
(0, dotenv_1.config)();
}
});
});
};
module.exports = plugin;
//# sourceMappingURL=index.js.map