Skip to content

Commit

Permalink
fix(prepare): lookup strategy for Cabal filename (#20)
Browse files Browse the repository at this point in the history
* fix(prepare): lookup strategy for Cabal filename

* fix linter warnings
  • Loading branch information
dalejo96 authored Mar 11, 2024
1 parent 9900d65 commit 6887e06
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
20 changes: 9 additions & 11 deletions src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrepareContext } from "semantic-release";

import { PluginConfig } from "./types/pluginConfig";
import { runExecCommand } from "./utils/exec";
import { getCabalFilename } from "./utils/prepare";
import { lookupCabalFilename } from "./utils/prepare";

import { readFile, writeFile } from "fs/promises";
import { resolve } from "path";
Expand All @@ -17,24 +17,22 @@ export const readAndWriteNewCabal = async (fullCabalPath: string, newVersion: st

export const prepare = async (
{ cabalFile, versionPrefix = "" }: PluginConfig,
{ nextRelease, logger }: PrepareContext,
{ nextRelease, logger, cwd }: PrepareContext,
): Promise<void> => {
const cabalFileName = cabalFile ?? getCabalFilename();
const realCwd = cwd ?? process.cwd();
logger.log("Current working directory: ", realCwd);
const cabalFileName = cabalFile ?? lookupCabalFilename(realCwd, logger);
const { version } = nextRelease ?? {};

logger.log("Check new version");
logger.log("Checking new version");
if (!version) {
throw new Error("Could not determine the version from semantic release. Check the plugin configuration");
}

logger.log("Check cabal file");
if (!cabalFileName) {
throw new Error("Could not determine the cabal filename. Check the plugin configuration");
}

const fullCabalPath = resolve("./", cabalFileName);
logger.log("Checking cabal file");
const fullCabalPath = resolve(realCwd, cabalFileName);
const fullVersion = `${versionPrefix}${version}`;
logger.log("Reading .cabal file");
logger.log("Reading .cabal file", fullCabalPath);
await readAndWriteNewCabal(fullCabalPath, fullVersion);
logger.log("Writing new version %s to `%s`", version, fullCabalPath);

Expand Down
18 changes: 14 additions & 4 deletions src/utils/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { PrepareContext } from "semantic-release";

import fs from "fs";

export const getCabalFilename: () => string | undefined = () => {
return fs
.readdirSync(__dirname)
export function lookupCabalFilename(cwd: string, logger: PrepareContext["logger"]): string {
const cabalFilename = fs
.readdirSync(cwd)
.filter(path => fs.statSync(path).isFile())
.filter(path => path.endsWith(".cabal"))
.at(0);
};

if (!cabalFilename) {
logger.error("Unable to find cabal file name in ", cwd);
throw new Error("Could not determine the cabal filename. Check the plugin configuration");
}
logger.info("Using cabal file: ", cabalFilename);

return cabalFilename;
}
2 changes: 1 addition & 1 deletion src/verifyConditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EnvVarError } from "./utils/EnvVarError";
export const verifyConditions = (_pluginConfig: PluginConfig, { logger }: BaseContext): void => {
const { HACKAGE_TOKEN } = process.env;

logger.log("Check environment variables");
logger.log("Checking environment variables");
if (!HACKAGE_TOKEN) {
throw new EnvVarError("HACKAGE_TOKEN");
}
Expand Down

0 comments on commit 6887e06

Please sign in to comment.