UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

183 lines 7.67 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const sdm_1 = require("@atomist/sdm"); const camelcaseKeys = require("camelcase-keys"); const changeCase = require("change-case"); const array_1 = require("../../util/misc/array"); function mapTests(tests, additionalTests, extensionTests) { return __awaiter(this, void 0, void 0, function* () { const newTests = []; for (const t of array_1.toArray(tests || [])) { const test = typeof t !== "string" && !Array.isArray(t) ? camelcaseKeys(t, { deep: true }) : t; newTests.push(yield mapTest(test, additionalTests, extensionTests)); } return newTests; }); } exports.mapTests = mapTests; const HasFile = (test) => __awaiter(void 0, void 0, void 0, function* () { if (test.hasFile) { return sdm_1.hasFile(test.hasFile); } return undefined; }); const IsRepo = (test) => __awaiter(void 0, void 0, void 0, function* () { if (test.isRepo) { return sdm_1.isRepo(typeof test.isRepo === "string" ? new RegExp(test.isRepo) : test.isRepo); } return undefined; }); const IsBranch = (test) => __awaiter(void 0, void 0, void 0, function* () { if (test.isBranch) { return sdm_1.isBranch(typeof test.isBranch === "string" ? new RegExp(test.isBranch) : test.isBranch); } return undefined; }); const IsDefaultBranch = (test) => __awaiter(void 0, void 0, void 0, function* () { if (["isDefaultBranch", "toDefaultBranch"].includes(changeCase.camel(test))) { return sdm_1.ToDefaultBranch; } return undefined; }); const IsGoal = (test, additionalTests, extensionTests) => __awaiter(void 0, void 0, void 0, function* () { if (test.isGoal) { return sdm_1.isGoal({ name: typeof test.isGoal.name === "string" ? new RegExp(test.isGoal.name) : test.isGoal.name, state: test.isGoal.state || sdm_1.SdmGoalState.success, pushTest: test.isGoal.test ? yield mapTest(test.isGoal.test, additionalTests, extensionTests) : undefined, output: typeof test.isGoal.output === "string" ? new RegExp(test.isGoal.output) : test.isGoal.output, data: typeof test.isGoal.data === "string" ? new RegExp(test.isGoal.data) : test.isGoal.data, }); } return undefined; }); const IsMaterialChange = (test) => __awaiter(void 0, void 0, void 0, function* () { if (test.isMaterialChange) { return sdm_1.isMaterialChange({ directories: array_1.toArray(test.isMaterialChange.directories), extensions: array_1.toArray(test.isMaterialChange.extensions), files: array_1.toArray(test.isMaterialChange.files), globs: getGlobPatterns(test.isMaterialChange), }); } return undefined; }); const HasFileContaining = (test) => __awaiter(void 0, void 0, void 0, function* () { if (test.hasFileContaining) { if (!test.hasFileContaining.content) { throw new Error("Push test 'hasFileContaining' can't be used without 'content' property"); } return sdm_1.hasFileContaining(getGlobPatterns(test.hasFileContaining) || "**/*", typeof test.hasFileContaining.content === "string" ? new RegExp(test.hasFileContaining.content) : test.hasFileContaining.content); } return undefined; }); const HasResourceProvider = (test) => __awaiter(void 0, void 0, void 0, function* () { if (test.hasResourceProvider) { if (!test.hasResourceProvider.type) { throw new Error("Push test 'hasResourceProvider' can't be used without 'type' property"); } return sdm_1.hasResourceProvider(test.hasResourceProvider.type, test.hasResourceProvider.name); } return undefined; }); const Not = (test, additionalTests, extensionTests) => __awaiter(void 0, void 0, void 0, function* () { if (test.not) { return sdm_1.not(yield mapTest(test.not, additionalTests, extensionTests)); } return undefined; }); const And = (test, additionalTests, extensionTests) => __awaiter(void 0, void 0, void 0, function* () { if (test.and) { return sdm_1.and(...array_1.toArray(yield mapTests(test.and, additionalTests, extensionTests))); } return undefined; }); const Or = (test, additionalTests, extensionTests) => __awaiter(void 0, void 0, void 0, function* () { if (test.or) { return sdm_1.or(...array_1.toArray(yield mapTests(test.or, additionalTests, extensionTests))); } return undefined; }); const AdditionalTest = (test, additionalTests) => __awaiter(void 0, void 0, void 0, function* () { if (!!test.use && !!additionalTests[test.use]) { return additionalTests[test.use]; } return undefined; }); const FunctionTest = (test) => __awaiter(void 0, void 0, void 0, function* () { if (typeof test === "function") { return sdm_1.pushTest(test.toString(), test); } return undefined; }); const ExtensionTest = (test, additionalTests, extensionTests) => __awaiter(void 0, void 0, void 0, function* () { for (const extTestName in extensionTests) { if (test.use === extTestName) { const extTest = yield extensionTests[extTestName](test.parameters || {}); if (!!extTest.name && !!extTest.mapping) { return extTest; } else { return sdm_1.pushTest(extTestName, extTest); } } } return undefined; }); exports.CreatePushTests = [ HasFile, IsRepo, IsBranch, IsDefaultBranch, IsGoal, IsMaterialChange, HasFileContaining, HasResourceProvider, Not, And, Or, AdditionalTest, FunctionTest, ExtensionTest, ]; function mapTest(test, additionalTests, extensionTests) { return __awaiter(this, void 0, void 0, function* () { for (const createPushTest of exports.CreatePushTests) { const pt = yield createPushTest(test, additionalTests, extensionTests); if (!!pt) { return pt; } } throw new Error(`Unable to construct push test from '${JSON.stringify(test)}'`); }); } exports.mapTest = mapTest; function getGlobPatterns(test) { const pattern = test.globPattern || test.pattern || test.globPatterns || test.patterns; return array_1.toArray(pattern); } //# sourceMappingURL=mapPushTests.js.map