Skip to content

Commit 50caf39

Browse files
feat: add cacheDefaults
1 parent 8486f66 commit 50caf39

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

configent.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const { existsSync, readdirSync } = require('fs')
22
const { resolve } = require('path')
33
let instances = {}
4+
let detectedFromDefaults = {}
45

56
const _defaults = {
67
name: '',
78
cacheConfig: true,
9+
cacheDetectedDefaults: true,
810
useDotEnv: true,
911
useEnv: true,
1012
usePackageConfig: true,
@@ -21,6 +23,7 @@ const _defaults = {
2123
* @param {object} [configentOptions] configent options
2224
* @param {string=} [configentOptions.name = ''] name to use for configs. If left empty, name from package.json is used
2325
* @param {boolean=} [configentOptions.cacheConfig = true] calling configent twice with same parameters will return the same instance
26+
* @param {boolean=} [configentOptions.cacheDetectedDefaults = true] calling configent twice from the same module will return the same defaults
2427
* @param {boolean=} [configentOptions.useDotEnv = true] include config from .env files
2528
* @param {boolean=} [configentOptions.useEnv = true] include config from process.env
2629
* @param {boolean=} [configentOptions.usePackageConfig = true] include config from package.json
@@ -36,6 +39,7 @@ function configent(defaults, input = {}, configentOptions) {
3639
const {
3740
name,
3841
cacheConfig,
42+
cacheDetectedDefaults,
3943
useDotEnv,
4044
sanitizeEnvValue,
4145
useConfig,
@@ -92,24 +96,30 @@ function configent(defaults, input = {}, configentOptions) {
9296
}
9397

9498
function getDetectDefaults() {
95-
const pkgjson = { dependencies: {}, devDependencies: {} };
96-
if (existsSync('package.json')) {
97-
Object.assign(pkgjson, require(resolve(process.cwd(), 'package.json')));
98-
}
99-
100-
Object.assign(pkgjson.dependencies, pkgjson.devDependencies)
101-
102-
const unsortedConfigTemplates = readdirSync(resolve(module['parent'].path, detectDefaultsConfigPath))
99+
const hash = JSON.stringify({ name, path: module['parent'].path })
100+
101+
// we only want to detect the defaults for any given module once
102+
if(!detectedFromDefaults[hash] || !cacheDetectedDefaults){
103+
const pkgjson = { dependencies: {}, devDependencies: {} };
104+
if (existsSync('package.json')) {
105+
Object.assign(pkgjson, require(resolve(process.cwd(), 'package.json')));
106+
}
107+
108+
Object.assign(pkgjson.dependencies, pkgjson.devDependencies)
109+
110+
const unsortedConfigTemplates = readdirSync(resolve(module['parent'].path, detectDefaultsConfigPath))
103111
.map(file => ({
104112
file,
105113
...require(resolve(module['parent'].path, detectDefaultsConfigPath, file))
106114
}))
107-
const configTemplates = sortBySupersedings(unsortedConfigTemplates)
108-
const configTemplate = configTemplates.find(configTemplate => configTemplate.condition({ pkgjson }))
109-
if (configTemplate) {
110-
console.log(`${name} found config for ${configTemplate.name}`)
111-
return configTemplate.config()
115+
const configTemplates = sortBySupersedings(unsortedConfigTemplates)
116+
const configTemplate = configTemplates.find(configTemplate => configTemplate.condition({ pkgjson }))
117+
if (configTemplate) {
118+
console.log(`${name} found config for ${configTemplate.name}`)
119+
detectedFromDefaults[hash] = configTemplate.config()
120+
}
112121
}
122+
return detectedFromDefaults[hash]
113123
}
114124
}
115125

test/context-configs/context.spec.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ process.chdir(__dirname) //change cwd to __dirname
55
const defaults = { fromContext: false }
66
const configentOptions = {
77
useDetectDefaults: true,
8-
cacheConfig: false
8+
cacheConfig: false,
9+
cacheDetectedDefaults: false
910
}
1011

1112
test('if no context is found, defaults are unchanged', async t => {
@@ -14,25 +15,25 @@ test('if no context is found, defaults are unchanged', async t => {
1415
})
1516

1617
test('if context is found, it sets defaults', async t => {
17-
process.env.USE_BASIC = true
18+
process.env.USE_BASIC = "1"
1819
const result = configent(defaults, {}, configentOptions)
1920
t.deepEqual(result, { fromContext: 'basic' })
2021
})
2122

2223
test('if multiple contexts are found, superseder wins', async t => {
23-
process.env.USE_SUPERSEDER = true
24+
process.env.USE_SUPERSEDER = "1"
2425
const result = configent(defaults, {}, configentOptions)
2526
t.deepEqual(result, { fromContext: 'superseder' })
2627
})
2728

2829
test('can read package.json from cwd', async t => {
29-
process.env.USE_PKGJSON = true
30+
process.env.USE_PKGJSON = "1"
3031
const result = configent(defaults, {}, configentOptions)
3132
t.deepEqual(result, { fromContext: 'usepkgjson' })
3233
})
3334

3435
test('circular tests create error', async t => {
35-
process.env.USE_CIRCULAR = true
36+
process.env.USE_CIRCULAR = "1"
3637
const result = configent(defaults, {}, configentOptions)
3738
t.deepEqual(result, { fromContext: 'usepkgjson' })
3839
})

0 commit comments

Comments
 (0)