@atomist/atomist-sdm
Version:
Atomist SDM to deliver our own projects
122 lines • 4.84 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) {
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) : new P(function (resolve) { resolve(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 sdm_1 = require("@atomist/sdm");
function loglog(log, msg) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.debug(msg);
log.write(`${msg}\n`);
yield log.flush();
});
}
exports.loglog = loglog;
/**
* Transform a SpawnWatchCommand into an ExecuteLogger suitable for
* execution by executeLoggers. The operation is awaited and any
* thrown exceptions are caught and transformed into an error result.
* If an error occurs, it is logged. The result of the operation is
* transformed into a ExecuteGoalResult. If an exception is caught,
* the returned code is guaranteed to be non-zero.
*/
function spawnExecuteLogger(swc) {
return (log) => __awaiter(this, void 0, void 0, function* () {
const opts = Object.assign({}, swc.cmd.options, { log });
if (swc.cwd) {
opts.cwd = swc.cwd;
}
let res;
try {
res = yield sdm_1.spawnLog(swc.cmd.command, swc.cmd.args, opts);
}
catch (e) {
res = {
code: -1,
message: `Spawned command errored: ${swc.cmd.command} ${swc.cmd.args.join(" ")}: ${e.message}`,
};
}
if (res.error) {
if (!res.message) {
res.message = `Spawned command failed (status:${res.code}): ${swc.cmd.command} ${swc.cmd.args.join(" ")}`;
}
automation_client_1.logger.error(res.message);
log.write(res.message);
}
return res;
});
}
exports.spawnExecuteLogger = spawnExecuteLogger;
/**
* Transform a GitCommandGitProject operation into an ExecuteLogger
* suitable for execution by executeLoggers. The operation is awaited
* and any thrown exceptions are caught and transformed into an error
* result. The returned standard out and standard error are written
* to the log. If an error occurs, it is logged. The result of the
* operation is transformed into a ExecuteGoalResult. If an error is
* returned or exception caught, the returned code is guaranteed to be
* non-zero.
*/
function gitExecuteLogger(gp, op, name) {
return (log) => __awaiter(this, void 0, void 0, function* () {
log.write(`Running: git ${name}`);
try {
yield op();
log.write(`Success: git ${name}`);
return { code: 0 };
}
catch (e) {
log.write(e.stdout);
log.write(e.stderr);
const message = `Failure: git ${name}: ${e.message}`;
log.write(message);
return {
code: e.code,
message,
};
}
});
}
exports.gitExecuteLogger = gitExecuteLogger;
/**
* Execute an array of logged commands, creating a line-delimited
* progress log beforehand, flushing after each command, and closing
* it at the end. If any command fails, bail out and return the
* failure result. Otherwise return Success.
*/
function executeLoggers(els, progressLog) {
return __awaiter(this, void 0, void 0, function* () {
const log = new sdm_1.DelimitedWriteProgressLogDecorator(progressLog, "\n");
for (const cmd of els) {
const res = yield cmd(log);
yield log.flush();
if (res.code !== 0) {
return res;
}
}
return automation_client_1.Success;
});
}
exports.executeLoggers = executeLoggers;
//# sourceMappingURL=executeLogger.js.map