@pnpm/config.env-replace
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -13,3 +13,3 @@ "use strict"; | ||
} | ||
const envValue = env[name]; | ||
const envValue = getEnvValue(env, name); | ||
if (envValue === undefined) { | ||
@@ -20,2 +20,13 @@ throw new Error(`Failed to replace env in config: ${orig}`); | ||
} | ||
const ENV_VALUE = /([^:-]+)(:?)-(.+)/; | ||
function getEnvValue(env, name) { | ||
const matched = name.match(ENV_VALUE); | ||
if (!matched) | ||
return env[name]; | ||
const [, variableName, colon, fallback] = matched; | ||
if (Object.prototype.hasOwnProperty.call(env, variableName)) { | ||
return !env[variableName] && colon ? fallback : env[variableName]; | ||
} | ||
return fallback; | ||
} | ||
//# sourceMappingURL=env-replace.js.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
bar: 'bar_value', | ||
zoo: '', | ||
}; | ||
@@ -14,2 +15,5 @@ test.each([ | ||
['\\\\${foo}', '\\foo_value'], | ||
['-${foo-fallback-value}-${bar:-fallback-value}', '-foo_value-bar_value'], | ||
['-${qar-fallback-value}-${zoo-fallback-value}', '-fallback-value-'], | ||
['-${qar-fallback-value}-${zoo:-fallback-for-empty-value}', '-fallback-value-fallback-for-empty-value'] | ||
])('success %s => %s', (settingValue, expected) => { | ||
@@ -21,3 +25,5 @@ const actual = (0, env_replace_1.envReplace)(settingValue, ENV); | ||
expect(() => (0, env_replace_1.envReplace)('${baz}', ENV)).toThrow(`Failed to replace env in config: \${baz}`); | ||
expect(() => (0, env_replace_1.envReplace)('${foo-}', ENV)).toThrow(`Failed to replace env in config: \${foo-}`); | ||
expect(() => (0, env_replace_1.envReplace)('${foo:-}', ENV)).toThrow(`Failed to replace env in config: \${foo:-}`); | ||
}); | ||
//# sourceMappingURL=env-replace.spec.js.map |
@@ -6,2 +6,3 @@ import { envReplace } from './env-replace'; | ||
bar: 'bar_value', | ||
zoo: '', | ||
} | ||
@@ -14,2 +15,5 @@ | ||
['\\\\${foo}', '\\foo_value'], | ||
['-${foo-fallback-value}-${bar:-fallback-value}', '-foo_value-bar_value'], | ||
['-${qar-fallback-value}-${zoo-fallback-value}', '-fallback-value-'], | ||
['-${qar-fallback-value}-${zoo:-fallback-for-empty-value}', '-fallback-value-fallback-for-empty-value'] | ||
])('success %s => %s', (settingValue, expected) => { | ||
@@ -22,3 +26,5 @@ const actual = envReplace(settingValue, ENV); | ||
expect(() => envReplace('${baz}', ENV)).toThrow(`Failed to replace env in config: \${baz}`); | ||
expect(() => envReplace('${foo-}', ENV)).toThrow(`Failed to replace env in config: \${foo-}`); | ||
expect(() => envReplace('${foo:-}', ENV)).toThrow(`Failed to replace env in config: \${foo:-}`); | ||
}) | ||
@@ -11,3 +11,3 @@ const ENV_EXPR = /(?<!\\)(\\*)\$\{([^${}]+)\}/g | ||
} | ||
const envValue = env[name] | ||
const envValue = getEnvValue(env, name) | ||
if (envValue === undefined) { | ||
@@ -18,1 +18,13 @@ throw new Error(`Failed to replace env in config: ${orig}`) | ||
} | ||
const ENV_VALUE = /([^:-]+)(:?)-(.+)/ | ||
function getEnvValue (env: NodeJS.ProcessEnv, name: string): string { | ||
const matched = name.match(ENV_VALUE) | ||
if (!matched) return env[name] | ||
const [, variableName, colon, fallback] = matched | ||
if (Object.prototype.hasOwnProperty.call(env, variableName)) { | ||
return !env[variableName] && colon ? fallback : env[variableName] | ||
} | ||
return fallback | ||
} |
{ | ||
"name": "@pnpm/config.env-replace", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "https://bit.cloud/pnpm/config/env-replace", | ||
"main": "dist/index.js", | ||
@@ -8,9 +9,9 @@ "componentId": { | ||
"name": "env-replace", | ||
"version": "1.0.0" | ||
"version": "1.1.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/runtime": "7.20.0", | ||
"@types/jest": "26.0.20", | ||
"@types/node": "12.20.4", | ||
"@types/jest": "26.0.20" | ||
"@babel/runtime": "7.20.0" | ||
}, | ||
@@ -17,0 +18,0 @@ "peerDependencies": {}, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
15187
242
0