@nu-art/ts-common 0.200.22 → 0.200.24
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/consts/consts.js +0 -1
- package/core/application.js +12 -31
- package/core/debug-flags.js +32 -38
- package/core/dispatcher.js +30 -92
- package/core/error-handling.js +1 -2
- package/core/exceptions.js +47 -84
- package/core/logger/BeLogged.js +19 -26
- package/core/logger/LogClient.js +14 -16
- package/core/logger/LogClient_BaseRotate.js +20 -42
- package/core/logger/LogClient_Browser.js +16 -36
- package/core/logger/LogClient_Function.js +14 -34
- package/core/logger/LogClient_MemBuffer.js +14 -35
- package/core/logger/LogClient_Terminal.js +12 -34
- package/core/logger/Logger.js +67 -152
- package/core/logger/types.js +0 -1
- package/core/logger/utils.js +16 -18
- package/core/module-manager.js +33 -61
- package/core/module.js +43 -65
- package/csv.js +0 -1
- package/index.js +0 -1
- package/modules/CSVModule.js +50 -110
- package/modules/CliParamsModule.js +32 -54
- package/modules/csv-serializer.js +10 -22
- package/package.json +8 -18
- package/permissions/permission-group.js +9 -12
- package/test-index.js +0 -1
- package/testing/consts.js +18 -54
- package/testing/types.js +0 -1
- package/tools/Replacer.js +71 -96
- package/tools/get-log-style.js +5 -10
- package/utils/array-tools.js +63 -127
- package/utils/crypto-tools.js +1 -2
- package/utils/date-time-tools.js +19 -75
- package/utils/db-object-tools.js +0 -1
- package/utils/filter-tools.js +37 -43
- package/utils/hash-tools.js +4 -8
- package/utils/merge-tools.js +4 -5
- package/utils/mimetype-tools.js +0 -1
- package/utils/number-tools.js +3 -4
- package/utils/object-tools.js +16 -20
- package/utils/query-params.js +8 -11
- package/utils/queue.js +47 -115
- package/utils/random-tools.js +2 -3
- package/utils/storage-capacity-tools.js +0 -1
- package/utils/string-tools.js +16 -18
- package/utils/tools.js +11 -42
- package/utils/types.js +1 -2
- package/utils/ui-tools.js +8 -15
- package/utils/version-tools.js +6 -7
- package/validator/type-validators.js +65 -84
- package/validator/validator-core.js +34 -58
- package/consts/consts.js.map +0 -1
- package/core/application.js.map +0 -1
- package/core/debug-flags.js.map +0 -1
- package/core/dispatcher.js.map +0 -1
- package/core/error-handling.js.map +0 -1
- package/core/exceptions.js.map +0 -1
- package/core/logger/BeLogged.js.map +0 -1
- package/core/logger/LogClient.js.map +0 -1
- package/core/logger/LogClient_BaseRotate.js.map +0 -1
- package/core/logger/LogClient_Browser.js.map +0 -1
- package/core/logger/LogClient_Function.js.map +0 -1
- package/core/logger/LogClient_MemBuffer.js.map +0 -1
- package/core/logger/LogClient_Terminal.js.map +0 -1
- package/core/logger/Logger.js.map +0 -1
- package/core/logger/types.js.map +0 -1
- package/core/logger/utils.js.map +0 -1
- package/core/module-manager.js.map +0 -1
- package/core/module.js.map +0 -1
- package/csv.js.map +0 -1
- package/index.js.map +0 -1
- package/modules/CSVModule.js.map +0 -1
- package/modules/CliParamsModule.js.map +0 -1
- package/modules/csv-serializer.js.map +0 -1
- package/permissions/permission-group.js.map +0 -1
- package/test-index.js.map +0 -1
- package/testing/consts.js.map +0 -1
- package/testing/types.js.map +0 -1
- package/tools/Replacer.js.map +0 -1
- package/tools/get-log-style.js.map +0 -1
- package/utils/array-tools.js.map +0 -1
- package/utils/crypto-tools.js.map +0 -1
- package/utils/date-time-tools.js.map +0 -1
- package/utils/db-object-tools.js.map +0 -1
- package/utils/filter-tools.js.map +0 -1
- package/utils/hash-tools.js.map +0 -1
- package/utils/merge-tools.js.map +0 -1
- package/utils/mimetype-tools.js.map +0 -1
- package/utils/number-tools.js.map +0 -1
- package/utils/object-tools.js.map +0 -1
- package/utils/query-params.js.map +0 -1
- package/utils/queue.js.map +0 -1
- package/utils/random-tools.js.map +0 -1
- package/utils/storage-capacity-tools.js.map +0 -1
- package/utils/string-tools.js.map +0 -1
- package/utils/tools.js.map +0 -1
- package/utils/types.js.map +0 -1
- package/utils/ui-tools.js.map +0 -1
- package/utils/version-tools.js.map +0 -1
- package/validator/type-validators.js.map +0 -1
- package/validator/validator-core.js.map +0 -1
package/core/logger/utils.js
CHANGED
|
@@ -21,14 +21,14 @@ exports._logger_indentNewLineBy = exports._logger_logException = exports._logger
|
|
|
21
21
|
/**
|
|
22
22
|
* Created by TacB0sS on 3/16/17.
|
|
23
23
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const array_tools_1 = require("../../utils/array-tools");
|
|
25
|
+
const tools_1 = require("../../utils/tools");
|
|
26
26
|
function _logger_logObject(instance) {
|
|
27
27
|
return (0, tools_1.__stringify)(instance, true);
|
|
28
28
|
}
|
|
29
29
|
exports._logger_logObject = _logger_logObject;
|
|
30
30
|
function _logger_convertLogParamsToStrings(params) {
|
|
31
|
-
return params.map(
|
|
31
|
+
return params.map(toLog => {
|
|
32
32
|
if (typeof toLog === 'undefined')
|
|
33
33
|
return 'undefined';
|
|
34
34
|
if (toLog === null)
|
|
@@ -36,7 +36,7 @@ function _logger_convertLogParamsToStrings(params) {
|
|
|
36
36
|
if (typeof toLog === 'string')
|
|
37
37
|
return toLog;
|
|
38
38
|
if (typeof toLog === 'number')
|
|
39
|
-
return
|
|
39
|
+
return `${toLog}`;
|
|
40
40
|
// @ts-ignore
|
|
41
41
|
if (toLog.stack)
|
|
42
42
|
return _logger_logException(toLog);
|
|
@@ -44,37 +44,35 @@ function _logger_convertLogParamsToStrings(params) {
|
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
46
|
exports._logger_convertLogParamsToStrings = _logger_convertLogParamsToStrings;
|
|
47
|
-
function _logger_logException(error, fullStack) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var isCustomException = false;
|
|
47
|
+
function _logger_logException(error, fullStack = '') {
|
|
48
|
+
let toPrint = '';
|
|
49
|
+
let errorMessage;
|
|
50
|
+
let isCustomException = false;
|
|
52
51
|
if (error.stack) {
|
|
53
|
-
|
|
52
|
+
const stackAsList = error.stack.split('\n');
|
|
54
53
|
errorMessage = stackAsList[0];
|
|
55
54
|
(0, array_tools_1.removeItemFromArray)(stackAsList, errorMessage);
|
|
56
|
-
toPrint = stackAsList.reduce(
|
|
55
|
+
toPrint = stackAsList.reduce((toRet, stacktrace) => {
|
|
57
56
|
if (fullStack.indexOf(stacktrace) !== -1)
|
|
58
57
|
return toRet;
|
|
59
58
|
return toRet + stacktrace + '\n';
|
|
60
59
|
}, toPrint);
|
|
61
60
|
isCustomException = toPrint.indexOf('CustomException') !== -1;
|
|
62
61
|
toPrint = toPrint.replace(/\s+at.*?CustomException.*?\n/, '\n');
|
|
63
|
-
toPrint = toPrint.replace(/\s+at.*?new\s+(.*?) \(.*?\n/,
|
|
62
|
+
toPrint = toPrint.replace(/\s+at.*?new\s+(.*?) \(.*?\n/, `${fullStack.length === 0 ? '' : '\nCaused by '}$1: ${errorMessage.replace('Error: ', '')}\n`);
|
|
64
63
|
if (!isCustomException && errorMessage)
|
|
65
64
|
toPrint = errorMessage + '\n' + toPrint;
|
|
66
65
|
}
|
|
67
|
-
|
|
66
|
+
const cause = error.cause;
|
|
68
67
|
if (cause) {
|
|
69
|
-
|
|
70
|
-
causeStack = causeStack.replace(
|
|
71
|
-
toPrint +=
|
|
68
|
+
let causeStack = _logger_logException(cause, toPrint);
|
|
69
|
+
causeStack = causeStack.replace(`Error: ${cause.message}`, `${cause.message}`);
|
|
70
|
+
toPrint += `${causeStack}`;
|
|
72
71
|
}
|
|
73
72
|
return toPrint;
|
|
74
73
|
}
|
|
75
74
|
exports._logger_logException = _logger_logException;
|
|
76
75
|
function _logger_indentNewLineBy(linePrefix, input) {
|
|
77
|
-
return linePrefix + input.replace(/\n/g,
|
|
76
|
+
return linePrefix + input.replace(/\n/g, `\n${linePrefix}`);
|
|
78
77
|
}
|
|
79
78
|
exports._logger_indentNewLineBy = _logger_indentNewLineBy;
|
|
80
|
-
//# sourceMappingURL=utils.js.map
|
package/core/module-manager.js
CHANGED
|
@@ -16,97 +16,69 @@
|
|
|
16
16
|
* See the License for the specific language governing permissions and
|
|
17
17
|
* limitations under the License.
|
|
18
18
|
*/
|
|
19
|
-
var __extends = (this && this.__extends) || (function () {
|
|
20
|
-
var extendStatics = function (d, b) {
|
|
21
|
-
extendStatics = Object.setPrototypeOf ||
|
|
22
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
23
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
24
|
-
return extendStatics(d, b);
|
|
25
|
-
};
|
|
26
|
-
return function (d, b) {
|
|
27
|
-
if (typeof b !== "function" && b !== null)
|
|
28
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
29
|
-
extendStatics(d, b);
|
|
30
|
-
function __() { this.constructor = d; }
|
|
31
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
32
|
-
};
|
|
33
|
-
})();
|
|
34
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
20
|
exports.ModuleManager = exports.moduleResolver = void 0;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
const dispatcher_1 = require("./dispatcher");
|
|
22
|
+
const exceptions_1 = require("./exceptions");
|
|
23
|
+
const Logger_1 = require("./logger/Logger");
|
|
24
|
+
const array_tools_1 = require("../utils/array-tools");
|
|
25
|
+
const _modules = [];
|
|
41
26
|
function moduleResolver() {
|
|
42
27
|
return _modules;
|
|
43
28
|
}
|
|
44
29
|
exports.moduleResolver = moduleResolver;
|
|
45
|
-
|
|
46
|
-
__extends(ModuleManager, _super);
|
|
30
|
+
class ModuleManager extends Logger_1.Logger {
|
|
47
31
|
// noinspection JSUnusedLocalSymbols
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
32
|
+
constructor() {
|
|
33
|
+
super();
|
|
34
|
+
this.modules = _modules;
|
|
51
35
|
if (ModuleManager.instance)
|
|
52
36
|
throw new exceptions_1.BadImplementationException('Already have one instance of ModuleManager');
|
|
53
|
-
ModuleManager.instance =
|
|
37
|
+
ModuleManager.instance = this;
|
|
54
38
|
dispatcher_1.Dispatcher.modulesResolver = moduleResolver;
|
|
55
|
-
return _this;
|
|
56
39
|
}
|
|
57
|
-
|
|
40
|
+
filterModules(filter) {
|
|
58
41
|
return this.modules.filter(filter);
|
|
59
|
-
}
|
|
60
|
-
|
|
42
|
+
}
|
|
43
|
+
setConfig(config) {
|
|
61
44
|
this.config = config || {};
|
|
62
45
|
return this;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
67
|
-
modules[_i] = arguments[_i];
|
|
68
|
-
}
|
|
69
|
-
modules.reduce(function (carry, module) {
|
|
46
|
+
}
|
|
47
|
+
addModules(...modules) {
|
|
48
|
+
modules.reduce((carry, module) => {
|
|
70
49
|
if (!carry.includes(module))
|
|
71
50
|
(0, array_tools_1.addItemToArray)(carry, module);
|
|
72
51
|
return carry;
|
|
73
52
|
}, this.modules);
|
|
74
53
|
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
var modules = [];
|
|
78
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
79
|
-
modules[_i] = arguments[_i];
|
|
80
|
-
}
|
|
54
|
+
}
|
|
55
|
+
setModules(...modules) {
|
|
81
56
|
this.modules = (0, array_tools_1.filterDuplicates)(modules);
|
|
82
57
|
return this;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.
|
|
87
|
-
this.modules.forEach(function (module) {
|
|
58
|
+
}
|
|
59
|
+
init() {
|
|
60
|
+
this.logInfo(`--------- initializing app ---------`);
|
|
61
|
+
this.modules.forEach((module) => {
|
|
88
62
|
// @ts-ignore
|
|
89
|
-
module.setManager(
|
|
90
|
-
if (
|
|
63
|
+
module.setManager(this);
|
|
64
|
+
if (this.config)
|
|
91
65
|
// @ts-ignore
|
|
92
|
-
module.setConfig(
|
|
66
|
+
module.setConfig(this.config[module.getName()]);
|
|
93
67
|
});
|
|
94
|
-
this.modules.forEach(
|
|
95
|
-
|
|
68
|
+
this.modules.forEach(module => {
|
|
69
|
+
this.logDebug(`--------- ${module.getName()} ---------`);
|
|
96
70
|
// @ts-ignore
|
|
97
71
|
module.init();
|
|
98
72
|
// @ts-ignore
|
|
99
73
|
module.initiated = true;
|
|
100
74
|
});
|
|
101
75
|
// @ts-ignore
|
|
102
|
-
this.modules.forEach(
|
|
103
|
-
this.logInfo(
|
|
76
|
+
this.modules.forEach(module => module.validate());
|
|
77
|
+
this.logInfo(`--------- INITIALIZED ---------`);
|
|
104
78
|
return this;
|
|
105
|
-
}
|
|
106
|
-
|
|
79
|
+
}
|
|
80
|
+
build() {
|
|
107
81
|
this.init();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
}(Logger_1.Logger));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
111
84
|
exports.ModuleManager = ModuleManager;
|
|
112
|
-
//# sourceMappingURL=module-manager.js.map
|
package/core/module.js
CHANGED
|
@@ -16,56 +16,38 @@
|
|
|
16
16
|
* See the License for the specific language governing permissions and
|
|
17
17
|
* limitations under the License.
|
|
18
18
|
*/
|
|
19
|
-
var __extends = (this && this.__extends) || (function () {
|
|
20
|
-
var extendStatics = function (d, b) {
|
|
21
|
-
extendStatics = Object.setPrototypeOf ||
|
|
22
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
23
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
24
|
-
return extendStatics(d, b);
|
|
25
|
-
};
|
|
26
|
-
return function (d, b) {
|
|
27
|
-
if (typeof b !== "function" && b !== null)
|
|
28
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
29
|
-
extendStatics(d, b);
|
|
30
|
-
function __() { this.constructor = d; }
|
|
31
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
32
|
-
};
|
|
33
|
-
})();
|
|
34
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
20
|
exports.Module = void 0;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
__extends(Module, _super);
|
|
21
|
+
const exceptions_1 = require("./exceptions");
|
|
22
|
+
const merge_tools_1 = require("../utils/merge-tools");
|
|
23
|
+
const Logger_1 = require("./logger/Logger");
|
|
24
|
+
const __1 = require("..");
|
|
25
|
+
class Module extends Logger_1.Logger {
|
|
42
26
|
// noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
setTimeout(
|
|
50
|
-
|
|
27
|
+
constructor(tag) {
|
|
28
|
+
super(tag);
|
|
29
|
+
this.initiated = false;
|
|
30
|
+
this.config = {};
|
|
31
|
+
this.timeoutMap = {};
|
|
32
|
+
this.runAsync = (label, toCall) => {
|
|
33
|
+
setTimeout(() => {
|
|
34
|
+
this.logDebug(`Running async: ${label}`);
|
|
51
35
|
new Promise(toCall)
|
|
52
|
-
.then(
|
|
53
|
-
|
|
36
|
+
.then(() => {
|
|
37
|
+
this.logDebug(`Async call completed: ${label}`);
|
|
54
38
|
})
|
|
55
|
-
.catch(
|
|
39
|
+
.catch(reason => this.logError(`Async call error: ${label}`, reason));
|
|
56
40
|
}, 0);
|
|
57
41
|
};
|
|
58
|
-
|
|
59
|
-
if (!
|
|
60
|
-
throw new exceptions_1.BadImplementationException(
|
|
61
|
-
|
|
62
|
-
return _this;
|
|
42
|
+
this.name = this.constructor['name'];
|
|
43
|
+
if (!this.name.endsWith('_Class'))
|
|
44
|
+
throw new exceptions_1.BadImplementationException(`Found module named: ${this.name}, Module class MUST end with '_Class' e.g. MyModule_Class`);
|
|
45
|
+
this.name = this.name.replace('_Class', '');
|
|
63
46
|
}
|
|
64
|
-
|
|
65
|
-
if (ms === void 0) { ms = 0; }
|
|
47
|
+
debounce(handler, key, ms = 0) {
|
|
66
48
|
(0, __1._clearTimeout)(this.timeoutMap[key]);
|
|
67
49
|
this.timeoutMap[key] = (0, __1._setTimeout)(handler, ms);
|
|
68
|
-
}
|
|
50
|
+
}
|
|
69
51
|
// // possibly to add
|
|
70
52
|
// public async debounceSync(handler: TimerHandler, key: string, ms = 0) {
|
|
71
53
|
// _clearTimeout(this.timeoutMap[key]);
|
|
@@ -81,46 +63,42 @@ var Module = /** @class */ (function (_super) {
|
|
|
81
63
|
// }, ms) as unknown as number;
|
|
82
64
|
// });
|
|
83
65
|
// }
|
|
84
|
-
|
|
85
|
-
var _this = this;
|
|
86
|
-
if (ms === void 0) { ms = 0; }
|
|
66
|
+
throttle(handler, key, ms = 0) {
|
|
87
67
|
if (this.timeoutMap[key])
|
|
88
68
|
return;
|
|
89
|
-
this.timeoutMap[key] = (0, __1._setTimeout)(
|
|
69
|
+
this.timeoutMap[key] = (0, __1._setTimeout)(() => {
|
|
90
70
|
handler();
|
|
91
|
-
delete
|
|
71
|
+
delete this.timeoutMap[key];
|
|
92
72
|
}, ms);
|
|
93
|
-
}
|
|
94
|
-
|
|
73
|
+
}
|
|
74
|
+
setConfigValidator(validator) {
|
|
95
75
|
// @ts-ignore
|
|
96
76
|
this.configValidator = validator;
|
|
97
|
-
}
|
|
98
|
-
|
|
77
|
+
}
|
|
78
|
+
setDefaultConfig(config) {
|
|
99
79
|
// @ts-ignore
|
|
100
80
|
this.config = (0, merge_tools_1.merge)(this.config, config);
|
|
101
|
-
}
|
|
102
|
-
|
|
81
|
+
}
|
|
82
|
+
getName() {
|
|
103
83
|
return this.name;
|
|
104
|
-
}
|
|
105
|
-
|
|
84
|
+
}
|
|
85
|
+
setName(name) {
|
|
106
86
|
this.name = name;
|
|
107
|
-
}
|
|
108
|
-
|
|
87
|
+
}
|
|
88
|
+
setConfig(config) {
|
|
109
89
|
// @ts-ignore
|
|
110
90
|
this.config = this.config ? (0, merge_tools_1.merge)(this.config, config || {}) : config;
|
|
111
91
|
this.config.minLogLevel && this.setMinLevel(this.config.minLogLevel);
|
|
112
|
-
}
|
|
113
|
-
|
|
92
|
+
}
|
|
93
|
+
setManager(manager) {
|
|
114
94
|
// @ts-ignore
|
|
115
95
|
this.manager = manager;
|
|
116
|
-
}
|
|
117
|
-
|
|
96
|
+
}
|
|
97
|
+
init() {
|
|
118
98
|
// ignorance is bliss
|
|
119
|
-
}
|
|
120
|
-
|
|
99
|
+
}
|
|
100
|
+
validate() {
|
|
121
101
|
// ignorance is bliss
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
}(Logger_1.Logger));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
125
104
|
exports.Module = Module;
|
|
126
|
-
//# sourceMappingURL=module.js.map
|
package/csv.js
CHANGED
package/index.js
CHANGED
|
@@ -75,4 +75,3 @@ __exportStar(require("./validator/type-validators"), exports);
|
|
|
75
75
|
__exportStar(require("./consts/consts"), exports);
|
|
76
76
|
__exportStar(require("./modules/CliParamsModule"), exports);
|
|
77
77
|
__exportStar(require("./modules/csv-serializer"), exports);
|
|
78
|
-
//# sourceMappingURL=index.js.map
|
package/modules/CSVModule.js
CHANGED
|
@@ -16,21 +16,6 @@
|
|
|
16
16
|
* See the License for the specific language governing permissions and
|
|
17
17
|
* limitations under the License.
|
|
18
18
|
*/
|
|
19
|
-
var __extends = (this && this.__extends) || (function () {
|
|
20
|
-
var extendStatics = function (d, b) {
|
|
21
|
-
extendStatics = Object.setPrototypeOf ||
|
|
22
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
23
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
24
|
-
return extendStatics(d, b);
|
|
25
|
-
};
|
|
26
|
-
return function (d, b) {
|
|
27
|
-
if (typeof b !== "function" && b !== null)
|
|
28
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
29
|
-
extendStatics(d, b);
|
|
30
|
-
function __() { this.constructor = d; }
|
|
31
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
32
|
-
};
|
|
33
|
-
})();
|
|
34
19
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
35
20
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
36
21
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -40,44 +25,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
40
25
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
41
26
|
});
|
|
42
27
|
};
|
|
43
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
44
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
45
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
46
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
47
|
-
function step(op) {
|
|
48
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
49
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
50
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
51
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
52
|
-
switch (op[0]) {
|
|
53
|
-
case 0: case 1: t = op; break;
|
|
54
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
55
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
56
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
57
|
-
default:
|
|
58
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
59
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
60
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
61
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
62
|
-
if (t[2]) _.ops.pop();
|
|
63
|
-
_.trys.pop(); continue;
|
|
64
|
-
}
|
|
65
|
-
op = body.call(thisArg, _);
|
|
66
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
67
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
71
29
|
exports.CSVModule = void 0;
|
|
72
30
|
/**
|
|
73
31
|
* Created by tacb0ss on 07/05/2018.
|
|
74
32
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
33
|
+
const export_to_csv_1 = require("export-to-csv");
|
|
34
|
+
const fs_1 = require("fs");
|
|
35
|
+
const module_1 = require("../core/module");
|
|
36
|
+
const stream_1 = require("stream");
|
|
37
|
+
const csvParser = require("csv-parser");
|
|
38
|
+
const DefaultConfig = {
|
|
81
39
|
options: {
|
|
82
40
|
fieldSeparator: ',',
|
|
83
41
|
quoteStrings: '"',
|
|
@@ -90,85 +48,67 @@ var DefaultConfig = {
|
|
|
90
48
|
useKeysAsHeaders: true,
|
|
91
49
|
}
|
|
92
50
|
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
_this.setDefaultConfig(DefaultConfig);
|
|
98
|
-
return _this;
|
|
51
|
+
class CSVModule_Class extends module_1.Module {
|
|
52
|
+
constructor() {
|
|
53
|
+
super();
|
|
54
|
+
this.setDefaultConfig(DefaultConfig);
|
|
99
55
|
}
|
|
100
|
-
|
|
56
|
+
init() {
|
|
101
57
|
this.csvExporter = CSVModule_Class.createExporter(this.config.options);
|
|
102
|
-
}
|
|
103
|
-
|
|
58
|
+
}
|
|
59
|
+
static createExporter(options) {
|
|
104
60
|
return new export_to_csv_1.ExportToCsv(options);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (returnCsv === void 0) { returnCsv = true; }
|
|
61
|
+
}
|
|
62
|
+
export(items, returnCsv = true) {
|
|
108
63
|
return this.csvExporter.generateCsv(items, returnCsv);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
112
|
-
|
|
113
|
-
return
|
|
114
|
-
csv = this.csvExporter.generateCsv(items, true);
|
|
115
|
-
return [2 /*return*/, fs_1.promises.writeFile(outputFile, csv, { encoding: 'utf8' })];
|
|
116
|
-
});
|
|
64
|
+
}
|
|
65
|
+
saveToFile(outputFile, items, columnsToProps) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const csv = this.csvExporter.generateCsv(items, true);
|
|
68
|
+
return fs_1.promises.writeFile(outputFile, csv, { encoding: 'utf8' });
|
|
117
69
|
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
121
|
-
|
|
122
|
-
return
|
|
123
|
-
stream = (0, fs_1.createReadStream)(inputFile, { encoding: 'utf8' });
|
|
124
|
-
return [2 /*return*/, this.readCsvFromStream(stream, readOptions)];
|
|
125
|
-
});
|
|
70
|
+
}
|
|
71
|
+
readCsvFromFile(inputFile, readOptions) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const stream = (0, fs_1.createReadStream)(inputFile, { encoding: 'utf8' });
|
|
74
|
+
return this.readCsvFromStream(stream, readOptions);
|
|
126
75
|
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
130
|
-
|
|
131
|
-
return
|
|
132
|
-
stream = stream_1.Readable.from(buffer.toString('utf-8'), { encoding: 'utf8' });
|
|
133
|
-
return [2 /*return*/, this.readCsvFromStream(stream, readOptions)];
|
|
134
|
-
});
|
|
76
|
+
}
|
|
77
|
+
readCsvFromBuffer(buffer, readOptions) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const stream = stream_1.Readable.from(buffer.toString('utf-8'), { encoding: 'utf8' });
|
|
80
|
+
return this.readCsvFromStream(stream, readOptions);
|
|
135
81
|
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
})
|
|
150
|
-
.on('error', function (err) { return reject(err); })
|
|
151
|
-
.on('end', function () { return resolve(results); });
|
|
152
|
-
})];
|
|
82
|
+
}
|
|
83
|
+
readCsvFromStream(stream, readOptions = {}) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
const results = [];
|
|
87
|
+
stream
|
|
88
|
+
.pipe(csvParser(this.createReadParserOptions(readOptions)))
|
|
89
|
+
.on('data', (instance) => {
|
|
90
|
+
delete instance['undefined'];
|
|
91
|
+
results.push(instance);
|
|
92
|
+
})
|
|
93
|
+
.on('error', (err) => reject(err))
|
|
94
|
+
.on('end', () => resolve(results));
|
|
153
95
|
});
|
|
154
96
|
});
|
|
155
|
-
}
|
|
156
|
-
|
|
97
|
+
}
|
|
98
|
+
createReadParserOptions(readOptions) {
|
|
157
99
|
return {
|
|
158
|
-
mapHeaders:
|
|
100
|
+
mapHeaders: (args) => {
|
|
159
101
|
var _a, _b;
|
|
160
102
|
return (_b = (_a = readOptions.columnsToProps) === null || _a === void 0 ? void 0 : _a[args.header]) !== null && _b !== void 0 ? _b : args.header;
|
|
161
103
|
},
|
|
162
|
-
mapValues:
|
|
104
|
+
mapValues: (args) => {
|
|
163
105
|
var _a;
|
|
164
|
-
|
|
106
|
+
const mapValues = (_a = readOptions.mapValues) === null || _a === void 0 ? void 0 : _a.call(readOptions, args.header, args.value, args.index);
|
|
165
107
|
return mapValues !== null && mapValues !== void 0 ? mapValues : args.value;
|
|
166
108
|
},
|
|
167
109
|
quote: readOptions.quote || '"',
|
|
168
110
|
headers: readOptions.headers
|
|
169
111
|
};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
}(module_1.Module));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
173
114
|
exports.CSVModule = new CSVModule_Class();
|
|
174
|
-
//# sourceMappingURL=CSVModule.js.map
|