UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2020 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18const fs = require("fs-extra");
19const path = require("path");
20const uuid = require("uuid/v4");
21const log_1 = require("./log");
22function 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}
35exports.toArray = toArray;
36async function handlerLoader(name) {
37 const path = await requirePath(name);
38 return require(path).handler;
39}
40exports.handlerLoader = handlerLoader;
41async 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}
53exports.requirePath = requirePath;
54function 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}
74exports.extractParameters = extractParameters;
75function 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}
86exports.replacer = replacer;
87function 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}
111exports.hideString = hideString;
112function guid() {
113 return uuid();
114}
115exports.guid = guid;
116async 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}
125exports.handleError = handleError;
126function handleErrorSync(f, cb = exports.DefaultErrorHandler) {
127 try {
128 return f();
129 }
130 catch (e) {
131 return cb(e);
132 }
133}
134exports.handleErrorSync = handleErrorSync;
135exports.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//# sourceMappingURL=util.js.map
\No newline at end of file