UNPKG

3.69 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 skill_logging_1 = require("@atomist/skill-logging");
19const log_1 = require("./log");
20const util_1 = require("./util");
21/**
22 * Execute provided skill steps in the order they are provided or until one fails
23 */
24async function runSteps(options) {
25 const parameters = {};
26 const context = options.context;
27 const listeners = util_1.toArray(options.listeners) || [];
28 let result;
29 for (const step of util_1.toArray(options.steps)) {
30 try {
31 if (!step.runWhen || !!(await step.runWhen(context, parameters))) {
32 await context.audit.log(`Running '${step.name}'`);
33 await invokeListeners(listeners.filter(l => !!l.starting), async (l) => l.starting(step, parameters));
34 const sr = await step.run(context, parameters);
35 if (sr) {
36 result = sr;
37 }
38 await invokeListeners(listeners.filter(l => !!l.completed), async (l) => l.completed(step, parameters, sr));
39 if (!!sr && sr.code !== 0) {
40 await context.audit.log(`'${step.name}' errored with: ${sr.reason}`, skill_logging_1.Severity.ERROR);
41 return sr;
42 }
43 else if (!!sr && !!sr.reason) {
44 await context.audit.log(`Completed '${step.name}' with: ${sr.reason}`);
45 }
46 else {
47 await context.audit.log(`Completed '${step.name}'`);
48 }
49 }
50 else {
51 await context.audit.log(`Skipping '${step.name}'`);
52 await invokeListeners(listeners.filter(l => !!l.skipped), async (l) => l.skipped(step, parameters));
53 }
54 }
55 catch (e) {
56 await context.audit.log(`'${step.name}' errored with: ${e.message}`, skill_logging_1.Severity.ERROR);
57 await context.audit.log(e.stack, skill_logging_1.Severity.ERROR);
58 await invokeListeners(listeners.filter(l => !!l.failed), async (l) => l.failed(step, parameters, e));
59 log_1.warn(`'${step.name}' errored with:`);
60 log_1.warn(e.stack);
61 return {
62 code: 1,
63 reason: `'${step.name}' errored`,
64 };
65 }
66 }
67 return invokeDone(listeners.filter(l => !!l.done), parameters, result);
68}
69exports.runSteps = runSteps;
70async function invokeListeners(listeners, cb) {
71 for (const listener of listeners) {
72 try {
73 await cb(listener);
74 }
75 catch (e) {
76 log_1.warn("Listener failed with");
77 log_1.warn(e);
78 }
79 }
80}
81async function invokeDone(listeners, parameters, inputResult) {
82 let result = inputResult;
83 for (const listener of listeners) {
84 try {
85 result = await listener.done(parameters, result);
86 }
87 catch (e) {
88 log_1.warn("Listener failed with:");
89 log_1.warn(e);
90 }
91 }
92 return result;
93}
94//# sourceMappingURL=steps.js.map
\No newline at end of file