@plugjs/plug
Version:
PlugJS Build System ===================
68 lines • 2.54 kB
JavaScript
/* ========================================================================== *
* BUILD FAILURES *
* ========================================================================== */
import { githubAnnotation } from "./logging/github.js";
/** A symbol marking {@link BuildFailure} instances */
const buildFailure = Symbol.for('plugjs:plug:types:BuildFailure');
/** A {@link BuildFailure} represents an error _already logged_ in our build. */
export class BuildFailure extends Error {
errors;
/** Construct a {@link BuildFailure} */
constructor(message, errors = []) {
super(message || '');
// Annotate this build failure in GitHub
if (message) {
githubAnnotation({ type: 'error', title: 'Build Failure' }, message);
}
/* Basic error setup: stack and errors */
Error.captureStackTrace(this, BuildFailure);
if (errors.length)
this.errors = Object.freeze([...errors]);
}
static fail() {
return new BuildFailure(undefined, []);
}
static withMessage(message) {
return new BuildFailure(message, []);
}
static withErrors(errors) {
return new BuildFailure(undefined, errors);
}
static [Symbol.hasInstance](instance) {
return instance && instance[buildFailure] === buildFailure;
}
static {
this.prototype[buildFailure] = buildFailure;
this.prototype.name = this.name;
}
}
/** Await and assert that all specified promises were fulfilled */
export async function assertPromises(promises) {
// Await for the settlement of all the promises
const settlements = await Promise.allSettled(promises);
// Separate the good from the bad...
const results = [];
const failures = new Set();
settlements.forEach((settlement) => {
if (settlement.status === 'fulfilled') {
results.push(settlement.value);
}
else {
failures.add(settlement.reason);
}
});
// Check for errors and report/fail if anything happened
if (failures.size)
throw BuildFailure.withErrors([...failures]);
return results;
}
/** Asserts something as _truthy_ and fail the build if not */
export function assert(assertion, message) {
if (!assertion)
throw BuildFailure.withMessage(message);
}
/** Fail a build with a message */
export function fail(message) {
throw BuildFailure.withMessage(message);
}
//# sourceMappingURL=asserts.js.map