Skip to content

Commit

Permalink
Improve package version fetching reliability (#437)
Browse files Browse the repository at this point in the history
* Improve version fetching code

* fix types

* changelog

* Use pkginfo
  • Loading branch information
Half-Shot authored Sep 15, 2022
1 parent 3f3994c commit 76438ff
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog.d/437.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `getBridgeVersion` sometimes reporting "unknown" when the package.json is accessible.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
"homepage": "https://github.com/matrix-org/matrix-appservice-bridge#readme",
"dependencies": {
"@alloc/quick-lru": "^5.2.0",
"chalk": "^4.1.0",
"@types/pkginfo": "^0.4.0",
"axios": "^0.27.2",
"express-rate-limit": "^6.2.0",
"chalk": "^4.1.0",
"express": "^4.18.1",
"express-rate-limit": "^6.2.0",
"extend": "^3.0.2",
"ip-cidr": "^3.0.4",
"is-my-json-valid": "^2.20.5",
Expand All @@ -44,6 +45,7 @@
"nedb": "^1.8.0",
"nopt": "^5.0.0",
"p-queue": "^6.6.2",
"pkginfo": "^0.4.1",
"prom-client": "^14.0.0",
"uuid": "^8.3.2",
"winston": "^3.3.3",
Expand Down
57 changes: 44 additions & 13 deletions src/utils/package-info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { join, resolve } from "path";
let BridgeVersion: string;
import { join, dirname } from "path";
import { statSync } from "fs";
import pkginfo from "pkginfo";
import Logging from "../components/logging";

const log = Logging.get("PackageInfo");

// This may be defined if the script is run via NPM: https://docs.npmjs.com/cli/v8/using-npm/scripts#packagejson-vars
let BridgeVersion: string|undefined = process.env.npm_package_version;

/**
* Forcibly set the version of the bridge, for use by other components.
Expand All @@ -11,23 +18,47 @@ export function setBridgeVersion(version: string): void {
BridgeVersion = version;
}

/**
* Try to determine the path of the `package.json` file for the current
* running module. Iterates through parent directories of `require.main.filename`
* until it finds a package.json. This **may** result in false positives.
* @returns The path to a package.json file, or undefined if one could not be found.
*/
export function identifyPackageFile(): string|undefined {
// Find the main module path first
let mainModulePath = require.main?.filename;
if (!mainModulePath) {
return undefined;
}
do {
mainModulePath = dirname(mainModulePath);
try {
const packagePath = join(mainModulePath, 'package.json');
statSync(packagePath);
return packagePath;
}
catch (ex) {
continue;
}
} while (mainModulePath !== '/')
return undefined;
}

/**
* Get the current version of the bridge from the package.json file.
* By default this uses `identifyPackageFile` to determine the file.
* @param packageJsonPath The path to the package.json of the bridge.
* @returns Either the version number, or unknown.
*/
export function getBridgeVersion(packageJsonPath = "./package.json"): string {
export function getBridgeVersion(packageJsonPath?: string): string {
if (BridgeVersion) {
return BridgeVersion;
}
packageJsonPath = join(resolve(packageJsonPath));
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodePackage = require(packageJsonPath);
BridgeVersion = nodePackage.version;
}
catch (err) {
BridgeVersion = "unknown";
}
return BridgeVersion;
BridgeVersion = require.main && pkginfo.read(
require.main,
packageJsonPath && dirname(packageJsonPath)
)?.package.version || "unknown";

// Need to be explicit here due to the type of the static BridgeVersion
return BridgeVersion as string;
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@
resolved "https://registry.yarnpkg.com/@types/nopt/-/nopt-3.0.29.tgz#f19df3db4c97ee1459a2740028320a71d70964ce"
integrity sha512-PAO73Sc7+IiTIuPY1r/l+TgdIK4lugz5QxPaQ25EsjBBuZAw8OOtNEEGXvGciYwWa+JBE5wNQ8mR6nJE+H2csQ==

"@types/pkginfo@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@types/pkginfo/-/pkginfo-0.4.0.tgz#00143b97e98aa7c9391943266d2e4aebd8f44c35"
integrity sha512-4DGKkOlWkMuVDZQvytWzzWWAjyqDmlLKRYE4lzeA8t0s7fK0aF25uPbX9eBVermUjLJdeLHu9k1WmNiAssqCcg==
dependencies:
"@types/node" "*"

"@types/qs@*":
version "6.9.7"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb"
Expand Down Expand Up @@ -2588,6 +2595,11 @@ pkg-dir@^4.1.0:
dependencies:
find-up "^4.0.0"

pkginfo@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/pkginfo/-/pkginfo-0.4.1.tgz#b5418ef0439de5425fc4995042dced14fb2a84ff"
integrity sha512-8xCNE/aT/EXKenuMDZ+xTVwkT8gsoHN2z/Q29l80u0ppGEXVvsKRzNMbtKhg8LS8k1tJLAHHylf6p4VFmP6XUQ==

postcss@^8.3.11:
version "8.4.14"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.14.tgz#ee9274d5622b4858c1007a74d76e42e56fd21caf"
Expand Down

0 comments on commit 76438ff

Please sign in to comment.