UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

96 lines 3.77 kB
"use strict"; /* * Copyright © 2020 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.parseKubernetesSpecString = exports.parseKubernetesSpecFile = exports.parseKubernetesSpec = exports.loadKubernetesSpec = void 0; const logger_1 = require("@atomist/automation-client/lib/util/logger"); const yaml = require("js-yaml"); const path = require("path"); /** * Read and parse either JSON or YAML file with basename `base` under * `.atomist/kubernetes` in project. This function looks for * `base`.json, `base`.yaml, and then `base`.yml. If it successfully * reads and parses one of them into a truthy value, it returns its * parsed value. If there are mulitple files matching those it looks * for, it does _not_ overlay/merge the parsed values. It stops after * the first successfully parsed file. */ async function loadKubernetesSpec(p, base) { for (const ext of ["json", "yaml", "yml"]) { const specFile = `${base}.${ext}`; const specPath = path.join(".atomist", "kubernetes", specFile); const spec = await parseKubernetesSpec(p, specPath); if (spec) { return spec; } } return undefined; } exports.loadKubernetesSpec = loadKubernetesSpec; /** * Reads and parses Kubernetes JSON or YAML spec from the project. * It swallows all exceptions, returning undefined if one occurs. * * If the `specPath` of the file ends with `.yaml` or `.yml`, the file * contents are parsed as YAML. Otherwise it is parsed as JSON. * * @param p Project to look for spec file in * @param specPath Path of spec file to load * @return Parsed object if the spec was successfully read and parsed, undefined otherwise */ async function parseKubernetesSpec(p, specPath) { try { const specFile = await p.getFile(specPath); if (!specFile) { return undefined; } const spec = await parseKubernetesSpecFile(specFile); return spec; } catch (e) { logger_1.logger.warn(`Failed to read and parse spec file ${specPath}: ${e.message}`); } return undefined; } exports.parseKubernetesSpec = parseKubernetesSpec; /** * Reads and parses Kubernetes JSON or YAML spec from the project. * * If the `specFile.path` of the file ends with `.yaml` or `.yml`, the file * contents are parsed as YAML. Otherwise it is parsed as JSON. * * @param specFile File object of spec file to load * @return Parsed object of the spec */ async function parseKubernetesSpecFile(specFile) { const specString = await specFile.getContent(); return parseKubernetesSpecString(specString, specFile.path); } exports.parseKubernetesSpecFile = parseKubernetesSpecFile; /** * Parses content string as Kubernetes JSON or YAML spec. It parses * the file as YAML, since JSON is valid YAML. * * @param specString String representation of Kubernetes spec * @param specPath File path of Kubernetes spec file * @return Parsed object of the spec */ async function parseKubernetesSpecString(specString, specPath) { const spec = yaml.safeLoad(specString); return spec; } exports.parseKubernetesSpecString = parseKubernetesSpecString; //# sourceMappingURL=spec.js.map