@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
85 lines • 4.13 kB
JavaScript
/*
* 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 automation_client_1 = require("@atomist/automation-client");
const _ = require("lodash");
const os = require("os");
const projectVersioner_1 = require("../../internal/delivery/build/local/projectVersioner");
const util_1 = require("./util");
const PlaceholderExpression = /\$\{([.a-zA-Z_-]+)([.:0-9a-zA-Z-_ \" ]+)*\}/g;
function resolvePlaceholder(value, goal, ctx, parameters, raiseError = true) {
return __awaiter(this, void 0, void 0, function* () {
if (!PlaceholderExpression.test(value)) {
return value;
}
PlaceholderExpression.lastIndex = 0;
let currentValue = value;
let result;
// tslint:disable-next-line:no-conditional-assignment
while (result = PlaceholderExpression.exec(currentValue)) {
const fm = result[0];
let envValue = _.get(goal, result[1]) ||
_.get(ctx.configuration, result[1]) ||
_.get(ctx.configuration, util_1.camelCase(result[1])) ||
_.get(ctx.context, result[1]) ||
_.get(ctx.context, util_1.camelCase(result[1])) ||
_.get({ parameters }, result[1]) ||
_.get({ parameters }, util_1.camelCase(result[1]));
if (result[1] === "home") {
envValue = os.userInfo().homedir;
}
else if (result[1] === "push.after.version" && !!goal) {
envValue = yield projectVersioner_1.getGoalVersion({
context: ctx.context,
owner: goal.repo.owner,
repo: goal.repo.name,
providerId: goal.repo.providerId,
branch: goal.branch,
sha: goal.sha,
});
}
const defaultValue = result[2] ? result[2].trim().slice(1) : undefined;
if (typeof envValue === "string") {
currentValue = currentValue.split(fm).join(envValue);
PlaceholderExpression.lastIndex = 0;
}
else if (typeof envValue === "object" && value === fm) {
return envValue;
}
else if (defaultValue) {
currentValue = currentValue.split(fm).join(defaultValue);
PlaceholderExpression.lastIndex = 0;
}
else if (raiseError) {
automation_client_1.logger.warn(`Placeholder replacement failed for '%s', value: '%j', goal: '%j', parameters: '%j'`, result[1], value, goal, parameters);
throw new Error(`Placeholder '${result[1]}' can't be resolved`);
}
}
return currentValue;
});
}
exports.resolvePlaceholder = resolvePlaceholder;
//# sourceMappingURL=resolvePlaceholder.js.map
;