UNPKG

2.13 kBPlain TextView Raw
1/*
2 * Copyright © 2018 Atomist, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {
18 Maker,
19} from "@atomist/automation-client";
20import { HandleCommand } from "@atomist/automation-client/lib/HandleCommand";
21import { isCommandHandlerMetadata } from "@atomist/automation-client/lib/internal/metadata/metadata";
22import { CommandHandlerMetadata } from "@atomist/automation-client/lib/metadata/automationMetadata";
23import { toFactory } from "@atomist/automation-client/lib/util/constructionUtils";
24import { FunctionalUnit } from "@atomist/sdm";
25
26export interface HandlerInfo {
27 maker: Maker<HandleCommand<any>>;
28 instance: HandleCommand<any> & CommandHandlerMetadata;
29}
30
31/**
32 * Return command handlers with a given tag.
33 * Note this may not find all, but it will find those that know their
34 * own metadata, which is true of all those returned by generatorCommand
35 * and the underlying commandHandlerFrom
36 * @param {FunctionalUnit} unit
37 * @param {string} tag
38 */
39export function commandHandlersWithTag(unit: FunctionalUnit, tag: string): HandlerInfo[] {
40 return selfDescribingHandlers(unit)
41 .filter(hi => hi.instance.tags.some(t => t.name === tag));
42}
43
44/**
45 * Return command handlers along with their metadata
46 * Note this may not find all, but it will find those that know their
47 * own metadata
48 * @param {FunctionalUnit} unit
49 */
50export function selfDescribingHandlers(unit: FunctionalUnit): HandlerInfo[] {
51 return unit.commandHandlers
52 .map(maker => ({maker, instance: toFactory(maker)()}))
53 .filter(hi => isCommandHandlerMetadata(hi.instance)) as HandlerInfo[];
54}