Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add logging around prepare and publish lifecycle #29

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/common-tags": "^1.8.2",
"@types/semantic-release": "^20.0.1",
"@types/semantic-release__error": "^3.0.0",
"@types/signale": "^1.4.4",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"@vitest/coverage-v8": "^0.34.4",
Expand Down
8 changes: 6 additions & 2 deletions src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { Pubspec } from './schemas.js';

const PUBSPEC_PATH = 'pubspec.yaml';

export const prepare = async ({ nextRelease }: PrepareContext) => {
export const prepare = async ({
nextRelease: { version },
logger
}: PrepareContext) => {
const data = readFileSync(PUBSPEC_PATH, 'utf-8');
const pubspec = Pubspec.parse(parse(data));

const newData = data.replace(
new RegExp(`version:[ \t]+${pubspec.version}`),
`version: ${nextRelease.version}`
`version: ${version}`
);

logger.log(`Writing version ${version} to ${PUBSPEC_PATH}`);
writeFileSync(PUBSPEC_PATH, newData);
};
15 changes: 9 additions & 6 deletions src/publish.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { execa } from 'execa';
import { PublishContext } from 'semantic-release';
import { PluginConfig } from './types.js';
import { getConfig, getGoogleIdentityToken } from './utils.js';

const SEMANTIC_RELEASE_PUB_TOKEN = 'SEMANTIC_RELEASE_PUB_TOKEN';

export const publish = async (pluginConfig: PluginConfig) => {
export const publish = async (
pluginConfig: PluginConfig,
{ nextRelease: { version }, logger }: PublishContext
) => {
const { cli } = getConfig(pluginConfig);
const { GOOGLE_SERVICE_ACCOUNT_KEY } = process.env;

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

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

const setPubToken = async (cli: string, idToken: string) => {
Expand All @@ -23,7 +30,3 @@ const setPubToken = async (cli: string, idToken: string) => {
`--env-var=${SEMANTIC_RELEASE_PUB_TOKEN}`
]);
};

const publishToPub = async (cli: string) => {
await execa(cli, ['pub', 'publish', '--force']);
};
21 changes: 14 additions & 7 deletions tests/prepare.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { codeBlock } from 'common-tags';
import { readFileSync, writeFileSync } from 'fs';
import { NextRelease, PrepareContext } from 'semantic-release';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { Signale } from 'signale';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { prepare } from '../src/index.js';

Expand All @@ -10,6 +11,7 @@ vi.mock('fs');
describe('prepare', () => {
const newVersion = '1.2.3';
const pubspecPath = 'pubspec.yaml';

const oldPubspec = codeBlock`
name: pub_package
version: 1.2.0
Expand All @@ -20,6 +22,7 @@ describe('prepare', () => {
dependencies:
cupertino_icons: 1.0.6
`;

const newPubspec = codeBlock`
name: pub_package
version: ${newVersion}
Expand All @@ -31,17 +34,21 @@ describe('prepare', () => {
cupertino_icons: 1.0.6
`;

const nextRelease = mock<NextRelease>();
const logger = mock<Signale>();
const context = mock<PrepareContext>();

beforeEach(() => {
nextRelease.version = newVersion;
context.logger = logger;
context.nextRelease = nextRelease;
});

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

test('success', async () => {
const nextRelease = mock<NextRelease>();
nextRelease.version = newVersion;

const context = mock<PrepareContext>();
context.nextRelease = nextRelease;

vi.mocked(readFileSync).mockReturnValue(oldPubspec);

await prepare(context);
Expand Down
16 changes: 13 additions & 3 deletions tests/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { execa } from 'execa';
import { NextRelease, PublishContext } from 'semantic-release';
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';

Expand All @@ -8,14 +11,21 @@ vi.mock('../src/utils');

describe('publish', () => {
const cli = 'dart';
const pubspecPath = 'pubspecPath';
const serviceAccount = 'serviceAccount';
const idToken = 'idToken';
const version = '1.2.3';
const semanticReleasePubToken = 'SEMANTIC_RELEASE_PUB_TOKEN';

const config: PluginConfig = { cli, pubspecPath };
const config: PluginConfig = { cli };
const nextRelease = mock<NextRelease>();
const logger = mock<Signale>();
const context = mock<PublishContext>();

beforeEach(() => {
nextRelease.version = version;
context.logger = logger;
context.nextRelease = nextRelease;

vi.mocked(getConfig).mockReturnValue(config);
vi.mocked(getGoogleIdentityToken).mockResolvedValue(idToken);
});
Expand All @@ -28,7 +38,7 @@ describe('publish', () => {
test('success', async () => {
stubEnv();

await publish(config);
await publish(config, context);

expect(process.env[semanticReleasePubToken]).toEqual(idToken);
expect(execa).toHaveBeenNthCalledWith(1, cli, [
Expand Down
3 changes: 1 addition & 2 deletions tests/verifyConditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ vi.mock('../src/utils');

describe('verifyConditions', () => {
const cli = 'dart';
const pubspecPath = 'pubspecPath';
const serviceAccount = 'serviceAccount';
const idToken = 'idToken';

const config: PluginConfig = { cli, pubspecPath };
const config: PluginConfig = { cli };

beforeEach(() => {
vi.mocked(getConfig).mockReturnValue(config);
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,13 @@
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.2.tgz#31f6eec1ed7ec23f4f05608d3a2d381df041f564"
integrity sha512-7aqorHYgdNO4DM36stTiGO3DvKoex9TQRwsJU6vMaFGyqpBA1MNZkz+PG3gaNUPpTAOYhT1WR7M1JyA3fbS9Cw==

"@types/signale@^1.4.4":
version "1.4.4"
resolved "https://registry.yarnpkg.com/@types/signale/-/signale-1.4.4.tgz#dbfd32b39f1084551ecda9ba0888e4fef49e9fea"
integrity sha512-VYy4VL64gA4uyUIYVj4tiGFF0VpdnRbJeqNENKGX42toNiTvt83rRzxdr0XK4DR3V01zPM0JQNIsL+IwWWfhsQ==
dependencies:
"@types/node" "*"

"@typescript-eslint/eslint-plugin@^6.7.2":
version "6.7.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.7.2.tgz#f18cc75c9cceac8080a9dc2e7d166008c5207b9f"
Expand Down