From c5a974163b7e24781ced15b04a0ee9e0a828ba78 Mon Sep 17 00:00:00 2001 From: "David I. Lehn" Date: Thu, 4 Jan 2024 18:32:47 -0500 Subject: [PATCH] Load version when used. Only load the package in the rare case of showing the version. This is somewhat dependent on the current behavior of commander and how it uses the version parameter. --- bin/jsonld.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bin/jsonld.js b/bin/jsonld.js index d6196c4..98234bf 100755 --- a/bin/jsonld.js +++ b/bin/jsonld.js @@ -8,19 +8,28 @@ * Copyright (c) 2013-2022 Digital Bazaar, Inc. * All rights reserved. */ +import {fileURLToPath} from 'node:url'; import https from 'node:https'; import {inspect} from 'node:util'; import jsonld from 'jsonld'; import {jsonldRequest} from 'jsonld-request'; import path from 'node:path'; import {program} from 'commander'; - -import {fileURLToPath} from 'node:url'; -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - import {readFileSync} from 'node:fs'; -const version = - JSON.parse(readFileSync(path.join(__dirname, '..', 'package.json'))).version; + +const version = { + value: undefined, + toString() { + // load version when used + if(this.value === undefined) { + const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const pkg = + JSON.parse(readFileSync(path.join(__dirname, '..', 'package.json'))); + this.value = pkg.version; + } + return this.value; + } +}; program.version(version);