Skip to content

Commit

Permalink
feat: implement prepare step
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 18, 2023
1 parent 24126fe commit 58dd830
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
23 changes: 16 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
19 changes: 14 additions & 5 deletions tests/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -17,6 +23,7 @@ describe("prepare", () => {
await prepare(
{},
{
...context,
cwd,
nextRelease: { version: "1.0.0" },
},
Expand All @@ -34,6 +41,7 @@ describe("prepare", () => {
await prepare(
{},
{
...context,
cwd,
nextRelease: { version: "1.0.0" },
},
Expand All @@ -59,6 +67,7 @@ describe("prepare", () => {
await prepare(
{},
{
...context,
cwd,
nextRelease: { version: "1.0.0" },
},
Expand All @@ -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();
}
});
});

0 comments on commit 58dd830

Please sign in to comment.