refactor: convert action to TS and bundle code (#95)

(cherry picked from commit 95bbb87724)
This commit is contained in:
Christian Svensson
2025-05-12 18:14:45 +02:00
committed by Leo Kettmeir
parent 909cc5acb0
commit d74ee56ed6
737 changed files with 39115 additions and 132946 deletions

View File

@@ -4,11 +4,9 @@ import * as fs from "node:fs/promises";
import process from "node:process";
import core from "@actions/core";
import tc from "@actions/tool-cache";
import type { Version } from "./version.ts";
/**
* @param {import("./version.mjs").Version} version
*/
export async function install(version) {
export async function install(version: Version) {
const cachedPath = tc.find(
"deno",
version.kind === "canary" ? `0.0.0-${version.version}` : version.version,
@@ -66,8 +64,7 @@ export async function install(version) {
core.addPath(denoInstallRoot);
}
/** @returns {string} */
function zipName() {
function zipName(): string {
let arch;
switch (process.arch) {
case "arm64":

65
src/main.ts Normal file
View File

@@ -0,0 +1,65 @@
import process from "node:process";
import core from "@actions/core";
import path from "node:path";
import {
getDenoVersionFromFile,
parseVersionRange,
resolveVersion,
} from "./version.ts";
import { install } from "./install.ts";
declare global {
interface ImportMeta {
dirname?: string;
filename?: string;
}
}
function exit(message: string): never {
core.setFailed(message);
process.exit();
}
async function main() {
try {
const denoVersionFile = core.getInput("deno-version-file");
const range = parseVersionRange(
denoVersionFile
? getDenoVersionFromFile(denoVersionFile)
: core.getInput("deno-version"),
);
if (range === null) {
exit("The passed version range is not valid.");
}
const version = await resolveVersion(range);
if (version === null) {
exit("Could not resolve a version for the given range.");
}
core.info(`Going to install ${version.kind} version ${version.version}.`);
await install(version);
core.info(
`::add-matcher::${
path.join(
import.meta.dirname ?? ".",
"..",
"deno-problem-matchers.json",
)
}`,
);
core.setOutput("deno-version", version.version);
core.setOutput("release-channel", version.kind);
core.info("Installation complete.");
} catch (err) {
core.setFailed((err instanceof Error) ? err : String(err));
process.exit();
}
}
main();

View File

@@ -1,28 +1,26 @@
// @ts-types="@types/semver"
import semver from "semver";
import { fetch } from "undici";
import * as fs from "node:fs";
import * as console from "node:console";
import { setTimeout } from "node:timers";
const GIT_HASH_RE = /^[0-9a-fA-F]{40}$/;
/**
* @typedef VersionRange
* @property {string} range
* @property {"canary" | "rc" | "stable"} kind
*/
export interface VersionRange {
range: string;
kind: "canary" | "rc" | "stable";
}
/**
* @typedef Version
* @property {string} version
* @property {"canary" | "rc" | "stable"} kind
*/
export interface Version {
version: string;
kind: "canary" | "rc" | "stable";
}
/**
* Parses the version from the user into a structure.
*
* @param {string | undefined} version
* @returns {VersionRange | null}
*/
export function parseVersionRange(version) {
/** Parses the version from the user into a structure */
export function parseVersionRange(
version: string | undefined,
): VersionRange | null {
version = String(version) || "2.x";
version = version.trim();
@@ -50,13 +48,10 @@ export function parseVersionRange(version) {
return null;
}
/**
* Parses the version from the version file
*
* @param {string} versionFilePath
* @returns {string | undefined}
*/
export function getDenoVersionFromFile(versionFilePath) {
/** Parses the version from the version file */
export function getDenoVersionFromFile(
versionFilePath: string,
): string | undefined {
if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`,
@@ -79,11 +74,9 @@ export function getDenoVersionFromFile(versionFilePath) {
return denoVersionInToolVersions?.groups?.version || contents.trim();
}
/**
* @param {VersionRange} range
* @returns {Promise<Version | null>}
*/
export function resolveVersion({ range, kind }) {
export function resolveVersion(
{ range, kind }: VersionRange,
): Promise<Version | null> {
if (kind === "canary") {
return resolveCanary(range);
} else if (kind === "rc") {
@@ -94,11 +87,7 @@ export function resolveVersion({ range, kind }) {
}
}
/**
* @param {string} range
* @returns {Promise<Version | null>}
*/
async function resolveCanary(range) {
async function resolveCanary(range: string): Promise<Version | null> {
if (range === "latest") {
const res = await fetchWithRetries(
"https://dl.deno.land/canary-latest.txt",
@@ -115,10 +104,7 @@ async function resolveCanary(range) {
}
}
/**
* @returns {Promise<Version | null>}
*/
async function resolveReleaseCandidate() {
async function resolveReleaseCandidate(): Promise<Version | null> {
const res = await fetchWithRetries(
"https://dl.deno.land/release-rc-latest.txt",
);
@@ -134,11 +120,7 @@ async function resolveReleaseCandidate() {
return { version, kind: "rc" };
}
/**
* @param {string} range
* @returns {Promise<Version | null>}
*/
async function resolveRelease(range) {
async function resolveRelease(range: string): Promise<Version | null> {
if (range === "latest") {
const res = await fetchWithRetries(
"https://dl.deno.land/release-latest.txt",
@@ -148,8 +130,7 @@ async function resolveRelease(range) {
"Failed to fetch release version info from dl.deno.land. Please try again later.",
);
}
/** @type {string | null} */
let version = (await res.text()).trim();
let version: string | null = (await res.text()).trim();
version = semver.clean(version);
if (version === null) {
throw new Error("Failed to parse release version.");
@@ -172,8 +153,7 @@ async function resolveRelease(range) {
if (!Array.isArray(versionJson.cli)) {
throw new Error("Fetched stable version info is invalid.");
}
/** @type {string[]} */
const versions = versionJson.cli;
const versions: string[] = versionJson.cli;
let version = semver.maxSatisfying(versions, range);
if (version === null) {
@@ -186,8 +166,7 @@ async function resolveRelease(range) {
}
}
/** @param {string} url */
async function fetchWithRetries(url, maxRetries = 5) {
async function fetchWithRetries(url: string, maxRetries = 5) {
let sleepMs = 250;
let iterationCount = 0;
while (true) {