@tramvai/module-common 5.16.2 → 5.17.0
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/CommonModule.browser.js +3 -11
- package/lib/CommonModule.es.js +3 -11
- package/lib/CommonModule.js +2 -10
- package/lib/command/CommandModule.browser.js +9 -7
- package/lib/command/CommandModule.es.js +9 -7
- package/lib/command/CommandModule.js +8 -6
- package/lib/command/commandLineRunner.new.browser.js +159 -0
- package/lib/command/commandLineRunner.new.d.ts +57 -0
- package/lib/command/commandLineRunner.new.es.js +159 -0
- package/lib/command/commandLineRunner.new.js +163 -0
- package/lib/hook/HookModule.browser.js +22 -0
- package/lib/hook/HookModule.d.ts +4 -0
- package/lib/hook/HookModule.es.js +22 -0
- package/lib/hook/HookModule.js +26 -0
- package/package.json +20 -20
- package/lib/command/commandLineRunner.browser.js +0 -145
- package/lib/command/commandLineRunner.es.js +0 -145
- package/lib/command/commandLineRunner.js +0 -149
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var errors = require('@tinkoff/errors');
|
|
6
|
-
var tokensCorePrivate = require('@tramvai/tokens-core-private');
|
|
7
|
-
var dippy = require('@tinkoff/dippy');
|
|
8
|
-
var tokensCommon = require('@tramvai/tokens-common');
|
|
9
|
-
|
|
10
|
-
class CommandLineRunner {
|
|
11
|
-
constructor({ lines, rootDi, logger, executionContextManager, executionEndHandlers }) {
|
|
12
|
-
this.executionContextByDi = new WeakMap();
|
|
13
|
-
this.abortControllerByDi = new WeakMap();
|
|
14
|
-
this.lines = lines;
|
|
15
|
-
this.rootDi = rootDi;
|
|
16
|
-
this.log = logger('command:command-line-runner');
|
|
17
|
-
this.executionContextManager = executionContextManager;
|
|
18
|
-
this.executionEndHandlers = executionEndHandlers;
|
|
19
|
-
}
|
|
20
|
-
run(type, status, providers, customDi, key) {
|
|
21
|
-
const di = customDi ?? this.resolveDi(type, status, this.rootDi, providers);
|
|
22
|
-
const rootExecutionContext = di.get({ token: tokensCommon.ROOT_EXECUTION_CONTEXT_TOKEN, optional: true });
|
|
23
|
-
this.log.debug({
|
|
24
|
-
event: 'command-run',
|
|
25
|
-
type,
|
|
26
|
-
status,
|
|
27
|
-
});
|
|
28
|
-
const timingInfo = {};
|
|
29
|
-
di.register({ provide: tokensCorePrivate.COMMAND_LINE_TIMING_INFO_TOKEN, useValue: timingInfo });
|
|
30
|
-
return (this.lines[type][status]
|
|
31
|
-
.reduce((chain, line) => {
|
|
32
|
-
return chain.then(() => {
|
|
33
|
-
const lineName = line.toString();
|
|
34
|
-
timingInfo[lineName] = { start: performance.now() };
|
|
35
|
-
// eslint-disable-next-line promise/no-nesting
|
|
36
|
-
return Promise.resolve()
|
|
37
|
-
.then(() => {
|
|
38
|
-
return this.executionContextManager.withContext(rootExecutionContext, `command-line:${lineName}`, async (executionContext, abortController) => {
|
|
39
|
-
this.executionContextByDi.set(di, executionContext);
|
|
40
|
-
this.abortControllerByDi.set(di, abortController);
|
|
41
|
-
await this.createLineChain(di, line);
|
|
42
|
-
});
|
|
43
|
-
})
|
|
44
|
-
.finally(() => {
|
|
45
|
-
timingInfo[lineName].end = performance.now();
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
}, Promise.resolve())
|
|
49
|
-
// После завершения цепочки отдаем context выполнения
|
|
50
|
-
.finally(() => {
|
|
51
|
-
this.executionContextByDi.delete(di);
|
|
52
|
-
this.abortControllerByDi.delete(di);
|
|
53
|
-
if (this.executionEndHandlers) {
|
|
54
|
-
for (const executionEndHandler of this.executionEndHandlers) {
|
|
55
|
-
executionEndHandler(di, type, status, timingInfo, key);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
})
|
|
59
|
-
.then(() => di));
|
|
60
|
-
}
|
|
61
|
-
resolveDi(type, status, rootDi, providers) {
|
|
62
|
-
let di = rootDi;
|
|
63
|
-
if (status === 'customer' && type !== 'client') {
|
|
64
|
-
di = dippy.createChildContainer(di);
|
|
65
|
-
}
|
|
66
|
-
if (providers) {
|
|
67
|
-
providers.forEach((item) => {
|
|
68
|
-
return di.register(item);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
return di;
|
|
72
|
-
}
|
|
73
|
-
resolveExecutionContextFromDi(di) {
|
|
74
|
-
return this.executionContextByDi.get(di) ?? null;
|
|
75
|
-
}
|
|
76
|
-
createLineChain(di, line) {
|
|
77
|
-
let lineInstance;
|
|
78
|
-
try {
|
|
79
|
-
lineInstance = di.get({ token: line, optional: true });
|
|
80
|
-
// Пропускаем step. Так как нет действий
|
|
81
|
-
if (lineInstance === null) {
|
|
82
|
-
return Promise.resolve();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
catch (e) {
|
|
86
|
-
// Логируем ошибку и дальше падаем
|
|
87
|
-
this.log.error(e);
|
|
88
|
-
return this.throwError(e, di);
|
|
89
|
-
}
|
|
90
|
-
if (!Array.isArray(lineInstance)) {
|
|
91
|
-
return this.instanceExecute(lineInstance, line, di);
|
|
92
|
-
}
|
|
93
|
-
return Promise.all(lineInstance.map((instance) => {
|
|
94
|
-
return this.instanceExecute(instance, line, di);
|
|
95
|
-
}));
|
|
96
|
-
}
|
|
97
|
-
instanceExecute(instance, line, di) {
|
|
98
|
-
if (!(instance instanceof Function)) {
|
|
99
|
-
const error = new TypeError(`Expected function in line processing "commandLineListTokens.${line.toString()}", received "${instance}".
|
|
100
|
-
Check that all commandLineListTokens subscribers return functions`);
|
|
101
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
102
|
-
const instances = di.get(line);
|
|
103
|
-
const record = di.getRecord(line.toString());
|
|
104
|
-
// пробегаемся по всем инстансам и для текущего получаем его запись, из которой можно получить стек
|
|
105
|
-
for (let i = 0; i < instances.length; i++) {
|
|
106
|
-
if (instances[i] === instance) {
|
|
107
|
-
// @ts-expect-error
|
|
108
|
-
error.stack = `${error.stack}\n---- caused by: ----\n${record.multi[i].stack || ''}`;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
this.log.error({
|
|
113
|
-
event: 'line-error',
|
|
114
|
-
error,
|
|
115
|
-
line: line.toString(),
|
|
116
|
-
});
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
const { name = '' } = instance;
|
|
120
|
-
this.log.debug({
|
|
121
|
-
event: 'line-run',
|
|
122
|
-
line: line.toString(),
|
|
123
|
-
command: name,
|
|
124
|
-
});
|
|
125
|
-
return Promise.resolve()
|
|
126
|
-
.then(() => instance())
|
|
127
|
-
.catch((err) => {
|
|
128
|
-
this.log[errors.isSilentError(err) ? 'debug' : 'error']({
|
|
129
|
-
event: 'line-error',
|
|
130
|
-
error: err,
|
|
131
|
-
line: line.toString(),
|
|
132
|
-
command: name,
|
|
133
|
-
});
|
|
134
|
-
// in case if any error happens during line execution results from other line handlers will not be used anyway
|
|
135
|
-
this.abortControllerByDi.get(di)?.abort(err);
|
|
136
|
-
this.throwError(err, di);
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
// eslint-disable-next-line class-methods-use-this
|
|
140
|
-
throwError(err, di) {
|
|
141
|
-
if (typeof err === 'object') {
|
|
142
|
-
// eslint-disable-next-line no-param-reassign
|
|
143
|
-
err.di = di;
|
|
144
|
-
}
|
|
145
|
-
throw err;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
exports.CommandLineRunner = CommandLineRunner;
|