sailfork / t3code-github / scripts/ resolve-previous-release-tag.ts

#!/usr/bin/env node

import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as String from "effect/String";
import { Command, Flag } from "effect/cli";
import { ChildProcess, ChildProcessSpawner } from "effect/process";

const ReleaseChannel = Schema.Literals(["stable", "nightly", "preview"]);
type ReleaseChannel = typeof ReleaseChannel.Type;

export class InvalidReleaseTagError extends Schema.TaggedError<InvalidReleaseTagError>()(
  "InvalidReleaseTagError",
  {
    channel: ReleaseChannel,
    currentTag: Schema.String,
  },
) {
  override get message(): string {
    return `Invalid ${this.channel} release tag '${this.currentTag}'.`;
  }
}

const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));

const releaseTagListProcessContext = {
  executable: Schema.Literal("git"),
  argumentCount: NonNegativeInt,
  cwd: Schema.String,
};

export class ReleaseTagListProcessError extends Schema.TaggedError<ReleaseTagListProcessError>()(
  "ReleaseTagListProcessError",
  {
    ...releaseTagListProcessContext,
    operation: Schema.Literals(["spawn", "read-stdout", "read-stderr", "wait-for-exit"]),
    cause: Schema.Defect(),
  },
) {
  override get message(): string {
    return `Failed to list release tags during process operation "${this.operation}".`;
  }
}

export class ReleaseTagListProcessExitError extends Schema.TaggedError<ReleaseTagListProcessExitError>()(
  "ReleaseTagListProcessExitError",
  {
    ...releaseTagListProcessContext,
    exitCode: Schema.Number,
    stdoutLength: NonNegativeInt,
    stderrLength: NonNegativeInt,
  },
) {
  override get message(): string {
    return `Release tag listing exited with code ${this.exitCode}.`;
  }
}

export class PreviousReleaseTagGitHubOutputConfigError extends Schema.TaggedError<PreviousReleaseTagGitHubOutputConfigError>()(
  "PreviousReleaseTagGitHubOutputConfigError",
  {
    cause: Schema.Defect(),
  },
) {
  override get message(): string {
    return "Failed to resolve the GITHUB_OUTPUT path for the previous release tag.";
  }
}

export class PreviousReleaseTagGitHubOutputAppendError extends Schema.TaggedError<PreviousReleaseTagGitHubOutputAppendError>()(
  "PreviousReleaseTagGitHubOutputAppendError",
  {
    outputPath: Schema.String,
    cause: Schema.Defect(),
  },
) {
  override get message(): string {
    return `Failed to append the previous release tag to ${this.outputPath}.`;
  }
}

interface StableVersion {
  readonly major: number;
  readonly minor: number;
  readonly patch: number;
  readonly prerelease: ReadonlyArray<string>;
}

interface NightlyVersion {
  readonly major: number;
  readonly minor: number;
  readonly patch: number;
  readonly date: number;
  readonly runNumber: number;
}

const parseNumericIdentifier = (identifier: string): number | undefined =>
  /^\d+$/.test(identifier) ? Number(identifier) : undefined;

const comparePrereleaseIdentifiers = (left: string, right: string): number => {
  const leftNumeric = parseNumericIdentifier(left);
  const rightNumeric = parseNumericIdentifier(right);

  if (leftNumeric !== undefined && rightNumeric !== undefined) {
    return leftNumeric - rightNumeric;
  }
  if (leftNumeric !== undefined) {
    return -1;
  }
  if (rightNumeric !== undefined) {
    return 1;
  }
  return left.localeCompare(right);
};

const compareStableVersions = (left: StableVersion, right: StableVersion): number => {
  if (left.major !== right.major) return left.major - right.major;
  if (left.minor !== right.minor) return left.minor - right.minor;
  if (left.patch !== right.patch) return left.patch - right.patch;

  const leftHasPrerelease = left.prerelease.length > 0;
  const rightHasPrerelease = right.prerelease.length > 0;
  if (!leftHasPrerelease && !rightHasPrerelease) return 0;
  if (!leftHasPrerelease) return 1;
  if (!rightHasPrerelease) return -1;

  const maxLength = Math.max(left.prerelease.length, right.prerelease.length);
  for (let index = 0; index < maxLength; index += 1) {
    const leftIdentifier = left.prerelease[index];
    const rightIdentifier = right.prerelease[index];
    if (leftIdentifier === undefined) return -1;
    if (rightIdentifier === undefined) return 1;

    const comparison = comparePrereleaseIdentifiers(leftIdentifier, rightIdentifier);
    if (comparison !== 0) return comparison;
  }

  return 0;
};

const parseStableTag = (tag: string): StableVersion | undefined => {
  const match = /^v(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(tag);
  if (!match) return undefined;

  const [, major, minor, patch, prerelease] = match;
  if (!major || !minor || !patch) return undefined;

  const prereleaseIdentifiers = prerelease ? prerelease.split(".") : [];
  if (prereleaseIdentifiers[0] === "nightly" || prereleaseIdentifiers[0] === "preview") {
    return undefined;
  }

  return {
    major: Number(major),
    minor: Number(minor),
    patch: Number(patch),
    prerelease: prereleaseIdentifiers,
  };
};

const compareNightlyVersions = (left: NightlyVersion, right: NightlyVersion): number => {
  if (left.major !== right.major) return left.major - right.major;
  if (left.minor !== right.minor) return left.minor - right.minor;
  if (left.patch !== right.patch) return left.patch - right.patch;
  if (left.date !== right.date) return left.date - right.date;
  return left.runNumber - right.runNumber;
};

const parseNightlyTag = (
  tag: string,
  channel: "nightly" | "preview" = "nightly",
): NightlyVersion | undefined => {
  const match = new RegExp(
    `^(?:nightly-)?v(\\d+)\\.(\\d+)\\.(\\d+)-${channel}\\.(\\d{8})\\.(\\d+)

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	scripts/resolve-previous-release-tag.ts at main · sailfork/t3code-github · Sailfork
	
		
, ).exec(tag); if (!match) return undefined; const [, major, minor, patch, date, runNumber] = match; if (!major || !minor || !patch || !date || !runNumber) return undefined; return { major: Number(major), minor: Number(minor), patch: Number(patch), date: Number(date), runNumber: Number(runNumber), }; }; export const resolvePreviousReleaseTag = Effect.fn( "resolve-previous-release-tag.resolvePreviousReleaseTag", )(function* (channel: ReleaseChannel, currentTag: string, tags: ReadonlyArray<string>) { if (channel === "stable") { const current = parseStableTag(currentTag); if (!current) { return yield* new InvalidReleaseTagError({ channel, currentTag }); } const candidates = tags .map((tag) => ({ tag, parsed: parseStableTag(tag) })) .filter( (entry): entry is { tag: string; parsed: StableVersion } => entry.parsed !== undefined, ) .filter((entry) => compareStableVersions(entry.parsed, current) < 0) .toSorted((left, right) => compareStableVersions(right.parsed, left.parsed)); return candidates[0]?.tag; } const current = parseNightlyTag(currentTag, channel); if (!current) { return yield* new InvalidReleaseTagError({ channel, currentTag }); } const candidates = tags .map((tag) => ({ tag, parsed: parseNightlyTag(tag, channel) })) .filter((entry): entry is { tag: string; parsed: NightlyVersion } => entry.parsed !== undefined) .filter((entry) => compareNightlyVersions(entry.parsed, current) < 0) .toSorted((left, right) => compareNightlyVersions(right.parsed, left.parsed)); return candidates[0]?.tag; }); const collectStreamAsString = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> => stream.pipe( Stream.decodeText(), Stream.runFold( () => "", (acc, chunk) => acc + chunk, ), ); export const listGitTags = Effect.fn("listGitTags")(function* (cwd = process.cwd()) { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const args = ["tag", "--list"] as const; const context = { executable: "git", argumentCount: args.length, cwd, } as const; const child = yield* spawner.spawn(ChildProcess.make("git", args, { cwd })).pipe( Effect.mapError( (cause) => new ReleaseTagListProcessError({ ...context, operation: "spawn", cause, }), ), ); const [stdout, stderr, exitCode] = yield* Effect.all( [ collectStreamAsString(child.stdout).pipe( Effect.mapError( (cause) => new ReleaseTagListProcessError({ ...context, operation: "read-stdout", cause, }), ), ), collectStreamAsString(child.stderr).pipe( Effect.mapError( (cause) => new ReleaseTagListProcessError({ ...context, operation: "read-stderr", cause, }), ), ), child.exitCode.pipe( Effect.map(Number), Effect.mapError( (cause) => new ReleaseTagListProcessError({ ...context, operation: "wait-for-exit", cause, }), ), ), ], { concurrency: "unbounded" }, ); if (exitCode !== 0) { return yield* new ReleaseTagListProcessExitError({ ...context, exitCode, stdoutLength: stdout.length, stderrLength: stderr.length, }); } return stdout.split(/\r?\n/).map(String.trim).filter(String.isNonEmpty); }); export const writePreviousReleaseTagOutput = Effect.fn("writePreviousReleaseTagOutput")(function* ( previousTag: string | undefined, writeGithubOutput: boolean, ) { const entry = `previous_tag=${previousTag ?? ""}\n`; if (writeGithubOutput) { const fs = yield* FileSystem.FileSystem; const githubOutputPath = yield* Config.NonEmptyString("GITHUB_OUTPUT").pipe( Effect.mapError( (cause) => new PreviousReleaseTagGitHubOutputConfigError({ cause, }), ), ); yield* fs.writeFileString(githubOutputPath, entry, { flag: "a" }).pipe( Effect.mapError( (cause) => new PreviousReleaseTagGitHubOutputAppendError({ outputPath: githubOutputPath, cause, }), ), ); return; } process.stdout.write(entry); }); const command = Command.make( "resolve-previous-release-tag", { channel: Flag.Literals("channel", ReleaseChannel.literals).pipe( Flag.withDescription("Release channel whose previous tag should be resolved."), ), currentTag: Flag.String("current-tag").pipe( Flag.withDescription("Current release tag to compare against."), ), githubOutput: Flag.Boolean("github-output").pipe( Flag.withDescription("Write values to GITHUB_OUTPUT instead of stdout."), Flag.withDefault(false), ), }, ({ channel, currentTag, githubOutput }) => listGitTags().pipe( Effect.flatMap((tags) => resolvePreviousReleaseTag(channel, currentTag, tags)), Effect.flatMap((previousTag) => writePreviousReleaseTagOutput(previousTag, githubOutput)), ), ).pipe(Command.withDescription("Resolve the previous release tag for a stable or nightly series.")); if (import.meta.main) { Command.run(command, { version: "0.0.0" }).pipe( Effect.scoped, Effect.provide(NodeServices.layer), NodeRuntime.runMain, ); }
scripts/resolve-previous-release-tag.ts at main · sailfork/t3code-github · Sailfork
,\n ).exec(tag);\n if (!match) return undefined;\n\n const [, major, minor, patch, date, runNumber] = match;\n if (!major || !minor || !patch || !date || !runNumber) return undefined;\n\n return {\n major: Number(major),\n minor: Number(minor),\n patch: Number(patch),\n date: Number(date),\n runNumber: Number(runNumber),\n };\n};\n\nexport const resolvePreviousReleaseTag = Effect.fn(\n \"resolve-previous-release-tag.resolvePreviousReleaseTag\",\n)(function* (channel: ReleaseChannel, currentTag: string, tags: ReadonlyArray\u003cstring>) {\n if (channel === \"stable\") {\n const current = parseStableTag(currentTag);\n if (!current) {\n return yield* new InvalidReleaseTagError({ channel, currentTag });\n }\n\n const candidates = tags\n .map((tag) => ({ tag, parsed: parseStableTag(tag) }))\n .filter(\n (entry): entry is { tag: string; parsed: StableVersion } => entry.parsed !== undefined,\n )\n .filter((entry) => compareStableVersions(entry.parsed, current) \u003c 0)\n .toSorted((left, right) => compareStableVersions(right.parsed, left.parsed));\n\n return candidates[0]?.tag;\n }\n\n const current = parseNightlyTag(currentTag, channel);\n if (!current) {\n return yield* new InvalidReleaseTagError({ channel, currentTag });\n }\n\n const candidates = tags\n .map((tag) => ({ tag, parsed: parseNightlyTag(tag, channel) }))\n .filter((entry): entry is { tag: string; parsed: NightlyVersion } => entry.parsed !== undefined)\n .filter((entry) => compareNightlyVersions(entry.parsed, current) \u003c 0)\n .toSorted((left, right) => compareNightlyVersions(right.parsed, left.parsed));\n\n return candidates[0]?.tag;\n});\n\nconst collectStreamAsString = \u003cE>(stream: Stream.Stream\u003cUint8Array, E>): Effect.Effect\u003cstring, E> =>\n stream.pipe(\n Stream.decodeText(),\n Stream.runFold(\n () => \"\",\n (acc, chunk) => acc + chunk,\n ),\n );\n\nexport const listGitTags = Effect.fn(\"listGitTags\")(function* (cwd = process.cwd()) {\n const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;\n const args = [\"tag\", \"--list\"] as const;\n const context = {\n executable: \"git\",\n argumentCount: args.length,\n cwd,\n } as const;\n const child = yield* spawner.spawn(ChildProcess.make(\"git\", args, { cwd })).pipe(\n Effect.mapError(\n (cause) =>\n new ReleaseTagListProcessError({\n ...context,\n operation: \"spawn\",\n cause,\n }),\n ),\n );\n const [stdout, stderr, exitCode] = yield* Effect.all(\n [\n collectStreamAsString(child.stdout).pipe(\n Effect.mapError(\n (cause) =>\n new ReleaseTagListProcessError({\n ...context,\n operation: \"read-stdout\",\n cause,\n }),\n ),\n ),\n collectStreamAsString(child.stderr).pipe(\n Effect.mapError(\n (cause) =>\n new ReleaseTagListProcessError({\n ...context,\n operation: \"read-stderr\",\n cause,\n }),\n ),\n ),\n child.exitCode.pipe(\n Effect.map(Number),\n Effect.mapError(\n (cause) =>\n new ReleaseTagListProcessError({\n ...context,\n operation: \"wait-for-exit\",\n cause,\n }),\n ),\n ),\n ],\n { concurrency: \"unbounded\" },\n );\n\n if (exitCode !== 0) {\n return yield* new ReleaseTagListProcessExitError({\n ...context,\n exitCode,\n stdoutLength: stdout.length,\n stderrLength: stderr.length,\n });\n }\n\n return stdout.split(/\\r?\\n/).map(String.trim).filter(String.isNonEmpty);\n});\n\nexport const writePreviousReleaseTagOutput = Effect.fn(\"writePreviousReleaseTagOutput\")(function* (\n previousTag: string | undefined,\n writeGithubOutput: boolean,\n) {\n const entry = `previous_tag=${previousTag ?? \"\"}\\n`;\n\n if (writeGithubOutput) {\n const fs = yield* FileSystem.FileSystem;\n const githubOutputPath = yield* Config.NonEmptyString(\"GITHUB_OUTPUT\").pipe(\n Effect.mapError(\n (cause) =>\n new PreviousReleaseTagGitHubOutputConfigError({\n cause,\n }),\n ),\n );\n yield* fs.writeFileString(githubOutputPath, entry, { flag: \"a\" }).pipe(\n Effect.mapError(\n (cause) =>\n new PreviousReleaseTagGitHubOutputAppendError({\n outputPath: githubOutputPath,\n cause,\n }),\n ),\n );\n return;\n }\n\n process.stdout.write(entry);\n});\n\nconst command = Command.make(\n \"resolve-previous-release-tag\",\n {\n channel: Flag.Literals(\"channel\", ReleaseChannel.literals).pipe(\n Flag.withDescription(\"Release channel whose previous tag should be resolved.\"),\n ),\n currentTag: Flag.String(\"current-tag\").pipe(\n Flag.withDescription(\"Current release tag to compare against.\"),\n ),\n githubOutput: Flag.Boolean(\"github-output\").pipe(\n Flag.withDescription(\"Write values to GITHUB_OUTPUT instead of stdout.\"),\n Flag.withDefault(false),\n ),\n },\n ({ channel, currentTag, githubOutput }) =>\n listGitTags().pipe(\n Effect.flatMap((tags) => resolvePreviousReleaseTag(channel, currentTag, tags)),\n Effect.flatMap((previousTag) => writePreviousReleaseTagOutput(previousTag, githubOutput)),\n ),\n).pipe(Command.withDescription(\"Resolve the previous release tag for a stable or nightly series.\"));\n\nif (import.meta.main) {\n Command.run(command, { version: \"0.0.0\" }).pipe(\n Effect.scoped,\n Effect.provide(NodeServices.layer),\n NodeRuntime.runMain,\n );\n}\n","preview":null,"history":null,"version":"6b53e031f9eb61309afa552e6946966183afe4d9"},"/_session?shell=1":null}