feat: read from all known files if not specified

This commit is contained in:
Jozef Steinhübl 2024-05-07 20:19:35 +02:00
parent 116db8199a
commit e95e252b85
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F

View file

@ -1,4 +1,4 @@
import { warning } from "@actions/core"; import { debug, warning } from "@actions/core";
import { existsSync, readFileSync } from "node:fs"; import { existsSync, readFileSync } from "node:fs";
import { join, basename } from "node:path"; import { join, basename } from "node:path";
@ -25,23 +25,37 @@ const FILE_VERSION_READERS = {
".bumrc": (content: string) => content, ".bumrc": (content: string) => content,
}; };
export function readVersionFromFile(file: string): string | undefined { export function readVersionFromFile(
files: string | string[]
): string | undefined {
const cwd = process.env.GITHUB_WORKSPACE; const cwd = process.env.GITHUB_WORKSPACE;
if (!cwd) { if (!cwd) {
return; return;
} }
const path = join(cwd, file); if (!files) {
const base = basename(file); warning("No version file specified, trying all known files.");
readVersionFromFile(Object.keys(FILE_VERSION_READERS));
return;
}
if (!existsSync(path)) return; if (!Array.isArray(files)) files = [files];
const reader = FILE_VERSION_READERS[base] ?? (() => undefined); for (const file of files) {
debug(`Reading version from ${file}`);
try { const path = join(cwd, file);
return reader(readFileSync(path, "utf8")); const base = basename(file);
} catch (error) {
const { message } = error as Error; if (!existsSync(path)) return;
warning(`Failed to read ${file}: ${message}`);
const reader = FILE_VERSION_READERS[base] ?? (() => undefined);
try {
return reader(readFileSync(path, "utf8"));
} catch (error) {
const { message } = error as Error;
warning(`Failed to read ${file}: ${message}`);
}
} }
} }