mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-18 04:28:28 +02:00
refactor: dont try all files if not defined
This commit is contained in:
parent
93c92af672
commit
1cea06d8fc
1 changed files with 26 additions and 33 deletions
59
src/utils.ts
59
src/utils.ts
|
@ -35,50 +35,43 @@ const FILE_VERSION_READERS = {
|
||||||
".bumrc": (content: string) => content,
|
".bumrc": (content: string) => content,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function readVersionFromFile(
|
export function readVersionFromFile(file: string): string | undefined {
|
||||||
files: string | string[]
|
|
||||||
): string | undefined {
|
|
||||||
const cwd = process.env.GITHUB_WORKSPACE;
|
const cwd = process.env.GITHUB_WORKSPACE;
|
||||||
if (!cwd) {
|
if (!cwd) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!files) {
|
if (!file) {
|
||||||
warning("No version file specified, trying all known files.");
|
return;
|
||||||
return readVersionFromFile(Object.keys(FILE_VERSION_READERS));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(files)) files = [files];
|
debug(`Reading version from ${file}`);
|
||||||
|
|
||||||
for (const file of files) {
|
const path = join(cwd, file);
|
||||||
debug(`Reading version from ${file}`);
|
const base = basename(file);
|
||||||
|
|
||||||
const path = join(cwd, file);
|
if (!existsSync(path)) {
|
||||||
const base = basename(file);
|
warning(`File ${path} not found`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!existsSync(path)) {
|
const reader = FILE_VERSION_READERS[base] ?? (() => undefined);
|
||||||
warning(`File ${path} not found`);
|
|
||||||
continue;
|
let output: string | undefined;
|
||||||
|
try {
|
||||||
|
output = reader(readFileSync(path, "utf8"))?.trim();
|
||||||
|
|
||||||
|
if (!output) {
|
||||||
|
warning(`Failed to read version from ${file}`);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
const reader = FILE_VERSION_READERS[base] ?? (() => undefined);
|
const { message } = error as Error;
|
||||||
|
warning(`Failed to read ${file}: ${message}`);
|
||||||
let output: string | undefined;
|
} finally {
|
||||||
try {
|
if (output) {
|
||||||
output = reader(readFileSync(path, "utf8"))?.trim();
|
info(`Obtained version ${output} from ${file}`);
|
||||||
|
return output;
|
||||||
if (!output) {
|
|
||||||
warning(`Failed to read version from ${file}`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
const { message } = error as Error;
|
|
||||||
warning(`Failed to read ${file}: ${message}`);
|
|
||||||
} finally {
|
|
||||||
if (output) {
|
|
||||||
info(`Obtained version ${output} from ${file}`);
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue