@app-config/node
Advanced tools
Comparing version 2.4.6 to 2.5.0
@@ -0,5 +1,27 @@ | ||
/** A mapping for "alias" names of environments, like "dev" => "development" */ | ||
export interface EnvironmentAliases { | ||
[alias: string]: string; | ||
} | ||
/** Options required for calling {@link currentEnvironment} */ | ||
export interface EnvironmentOptions { | ||
/** Absolute override for what the current environment is, still abiding by aliases */ | ||
override?: string; | ||
/** A mapping for "alias" names of environments, like "dev" => "development" */ | ||
aliases: EnvironmentAliases; | ||
/** What environment variable(s) define the current environment, if override is not defined */ | ||
envVarNames: string[]; | ||
} | ||
/** Default aliases that app-config will resolve for you */ | ||
export declare const defaultAliases: EnvironmentAliases; | ||
/** Default environment variables that app-config will read */ | ||
export declare const defaultEnvVarNames: string[]; | ||
/** Default options for {@link currentEnvironment} */ | ||
export declare const defaultEnvOptions: EnvironmentOptions; | ||
/** Conversion function useful for old usage of the deprecated {@link currentEnvironment} form */ | ||
export declare function asEnvOptions(override?: string, aliases?: EnvironmentAliases, environmentSourceNames?: string[] | string): EnvironmentOptions; | ||
/** Retrieve what app-config thinks the current deployment environment is (ie QA, dev, staging, production) */ | ||
export declare function currentEnvironment(options?: EnvironmentOptions): string | undefined; | ||
/** @deprecated use currentEnvironment(EnvironmentOptions) instead */ | ||
export declare function currentEnvironment(environmentAliases?: EnvironmentAliases, environmentSourceNames?: string[] | string): string | undefined; | ||
/** Reverse lookup of any aliases for some environment */ | ||
export declare function aliasesFor(env: string, aliases: EnvironmentAliases): string[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.currentEnvironment = exports.defaultAliases = void 0; | ||
exports.aliasesFor = exports.currentEnvironment = exports.asEnvOptions = exports.defaultEnvOptions = exports.defaultEnvVarNames = exports.defaultAliases = void 0; | ||
const logging_1 = require("@app-config/logging"); | ||
/** Default aliases that app-config will resolve for you */ | ||
exports.defaultAliases = { | ||
@@ -8,8 +10,61 @@ prod: 'production', | ||
}; | ||
function currentEnvironment(environmentAliases = exports.defaultAliases, environmentSourceNames = ['APP_CONFIG_ENV', 'NODE_ENV', 'ENV']) { | ||
/** Default environment variables that app-config will read */ | ||
exports.defaultEnvVarNames = ['APP_CONFIG_ENV', 'NODE_ENV', 'ENV']; | ||
/** Default options for {@link currentEnvironment} */ | ||
exports.defaultEnvOptions = { | ||
aliases: exports.defaultAliases, | ||
envVarNames: exports.defaultEnvVarNames, | ||
}; | ||
/** Conversion function useful for old usage of the deprecated {@link currentEnvironment} form */ | ||
function asEnvOptions(override, aliases = exports.defaultAliases, environmentSourceNames = exports.defaultEnvVarNames) { | ||
return { | ||
override, | ||
aliases, | ||
envVarNames: typeof environmentSourceNames === 'string' | ||
? [environmentSourceNames] | ||
: environmentSourceNames, | ||
}; | ||
} | ||
exports.asEnvOptions = asEnvOptions; | ||
function currentEnvironment(...args) { | ||
var _a; | ||
let environmentSourceNames = exports.defaultEnvVarNames; | ||
let environmentAliases = exports.defaultAliases; | ||
let environmentOverride; | ||
if (args[0] && | ||
typeof args[0] === 'object' && | ||
('override' in args[0] || 'aliases' in args[0] || 'envVarNames' in args[0])) { | ||
const options = args[0]; | ||
if (options.override) { | ||
environmentOverride = options.override; | ||
} | ||
if (options.aliases) { | ||
environmentAliases = options.aliases; | ||
} | ||
if (options.envVarNames) { | ||
environmentSourceNames = options.envVarNames; | ||
} | ||
} | ||
else { | ||
if (args[0]) { | ||
environmentAliases = args[0]; | ||
logging_1.logger.warn('Detected deprecated usage of currentEnvironment'); | ||
} | ||
if (Array.isArray(args[1])) { | ||
environmentSourceNames = args[1]; | ||
logging_1.logger.warn('Detected deprecated usage of currentEnvironment'); | ||
} | ||
else if (typeof args[1] === 'string') { | ||
environmentSourceNames = [args[1]]; | ||
logging_1.logger.warn('Detected deprecated usage of currentEnvironment'); | ||
} | ||
} | ||
if (environmentOverride) { | ||
if (environmentAliases[environmentOverride]) { | ||
return environmentAliases[environmentOverride]; | ||
} | ||
return environmentOverride; | ||
} | ||
let value; | ||
for (const name of Array.isArray(environmentSourceNames) | ||
? environmentSourceNames | ||
: [environmentSourceNames]) { | ||
for (const name of environmentSourceNames) { | ||
if ((_a = process.env[name]) === null || _a === void 0 ? void 0 : _a.length) { | ||
@@ -28,2 +83,9 @@ value = process.env[name]; | ||
exports.currentEnvironment = currentEnvironment; | ||
/** Reverse lookup of any aliases for some environment */ | ||
function aliasesFor(env, aliases) { | ||
return Object.entries(aliases) | ||
.filter(([, value]) => value === env) | ||
.map(([key]) => key); | ||
} | ||
exports.aliasesFor = aliasesFor; | ||
//# sourceMappingURL=environment.js.map |
@@ -0,5 +1,27 @@ | ||
/** A mapping for "alias" names of environments, like "dev" => "development" */ | ||
export interface EnvironmentAliases { | ||
[alias: string]: string; | ||
} | ||
/** Options required for calling {@link currentEnvironment} */ | ||
export interface EnvironmentOptions { | ||
/** Absolute override for what the current environment is, still abiding by aliases */ | ||
override?: string; | ||
/** A mapping for "alias" names of environments, like "dev" => "development" */ | ||
aliases: EnvironmentAliases; | ||
/** What environment variable(s) define the current environment, if override is not defined */ | ||
envVarNames: string[]; | ||
} | ||
/** Default aliases that app-config will resolve for you */ | ||
export declare const defaultAliases: EnvironmentAliases; | ||
/** Default environment variables that app-config will read */ | ||
export declare const defaultEnvVarNames: string[]; | ||
/** Default options for {@link currentEnvironment} */ | ||
export declare const defaultEnvOptions: EnvironmentOptions; | ||
/** Conversion function useful for old usage of the deprecated {@link currentEnvironment} form */ | ||
export declare function asEnvOptions(override?: string, aliases?: EnvironmentAliases, environmentSourceNames?: string[] | string): EnvironmentOptions; | ||
/** Retrieve what app-config thinks the current deployment environment is (ie QA, dev, staging, production) */ | ||
export declare function currentEnvironment(options?: EnvironmentOptions): string | undefined; | ||
/** @deprecated use currentEnvironment(EnvironmentOptions) instead */ | ||
export declare function currentEnvironment(environmentAliases?: EnvironmentAliases, environmentSourceNames?: string[] | string): string | undefined; | ||
/** Reverse lookup of any aliases for some environment */ | ||
export declare function aliasesFor(env: string, aliases: EnvironmentAliases): string[]; |
@@ -0,1 +1,3 @@ | ||
import { logger } from '@app-config/logging'; | ||
/** Default aliases that app-config will resolve for you */ | ||
export const defaultAliases = { | ||
@@ -5,7 +7,59 @@ prod: 'production', | ||
}; | ||
export function currentEnvironment(environmentAliases = defaultAliases, environmentSourceNames = ['APP_CONFIG_ENV', 'NODE_ENV', 'ENV']) { | ||
/** Default environment variables that app-config will read */ | ||
export const defaultEnvVarNames = ['APP_CONFIG_ENV', 'NODE_ENV', 'ENV']; | ||
/** Default options for {@link currentEnvironment} */ | ||
export const defaultEnvOptions = { | ||
aliases: defaultAliases, | ||
envVarNames: defaultEnvVarNames, | ||
}; | ||
/** Conversion function useful for old usage of the deprecated {@link currentEnvironment} form */ | ||
export function asEnvOptions(override, aliases = defaultAliases, environmentSourceNames = defaultEnvVarNames) { | ||
return { | ||
override, | ||
aliases, | ||
envVarNames: typeof environmentSourceNames === 'string' | ||
? [environmentSourceNames] | ||
: environmentSourceNames, | ||
}; | ||
} | ||
export function currentEnvironment(...args) { | ||
let environmentSourceNames = defaultEnvVarNames; | ||
let environmentAliases = defaultAliases; | ||
let environmentOverride; | ||
if (args[0] && | ||
typeof args[0] === 'object' && | ||
('override' in args[0] || 'aliases' in args[0] || 'envVarNames' in args[0])) { | ||
const options = args[0]; | ||
if (options.override) { | ||
environmentOverride = options.override; | ||
} | ||
if (options.aliases) { | ||
environmentAliases = options.aliases; | ||
} | ||
if (options.envVarNames) { | ||
environmentSourceNames = options.envVarNames; | ||
} | ||
} | ||
else { | ||
if (args[0]) { | ||
environmentAliases = args[0]; | ||
logger.warn('Detected deprecated usage of currentEnvironment'); | ||
} | ||
if (Array.isArray(args[1])) { | ||
environmentSourceNames = args[1]; | ||
logger.warn('Detected deprecated usage of currentEnvironment'); | ||
} | ||
else if (typeof args[1] === 'string') { | ||
environmentSourceNames = [args[1]]; | ||
logger.warn('Detected deprecated usage of currentEnvironment'); | ||
} | ||
} | ||
if (environmentOverride) { | ||
if (environmentAliases[environmentOverride]) { | ||
return environmentAliases[environmentOverride]; | ||
} | ||
return environmentOverride; | ||
} | ||
let value; | ||
for (const name of Array.isArray(environmentSourceNames) | ||
? environmentSourceNames | ||
: [environmentSourceNames]) { | ||
for (const name of environmentSourceNames) { | ||
if (process.env[name]?.length) { | ||
@@ -23,2 +77,8 @@ value = process.env[name]; | ||
} | ||
/** Reverse lookup of any aliases for some environment */ | ||
export function aliasesFor(env, aliases) { | ||
return Object.entries(aliases) | ||
.filter(([, value]) => value === env) | ||
.map(([key]) => key); | ||
} | ||
//# sourceMappingURL=environment.js.map |
import { ConfigSource, FileType, ParsedValue, ParsingExtension } from '@app-config/core'; | ||
import { EnvironmentAliases } from './environment'; | ||
import { EnvironmentAliases, EnvironmentOptions } from './environment'; | ||
/** Read configuration from a single file */ | ||
@@ -13,7 +13,7 @@ export declare class FileSource extends ConfigSource { | ||
private readonly filePath; | ||
private readonly environmentOverride?; | ||
private readonly environmentAliases; | ||
private readonly fileExtensions; | ||
private readonly environmentSourceNames?; | ||
constructor(filePath: string, environmentOverride?: string | undefined, environmentAliases?: EnvironmentAliases, fileExtensions?: string[], environmentSourceNames?: string | string[] | undefined); | ||
private readonly environmentOptions; | ||
/** @deprecated use constructor with environmentOptions instead */ | ||
constructor(filePath: string, environmentOverride?: string, environmentAliases?: EnvironmentAliases, fileExtensions?: string[], environmentSourceNames?: string[] | string); | ||
constructor(filePath: string, fileExtensions?: string[], environmentOptions?: EnvironmentOptions); | ||
private resolveSource; | ||
@@ -20,0 +20,0 @@ readContents(): Promise<[string, FileType]>; |
@@ -5,3 +5,3 @@ import { isAbsolute, join, dirname, resolve } from 'path'; | ||
import { logger } from '@app-config/logging'; | ||
import { currentEnvironment, defaultAliases } from './environment'; | ||
import { aliasesFor, asEnvOptions, currentEnvironment, defaultEnvOptions, } from './environment'; | ||
/** Read configuration from a single file */ | ||
@@ -30,15 +30,30 @@ export class FileSource extends ConfigSource { | ||
export class FlexibleFileSource extends ConfigSource { | ||
constructor(filePath, environmentOverride, environmentAliases = defaultAliases, fileExtensions = ['yml', 'yaml', 'toml', 'json', 'json5'], environmentSourceNames) { | ||
constructor(filePath, environmentOverrideOrFileExtensions, environmentAliasesOrEnvironmentOptions, fileExtensions, environmentSourceNames) { | ||
super(); | ||
this.filePath = filePath; | ||
this.environmentOverride = environmentOverride; | ||
this.environmentAliases = environmentAliases; | ||
this.fileExtensions = fileExtensions; | ||
this.environmentSourceNames = environmentSourceNames; | ||
const defaultFileExtensions = ['yml', 'yaml', 'toml', 'json', 'json5']; | ||
if ((Array.isArray(environmentOverrideOrFileExtensions) || | ||
environmentOverrideOrFileExtensions === undefined) && | ||
(environmentAliasesOrEnvironmentOptions | ||
? 'aliases' in environmentAliasesOrEnvironmentOptions || | ||
'envVarNames' in environmentAliasesOrEnvironmentOptions | ||
: true) && | ||
fileExtensions === undefined && | ||
environmentSourceNames === undefined) { | ||
this.fileExtensions = environmentOverrideOrFileExtensions ?? defaultFileExtensions; | ||
this.environmentOptions = | ||
environmentAliasesOrEnvironmentOptions ?? defaultEnvOptions; | ||
} | ||
else { | ||
logger.warn(`Detected deprecated usage of FlexibleFileSource constructor loading ${filePath}`); | ||
this.fileExtensions = fileExtensions ?? defaultFileExtensions; | ||
this.environmentOptions = asEnvOptions(environmentOverrideOrFileExtensions, environmentAliasesOrEnvironmentOptions, environmentSourceNames); | ||
} | ||
} | ||
// share 'resolveSource' so that read() returns a ParsedValue pointed to the FileSource, not FlexibleFileSource | ||
async resolveSource() { | ||
const aliases = this.environmentAliases; | ||
const environment = this.environmentOverride ?? currentEnvironment(aliases, this.environmentSourceNames); | ||
const environmentAlias = Object.entries(aliases).find(([, v]) => v === environment)?.[0]; | ||
const environment = currentEnvironment(this.environmentOptions); | ||
const aliasesForCurrentEnv = environment | ||
? aliasesFor(environment, this.environmentOptions.aliases) | ||
: []; | ||
const filesToTry = []; | ||
@@ -48,4 +63,5 @@ for (const ext of this.fileExtensions) { | ||
filesToTry.push(`${this.filePath}.${environment}.${ext}`); | ||
if (environmentAlias) | ||
filesToTry.push(`${this.filePath}.${environmentAlias}.${ext}`); | ||
for (const alias of aliasesForCurrentEnv) { | ||
filesToTry.push(`${this.filePath}.${alias}.${ext}`); | ||
} | ||
} | ||
@@ -52,0 +68,0 @@ // try these after trying environments, which take precedent |
export { FileSource, FlexibleFileSource, resolveFilepath } from './file-source'; | ||
export { currentEnvironment, defaultAliases, EnvironmentAliases } from './environment'; | ||
export { currentEnvironment, defaultAliases, defaultEnvOptions, defaultEnvVarNames, asEnvOptions, EnvironmentAliases, EnvironmentOptions, } from './environment'; | ||
export { EnvironmentSource } from './environment-source'; | ||
export { promptUser, promptUserWithRetry, consumeStdin } from './prompts'; |
export { FileSource, FlexibleFileSource, resolveFilepath } from './file-source'; | ||
export { currentEnvironment, defaultAliases } from './environment'; | ||
export { currentEnvironment, defaultAliases, defaultEnvOptions, defaultEnvVarNames, asEnvOptions, } from './environment'; | ||
export { EnvironmentSource } from './environment-source'; | ||
export { promptUser, promptUserWithRetry, consumeStdin } from './prompts'; | ||
//# sourceMappingURL=index.js.map |
import { ConfigSource, FileType, ParsedValue, ParsingExtension } from '@app-config/core'; | ||
import { EnvironmentAliases } from './environment'; | ||
import { EnvironmentAliases, EnvironmentOptions } from './environment'; | ||
/** Read configuration from a single file */ | ||
@@ -13,7 +13,7 @@ export declare class FileSource extends ConfigSource { | ||
private readonly filePath; | ||
private readonly environmentOverride?; | ||
private readonly environmentAliases; | ||
private readonly fileExtensions; | ||
private readonly environmentSourceNames?; | ||
constructor(filePath: string, environmentOverride?: string | undefined, environmentAliases?: EnvironmentAliases, fileExtensions?: string[], environmentSourceNames?: string | string[] | undefined); | ||
private readonly environmentOptions; | ||
/** @deprecated use constructor with environmentOptions instead */ | ||
constructor(filePath: string, environmentOverride?: string, environmentAliases?: EnvironmentAliases, fileExtensions?: string[], environmentSourceNames?: string[] | string); | ||
constructor(filePath: string, fileExtensions?: string[], environmentOptions?: EnvironmentOptions); | ||
private resolveSource; | ||
@@ -20,0 +20,0 @@ readContents(): Promise<[string, FileType]>; |
@@ -33,16 +33,31 @@ "use strict"; | ||
class FlexibleFileSource extends core_1.ConfigSource { | ||
constructor(filePath, environmentOverride, environmentAliases = environment_1.defaultAliases, fileExtensions = ['yml', 'yaml', 'toml', 'json', 'json5'], environmentSourceNames) { | ||
constructor(filePath, environmentOverrideOrFileExtensions, environmentAliasesOrEnvironmentOptions, fileExtensions, environmentSourceNames) { | ||
var _a; | ||
super(); | ||
this.filePath = filePath; | ||
this.environmentOverride = environmentOverride; | ||
this.environmentAliases = environmentAliases; | ||
this.fileExtensions = fileExtensions; | ||
this.environmentSourceNames = environmentSourceNames; | ||
const defaultFileExtensions = ['yml', 'yaml', 'toml', 'json', 'json5']; | ||
if ((Array.isArray(environmentOverrideOrFileExtensions) || | ||
environmentOverrideOrFileExtensions === undefined) && | ||
(environmentAliasesOrEnvironmentOptions | ||
? 'aliases' in environmentAliasesOrEnvironmentOptions || | ||
'envVarNames' in environmentAliasesOrEnvironmentOptions | ||
: true) && | ||
fileExtensions === undefined && | ||
environmentSourceNames === undefined) { | ||
this.fileExtensions = environmentOverrideOrFileExtensions !== null && environmentOverrideOrFileExtensions !== void 0 ? environmentOverrideOrFileExtensions : defaultFileExtensions; | ||
this.environmentOptions = | ||
(_a = environmentAliasesOrEnvironmentOptions) !== null && _a !== void 0 ? _a : environment_1.defaultEnvOptions; | ||
} | ||
else { | ||
logging_1.logger.warn(`Detected deprecated usage of FlexibleFileSource constructor loading ${filePath}`); | ||
this.fileExtensions = fileExtensions !== null && fileExtensions !== void 0 ? fileExtensions : defaultFileExtensions; | ||
this.environmentOptions = environment_1.asEnvOptions(environmentOverrideOrFileExtensions, environmentAliasesOrEnvironmentOptions, environmentSourceNames); | ||
} | ||
} | ||
// share 'resolveSource' so that read() returns a ParsedValue pointed to the FileSource, not FlexibleFileSource | ||
async resolveSource() { | ||
var _a, _b; | ||
const aliases = this.environmentAliases; | ||
const environment = (_a = this.environmentOverride) !== null && _a !== void 0 ? _a : environment_1.currentEnvironment(aliases, this.environmentSourceNames); | ||
const environmentAlias = (_b = Object.entries(aliases).find(([, v]) => v === environment)) === null || _b === void 0 ? void 0 : _b[0]; | ||
const environment = environment_1.currentEnvironment(this.environmentOptions); | ||
const aliasesForCurrentEnv = environment | ||
? environment_1.aliasesFor(environment, this.environmentOptions.aliases) | ||
: []; | ||
const filesToTry = []; | ||
@@ -52,4 +67,5 @@ for (const ext of this.fileExtensions) { | ||
filesToTry.push(`${this.filePath}.${environment}.${ext}`); | ||
if (environmentAlias) | ||
filesToTry.push(`${this.filePath}.${environmentAlias}.${ext}`); | ||
for (const alias of aliasesForCurrentEnv) { | ||
filesToTry.push(`${this.filePath}.${alias}.${ext}`); | ||
} | ||
} | ||
@@ -56,0 +72,0 @@ // try these after trying environments, which take precedent |
export { FileSource, FlexibleFileSource, resolveFilepath } from './file-source'; | ||
export { currentEnvironment, defaultAliases, EnvironmentAliases } from './environment'; | ||
export { currentEnvironment, defaultAliases, defaultEnvOptions, defaultEnvVarNames, asEnvOptions, EnvironmentAliases, EnvironmentOptions, } from './environment'; | ||
export { EnvironmentSource } from './environment-source'; | ||
export { promptUser, promptUserWithRetry, consumeStdin } from './prompts'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.consumeStdin = exports.promptUserWithRetry = exports.promptUser = exports.EnvironmentSource = exports.defaultAliases = exports.currentEnvironment = exports.resolveFilepath = exports.FlexibleFileSource = exports.FileSource = void 0; | ||
exports.consumeStdin = exports.promptUserWithRetry = exports.promptUser = exports.EnvironmentSource = exports.asEnvOptions = exports.defaultEnvVarNames = exports.defaultEnvOptions = exports.defaultAliases = exports.currentEnvironment = exports.resolveFilepath = exports.FlexibleFileSource = exports.FileSource = void 0; | ||
var file_source_1 = require("./file-source"); | ||
@@ -11,2 +11,5 @@ Object.defineProperty(exports, "FileSource", { enumerable: true, get: function () { return file_source_1.FileSource; } }); | ||
Object.defineProperty(exports, "defaultAliases", { enumerable: true, get: function () { return environment_1.defaultAliases; } }); | ||
Object.defineProperty(exports, "defaultEnvOptions", { enumerable: true, get: function () { return environment_1.defaultEnvOptions; } }); | ||
Object.defineProperty(exports, "defaultEnvVarNames", { enumerable: true, get: function () { return environment_1.defaultEnvVarNames; } }); | ||
Object.defineProperty(exports, "asEnvOptions", { enumerable: true, get: function () { return environment_1.asEnvOptions; } }); | ||
var environment_source_1 = require("./environment-source"); | ||
@@ -13,0 +16,0 @@ Object.defineProperty(exports, "EnvironmentSource", { enumerable: true, get: function () { return environment_source_1.EnvironmentSource; } }); |
{ | ||
"name": "@app-config/node", | ||
"description": "Node.js API for @app-config", | ||
"version": "2.4.6", | ||
"version": "2.5.0", | ||
"license": "MPL-2.0", | ||
@@ -33,4 +33,4 @@ "author": { | ||
"dependencies": { | ||
"@app-config/core": "^2.4.6", | ||
"@app-config/logging": "^2.4.6", | ||
"@app-config/core": "^2.5.0", | ||
"@app-config/logging": "^2.5.0", | ||
"@types/prompts": "2", | ||
@@ -41,3 +41,3 @@ "fs-extra": "9", | ||
"devDependencies": { | ||
"@app-config/test-utils": "^2.4.6", | ||
"@app-config/test-utils": "^2.5.0", | ||
"@types/fs-extra": "9" | ||
@@ -44,0 +44,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
49263
621
8
Updated@app-config/core@^2.5.0
Updated@app-config/logging@^2.5.0