UNPKG

2.62 kBPlain TextView Raw
1/*
2 * Copyright © 2019 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 { Project } from "@atomist/automation-client";
18import {
19 CodeTransform,
20 ExtensionPack,
21 GeneratorRegistration,
22 metadata,
23 ParametersObject,
24} from "@atomist/sdm";
25import { toArray } from "../../util/misc/array";
26import { universalGenerator } from "./generator";
27
28/**
29 * Registration for CodeTransform, ParametersObject and an additional test
30 */
31export interface UniversalTransform<PARAMS = any> {
32 /** Return true if provided CodeTransforms should be executed against Project p */
33 test?: (p: Project) => Promise<boolean>;
34 /** CodeTransforms to execute */
35 transforms: CodeTransform<PARAMS> | Array<CodeTransform<PARAMS>>;
36 /** Additional parameters the CodeTransforms need */
37 parameters?: ParametersObject<PARAMS>;
38}
39
40/**
41 * Configuration options for the universalGeneratorSupport extension pack
42 */
43export interface UniversalGeneratorSupportOptions {
44 /** Generators to enrich with additional CodeTransforms */
45 generators: GeneratorRegistration<any> | Array<GeneratorRegistration<any>>;
46 /** Additional CodeTransforms, their parameters and optional project test */
47 transformsAndParameters?: UniversalTransform<any> | Array<UniversalTransform<any>>;
48}
49
50/**
51 * Configure generators in the form GeneratorRegistrations to run additional CodeTransforms
52 *
53 * The CodeTransforms are getting registered with an optional test which can be used to
54 * interrogate the seed project and determine if the CodeTransform should be executed
55 * against the seed.
56 * CodeTransforms can also register parameter definitions to ask for more parameters via a
57 * promptFor question flow.
58 */
59export function universalGeneratorSupport(options: UniversalGeneratorSupportOptions): ExtensionPack {
60 return {
61 ...metadata(),
62 configure: sdm => {
63 toArray(options.generators)
64 .map(g => universalGenerator<any>(sdm, g, toArray(options.transformsAndParameters || [])))
65 .forEach(g => sdm.addGeneratorCommand(g));
66 },
67 };
68}