Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
7655318629 | |||
754ffa6cac | |||
b644ca0c1a | |||
9f638d687b | |||
a9bdfe9373 | |||
2017d51f11 | |||
765011ad2a | |||
d807cc6de2 | |||
53721a41c2 | |||
c9f79e6ea4 | |||
3c7e3e2589 | |||
205d27f9a0 |
@ -119,6 +119,6 @@ jobs:
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @gitzone/tsdoc
|
||||
pnpm install -g @git.zone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
||||
|
17
package.json
17
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/qenv",
|
||||
"version": "5.0.5",
|
||||
"version": "6.0.5",
|
||||
"private": false,
|
||||
"description": "easy promised environments",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -27,15 +27,16 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsrun": "^1.2.44",
|
||||
"@gitzone/tstest": "^1.0.77",
|
||||
"@push.rocks/tapbundle": "^5.0.12",
|
||||
"@types/node": "^20.4.6"
|
||||
"@git.zone/tsbuild": "^2.1.72",
|
||||
"@git.zone/tsrun": "^1.2.44",
|
||||
"@git.zone/tstest": "^1.0.86",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.11.17"
|
||||
},
|
||||
"dependencies": {
|
||||
"@configvault.io/interfaces": "^1.0.2",
|
||||
"@push.rocks/smartfile": "^10.0.28",
|
||||
"@api.global/typedrequest": "^3.0.4",
|
||||
"@configvault.io/interfaces": "^1.0.17",
|
||||
"@push.rocks/smartfile": "^11.0.4",
|
||||
"@push.rocks/smartlog": "^3.0.3",
|
||||
"@push.rocks/smartpath": "^5.0.11"
|
||||
},
|
||||
|
3127
pnpm-lock.yaml
generated
3127
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
12
test/test.ts
12
test/test.ts
@ -18,18 +18,18 @@ tap.test('should create a new class', async () => {
|
||||
});
|
||||
|
||||
tap.test('key1 should be not be overwritten since it is already present', async () => {
|
||||
expect(testQenv.getEnvVarOnDemand('key1')).toEqual('original');
|
||||
expect(testQenv.getEnvVarOnDemand('key1')).toEqual('original');
|
||||
expect(await testQenv.getEnvVarOnDemand('key1')).toEqual('original');
|
||||
expect(await testQenv.getEnvVarOnDemand('key1')).toEqual('original');
|
||||
});
|
||||
|
||||
tap.test('key2 should be read from Yml', async () => {
|
||||
expect(testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
|
||||
expect(testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
|
||||
expect(await testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
|
||||
expect(await testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
|
||||
});
|
||||
|
||||
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
|
||||
expect(testQenv.keyValueObject.key1).toEqual('original');
|
||||
expect(testQenv.keyValueObject.key2).toEqual('fromJson');
|
||||
expect(await testQenv.keyValueObject.key1).toEqual('original');
|
||||
expect(await testQenv.keyValueObject.key2).toEqual('fromJson');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/qenv',
|
||||
version: '5.0.5',
|
||||
version: '6.0.5',
|
||||
description: 'easy promised environments'
|
||||
}
|
||||
|
30
ts/qenv.classes.configvaultadapter.ts
Normal file
30
ts/qenv.classes.configvaultadapter.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import * as plugins from './qenv.plugins.js';
|
||||
|
||||
export class CloudlyAdapter {
|
||||
public configVaultUrl: string;
|
||||
|
||||
constructor(configVaultUrl?: string) {
|
||||
this.configVaultUrl = configVaultUrl;
|
||||
}
|
||||
|
||||
public async getConfigBundle(): Promise<plugins.configvaultInterfaces.data.IConfigBundle> {
|
||||
if (this.configVaultUrl) {
|
||||
console.log(`ConfigVault specified through constructor`)
|
||||
} else if (process.env['CONFIGVAULT_URL']) {
|
||||
this.configVaultUrl = process.env['CONFIGVAULT_URL'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsedUrl = new URL(this.configVaultUrl);
|
||||
|
||||
const tr =
|
||||
new plugins.typedrequest.TypedRequest<plugins.configvaultInterfaces.requests.IReq_GetEnvBundle>(
|
||||
`${parsedUrl.host}/typedrequest`,
|
||||
'getEnvBundle'
|
||||
);
|
||||
const response = await tr.fire({
|
||||
authorization: parsedUrl.pathname.replace('/', ''),
|
||||
})
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
import { CloudlyAdapter } from './qenv.classes.configvaultadapter.js';
|
||||
import * as plugins from './qenv.plugins.js';
|
||||
|
||||
export type TEnvVarRef = string | (() => Promise<string>);
|
||||
|
||||
export class Qenv {
|
||||
public requiredEnvVars: string[] = [];
|
||||
public availableEnvVars: string[] = [];
|
||||
@ -7,14 +10,17 @@ export class Qenv {
|
||||
public keyValueObject: { [key: string]: any } = {};
|
||||
public logger = new plugins.smartlog.ConsoleLog();
|
||||
|
||||
public cloudlyAdapter: CloudlyAdapter;
|
||||
|
||||
public qenvFilePathAbsolute: string;
|
||||
public envFilePathAbsolute: string;
|
||||
|
||||
constructor(
|
||||
qenvFileBasePathArg: string = process.cwd(),
|
||||
envFileBasePathArg: string,
|
||||
envFileBasePathArg?: string,
|
||||
failOnMissing: boolean = true
|
||||
) {
|
||||
this.cloudlyAdapter = new CloudlyAdapter();
|
||||
this.initializeFilePaths(qenvFileBasePathArg, envFileBasePathArg);
|
||||
this.loadRequiredEnvVars();
|
||||
this.loadAvailableEnvVars();
|
||||
@ -22,8 +28,16 @@ export class Qenv {
|
||||
}
|
||||
|
||||
private initializeFilePaths(qenvFileBasePathArg: string, envFileBasePathArg: string) {
|
||||
this.qenvFilePathAbsolute = plugins.path.join(plugins.path.resolve(qenvFileBasePathArg), 'qenv.yml');
|
||||
this.envFilePathAbsolute = plugins.path.join(plugins.path.resolve(envFileBasePathArg), 'env.json');
|
||||
this.qenvFilePathAbsolute = plugins.path.join(
|
||||
plugins.path.resolve(qenvFileBasePathArg),
|
||||
'qenv.yml'
|
||||
);
|
||||
if (envFileBasePathArg) {
|
||||
this.envFilePathAbsolute = plugins.path.join(
|
||||
plugins.path.resolve(envFileBasePathArg),
|
||||
'env.json'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private loadRequiredEnvVars() {
|
||||
@ -48,7 +62,9 @@ export class Qenv {
|
||||
}
|
||||
|
||||
private checkForMissingEnvVars(failOnMissing: boolean) {
|
||||
this.missingEnvVars = this.requiredEnvVars.filter((envVar) => !this.availableEnvVars.includes(envVar));
|
||||
this.missingEnvVars = this.requiredEnvVars.filter(
|
||||
(envVar) => !this.availableEnvVars.includes(envVar)
|
||||
);
|
||||
|
||||
if (this.missingEnvVars.length > 0) {
|
||||
console.info('Required Env Vars are:', this.requiredEnvVars);
|
||||
@ -62,7 +78,61 @@ export class Qenv {
|
||||
}
|
||||
}
|
||||
|
||||
public getEnvVarOnDemand(envVarName: string): string | undefined {
|
||||
public async getEnvVarOnDemand(
|
||||
envVarNameOrNames: TEnvVarRef | TEnvVarRef[]
|
||||
): Promise<string | undefined> {
|
||||
if (Array.isArray(envVarNameOrNames)) {
|
||||
for (const envVarName of envVarNameOrNames) {
|
||||
const value = await this.tryGetEnvVar(envVarName);
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
} else {
|
||||
return await this.tryGetEnvVar(envVarNameOrNames);
|
||||
}
|
||||
}
|
||||
|
||||
public getEnvVarOnDemandSync(envVarNameOrNames: string | string[]): string | undefined {
|
||||
console.warn('requesting env var sync leaves out potentially important async env sources.');
|
||||
|
||||
if (Array.isArray(envVarNameOrNames)) {
|
||||
for (const envVarName of envVarNameOrNames) {
|
||||
const value = this.tryGetEnvVarSync(envVarName);
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
} else {
|
||||
return this.tryGetEnvVarSync(envVarNameOrNames);
|
||||
}
|
||||
}
|
||||
|
||||
public async getEnvVarOnDemandAsObject(envVarNameOrNames: string | string[]): Promise<any> {
|
||||
const rawValue = await this.getEnvVarOnDemand(envVarNameOrNames);
|
||||
if (rawValue && rawValue.startsWith('base64Object:')) {
|
||||
const base64Part = rawValue.split('base64Object:')[1];
|
||||
return this.decodeBase64(base64Part);
|
||||
}
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
private async tryGetEnvVar(envVarRefArg: TEnvVarRef): Promise<string | undefined> {
|
||||
if (typeof envVarRefArg === 'function') {
|
||||
return await envVarRefArg();
|
||||
}
|
||||
|
||||
return (
|
||||
this.getFromEnvironmentVariable(envVarRefArg) ||
|
||||
this.getFromEnvJsonFile(envVarRefArg) ||
|
||||
this.getFromDockerSecret(envVarRefArg) ||
|
||||
this.getFromDockerSecretJson(envVarRefArg)
|
||||
);
|
||||
}
|
||||
|
||||
private tryGetEnvVarSync(envVarName: string): string | undefined {
|
||||
return (
|
||||
this.getFromEnvironmentVariable(envVarName) ||
|
||||
this.getFromEnvJsonFile(envVarName) ||
|
||||
@ -71,20 +141,14 @@ export class Qenv {
|
||||
);
|
||||
}
|
||||
|
||||
public getEnvVarOnDemandAsObject(envVarName: string): any {
|
||||
const rawValue = this.getEnvVarOnDemand(envVarName);
|
||||
if (rawValue && rawValue.startsWith('base64Object:')) {
|
||||
const base64Part = rawValue.split('base64Object:')[1];
|
||||
return this.decodeBase64(base64Part);
|
||||
}
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
private getFromEnvironmentVariable(envVarName: string): string | undefined {
|
||||
return process.env[envVarName];
|
||||
}
|
||||
|
||||
private getFromEnvJsonFile(envVarName: string): string | undefined {
|
||||
if (!plugins.smartfile.fs.fileExistsSync(this.envFilePathAbsolute)) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const envJson = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
|
||||
const value = envJson[envVarName];
|
||||
|
@ -3,8 +3,20 @@ import * as path from 'path';
|
||||
|
||||
export { path };
|
||||
|
||||
// @api.global scope
|
||||
import * as typedrequest from '@api.global/typedrequest';
|
||||
|
||||
export {
|
||||
typedrequest,
|
||||
}
|
||||
|
||||
// @pushrocks scope
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
import * as smartlog from '@push.rocks/smartlog';
|
||||
|
||||
export { smartfile, smartlog };
|
||||
|
||||
// @configvault.io scope
|
||||
import * as configvaultInterfaces from '@configvault.io/interfaces';
|
||||
|
||||
export { configvaultInterfaces };
|
||||
|
@ -3,9 +3,12 @@
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
}
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user