Skip to content

Commit

Permalink
fix: Environment vars: allow them to work (#184)
Browse files Browse the repository at this point in the history
* Env vars: don't overrwite them if there's no config

Previously, commands like `packer deploy` would *require* there to be a
packer.env.json config file or it'd crash. It seems like env vars were
supposed to be allowed as a way to configure packer. But, if you *did*
provide a config file, all your env vars would be overridden.

Here we print an error message if the packer.env.json file can't be
found, but continue, assuming that the environment contains the needed
vars.

* env: fix error messages

These were using a non-existent (maybe old?) config value. The correct
one is env.keys.id (holding the name of the ENV var for the theme id).

Accessing this incorrect value caused *another* "value not found" error.
  • Loading branch information
danielbeardsley committed Mar 9, 2022
1 parent db9bc41 commit 009f704
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/env/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const PackerConfig = require('../config');
const chalk = require('chalk');

const config = new PackerConfig(require('./packer-env.schema'));

Expand All @@ -21,8 +22,18 @@ const DEFAULT_ENV_VARS = [

function assign(envName = undefined) {
const name = typeof envName === 'undefined' ? 'development' : envName;
const env = require(config.get('packer.env'));
process.env[config.get('env.keys.name')] = name;

const packerEnvFile = config.get('packer.env');
let env;
try {
env = require(packerEnvFile);
} catch (err) {
console.error(
chalk.red(`\nFailed to read ${packerEnvFile}, using env variables\n`)
);
return;
}
DEFAULT_ENV_VARS.forEach((key) => {
process.env[key] = env[name][_getConfigKey(key)];
});
Expand Down Expand Up @@ -153,14 +164,12 @@ function _validateThemeId() {
const themeId = getThemeIdValue();

if (themeId.length === 0) {
errors.push(
new Error(`${config.get('env.keys.themeId')} must not be empty`)
);
errors.push(new Error(`${config.get('env.keys.id')} must not be empty`));
} else if (themeId !== 'live' && !/^\d+$/.test(themeId)) {
errors.push(
new Error(
`${config.get(
'env.keys.themeId'
'env.keys.id'
)} can be set to 'live' or a valid theme ID containing only numbers`
)
);
Expand Down

0 comments on commit 009f704

Please sign in to comment.