Skip to content
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
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './iterators';
export * from './time';
export * from './values';
export * from './is-debugging';
12 changes: 12 additions & 0 deletions src/helpers/is-debugging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Reliable way to check if we are debugging. Supports ts-node and other tools unlike some other
* approaches that check argv or env vars. It also lazy-loads the `node:inspector` module to avoid
* unnecessary overhead in production environments where this function might not be called.
*/
export function isDebugging() {
type NodeInspectorType = typeof import('node:inspector');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const inspector = require('node:inspector') as NodeInspectorType;
const url = inspector.url();
return url !== undefined;
}
26 changes: 26 additions & 0 deletions src/server-version/__tests__/server-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@ import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { spawnSync, execSync } from 'child_process';
import { getServerVersion } from '../index';
import { isDebugging } from '../../helpers';

const scriptFilePath = path.resolve('bin/api-toolkit-git-info.js');
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), '.tmp'));

describe('getServerVersion does not throw when debugging', () => {
let debuggingEnabled: boolean;
let originalEnv: string | undefined;

beforeAll(() => {
debuggingEnabled = isDebugging();
originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'prod';
});

afterAll(() => {
process.env.NODE_ENV = originalEnv;
});

test('getServerVersion does not throw when debugging', () => {
if (!debuggingEnabled) {
console.log(`Skipping test because debugging is not enabled.`);
return;
}
const version = getServerVersion();
expect(version.branch).toBe('debugging');
});
});

describe('git info script', () => {
test('error when git repo data not available', () => {
const result = spawnSync(`node "${scriptFilePath}"`, {
Expand Down
23 changes: 19 additions & 4 deletions src/server-version/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { readFileSync } from 'fs';
import { readFileSync } from 'node:fs';
import { isDebugging } from '../helpers/is-debugging';

interface ServerVersion {
branch: string;
commit: string;
tag: string;
}

function getServerVersion(): ServerVersion {
export function getServerVersion(): ServerVersion {
if (process.env.NODE_ENV === 'test') {
return {
branch: 'test',
commit: '123456',
tag: 'v0.0.1',
};
}
const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n');
return { branch, commit, tag };

try {
const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n');
return { branch, commit, tag };
} catch (error: unknown) {
// If .git-info file does not exist and we are debugging, return a default version
const fileNotExists = (error as NodeJS.ErrnoException).code === 'ENOENT';
if (fileNotExists && isDebugging()) {
return {
branch: 'debugging',
commit: '123456',
tag: 'v0.0.1',
};
}
throw error;
}
}

export const SERVER_VERSION = getServerVersion();
Loading