diff --git a/lib/index.js b/lib/index.js index e64f697..1fd8ad8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,7 @@ -/* eslint-disable no-unused-vars */ import SemanticReleaseError from "@semantic-release/error"; import fs from "node:fs"; import path from "node:path"; -import { versionRegex } from "./helpers/regexes"; - -let verified; -let prepared; +import { replaceVersionInContent, versionRegex } from "./helpers/regexes"; export async function verifyConditions(_, context) { const { cwd } = context; @@ -31,6 +27,19 @@ export async function verifyConditions(_, context) { } } -export async function prepare(pluginConfig, context) { - const { cwd, nextRelease } = context; +export async function prepare(_, { cwd, nextRelease: { version }, logger }) { + const projectPath = path.join(cwd, "mix.exs"); + + const projectContent = fs.readFileSync(projectPath, { + encoding: "utf-8", + }); + + const updatedProjectContent = replaceVersionInContent( + projectContent, + version, + ); + + logger.log("Write version %s to mix.exs in %s", version, cwd); + + fs.writeFileSync(projectPath, updatedProjectContent); } diff --git a/tests/prepare.test.js b/tests/prepare.test.js index 44f963a..b04cad5 100644 --- a/tests/prepare.test.js +++ b/tests/prepare.test.js @@ -6,6 +6,12 @@ import { createTestProject } from "./helpers/create-test-project"; import { readProjectVersion } from "./helpers/read-project-version"; describe("prepare", () => { + const context = { logger: { log: jest.fn() } }; + + afterEach(() => { + jest.clearAllMocks(); + }); + it("should not error in good conditions", async () => { expect.assertions(2); @@ -17,6 +23,7 @@ describe("prepare", () => { await prepare( {}, { + ...context, cwd, nextRelease: { version: "1.0.0" }, }, @@ -34,6 +41,7 @@ describe("prepare", () => { await prepare( {}, { + ...context, cwd, nextRelease: { version: "1.0.0" }, }, @@ -59,6 +67,7 @@ describe("prepare", () => { await prepare( {}, { + ...context, cwd, nextRelease: { version: "1.0.0" }, }, @@ -72,27 +81,27 @@ describe("prepare", () => { }); it("should call the logger with the updated version and cwd", async () => { - expect.assertions(2); + expect.assertions(4); for (let asAttribute of [false, true]) { const { cwd } = createTestProject("0.0.1-dev", asAttribute); - const logger = { log: jest.fn() }; - await prepare( {}, { + ...context, cwd, nextRelease: { version: "1.0.0" }, - logger, }, ); - expect(logger.log).toHaveBeenCalledWith( + expect(context.logger.log).toHaveBeenCalledTimes(1); + expect(context.logger.log).toHaveBeenCalledWith( "Write version %s to mix.exs in %s", "1.0.0", cwd, ); + context.logger.log.mockReset(); } }); });