All files / src/util misc.ts

100% Statements 42/42
100% Branches 10/10
100% Functions 4/4
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x 1x 1x 1x 20x 3x 3x 3x 3x 4x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 8x 8x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x  
import { Maybe } from "purify-ts";
import fs from "node:fs";
import * as yup from "yup";
 
export function ensureError(error: unknown): Error {
  if (error instanceof Error) return error;
 
  let stringified: string;
  try {
    stringified = JSON.stringify(error);
  } catch {
    stringified = "[Unable to stringify the thrown value]";
  }
 
  return Error(
    `This value was thrown as is, not through an Error: ${stringified}`,
  );
}
 
const SECRET_REGEX = /(-\S*(?:key|token|pass)[^\s=]*(?:=| +))(\S*)/gi;
 
export function hideSecrets(input: string) {
  return input.replace(SECRET_REGEX, "$1<secret value>");
}
 
export function orThrow<T>(value: Maybe<T>, error: string): T {
  return value.toEither(Error(error)).unsafeCoerce();
}
 
const packageInfoSchema = yup.object({
  name: yup.string().required(),
  version: yup.string().required(),
});
 
export function readPackageJson() {
  return Maybe.encase(() => {
    const packageJson = fs.readFileSync("package.json").toString();
    return packageInfoSchema.validateSync(JSON.parse(packageJson), {
      strict: true,
    });
  });
}