Skip to content

Add version.ts instead of importing package.json in source code #61

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

Merged
merged 2 commits into from
May 8, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/scripts/update-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async function updateVersion(newVersion) {
const files = {
packageJson: './package.json',
packageLockJson: './package-lock.json',
versionTs: './src/version.ts',
};

// package.json
Expand All @@ -23,6 +24,16 @@ async function updateVersion(newVersion) {
packageLockJsonData.packages[""].version = newVersion;
}
await fs.writeFile(files.packageLockJson, JSON.stringify(packageLockJsonData, null, 2) + '\n');

// src/version.ts
const versionTsData = await fs.readFile(files.versionTs, 'utf8');
const updatedVersionTsData = versionTsData.replace(
/const LINE_BOT_MCP_SERVER_VERSION = ".*?";/,
`const LINE_BOT_MCP_SERVER_VERSION = "${newVersion}";`
);
await fs.writeFile(files.versionTs, updatedVersionTsData);

console.log(`Version updated to ${newVersion} in all files.`);
}

async function verifyVersion(expectedVersion) {
Expand All @@ -41,6 +52,12 @@ async function verifyVersion(expectedVersion) {
throw new Error(`package-lock.json root package version mismatch: expected ${expectedVersion}, found ${packageLockJsonData.packages[""].version}`);
}

// src/version.ts
const versionTsData = await fs.readFile('./src/version.ts', 'utf8');
if (!versionTsData.includes(`const LINE_BOT_MCP_SERVER_VERSION = "${expectedVersion}";`)) {
throw new Error(`src/version.ts version mismatch: expected ${expectedVersion}`);
}

console.log(`All files have the correct version: ${expectedVersion}`);
}

Expand All @@ -54,7 +71,7 @@ async function verifyGitDiff() {
return acc;
}, { addedLines: 0, deletedLines: 0 });

if (addedLines !== 3 || deletedLines !== 3) {
if (addedLines !== 4 || deletedLines !== 4) {
throw new Error(`Unexpected number of changed lines: expected 4 added and 4 deleted, found ${addedLines} added and ${deletedLines} deleted`);
}

Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import * as line from "@line/bot-sdk";
import { z } from "zod";
import pkg from "../package.json" with { type: "json" };
import { LINE_BOT_MCP_SERVER_VERSION, USER_AGENT } from "./version.js";

const NO_USER_ID_ERROR =
"Error: Specify the userId or set the DESTINATION_USER_ID in the environment variables of this MCP Server.";

const server = new McpServer({
name: "line-bot",
version: pkg.version,
version: LINE_BOT_MCP_SERVER_VERSION,
});

const channelAccessToken = process.env.CHANNEL_ACCESS_TOKEN || "";
Expand All @@ -36,7 +36,7 @@ const destinationId = process.env.DESTINATION_USER_ID || "";
const messagingApiClient = new line.messagingApi.MessagingApiClient({
channelAccessToken: channelAccessToken,
defaultHeaders: {
"User-Agent": `${pkg.name}/${pkg.version}`,
"User-Agent": USER_AGENT,
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const LINE_BOT_MCP_SERVER_VERSION = "0.1.0-local";
export const USER_AGENT = `@line/line-bot-mcp-server/${LINE_BOT_MCP_SERVER_VERSION}`;