Skip to content

Commit

Permalink
Show @stellar/anchor-tests version on UI (#113)
Browse files Browse the repository at this point in the history
* Show @stellar/anchor-tests version on UI

* Use local package.json

* Use peer dependency

* Get package version from server

* Cleanup
  • Loading branch information
quietbits authored Jun 6, 2023
1 parent 99049fa commit 947d3ed
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
lib
config.json
tsconfig.tsbuildinfo

.DS_Store

Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"license": "Apache-2.0",
"scripts": {
"build": "tsc",
"build": "tsc -p ./src/tsconfig.json",
"start": "node ./lib/index.js"
},
"prettier": "@stellar/prettier-config",
Expand Down
4 changes: 4 additions & 0 deletions server/src/anchorTestsPkgVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import PackageJson from "../package.json";

export const anchorTestsPkgVersion = () =>
PackageJson.peerDependencies["@stellar/anchor-tests"].replace(/^(\^)/, "");
19 changes: 19 additions & 0 deletions server/src/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
SerializedError,
} from "./serializers";
import logger from "./logging";
import { anchorTestsPkgVersion } from "./anchorTestsPkgVersion";

export const getTestsEventName = "getTests";
export const runTestsEventName = "runTests";
export const getVersionEventName = "getVersion";

export async function onGetTests(
this: Socket,
Expand Down Expand Up @@ -51,3 +53,20 @@ export async function onRunTests(
return;
}
}

export async function onGetVersion(
this: Socket,
callback: (error: SerializedError) => void,
) {
logger.info(`received '${getVersionEventName}' request from ${this.id}`);
try {
this.emit(getVersionEventName, anchorTestsPkgVersion());
} catch (e) {
const error = serializeError(e);
logger.error(
`anchorTestsPkgVersion() threw an exception: '${error.name}: ${error.message}\n${e.stack}'`,
);
if (callback) callback(error);
return;
}
}
9 changes: 8 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { createServer } from "http";
import {
getTestsEventName,
runTestsEventName,
getVersionEventName,
onGetTests,
onRunTests,
onGetVersion,
} from "./eventHandlers";
import logger from "./logging";

Expand All @@ -23,11 +25,16 @@ io.on("connection", (socket: Socket) => {
logger.info(`Connection established with socket ${socket.id}`);
socket.on(getTestsEventName, onGetTests);
socket.on(runTestsEventName, onRunTests);
socket.on(getVersionEventName, onGetVersion);
socket.on("disconnect", (reason: string) => {
logger.info(`socket ${socket.id} disconnected: ${reason}`);
});
socket.onAny((event, ..._args) => {
if (![getTestsEventName, runTestsEventName].includes(event)) {
if (
![getTestsEventName, runTestsEventName, getVersionEventName].includes(
event,
)
) {
logger.warning(`unexpected event received: ${event}`);
}
});
Expand Down
19 changes: 19 additions & 0 deletions server/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "es5",
"declaration": true,
"declarationDir": "../lib",
"rootDir": ".",
"outDir": "../lib",
"lib": ["esnext"],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true // required to make package.json file import to work
},
"references": [
{
"path": "../"
}
],
}
13 changes: 7 additions & 6 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"target": "es5",
"declaration": true,
"declarationDir": "lib",
"rootDir": "src",
"outDir": "lib",
"rootDir": ".",
"outDir": ".",
"lib": ["esnext"],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"composite": true,
"resolveJsonModule": true
},
"include": ["src"]
"include": ["src"],
"files": ["package.json"]
}
7 changes: 7 additions & 0 deletions ui/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ server {
proxy_pass http://localhost:8000;
}

location /version {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;

proxy_pass http://localhost:8000;
}

location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
Expand Down
33 changes: 31 additions & 2 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import * as Sentry from "@sentry/browser";
import { Integrations } from "@sentry/tracing";
import { Layout } from "@stellar/design-system";

import { METRIC_NAMES } from "constants/metricNames";
import { emitMetric } from "helpers/metrics";
import { socket } from "helpers/socketConnection";
import { TestRunner } from "views/TestRunner";

import "./styles.css";

if (process.env.REACT_APP_SENTRY_KEY) {
Sentry.init({
dsn: process.env.REACT_APP_SENTRY_KEY,
Expand All @@ -17,6 +20,28 @@ if (process.env.REACT_APP_SENTRY_KEY) {
}

export const App = () => {
const [pkgVersion, setPkgVersion] = useState("");

useEffect(() => {
socket.on("connect", () => {
socket.emit("getVersion");
});

return () => {
socket.disconnect();
};
}, []);

useEffect(() => {
socket.on("getVersion", (version) => {
setPkgVersion(version);
});

return () => {
socket.off("getVersion");
};
}, []);

useEffect(() => {
emitMetric(METRIC_NAMES.viewHome);
}, []);
Expand All @@ -35,7 +60,11 @@ export const App = () => {
</Layout.Inset>
</Layout.Content>

<Layout.Footer gitHubLink="https://github.com/stellar/stellar-anchor-tests" />
<Layout.Footer gitHubLink="https://github.com/stellar/stellar-anchor-tests">
{pkgVersion ? (
<div className="Footer__note">{`@stellar/anchor-tests v${pkgVersion}`}</div>
) : null}
</Layout.Footer>
</>
);
};
8 changes: 8 additions & 0 deletions ui/src/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.Footer__note {
color: var(--pal-text-secondary);
font-size: 14px;
}

.Layout__footer__content > a.TextLink {
order: 1;
}

0 comments on commit 947d3ed

Please sign in to comment.