UNPKG

2.93 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 });
18exports.projectConfigurationValue = void 0;
19const configuration_1 = require("@atomist/automation-client/lib/configuration");
20const logger_1 = require("@atomist/automation-client/lib/util/logger");
21const yaml = require("js-yaml");
22const _ = require("lodash");
23/**
24 * Gather a configuration value from:
25 *
26 * 1) within a file .atomist/config.json or .atomist/config.yaml within the Project
27 * 2) from within the `sdm` property in the wider, globally available configuration. For where these come from, see:
28 * https://atomist.github.io/automation-client/modules/_lib_configuration_.html#loadconfiguration
29 * 3) the default passed in, if any
30 * 4) ... or else throw.
31 *
32 * This will not merge the configuration objects. It gives the first one that is not undefined.
33 */
34async function projectConfigurationValue(path, p, defaultValue) {
35 // Project specific configuration first
36 let cf = await p.getFile(".atomist/config.json");
37 if (!!cf) {
38 try {
39 const conf = JSON.parse(await cf.getContent());
40 const value = _.get(conf, path);
41 if (value !== undefined) {
42 return value;
43 }
44 }
45 catch (e) {
46 logger_1.logger.warn(`Failed to load/parse '.atomist/config.json' from '${p.id.owner}/${p.id.repo}': ${e.message}`);
47 }
48 }
49 cf = await p.getFile(".atomist/config.yaml");
50 if (!!cf) {
51 try {
52 const conf = yaml.safeLoad(await cf.getContent());
53 const value = _.get(conf, path);
54 if (value !== undefined) {
55 return value;
56 }
57 }
58 catch (e) {
59 logger_1.logger.warn(`Failed to load/parse '.atomist/config.yaml' from '${p.id.owner}/${p.id.repo}': ${e.message}`);
60 }
61 }
62 // SDM configuration as fallback
63 const cfg = configuration_1.configurationValue(`sdm.${path}`, defaultValue);
64 if (!!cfg) {
65 return cfg;
66 }
67 // Lastly use the defaultValue if provided
68 if (defaultValue !== undefined) {
69 return defaultValue;
70 }
71 throw new Error(`Required project configuration value '${path}' not available`);
72}
73exports.projectConfigurationValue = projectConfigurationValue;
74//# sourceMappingURL=projectConfiguration.js.map
\No newline at end of file