Skip to content

Commit 3df4326

Browse files
Avoid reading package.json as it may be unavailable in some cases (#95)
#73 Avoid reading package.json as it may be unavailable in some cases Signed-off-by: Levko Kravets <levko.ne@gmail.com> Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent c99dfd1 commit 3df4326

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ coverage_unit
88
*.code-workspace
99
dist
1010
*.DS_Store
11+
lib/version.ts

bin/update-version.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const packageJson = require('../package.json');
5+
6+
const outputPath = path.join(__dirname, '../lib/version.ts');
7+
8+
fs.writeFileSync(outputPath, `export default ${JSON.stringify(packageJson.version)}\n`);

lib/utils/buildUserAgentString.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import fs from 'fs';
2-
import path from 'path';
31
import os from 'os';
2+
import packageVersion from '../version';
43

54
const productName = 'NodejsDatabricksSqlConnector';
65

7-
function getPackageVersion(): string {
8-
const json = JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json')).toString());
9-
return json.version;
10-
}
11-
126
function getNodeVersion(): string {
137
return `Node.js ${process.versions.node}`;
148
}
@@ -19,5 +13,5 @@ function getOperatingSystemVersion(): string {
1913

2014
export default function buildUserAgentString(clientId?: string): string {
2115
const extra = [clientId, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
22-
return `${productName}/${getPackageVersion()} (${extra.join('; ')})`;
16+
return `${productName}/${packageVersion} (${extra.join('; ')})`;
2317
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"prepare": "npm run build",
1616
"e2e": "nyc --reporter=lcov --report-dir=coverage_e2e mocha --config tests/e2e/.mocharc.js",
1717
"test": "nyc --reporter=lcov --report-dir=coverage_unit mocha --config tests/unit/.mocharc.js",
18-
"build": "tsc",
18+
"update-version": "node bin/update-version.js && prettier --write ./lib/version.ts",
19+
"build": "npm run update-version && tsc",
1920
"watch": "tsc -w",
2021
"type-check": "tsc --noEmit",
2122
"prettier": "prettier . --check",

0 commit comments

Comments
 (0)