Skip to content

Commit

Permalink
feat: package.jsonの読み込み方法を変更
Browse files Browse the repository at this point in the history
  • Loading branch information
nana4rider committed Jan 19, 2025
1 parent 7865190 commit 7704a45
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
workflow_dispatch:

jobs:
test:
Expand Down Expand Up @@ -35,7 +36,7 @@ jobs:
release:
runs-on: ubuntu-latest
needs: get-version
if: ${{ needs.get-version.outputs.current != needs.get-version.outputs.previous }}
if: ${{ github.event_name == 'workflow_dispatch' || needs.get-version.outputs.current != needs.get-version.outputs.previous }}
steps:
- uses: actions/checkout@v4

Expand All @@ -52,7 +53,7 @@ jobs:
build-and-publish:
runs-on: ubuntu-latest
needs: get-version
if: ${{ needs.get-version.outputs.current != needs.get-version.outputs.previous }}
if: ${{ github.event_name == 'workflow_dispatch' || needs.get-version.outputs.current != needs.get-version.outputs.previous }}
steps:
- name: checkout
uses: actions/checkout@v4
Expand Down
6 changes: 5 additions & 1 deletion __tests__/connector/BP35Connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,11 @@ describe("sendCommand", () => {
});

try {
await connector.sendCommand(Buffer.from("SKTEST", "utf8"), undefined, 10);
await connector.sendCommand(
Buffer.from("SKTEST", "utf8"),
undefined,
100,
);
} catch (_) {
// エコーバックに改行を含まないためfilterが呼び出されずタイムアウトする
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/manager/mqttDeviceManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("setupMqttDeviceManager", () => {
};

(initializeMqttClient as jest.Mock).mockResolvedValue(mockMqttClient);
(buildOrigin as jest.Mock).mockResolvedValue({ origin: "test-origin" });
(buildOrigin as jest.Mock).mockReturnValue({ origin: "test-origin" });
(buildDevice as jest.Mock).mockReturnValue({ device: "test-device" });
(buildEntity as jest.Mock).mockImplementation(
(deviceId: string, entity: Entity) => ({
Expand Down
4 changes: 2 additions & 2 deletions __tests__/payload/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ describe("buildDevice", () => {
});

describe("buildOrigin", () => {
test("必要な属性が揃っている", async () => {
const origin = await buildOrigin();
test("必要な属性が揃っている", () => {
const origin = buildOrigin();
expect(origin).toHaveProperty("origin.name");
expect(origin).toHaveProperty("origin.sw_version");
expect(origin).toHaveProperty("origin.support_url");
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wisun2mqtt",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.js",
"type": "module",
"homepage": "https://github.com/nana4rider/wisun2mqtt",
Expand Down
2 changes: 1 addition & 1 deletion src/manager/mqttDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function setupMqttDeviceManager(
device: { deviceId, entities, manufacturer },
} = smartMeterClient;

const origin = await buildOrigin();
const origin = buildOrigin();
const device = buildDevice(deviceId, manufacturer);

const mqtt = await initializeMqttClient();
Expand Down
19 changes: 10 additions & 9 deletions src/payload/builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Entity } from "@/entity";
import env from "@/env";
import { getTopic, TopicType } from "@/payload/topic";
import { readFile } from "fs/promises";
import type { JsonObject, PackageJson } from "type-fest";
import type { JsonObject } from "type-fest";
import {
homepage as packageHomepage,
name as packageName,
version as packageVersion,
} from "~/package.json";

export type Payload = JsonObject;

Expand Down Expand Up @@ -51,13 +55,10 @@ export function buildDevice(
};
}

export async function buildOrigin(): Promise<Readonly<Payload>> {
const { homepage, name, version } = JSON.parse(
await readFile("package.json", "utf-8"),
) as PackageJson;
export function buildOrigin(): Readonly<Payload> {
const origin: Payload = {};
if (typeof name === "string") origin.name = name;
if (typeof version === "string") origin.sw_version = version;
if (typeof homepage === "string") origin.support_url = homepage;
if (typeof packageName === "string") origin.name = packageName;
if (typeof packageVersion === "string") origin.sw_version = packageVersion;
if (typeof packageHomepage === "string") origin.support_url = packageHomepage;
return { origin };
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"paths": {
"@/*": ["src/*"]
"@/*": ["src/*"],
"~/*": ["*"]
}
}
}

0 comments on commit 7704a45

Please sign in to comment.