Commit d8d52899 authored by mohoff's avatar mohoff Committed by Richard Crosby

refactor: use NODE_ENV as default runtimeEnv variable

parent 39174570
......@@ -23,11 +23,11 @@
"watch": "tsc --watch",
"clean": "rimraf build/",
"build": "tsc",
"dev:load:commonjs": "yarn build; echo 'Loaded Config:\n'; cross-env RUNTIME_ENVIRONMENT=development time node scripts/dev/load-commonjs.js | jq .",
"dev:load:es6": "echo 'Loaded Config:\n'; cross-env RUNTIME_ENVIRONMENT=development time ts-node -T scripts/dev/load-es6.ts | jq .",
"dev:load:commonjs": "yarn build; echo 'Loaded Config:\n'; cross-env NODE_ENV=development time node scripts/dev/load-commonjs.js | jq .",
"dev:load:es6": "echo 'Loaded Config:\n'; cross-env NODE_ENV=development time ts-node -T scripts/dev/load-es6.ts | jq .",
"dev:load": "yarn dev:load:es6",
"dev:load:watch": "nodemon --exec 'yarn dev:load:es6'",
"dev:validate": "echo 'Validation Result:\n'; cross-env RUNTIME_ENVIRONMENT=development time ts-node -T scripts/dev/validate.ts",
"dev:validate": "echo 'Validation Result:\n'; cross-env NODE_ENV=development time ts-node -T scripts/dev/validate.ts",
"dev:validate:watch": "nodemon --exec 'yarn dev:validate'"
},
"devDependencies": {
......
......@@ -13,7 +13,8 @@ const mockedValidateParams = validateParams as jest.MockedFunction<
typeof validateParams
>
const mockedConfig = { some: 'config', runtimeEnvironment: 'development' }
const runtimeEnv = process.env.NODE_ENV || 'test'
const mockedConfig = { some: 'config', runtimeEnv }
const mockedParameters = defaultParameters
const mockedConfigPath = 'some/config/path'
const mockedValidationResult = true
......
......@@ -15,10 +15,9 @@ import { decryptToObject } from './utils/sops'
import { HydratedConfig } from './types'
const mockedParameters = defaultParameters
// Note: This variable must be named according to defaultParameters.runtimeEnvName
const RUNTIME_ENVIRONMENT = 'development'
const runtimeEnv = process.env.NODE_ENV || 'test'
const mockedConfigFile = {
filePath: './config/development.yaml',
filePath: './config/test.yaml',
contents: {
key: 'config',
sops: { some: 'sopsdetails' },
......@@ -33,7 +32,7 @@ const mockedSchemaFile = {
}
const mockedHydratedConfig: HydratedConfig = {
some: 'config',
runtimeEnvironment: RUNTIME_ENVIRONMENT,
runtimeEnv,
}
const mockedReadConfigFile = readConfigFile as jest.MockedFunction<
......@@ -66,7 +65,7 @@ describe('load()', () => {
jest.clearAllMocks()
jest.resetModules()
process.env = Object.assign(process.env, {
[defaultParameters.runtimeEnvName]: RUNTIME_ENVIRONMENT,
[defaultParameters.runtimeEnvName]: runtimeEnv,
})
})
......@@ -74,22 +73,22 @@ describe('load()', () => {
process.env = OLD_ENV
})
it('throws if RUNTIME_ENVIRONMENT is not set', () => {
it('throws if NODE_ENV is not set', () => {
delete process.env[defaultParameters.runtimeEnvName]
expect(() => load(mockedParameters)).toThrow('runtimeEnv must be defined')
process.env = Object.assign(process.env, {
[defaultParameters.runtimeEnvName]: RUNTIME_ENVIRONMENT,
[defaultParameters.runtimeEnvName]: runtimeEnv,
})
})
it('reads the config based on process.env.RUNTIME_ENVIRONMENT', () => {
it('reads the config based on process.env.NODE_ENV', () => {
load(mockedParameters)
expect(mockedReadConfigFile).toHaveBeenCalledWith(
expect.any(String),
RUNTIME_ENVIRONMENT
runtimeEnv
)
})
......@@ -106,7 +105,7 @@ describe('load()', () => {
load(mockedParameters)
expect(mockedHydrateConfig).toHaveBeenCalledWith(
RUNTIME_ENVIRONMENT,
runtimeEnv,
mockedParameters
)
expect(innerHydrateFunction).toHaveBeenCalledWith(mockedDecryptedConfigFile)
......
......@@ -12,7 +12,7 @@ export interface Parameters {
}
export const defaultParameters: Parameters = {
runtimeEnvName: 'RUNTIME_ENVIRONMENT',
runtimeEnvName: 'NODE_ENV',
types: {
rootTypeName: 'Config',
filePath: 'strong-config.d.ts',
......
......@@ -14,7 +14,7 @@ export const parametersSchema = {
title: 'Runtime environment variable name',
description:
'The value of this variable determines which config is loaded',
examples: ['RUNTIME_ENVIRONMENT', 'RUNTIME_ENV'],
examples: ['NODE_ENV', 'RUNTIME_ENVIRONMENT'],
type: 'string',
pattern: '^[a-zA-Z]\\w*$',
},
......
......@@ -22,5 +22,5 @@ type SopsMetadata = {
export type EncryptedConfig = { sops?: SopsMetadata } & BaseConfig
export type DecryptedConfig = Omit<BaseConfig, 'sops'>
export type HydratedConfig = { runtimeEnvironment: string } & DecryptedConfig
export type HydratedConfig = { runtimeEnv: string } & DecryptedConfig
export type MemoizedConfig = HydratedConfig | undefined
......@@ -8,7 +8,7 @@ const mockedCompiledTypes = `
}
`
const expectedRootType = `export interface Config extends TheTopLevelInterface {
${mockedParameters.runtimeEnvName}: string;
runtimeEnv: string;
}
`
const mockedSchemaString = `
......
......@@ -12,7 +12,6 @@ export const pascalCase = (input: string): string =>
.join('')
export const generateTypeFromSchema = async ({
runtimeEnvName,
schemaPath,
types,
}: Parameters): Promise<void> => {
......@@ -37,7 +36,7 @@ export const generateTypeFromSchema = async ({
const configInterfaceAsString = `export interface ${
types.rootTypeName
} extends ${pascalCase(title)} {
${runtimeEnvName}: string;
runtimeEnv: string;
}
`
const exportedTypes = baseTypes.concat(configInterfaceAsString)
......
......@@ -6,7 +6,7 @@ const mockedSubstituteWithEnv = substituteWithEnv as jest.MockedFunction<
typeof substituteWithEnv
>
const mockedParameters = defaultParameters
const mockedRuntimeEnvironment = 'development'
const runtimeEnv = 'test'
const mockedConfig = {
field: 'value',
replaceMe: '${asdf}',
......@@ -15,15 +15,12 @@ const mockedSubstitutedConfig = '{"field":"value","replaceMe":"PASTE"}'
const mockedHydratedConfig = {
...mockedConfig,
replaceMe: 'PASTE',
[mockedParameters.runtimeEnvName]: mockedRuntimeEnvironment,
[mockedParameters.runtimeEnvName]: runtimeEnv,
}
mockedSubstituteWithEnv.mockReturnValue(() => mockedSubstitutedConfig)
import { hydrateConfig } from './hydrate-config'
const hydrateConfigInited = hydrateConfig(
mockedRuntimeEnvironment,
mockedParameters
)
const hydrateConfigInited = hydrateConfig(runtimeEnv, mockedParameters)
describe('hydrateConfig()', () => {
it('calls substituteWithEnv with substitutionPattern', () => {
......@@ -34,10 +31,10 @@ describe('hydrateConfig()', () => {
)
})
it('adds runtimeEnvironment as top-level field', () => {
it('adds runtimeEnv as top-level field', () => {
expect(hydrateConfigInited(mockedConfig)).toEqual(
expect.objectContaining({
[mockedParameters.runtimeEnvName]: mockedRuntimeEnvironment,
[mockedParameters.runtimeEnvName]: runtimeEnv,
})
)
})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment