Skip to content

Commit

Permalink
style: add eslint-config-prettier and format with prettier (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshuaro committed Dec 29, 2023
1 parent 15f0512 commit 06c5508
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 145 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"extends": [
"semistandard",
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": { "project": ["tsconfig.eslint.json"] },
Expand Down
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"common-tags": "1.8.2",
"conventional-changelog-conventionalcommits": "7.0.2",
"eslint": "8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-semistandard": "17.0.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.29.1",
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { verifyConditions } from './verifyConditions.js';
export { PluginConfig } from './types.js';
export { prepare } from './prepare.js';
export { publish } from './publish.js';
export { verifyConditions } from "./verifyConditions.js";
export { PluginConfig } from "./types.js";
export { prepare } from "./prepare.js";
export { publish } from "./publish.js";
20 changes: 10 additions & 10 deletions src/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { readFileSync, writeFileSync } from 'fs';
import { PrepareContext } from 'semantic-release';
import { parse } from 'yaml';
import { Pubspec } from './schemas.js';
import { PluginConfig } from './types.js';
import { readFileSync, writeFileSync } from "fs";
import { PrepareContext } from "semantic-release";
import { parse } from "yaml";
import { Pubspec } from "./schemas.js";
import { PluginConfig } from "./types.js";

const PUBSPEC_PATH = 'pubspec.yaml';
const PUBSPEC_PATH = "pubspec.yaml";

export const prepare = async (
_pluginConfig: PluginConfig,
{ nextRelease: { version }, logger }: PrepareContext
{ nextRelease: { version }, logger }: PrepareContext,
) => {
const data = readFileSync(PUBSPEC_PATH, 'utf-8');
const data = readFileSync(PUBSPEC_PATH, "utf-8");
const pubspec = Pubspec.parse(parse(data));
const pubspecVersionEscaped = pubspec.version.replace(
/[/\-\\^$*+?.()|[\]{}]/g,
'\\$&'
"\\$&",
);

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

logger.log(`Writing version ${version} to ${PUBSPEC_PATH}`);
Expand Down
26 changes: 13 additions & 13 deletions src/publish.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { execa } from 'execa';
import { PublishContext } from 'semantic-release';
import { PluginConfig } from './types.js';
import { getConfig, getGoogleIdentityToken } from './utils.js';
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';
const SEMANTIC_RELEASE_PUB_TOKEN = "SEMANTIC_RELEASE_PUB_TOKEN";

export const publish = async (
pluginConfig: PluginConfig,
{ nextRelease: { version }, logger }: PublishContext
{ nextRelease: { version }, logger }: PublishContext,
) => {
const { cli, publishPub } = getConfig(pluginConfig);
if (!publishPub) {
Expand All @@ -21,17 +21,17 @@ export const publish = async (
await setPubToken(cli, idToken);

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

const setPubToken = async (cli: string, idToken: string) => {
process.env[SEMANTIC_RELEASE_PUB_TOKEN] = idToken;
await execa(cli, [
'pub',
'token',
'add',
'https://pub.dev',
`--env-var=${SEMANTIC_RELEASE_PUB_TOKEN}`
"pub",
"token",
"add",
"https://pub.dev",
`--env-var=${SEMANTIC_RELEASE_PUB_TOKEN}`,
]);
};
4 changes: 2 additions & 2 deletions src/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';
import { z } from "zod";

export const ServiceAccount = z.object({
client_email: z.string(),
private_key: z.string()
private_key: z.string(),
});

export type ServiceAccount = z.infer<typeof ServiceAccount>;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type PluginConfig = {
cli: 'dart' | 'flutter';
cli: "dart" | "flutter";
publishPub: boolean;
};
18 changes: 9 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import SemanticReleaseError from '@semantic-release/error';
import { JWT } from 'google-auth-library';
import { ServiceAccount } from './schemas.js';
import { PluginConfig } from './types.js';
import SemanticReleaseError from "@semantic-release/error";
import { JWT } from "google-auth-library";
import { ServiceAccount } from "./schemas.js";
import { PluginConfig } from "./types.js";

const DEFAULT_CONFIG: PluginConfig = {
cli: 'dart',
publishPub: true
cli: "dart",
publishPub: true,
};

const PUB_DEV_AUDIENCE = 'https://pub.dev';
const PUB_DEV_AUDIENCE = "https://pub.dev";

export const getConfig = (config: PluginConfig): PluginConfig => {
return { ...DEFAULT_CONFIG, ...config };
Expand All @@ -20,13 +20,13 @@ export const getGoogleIdentityToken = async (serviceAccountStr: string) => {
serviceAccountJson.client_email,
undefined,
serviceAccountJson.private_key,
PUB_DEV_AUDIENCE
PUB_DEV_AUDIENCE,
);

const creds = await jwtClient.authorize();
if (!creds.id_token) {
throw new SemanticReleaseError(
'Failed to retrieve identity token from Google'
"Failed to retrieve identity token from Google",
);
}

Expand Down
16 changes: 8 additions & 8 deletions src/verifyConditions.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import SemanticReleaseError from '@semantic-release/error';
import { execa } from 'execa';
import { VerifyConditionsContext } from 'semantic-release';
import { PluginConfig } from './types.js';
import { getConfig, getGoogleIdentityToken } from './utils.js';
import SemanticReleaseError from "@semantic-release/error";
import { execa } from "execa";
import { VerifyConditionsContext } from "semantic-release";
import { PluginConfig } from "./types.js";
import { getConfig, getGoogleIdentityToken } from "./utils.js";

export const verifyConditions = async (
pluginConfig: PluginConfig,
{ logger }: VerifyConditionsContext
{ logger }: VerifyConditionsContext,
) => {
const { cli, publishPub } = getConfig(pluginConfig);
const { GOOGLE_SERVICE_ACCOUNT_KEY } = process.env;

if (publishPub) {
if (!GOOGLE_SERVICE_ACCOUNT_KEY) {
throw new SemanticReleaseError(
'Environment variable not found: GOOGLE_SERVICE_ACCOUNT_KEY'
"Environment variable not found: GOOGLE_SERVICE_ACCOUNT_KEY",
);
}

await getGoogleIdentityToken(GOOGLE_SERVICE_ACCOUNT_KEY);
await verifyCommand(cli);
} else {
logger.log(
`Skipping Google service account key and ${cli} CLI verification as publishPub is ${publishPub}`
`Skipping Google service account key and ${cli} CLI verification as publishPub is ${publishPub}`,
);
}
};
Expand Down
40 changes: 20 additions & 20 deletions tests/prepare.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { codeBlock } from 'common-tags';
import { readFileSync, writeFileSync } from 'fs';
import { NextRelease, PrepareContext } from 'semantic-release';
import { Signale } from 'signale';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { PluginConfig, prepare } from '../src/index.js';
import { codeBlock } from "common-tags";
import { readFileSync, writeFileSync } from "fs";
import { NextRelease, PrepareContext } from "semantic-release";
import { Signale } from "signale";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { mock } from "vitest-mock-extended";
import { PluginConfig, prepare } from "../src/index.js";

vi.mock('fs');
vi.mock("fs");

describe('prepare', () => {
const oldVersion = '1.2.0';
const oldVersionWithBuild = '1.2.0+1';
const newVersion = '1.2.3';
const versionPlaceholder = '__version__';
const pubspecPath = 'pubspec.yaml';
const cli = 'dart';
describe("prepare", () => {
const oldVersion = "1.2.0";
const oldVersionWithBuild = "1.2.0+1";
const newVersion = "1.2.3";
const versionPlaceholder = "__version__";
const pubspecPath = "pubspec.yaml";
const cli = "dart";

const config: PluginConfig = { cli, publishPub: true };

Expand All @@ -34,7 +34,7 @@ describe('prepare', () => {

const newPubspec = basePubspec.replace(
new RegExp(versionPlaceholder),
newVersion
newVersion,
);

const nextRelease = mock<NextRelease>();
Expand All @@ -52,18 +52,18 @@ describe('prepare', () => {
});

test.each([oldVersion, oldVersionWithBuild])(
'success with pubspec version %s',
"success with pubspec version %s",
async (version) => {
const pubspec = basePubspec.replace(
new RegExp(versionPlaceholder),
version
version,
);
vi.mocked(readFileSync).mockReturnValue(pubspec);

await prepare(config, context);

expect(readFileSync).toHaveBeenNthCalledWith(1, pubspecPath, 'utf-8');
expect(readFileSync).toHaveBeenNthCalledWith(1, pubspecPath, "utf-8");
expect(writeFileSync).toHaveBeenNthCalledWith(1, pubspecPath, newPubspec);
}
},
);
});
52 changes: 26 additions & 26 deletions tests/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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';
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";

vi.mock('execa');
vi.mock('../src/utils');
vi.mock("execa");
vi.mock("../src/utils");

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

const config: PluginConfig = { cli, publishPub: true };
const nextRelease = mock<NextRelease>();
Expand All @@ -35,28 +35,28 @@ describe('publish', () => {
vi.restoreAllMocks();
});

test('success', async () => {
test("success", async () => {
stubEnv();

await publish(config, context);

expect(process.env[semanticReleasePubToken]).toEqual(idToken);
expect(getGoogleIdentityToken).toHaveBeenNthCalledWith(1, serviceAccount);
expect(execa).toHaveBeenNthCalledWith(1, cli, [
'pub',
'token',
'add',
'https://pub.dev',
`--env-var=${semanticReleasePubToken}`
"pub",
"token",
"add",
"https://pub.dev",
`--env-var=${semanticReleasePubToken}`,
]);
expect(execa).toHaveBeenNthCalledWith(2, cli, [
'pub',
'publish',
'--force'
"pub",
"publish",
"--force",
]);
});

test('skip publish', async () => {
test("skip publish", async () => {
const newConfig = { ...config, publishPub: false };
vi.mocked(getConfig).mockReturnValue(newConfig);

Expand All @@ -67,5 +67,5 @@ describe('publish', () => {
});

const stubEnv = () =>
vi.stubEnv('GOOGLE_SERVICE_ACCOUNT_KEY', serviceAccount);
vi.stubEnv("GOOGLE_SERVICE_ACCOUNT_KEY", serviceAccount);
});

0 comments on commit 06c5508

Please sign in to comment.