@tramvai/module-environment 4.25.0 → 4.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/server/ClientEnvironmentRepository.d.ts +3 -3
- package/lib/server/EnvironmentManagerServer.d.ts +6 -3
- package/lib/server/EnvironmentManagerServer.es.js +20 -2
- package/lib/server/EnvironmentManagerServer.js +20 -2
- package/lib/server.es.js +7 -3
- package/lib/server.js +6 -2
- package/lib/shared/EnvironmentManager.d.ts +4 -4
- package/lib/shared/template.d.ts +8 -0
- package/lib/shared/template.es.js +20 -0
- package/lib/shared/template.js +24 -0
- package/package.json +6 -6
|
@@ -2,11 +2,11 @@ import type { EnvParameter, EnvironmentManager, ClientEnvironmentRepository as I
|
|
|
2
2
|
export declare class ClientEnvironmentRepository implements Interface {
|
|
3
3
|
private envManager;
|
|
4
4
|
private tokens;
|
|
5
|
-
protected parameters: Record<string, string>;
|
|
5
|
+
protected parameters: Record<string, string | undefined>;
|
|
6
6
|
constructor(envManager: EnvironmentManager, tokens: EnvParameter[]);
|
|
7
7
|
get(name: string): string | undefined;
|
|
8
|
-
set(name: string, value: string): void;
|
|
9
|
-
getAll(): Record<string, string>;
|
|
8
|
+
set(name: string, value: string | undefined): void;
|
|
9
|
+
getAll(): Record<string, string | undefined>;
|
|
10
10
|
update(result: Record<string, string>): void;
|
|
11
11
|
private processing;
|
|
12
12
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import type { EnvParameter } from '@tramvai/tokens-common';
|
|
1
|
+
import type { EnvParameter, EnvTemplate } from '@tramvai/tokens-common';
|
|
2
2
|
import { EnvironmentManager } from '../shared/EnvironmentManager';
|
|
3
3
|
export declare class EnvironmentManagerServer extends EnvironmentManager {
|
|
4
4
|
private tokens;
|
|
5
5
|
private clientEnvRepository;
|
|
6
|
-
|
|
6
|
+
private templates;
|
|
7
|
+
constructor(tokens: EnvParameter[], templates?: EnvTemplate[]);
|
|
8
|
+
get(name: string): string | undefined;
|
|
9
|
+
getAll(): Record<string, string | undefined>;
|
|
7
10
|
/**
|
|
8
11
|
* @deprecated use CLIENT_ENV_MANAGER_TOKEN
|
|
9
12
|
*/
|
|
10
|
-
clientUsed(): Record<string, string>;
|
|
13
|
+
clientUsed(): Record<string, string | undefined>;
|
|
11
14
|
/**
|
|
12
15
|
* @deprecated use CLIENT_ENV_MANAGER_TOKEN
|
|
13
16
|
*/
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import noop from '@tinkoff/utils/function/noop';
|
|
2
2
|
import { EnvironmentManager } from '../shared/EnvironmentManager.es.js';
|
|
3
3
|
import { ClientEnvironmentRepository } from './ClientEnvironmentRepository.es.js';
|
|
4
|
+
import { interpolate } from '../shared/template.es.js';
|
|
4
5
|
|
|
5
6
|
const readFileWithEnv = (path) => {
|
|
6
7
|
try {
|
|
@@ -14,13 +15,30 @@ const readFileWithEnv = (path) => {
|
|
|
14
15
|
}
|
|
15
16
|
};
|
|
16
17
|
class EnvironmentManagerServer extends EnvironmentManager {
|
|
17
|
-
constructor(tokens) {
|
|
18
|
+
constructor(tokens, templates = []) {
|
|
18
19
|
super();
|
|
19
20
|
this.tokens = tokens;
|
|
21
|
+
this.templates = templates.reduce((acc, { key, fn }) => {
|
|
22
|
+
// TODO: key duplicates?
|
|
23
|
+
acc[key] = fn;
|
|
24
|
+
return acc;
|
|
25
|
+
}, {});
|
|
20
26
|
this.processing();
|
|
21
|
-
// for backward compatibility
|
|
27
|
+
// for backward compatibility.
|
|
22
28
|
this.clientEnvRepository = new ClientEnvironmentRepository(this, this.tokens);
|
|
23
29
|
}
|
|
30
|
+
get(name) {
|
|
31
|
+
const value = super.get(name);
|
|
32
|
+
return interpolate({ envKey: name, envValue: value, templates: this.templates });
|
|
33
|
+
}
|
|
34
|
+
getAll() {
|
|
35
|
+
const values = super.getAll();
|
|
36
|
+
const result = {};
|
|
37
|
+
for (const name in values) {
|
|
38
|
+
result[name] = this.get(name);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
24
42
|
/**
|
|
25
43
|
* @deprecated use CLIENT_ENV_MANAGER_TOKEN
|
|
26
44
|
*/
|
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var noop = require('@tinkoff/utils/function/noop');
|
|
6
6
|
var EnvironmentManager = require('../shared/EnvironmentManager.js');
|
|
7
7
|
var ClientEnvironmentRepository = require('./ClientEnvironmentRepository.js');
|
|
8
|
+
var template = require('../shared/template.js');
|
|
8
9
|
|
|
9
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
11
|
|
|
@@ -22,13 +23,30 @@ const readFileWithEnv = (path) => {
|
|
|
22
23
|
}
|
|
23
24
|
};
|
|
24
25
|
class EnvironmentManagerServer extends EnvironmentManager.EnvironmentManager {
|
|
25
|
-
constructor(tokens) {
|
|
26
|
+
constructor(tokens, templates = []) {
|
|
26
27
|
super();
|
|
27
28
|
this.tokens = tokens;
|
|
29
|
+
this.templates = templates.reduce((acc, { key, fn }) => {
|
|
30
|
+
// TODO: key duplicates?
|
|
31
|
+
acc[key] = fn;
|
|
32
|
+
return acc;
|
|
33
|
+
}, {});
|
|
28
34
|
this.processing();
|
|
29
|
-
// for backward compatibility
|
|
35
|
+
// for backward compatibility.
|
|
30
36
|
this.clientEnvRepository = new ClientEnvironmentRepository.ClientEnvironmentRepository(this, this.tokens);
|
|
31
37
|
}
|
|
38
|
+
get(name) {
|
|
39
|
+
const value = super.get(name);
|
|
40
|
+
return template.interpolate({ envKey: name, envValue: value, templates: this.templates });
|
|
41
|
+
}
|
|
42
|
+
getAll() {
|
|
43
|
+
const values = super.getAll();
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const name in values) {
|
|
46
|
+
result[name] = this.get(name);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
32
50
|
/**
|
|
33
51
|
* @deprecated use CLIENT_ENV_MANAGER_TOKEN
|
|
34
52
|
*/
|
package/lib/server.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
2
|
import { Module, provide, Scope, commandLineListTokens } from '@tramvai/core';
|
|
3
3
|
import flatten from '@tinkoff/utils/array/flatten';
|
|
4
|
-
import { ENV_MANAGER_TOKEN, ENV_USED_TOKEN, CLIENT_ENV_REPOSITORY_TOKEN, COMBINE_REDUCERS, CONTEXT_TOKEN } from '@tramvai/tokens-common';
|
|
4
|
+
import { ENV_MANAGER_TOKEN, ENV_USED_TOKEN, ENV_TEMPLATE_TOKEN, CLIENT_ENV_REPOSITORY_TOKEN, COMBINE_REDUCERS, CONTEXT_TOKEN } from '@tramvai/tokens-common';
|
|
5
5
|
export { ENV_MANAGER_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
|
|
6
6
|
import { SERVER_MODULE_PAPI_PUBLIC_ROUTE } from '@tramvai/tokens-server';
|
|
7
7
|
import { createPapiMethod } from '@tramvai/papi';
|
|
@@ -17,14 +17,18 @@ EnvironmentModule = __decorate([
|
|
|
17
17
|
provide({
|
|
18
18
|
provide: ENV_MANAGER_TOKEN,
|
|
19
19
|
scope: Scope.SINGLETON,
|
|
20
|
-
useFactory: ({ tokens }) => {
|
|
21
|
-
return new EnvironmentManagerServer(flatten(tokens !== null && tokens !== void 0 ? tokens : []));
|
|
20
|
+
useFactory: ({ tokens, templates }) => {
|
|
21
|
+
return new EnvironmentManagerServer(flatten(tokens !== null && tokens !== void 0 ? tokens : []), templates !== null && templates !== void 0 ? templates : []);
|
|
22
22
|
},
|
|
23
23
|
deps: {
|
|
24
24
|
tokens: {
|
|
25
25
|
token: ENV_USED_TOKEN,
|
|
26
26
|
optional: true,
|
|
27
27
|
},
|
|
28
|
+
templates: {
|
|
29
|
+
token: ENV_TEMPLATE_TOKEN,
|
|
30
|
+
optional: true,
|
|
31
|
+
},
|
|
28
32
|
},
|
|
29
33
|
}),
|
|
30
34
|
provide({
|
package/lib/server.js
CHANGED
|
@@ -24,14 +24,18 @@ exports.EnvironmentModule = tslib.__decorate([
|
|
|
24
24
|
core.provide({
|
|
25
25
|
provide: tokensCommon.ENV_MANAGER_TOKEN,
|
|
26
26
|
scope: core.Scope.SINGLETON,
|
|
27
|
-
useFactory: ({ tokens }) => {
|
|
28
|
-
return new EnvironmentManagerServer.EnvironmentManagerServer(flatten__default["default"](tokens !== null && tokens !== void 0 ? tokens : []));
|
|
27
|
+
useFactory: ({ tokens, templates }) => {
|
|
28
|
+
return new EnvironmentManagerServer.EnvironmentManagerServer(flatten__default["default"](tokens !== null && tokens !== void 0 ? tokens : []), templates !== null && templates !== void 0 ? templates : []);
|
|
29
29
|
},
|
|
30
30
|
deps: {
|
|
31
31
|
tokens: {
|
|
32
32
|
token: tokensCommon.ENV_USED_TOKEN,
|
|
33
33
|
optional: true,
|
|
34
34
|
},
|
|
35
|
+
templates: {
|
|
36
|
+
token: tokensCommon.ENV_TEMPLATE_TOKEN,
|
|
37
|
+
optional: true,
|
|
38
|
+
},
|
|
35
39
|
},
|
|
36
40
|
}),
|
|
37
41
|
core.provide({
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { EnvironmentManager as Interface } from '@tramvai/tokens-common';
|
|
2
2
|
export declare class EnvironmentManager implements Interface {
|
|
3
|
-
protected parameters: Record<string, string>;
|
|
3
|
+
protected parameters: Record<string, string | undefined>;
|
|
4
4
|
constructor();
|
|
5
5
|
get(name: string): string | undefined;
|
|
6
6
|
getInt(name: string, def: number): number;
|
|
7
|
-
getAll(): Record<string, string>;
|
|
8
|
-
update(result: Record<string, string>): void;
|
|
9
|
-
clientUsed(): Record<string, string>;
|
|
7
|
+
getAll(): Record<string, string | undefined>;
|
|
8
|
+
update(result: Record<string, string | undefined>): void;
|
|
9
|
+
clientUsed(): Record<string, string | undefined>;
|
|
10
10
|
updateClientUsed(result: Record<string, string>): void;
|
|
11
11
|
}
|
|
12
12
|
//# sourceMappingURL=EnvironmentManager.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { EnvTemplate } from '@tramvai/tokens-common';
|
|
2
|
+
export type Templates = Record<string, EnvTemplate['fn']>;
|
|
3
|
+
export declare function interpolate({ envKey, envValue, templates, }: {
|
|
4
|
+
envKey: string;
|
|
5
|
+
envValue: string | undefined;
|
|
6
|
+
templates: Templates;
|
|
7
|
+
}): string | undefined;
|
|
8
|
+
//# sourceMappingURL=template.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// use `$[...]` as template placeholder, because this symbols are not used in popular terminal shell for string interpolation
|
|
2
|
+
const templateRegex = /(?:\$\[(.+?)\])/g;
|
|
3
|
+
function interpolate({ envKey, envValue, templates, }) {
|
|
4
|
+
const originalValue = envValue;
|
|
5
|
+
if (!envValue || typeof envValue !== 'string') {
|
|
6
|
+
return envValue;
|
|
7
|
+
}
|
|
8
|
+
return envValue.replace(templateRegex, (templateRaw, templateStr) => {
|
|
9
|
+
// expect string in `key:param1,param2` format
|
|
10
|
+
const [key, paramsRaw = ''] = templateStr.split(':');
|
|
11
|
+
const params = paramsRaw.split(',').filter(Boolean);
|
|
12
|
+
const template = templates[key];
|
|
13
|
+
if (template) {
|
|
14
|
+
return template(...params);
|
|
15
|
+
}
|
|
16
|
+
throw Error(`Problem with "${envKey}=${originalValue}" env variable - template for ${templateRaw} not found.`);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { interpolate };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
// use `$[...]` as template placeholder, because this symbols are not used in popular terminal shell for string interpolation
|
|
6
|
+
const templateRegex = /(?:\$\[(.+?)\])/g;
|
|
7
|
+
function interpolate({ envKey, envValue, templates, }) {
|
|
8
|
+
const originalValue = envValue;
|
|
9
|
+
if (!envValue || typeof envValue !== 'string') {
|
|
10
|
+
return envValue;
|
|
11
|
+
}
|
|
12
|
+
return envValue.replace(templateRegex, (templateRaw, templateStr) => {
|
|
13
|
+
// expect string in `key:param1,param2` format
|
|
14
|
+
const [key, paramsRaw = ''] = templateStr.split(':');
|
|
15
|
+
const params = paramsRaw.split(',').filter(Boolean);
|
|
16
|
+
const template = templates[key];
|
|
17
|
+
if (template) {
|
|
18
|
+
return template(...params);
|
|
19
|
+
}
|
|
20
|
+
throw Error(`Problem with "${envKey}=${originalValue}" env variable - template for ${templateRaw} not found.`);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.interpolate = interpolate;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-environment",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.26.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"@tinkoff/utils": "^2.1.2",
|
|
22
|
-
"@tramvai/core": "4.
|
|
23
|
-
"@tramvai/papi": "4.
|
|
24
|
-
"@tramvai/state": "4.
|
|
25
|
-
"@tramvai/tokens-common": "4.
|
|
26
|
-
"@tramvai/tokens-server": "4.
|
|
22
|
+
"@tramvai/core": "4.26.1",
|
|
23
|
+
"@tramvai/papi": "4.26.1",
|
|
24
|
+
"@tramvai/state": "4.26.1",
|
|
25
|
+
"@tramvai/tokens-common": "4.26.1",
|
|
26
|
+
"@tramvai/tokens-server": "4.26.1",
|
|
27
27
|
"@tinkoff/dippy": "0.10.8",
|
|
28
28
|
"react": ">=16.14.0",
|
|
29
29
|
"react-dom": ">=16.14.0",
|