Skip to content

Commit

Permalink
Fix config order issue
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicPoullain committed Feb 16, 2019
1 parent 8258c83 commit fd6ff0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions packages/core/src/core/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,23 @@ describe('Config', () => {
mkdirSync('config');

const dotEnvFileContent = 'BAR_FOO=foo2';
const defaultJSONFileContent = JSON.stringify({ barFoo: 'foo3' });
const defaultYAMLFileContent = 'barFoo: foo4';
const envJSONFileContent = JSON.stringify({ barFoo: 'foo5' });
const envYAMLFileContent = 'barFoo: foo6';
const envJSONFileContent = JSON.stringify({ barFoo: 'foo3' });
const envYAMLFileContent = 'barFoo: foo4';
const defaultJSONFileContent = JSON.stringify({ barFoo: 'foo5' });
const defaultYAMLFileContent = 'barFoo: foo6';

strictEqual(Config.get('barFoo', 'foo7'), 'foo7');

writeFileSync('config/test.yml', envYAMLFileContent, 'utf8');
writeFileSync('config/default.yml', defaultYAMLFileContent, 'utf8');
strictEqual(Config.get('barFoo', 'foo7'), 'foo6');

writeFileSync('config/test.json', envJSONFileContent, 'utf8');
writeFileSync('config/default.json', defaultJSONFileContent, 'utf8');
strictEqual(Config.get('barFoo', 'foo7'), 'foo5');

writeFileSync('config/default.yml', defaultYAMLFileContent, 'utf8');
writeFileSync('config/test.yml', envYAMLFileContent, 'utf8');
strictEqual(Config.get('barFoo', 'foo7'), 'foo4');

writeFileSync('config/default.json', defaultJSONFileContent, 'utf8');
writeFileSync('config/test.json', envJSONFileContent, 'utf8');
strictEqual(Config.get('barFoo', 'foo7'), 'foo3');

writeFileSync('.env', dotEnvFileContent, 'utf8');
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ export class Config {
return dotEnvValue as any;
}

const defaultJSONValue = this.readJSONValue('config/default.json', key);
if (defaultJSONValue !== undefined) {
return defaultJSONValue;
}

const defaultYAMLValue = this.readYAMLValue('config/default.yml', key);
if (defaultYAMLValue !== undefined) {
return defaultYAMLValue;
}

const envJSONFilePath = `config/${process.env.NODE_ENV || 'development'}.json`;
const envJSONValue = this.readJSONValue(envJSONFilePath, key);
if (envJSONValue !== undefined) {
Expand All @@ -38,6 +28,16 @@ export class Config {
return envYAMLValue;
}

const defaultJSONValue = this.readJSONValue('config/default.json', key);
if (defaultJSONValue !== undefined) {
return defaultJSONValue;
}

const defaultYAMLValue = this.readYAMLValue('config/default.yml', key);
if (defaultYAMLValue !== undefined) {
return defaultYAMLValue;
}

return defaultValue as T;
}

Expand Down

0 comments on commit fd6ff0e

Please sign in to comment.