mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 07:37:08 +00:00
Add version reporting library.
This reports the current server version, from Git by default. Also, and of more interest, it reports whether the current database schema is compatible with the server code.
This commit is contained in:
68
lib/www/server/lib/version.js
Normal file
68
lib/www/server/lib/version.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const semver = require("semver");
|
||||
const { exec } = require("child_process");
|
||||
const pkg = require("../package.json");
|
||||
|
||||
/** Report whether the database schema version is
|
||||
* compatible with the version required by this server.
|
||||
*
|
||||
* The current schema version is retrieved from the
|
||||
* public.info table.
|
||||
*
|
||||
* The wanted version is retrieved from package.json
|
||||
* (config.db_schema).
|
||||
*
|
||||
* @returns true if the versions are compatible,
|
||||
* false otherwise.
|
||||
*/
|
||||
function compatible () {
|
||||
const { info } = require('./db');
|
||||
return new Promise ( async (resolve, reject) => {
|
||||
const current = await info.get(null, "version/db_schema");
|
||||
const wanted = pkg.config.db_schema;
|
||||
if (semver.satisfies(current, wanted)) {
|
||||
resolve({current, wanted});
|
||||
} else {
|
||||
reject({current, wanted});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/** Return software name.
|
||||
*
|
||||
*/
|
||||
function name () {
|
||||
const pkg = require("../package.json");
|
||||
return pkg.name ?? pkg.description ?? "Unknown";
|
||||
}
|
||||
|
||||
/** Return software version, from Git if possible.
|
||||
*
|
||||
*/
|
||||
async function describe () {
|
||||
return new Promise( (resolve, reject) => {
|
||||
const cmd = `git describe || echo git+$(git describe --always);`;
|
||||
exec(cmd, {cwd: __dirname}, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
|
||||
if (stdout) {
|
||||
resolve(stdout.trim());
|
||||
} else {
|
||||
// Most likely not installed from Git, use the
|
||||
// version number in package.json.
|
||||
resolve(pkg.version ?? "Unknown")
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function version () {
|
||||
return pkg.version;
|
||||
}
|
||||
version.compatible = compatible;
|
||||
version.name = name;
|
||||
version.describe = describe;
|
||||
|
||||
module.exports = version;
|
||||
Reference in New Issue
Block a user