1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const fs = require("fs-extra");
|
19 | const path = require("path");
|
20 | const uuid = require("uuid/v4");
|
21 | const log_1 = require("./log");
|
22 | function toArray(value) {
|
23 | if (value) {
|
24 | if (Array.isArray(value)) {
|
25 | return value;
|
26 | }
|
27 | else {
|
28 | return [value];
|
29 | }
|
30 | }
|
31 | else {
|
32 | return undefined;
|
33 | }
|
34 | }
|
35 | exports.toArray = toArray;
|
36 | async function handlerLoader(name) {
|
37 | const path = await requirePath(name);
|
38 | return require(path).handler;
|
39 | }
|
40 | exports.handlerLoader = handlerLoader;
|
41 | async function requirePath(folderOrFile) {
|
42 | const p = __dirname.split("/node_modules/");
|
43 | const rp = path.join(p[0], folderOrFile);
|
44 | const lp = path.join(p[0], "lib", folderOrFile);
|
45 | if (await fs.pathExists(rp + ".js")) {
|
46 | return rp;
|
47 | }
|
48 | else if (await fs.pathExists(lp + ".js")) {
|
49 | return lp;
|
50 | }
|
51 | throw new Error(`'${folderOrFile}' not found in '${p[0]}' or '${p[0]}/lib'`);
|
52 | }
|
53 | exports.requirePath = requirePath;
|
54 | function extractParameters(intent) {
|
55 | const args = [];
|
56 | const regexp = /^[a-zA-Z\s]*(\s+--([a-z.A-Z_]*)=(?:'([^']*?)'|"([^"]*?)"|([\w]*?)))*$/g;
|
57 | let intentToMatch = intent.trim();
|
58 | let match = regexp.exec(intentToMatch);
|
59 | while (!!match && !!match[1] && !!match[2]) {
|
60 | const name = match[2];
|
61 | const value = match[3] || match[4] || match[5];
|
62 | args.push({ name, value });
|
63 | intentToMatch = intentToMatch.replace(match[1], "").trim();
|
64 | regexp.lastIndex = 0;
|
65 | match = regexp.exec(intentToMatch);
|
66 | }
|
67 | return args.reduce((p, c) => {
|
68 | if (!p.some(e => e.name === c.name)) {
|
69 | p.push(c);
|
70 | }
|
71 | return p;
|
72 | }, []).reverse();
|
73 | }
|
74 | exports.extractParameters = extractParameters;
|
75 | function replacer(key, value) {
|
76 | if (key === "secrets" && value) {
|
77 | return value.map(v => ({ uri: v.uri, value: hideString(v.value) }));
|
78 | }
|
79 | else if (/token|password|jwt|url|secret|authorization|key|cert|pass|user/i.test(key)) {
|
80 | return hideString(value);
|
81 | }
|
82 | else {
|
83 | return value;
|
84 | }
|
85 | }
|
86 | exports.replacer = replacer;
|
87 | function hideString(value) {
|
88 | if (!value) {
|
89 | return value;
|
90 | }
|
91 | if (typeof value === "string") {
|
92 | let newValue = "";
|
93 | for (let i = 0; i < value.length; i++) {
|
94 | if (i === 0) {
|
95 | newValue = value.charAt(0);
|
96 | }
|
97 | else if (i < value.length - 1) {
|
98 | newValue += "*";
|
99 | }
|
100 | else {
|
101 | newValue += value.slice(-1);
|
102 | }
|
103 | }
|
104 | return newValue;
|
105 | }
|
106 | else if (Array.isArray(value)) {
|
107 | return value.map(hideString);
|
108 | }
|
109 | return value;
|
110 | }
|
111 | exports.hideString = hideString;
|
112 | function guid() {
|
113 | return uuid();
|
114 | }
|
115 | exports.guid = guid;
|
116 | async function handleError(f, cb = exports.DefaultErrorHandler) {
|
117 | try {
|
118 | const result = await f();
|
119 | return result;
|
120 | }
|
121 | catch (e) {
|
122 | return cb(e);
|
123 | }
|
124 | }
|
125 | exports.handleError = handleError;
|
126 | function handleErrorSync(f, cb = exports.DefaultErrorHandler) {
|
127 | try {
|
128 | return f();
|
129 | }
|
130 | catch (e) {
|
131 | return cb(e);
|
132 | }
|
133 | }
|
134 | exports.handleErrorSync = handleErrorSync;
|
135 | exports.DefaultErrorHandler = err => {
|
136 | log_1.error(`Error occurred: %s`, err.message);
|
137 | if (err.stack) {
|
138 | log_1.error(err.stack);
|
139 | }
|
140 | return undefined;
|
141 | };
|
142 |
|
\ | No newline at end of file |