Skip to content

Commit 66f44d5

Browse files
authored
fix: git info check should not throw when debugging (#39)
1 parent bca1dda commit 66f44d5

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './iterators';
22
export * from './time';
33
export * from './values';
4+
export * from './is-debugging';

src/helpers/is-debugging.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Reliable way to check if we are debugging. Supports ts-node and other tools unlike some other
3+
* approaches that check argv or env vars. It also lazy-loads the `node:inspector` module to avoid
4+
* unnecessary overhead in production environments where this function might not be called.
5+
*/
6+
export function isDebugging() {
7+
type NodeInspectorType = typeof import('node:inspector');
8+
// eslint-disable-next-line @typescript-eslint/no-var-requires
9+
const inspector = require('node:inspector') as NodeInspectorType;
10+
const url = inspector.url();
11+
return url !== undefined;
12+
}

src/server-version/__tests__/server-version.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,36 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import * as os from 'os';
44
import { spawnSync, execSync } from 'child_process';
5+
import { getServerVersion } from '../index';
6+
import { isDebugging } from '../../helpers';
57

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

11+
describe('getServerVersion does not throw when debugging', () => {
12+
let debuggingEnabled: boolean;
13+
let originalEnv: string | undefined;
14+
15+
beforeAll(() => {
16+
debuggingEnabled = isDebugging();
17+
originalEnv = process.env.NODE_ENV;
18+
process.env.NODE_ENV = 'prod';
19+
});
20+
21+
afterAll(() => {
22+
process.env.NODE_ENV = originalEnv;
23+
});
24+
25+
test('getServerVersion does not throw when debugging', () => {
26+
if (!debuggingEnabled) {
27+
console.log(`Skipping test because debugging is not enabled.`);
28+
return;
29+
}
30+
const version = getServerVersion();
31+
expect(version.branch).toBe('debugging');
32+
});
33+
});
34+
935
describe('git info script', () => {
1036
test('error when git repo data not available', () => {
1137
const result = spawnSync(`node "${scriptFilePath}"`, {

src/server-version/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import { readFileSync } from 'fs';
1+
import { readFileSync } from 'node:fs';
2+
import { isDebugging } from '../helpers/is-debugging';
23

34
interface ServerVersion {
45
branch: string;
56
commit: string;
67
tag: string;
78
}
89

9-
function getServerVersion(): ServerVersion {
10+
export function getServerVersion(): ServerVersion {
1011
if (process.env.NODE_ENV === 'test') {
1112
return {
1213
branch: 'test',
1314
commit: '123456',
1415
tag: 'v0.0.1',
1516
};
1617
}
17-
const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n');
18-
return { branch, commit, tag };
18+
19+
try {
20+
const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n');
21+
return { branch, commit, tag };
22+
} catch (error: unknown) {
23+
// If .git-info file does not exist and we are debugging, return a default version
24+
const fileNotExists = (error as NodeJS.ErrnoException).code === 'ENOENT';
25+
if (fileNotExists && isDebugging()) {
26+
return {
27+
branch: 'debugging',
28+
commit: '123456',
29+
tag: 'v0.0.1',
30+
};
31+
}
32+
throw error;
33+
}
1934
}
2035

2136
export const SERVER_VERSION = getServerVersion();

0 commit comments

Comments
 (0)