diff --git a/src/lib/SystemQuery.mjs b/src/lib/SystemQuery.mjs index 19d44fd..c11a132 100644 --- a/src/lib/SystemQuery.mjs +++ b/src/lib/SystemQuery.mjs @@ -1,15 +1,18 @@ "use strict"; +import fs from 'fs'; +import path from 'path'; import { once, EventEmitter } from 'events'; import log from './io/NamespacedLog.mjs'; const l = log("systemquery"); +import ItemQueue from './async/ItemQueue.mjs'; +import current_git_commit from './io/current_git_commit.mjs'; import Agent from './agent/Agent.mjs'; import InfoBroker from './core/InfoBroker.mjs'; import HttpSubsystem from './agent/subsystems/http/HttpSubsystem.mjs'; -import ItemQueue from './async/ItemQueue.mjs'; - +const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/")); class SystemQuery extends EventEmitter { /** @@ -43,6 +46,11 @@ class SystemQuery extends EventEmitter { this.config = config; this.info = new InfoBroker(); + this.pkg = null; + this.version = null; + this.commit = null; + + this.agent = null; this.http = new HttpSubsystem(this); } @@ -51,6 +59,13 @@ class SystemQuery extends EventEmitter { * @return {Promise} A Promise that resolves when initialisation is complete. */ async init() { + /// + // 0: Preamble + /// + this.pkg = JSON.parse(await fs.promises.readFile(path.join(__dirname, "../../package.json"))); + this.version = this.pkg.version; + this.commit = await current_git_commit(path.join(__dirname, "../../.git")); + /// // 1: Create agent /// diff --git a/src/lib/io/current_git_commit.mjs b/src/lib/io/current_git_commit.mjs new file mode 100644 index 0000000..286e908 --- /dev/null +++ b/src/lib/io/current_git_commit.mjs @@ -0,0 +1,13 @@ +"use strict"; + +import fs from 'fs'; +import path from 'path'; + +export default async function(git_dir) { + const gitId = await fs.promises.readFile(path.join(git_dir, "HEAD"), 'utf8'); + if (gitId.indexOf(':') === -1) + return gitId; + + const refPath = path.join(git_dir, gitId.substring(5).trim()); + return await fs.promises.readFile(refPath, 'utf8'); +}