Skip to content

Commit 01285bb

Browse files
committed
feat(nodeplotlib): add possibility to set NODEPLOTLIB_PORT
1 parent 9a1143c commit 01285bb

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

libs/nodeplotlib/src/lib/nodeplotlib.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BehaviorSubject, Observable, of, Subscription } from 'rxjs';
55
import { PlotsService } from './server/plots/plots.service';
66
import { ServerModule } from './server/server.module';
77
import { BridgeService } from './server/services/bridge.service';
8+
import { getPort } from './utils/get-port';
89
let app: INestApplication | null = null;
910
let plotsService: PlotsService;
1011
let bridgeService: BridgeService;
@@ -16,6 +17,7 @@ let bridgeService: BridgeService;
1617
let appRuns = false;
1718
let shutdownSubscription: Subscription;
1819
const plotsBuffer$ = new BehaviorSubject<Omit<PlotDataStream, 'id'>[]>([]);
20+
const port = getPort();
1921

2022
/**
2123
* Plots the given data with the given layout. This function
@@ -25,7 +27,7 @@ const plotsBuffer$ = new BehaviorSubject<Omit<PlotDataStream, 'id'>[]>([]);
2527
* @param cb
2628
*/
2729
export function plot(data: Plot[] | Observable<Plot[]>, layout?: Layout) {
28-
bootstrap();
30+
bootstrap(port);
2931
const bufferedPlots = plotsBuffer$.value;
3032

3133
const streamData$: Observable<Plot[]> =
@@ -46,7 +48,7 @@ export async function clear() {
4648
}
4749
}
4850

49-
export async function bootstrap(port = 0) {
51+
export async function bootstrap(port: number) {
5052
if (appRuns) {
5153
console.log('App is already up and running');
5254
return;

libs/nodeplotlib/src/lib/server/services/bridge.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { Subject } from 'rxjs';
3-
import { openWindow } from '../../open-window';
3+
import { openWindow } from '../../utils/open-window';
44

55
@Injectable()
66
export class BridgeService {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { getPort } from './get-port';
2+
3+
describe('getPort', () => {
4+
it('should return 0 if NODEPLOTLIB_PORT is undefined', () => {
5+
process.env.NODEPLOTLIB_PORT = undefined;
6+
expect(getPort()).toBe(0);
7+
});
8+
9+
it('should return 0 if NODEPLOTLIB_PORT is null', () => {
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
process.env.NODEPLOTLIB_PORT = null as any;
12+
expect(getPort()).toBe(0);
13+
});
14+
15+
it('should return 0 if NODEPLOTLIB_PORT is empty string', () => {
16+
process.env.NODEPLOTLIB_PORT = '';
17+
expect(getPort()).toBe(0);
18+
});
19+
20+
it('should return 0 if NODEPLOTLIB_PORT is 0', () => {
21+
process.env.NODEPLOTLIB_PORT = '0';
22+
expect(getPort()).toBe(0);
23+
});
24+
25+
it('should return the number if NODEPLOTLIB_PORT is a number and not 0', () => {
26+
process.env.NODEPLOTLIB_PORT = '123';
27+
expect(getPort()).toBe(123);
28+
});
29+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function getPort(): number {
2+
const portAsString = process.env.NODEPLOTLIB_PORT;
3+
const port = Number(portAsString);
4+
if (isNaN(port)) {
5+
return 0;
6+
}
7+
return port;
8+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { openWindow } from './open-window';
2+
import { exec } from 'child_process';
3+
import { type } from 'os';
4+
jest.mock('child_process');
5+
jest.mock('os');
6+
7+
describe('openWindow', () => {
8+
let execMock: jest.MockedFunction<typeof exec>;
9+
let typeMock: jest.MockedFunction<typeof type>;
10+
11+
beforeEach(() => {
12+
execMock = exec as jest.MockedFunction<typeof exec>;
13+
typeMock = type as jest.MockedFunction<typeof type>;
14+
});
15+
16+
afterEach(() => {
17+
delete process.env.NODEPLOTLIB_PORT;
18+
});
19+
20+
it('should not call the exec function if NODEPLOTLIB_PORT is set', () => {
21+
process.env.NODEPLOTLIB_PORT = '123';
22+
openWindow('location');
23+
expect(execMock).not.toBeCalled();
24+
});
25+
26+
it('should not call the exec function if NODEPLOTLIB_PORT is set to ""', () => {
27+
process.env.NODEPLOTLIB_PORT = '';
28+
openWindow('location');
29+
expect(execMock).not.toBeCalled();
30+
});
31+
32+
it('should call the exec function for linux correctly', () => {
33+
typeMock.mockImplementation(() => 'Linux');
34+
openWindow('location');
35+
expect(execMock).toBeCalledWith('xdg-open location');
36+
});
37+
38+
it('should call the exec function for windows correctly', () => {
39+
typeMock.mockImplementation(() => 'Windows_NT');
40+
openWindow('location');
41+
expect(execMock).toBeCalledWith('start location');
42+
});
43+
44+
it('should call the exec function for mac correctly', () => {
45+
typeMock.mockImplementation(() => 'Darwin');
46+
openWindow('location');
47+
expect(execMock).toBeCalledWith('open location');
48+
});
49+
});

libs/nodeplotlib/src/lib/open-window.ts renamed to libs/nodeplotlib/src/lib/utils/open-window.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { exec } from 'child_process';
22
import { type } from 'os';
33

44
export function openWindow(location: string) {
5+
if (process.env.NODEPLOTLIB_PORT) {
6+
return;
7+
}
8+
59
switch (type()) {
610
case 'Linux':
711
exec(`xdg-open ${location}`);

0 commit comments

Comments
 (0)