@cucumber/cucumber
Version:
The official JavaScript implementation of Cucumber.
53 lines • 2.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishPlugin = void 0;
const node_stream_1 = require("node:stream");
const node_util_1 = require("node:util");
const supports_color_1 = require("supports-color");
const has_ansi_1 = __importDefault(require("has-ansi"));
const http_stream_1 = __importDefault(require("./http_stream"));
const DEFAULT_CUCUMBER_PUBLISH_URL = 'https://messages.cucumber.io/api/reports';
exports.publishPlugin = {
type: 'plugin',
coordinator: async ({ on, logger, options, environment }) => {
if (!options) {
return undefined;
}
const { url = DEFAULT_CUCUMBER_PUBLISH_URL, token } = options;
const headers = {};
if (token !== undefined) {
headers.Authorization = `Bearer ${token}`;
}
const stream = new http_stream_1.default(url, 'GET', headers);
const readerStream = new node_stream_1.Writable({
objectMode: true,
write: function (responseBody, encoding, writeCallback) {
environment.stderr.write(sanitisePublishOutput(responseBody, environment.stderr) + '\n');
writeCallback();
},
});
stream.pipe(readerStream);
stream.on('error', (error) => logger.error(error.message));
on('message', (value) => stream.write(JSON.stringify(value) + '\n'));
return () => new Promise((resolve) => {
stream.on('finish', () => resolve());
stream.end();
});
},
};
/*
This is because the Cucumber Reports service returns a pre-formatted console message
including ANSI escapes, so if our stderr stream doesn't support those we need to
strip them back out. Ideally we should get structured data from the service and
compose the console message on this end.
*/
function sanitisePublishOutput(raw, stderr) {
if (!(0, supports_color_1.supportsColor)(stderr) && (0, has_ansi_1.default)(raw)) {
return (0, node_util_1.stripVTControlCharacters)(raw);
}
return raw;
}
//# sourceMappingURL=publish_plugin.js.map