Skip to content

Commit

Permalink
feat(publish): return published package details (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshuaro committed Jan 21, 2024
1 parent 747035c commit 559d2b4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 25 deletions.
11 changes: 8 additions & 3 deletions src/publish.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execa } from "execa";
import { PublishContext } from "semantic-release";
import { PluginConfig } from "./types.js";
import { getConfig, getGoogleIdentityToken } from "./utils.js";
import { getConfig, getGoogleIdentityToken, getPubspec } from "./utils.js";

const SEMANTIC_RELEASE_PUB_TOKEN = "SEMANTIC_RELEASE_PUB_TOKEN";

Expand All @@ -15,14 +15,19 @@ export const publish = async (
return;
}

const pubspec = getPubspec();
const { GOOGLE_SERVICE_ACCOUNT_KEY } = process.env;

const idToken = await getGoogleIdentityToken(GOOGLE_SERVICE_ACCOUNT_KEY);
await setPubToken(cli, idToken);

logger.log(`Publishing version ${version} to pub.dev`);
await execa(cli, ["pub", "publish", "--force"]);
logger.log("Published package");
logger.log(`Published ${pubspec.name}@${version} on pub.dev`);

return {
name: "pub.dev package",
url: `https://pub.dev/packages/${pubspec.name}/versions/${version}`,
};
};

const setPubToken = async (cli: string, idToken: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export const ServiceAccount = z.object({

export type ServiceAccount = z.infer<typeof ServiceAccount>;

export const Pubspec = z.object({ version: z.string() });
export const Pubspec = z.object({ name: z.string(), version: z.string() });

export type Pubspec = z.infer<typeof Pubspec>;
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const getPubspecFromString = (data: string) => {
return Pubspec.parse(parse(data));
};

export const getPubspec = () => {
const data = getPubspecString();
return getPubspecFromString(data);
};

const getServiceAccount = (serviceAccountStr: string) => {
try {
return ServiceAccount.parse(JSON.parse(serviceAccountStr));
Expand Down
20 changes: 17 additions & 3 deletions tests/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Signale } from "signale";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { mock } from "vitest-mock-extended";
import { PluginConfig, publish } from "../src/index.js";
import { getConfig, getGoogleIdentityToken } from "../src/utils.js";
import { Pubspec } from "../src/schemas.js";
import { getConfig, getGoogleIdentityToken, getPubspec } from "../src/utils.js";

vi.mock("execa");
vi.mock("../src/utils");
Expand All @@ -21,6 +22,12 @@ describe("publish", () => {
publishPub: true,
updateBuildNumber: false,
};

const pubspec: Pubspec = {
name: "pub_package",
version,
};

const nextRelease = mock<NextRelease>();
const logger = mock<Signale>();
const context = mock<PublishContext>();
Expand All @@ -32,6 +39,7 @@ describe("publish", () => {

vi.mocked(getConfig).mockReturnValue(config);
vi.mocked(getGoogleIdentityToken).mockResolvedValue(idToken);
vi.mocked(getPubspec).mockReturnValue(pubspec);
});

afterEach(() => {
Expand All @@ -42,9 +50,14 @@ describe("publish", () => {
test("success", async () => {
stubEnv();

await publish(config, context);
const actual = await publish(config, context);

expect(actual).toEqual({
name: "pub.dev package",
url: `https://pub.dev/packages/${pubspec.name}/versions/${version}`,
});
expect(process.env[semanticReleasePubToken]).toEqual(idToken);

expect(getGoogleIdentityToken).toHaveBeenNthCalledWith(1, serviceAccount);
expect(execa).toHaveBeenNthCalledWith(1, cli, [
"pub",
Expand All @@ -64,8 +77,9 @@ describe("publish", () => {
const newConfig = { ...config, publishPub: false };
vi.mocked(getConfig).mockReturnValue(newConfig);

await publish(newConfig, context);
const actual = await publish(newConfig, context);

expect(actual).toBeUndefined();
expect(getGoogleIdentityToken).toBeCalledTimes(0);
expect(execa).toBeCalledTimes(0);
});
Expand Down
57 changes: 39 additions & 18 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import SemanticReleaseError from "@semantic-release/error";
import { codeBlock } from "common-tags";
import { readFileSync } from "fs";
import { Credentials, JWT } from "google-auth-library";
import { afterEach, describe, expect, test, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { mock } from "vitest-mock-extended";
import { parse } from "yaml";
import { PluginConfig } from "../src";
import {
getConfig,
getGoogleIdentityToken,
getPubspec,
getPubspecFromString,
getPubspecString,
} from "../src/utils";
Expand Down Expand Up @@ -93,36 +94,56 @@ describe("getGoogleIdentityToken", () => {
};
});

describe("getPubspecString", () => {
describe("pubspecUtils", () => {
const pubspecPath = "pubspec.yaml";
const fileContent = "fileContent";
const pubspec = {
name: "pub_package",
version: "1.0.0",
};

beforeEach(() => {
vi.mocked(readFileSync).mockReturnValue(fileContent);
vi.mocked(parse).mockReturnValue(pubspec);
});

afterEach(() => {
vi.restoreAllMocks();
});

test("success", () => {
vi.mocked(readFileSync).mockReturnValue(fileContent);
describe("getPubspecString", () => {
test("success", () => {
const actual = getPubspecString();

const actual = getPubspecString();
expect(actual).toEqual(fileContent);
expectReadFileCalled();
});
});

expect(actual).toEqual(fileContent);
expect(readFileSync).toHaveBeenNthCalledWith(1, pubspecPath, "utf-8");
describe("getPubspecFromString", () => {
test("success", () => {
const actual = getPubspecFromString(fileContent);

expect(actual).toEqual(pubspec);
expectYamlParseCalled();
});
});
});

describe("getPubspecFromString", () => {
const fileContent = "fileContent";
const pubspec = {
version: "1.0.0",
};
describe("getPubspec", () => {
test("success", () => {
const actual = getPubspec();

test("success", () => {
vi.mocked(parse).mockReturnValue(pubspec);
expect(actual).toEqual(pubspec);
expectReadFileCalled();
expectYamlParseCalled();
});
});

const actual = getPubspecFromString(fileContent);
const expectReadFileCalled = () => {
expect(readFileSync).toHaveBeenNthCalledWith(1, pubspecPath, "utf-8");
};

expect(actual).toEqual(pubspec);
const expectYamlParseCalled = () => {
expect(parse).toHaveBeenNthCalledWith(1, fileContent);
});
};
});

0 comments on commit 559d2b4

Please sign in to comment.