Skip to content

Commit

Permalink
added get version shenanigans.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed Dec 11, 2024
1 parent be6b545 commit 025aa0b
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 13 deletions.
46 changes: 41 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"passport": "^0.7.0",
"passport-google-oauth20": "^2.0.0",
"passport-google-web": "^1.1.0",
"pkg-dir": "^8.0.0",
"rpgdicejs": "^2.0.3",
"socket.io": "^4.8.0",
"trivialperms": "^2.0.0",
Expand Down Expand Up @@ -73,6 +74,7 @@
"eslint": "^9.16.0",
"eslint-formatter-junit": "^8.40.0",
"eslint-plugin-vue": "^9.32.0",
"git-last-commit": "^1.0.1",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"marked": "^15.0.3",
Expand Down
21 changes: 21 additions & 0 deletions src/common/models/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//----------------------------------------------------------------------------------------------------------------------
// Application Version
//----------------------------------------------------------------------------------------------------------------------

export interface AppVersion
{
name : string;
version : {
full : string;
short : string;
};
environment : string;
build : {
commitHash : string;
commitRef : string;
date : string;
release : string;
}
}

//----------------------------------------------------------------------------------------------------------------------
6 changes: 4 additions & 2 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function main() : Promise<void>
//------------------------------------------------------------------------------------------------------------------

// Get version
const version = await getVersion();
const appVersion = await getVersion();

// Build the express app
const app = express();
Expand Down Expand Up @@ -192,6 +192,8 @@ async function main() : Promise<void>
logger.debug(`Starting real http server on port ${ httpPort }...`);
}

console.log('VERSION!!!!', appVersion);

// Start the server
server.listen(httpPort, config.http.host, () =>
{
Expand All @@ -214,7 +216,7 @@ async function main() : Promise<void>
}

const url = `http://${ host }:${ actualPort }`;
logger.info(`RPGKeeper v${ version } listening at ${ url }.`);
logger.info(`RPGKeeper v${ appVersion.version.full } listening at ${ url }.`);
});
}

Expand Down
61 changes: 61 additions & 0 deletions src/server/utils/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ---------------------------------------------------------------------------------------------------------------------
// Git Utils
// ---------------------------------------------------------------------------------------------------------------------

import { isModuleInstalled } from './modules.js';

// ---------------------------------------------------------------------------------------------------------------------

export interface GitInfo
{
shortHash : string;
hash : string;
branch : string;
commitDate : string;
tags : string[];
}

// ---------------------------------------------------------------------------------------------------------------------

export async function getGitInfo() : Promise<GitInfo>
{
const commit : GitInfo = {
shortHash: process.env.COMMIT_SHA ?? 'unknown',
hash: process.env.COMMIT_SHA ?? 'unknown',
branch: process.env.COMMIT_REF ?? 'unknown',
commitDate: process.env.BUILD_DATE ?? (new Date()).toISOString(),
tags: [],
};

if(isModuleInstalled('git-last-commit'))
{
const git = await import('git-last-commit');

await new Promise((resolve) =>
{
git.getLastCommit((err, cmt) =>
{
if(!err && cmt)
{
let commitDate;
if(cmt.committedOn)
{
commitDate = (new Date(parseInt(cmt.committedOn) * 1000)).toISOString();
}

commit.shortHash = cmt.shortHash ? cmt.shortHash : commit.shortHash;
commit.hash = cmt.hash ? cmt.hash : commit.hash;
commit.branch = cmt.branch ? cmt.branch : commit.branch;
commit.commitDate = commitDate ? commitDate : commit.commitDate;
commit.tags = cmt.tags ?? ([]) as string[];
}

resolve(undefined);
});
});
}

return commit;
}

// ---------------------------------------------------------------------------------------------------------------------
30 changes: 30 additions & 0 deletions src/server/utils/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ---------------------------------------------------------------------------------------------------------------------
// Module Utils
// ---------------------------------------------------------------------------------------------------------------------

import { resolve } from 'node:path';
import { existsSync } from 'node:fs';
import { packageDirectorySync } from 'pkg-dir';

// ---------------------------------------------------------------------------------------------------------------------

export function isModuleInstalled(modulePath : string) : boolean
{
try
{
const pkgPath = packageDirectorySync();
if(!pkgPath)
{
return false;
}

const resolvedPath = resolve(pkgPath, 'node_modules', modulePath);
return existsSync(resolvedPath);
}
catch (_error)
{
return false;
}
}

// ---------------------------------------------------------------------------------------------------------------------
84 changes: 79 additions & 5 deletions src/server/utils/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,59 @@ import { resolve } from 'node:path';
import { readFile } from 'node:fs/promises';

import logging from '@strata-js/util-logging';
import { packageDirectorySync } from 'pkg-dir';

// Utils
import { GitInfo, getGitInfo } from './git.js';
import { AppVersion } from '../../common/models/version.js';

// ---------------------------------------------------------------------------------------------------------------------

const logger = logging.getLogger('server');

// ---------------------------------------------------------------------------------------------------------------------

export async function getVersion() : Promise<string>
function _getEnvironment() : string
{
return process.env.ENVIRONMENT ?? 'local';
}

function _getBuildVersion() : string
{
return process.env.BUILD_VERSION ?? 'edge';
}

async function _getGitInfo() : Promise<GitInfo>
{
return getGitInfo();
}

async function _getAppName() : Promise<string>
{
let appName : string = process.env.npm_package_name ?? '';
if(!appName)
{
const pgkText = await readFile(resolve(packageDirectorySync()), { encoding: 'utf8' });

try
{
appName = (JSON.parse(pgkText)).name;
}
catch (err : any)
{
logger.warn(`Failed to parse version from 'package.json':`, err.stack);
appName = '0.0.0';
}
}
return appName;
}

async function _getVersion() : Promise<string>
{
let version : string = process.env.npm_package_version ?? '';
if(!version)
{
const pgkText = await readFile(
resolve(import.meta.dirname, '..', '..', '..', 'package.json'),
{ encoding: 'utf8' }
);
const pgkText = await readFile(resolve(packageDirectorySync()), { encoding: 'utf8' });

try
{
Expand All @@ -38,3 +75,40 @@ export async function getVersion() : Promise<string>
}

// ---------------------------------------------------------------------------------------------------------------------

export async function getVersion() : Promise<AppVersion>
{
const environment = _getEnvironment();
const gitInfo = await _getGitInfo();
const release = _getBuildVersion();
const version = await _getVersion();

let fullVersion = version;

if(gitInfo)
{
fullVersion = `${ fullVersion }+${ gitInfo.shortHash }`;

if(environment === 'local')
{
fullVersion = `${ fullVersion }-${ gitInfo.branch }`;
}
}

return {
name: await _getAppName(),
version: {
full: fullVersion,
short: version,
},
environment,
build: {
commitHash: gitInfo.shortHash,
commitRef: gitInfo.branch,
date: gitInfo.commitDate,
release,
},
};
}

// ---------------------------------------------------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ServerConfig } from './src/server/interfaces/config.js';

// Utils
import configUtil from '@strata-js/util-config';
import { getVersion } from './src/server/utils/version.js';

// ---------------------------------------------------------------------------------------------------------------------
// Configuration
Expand Down Expand Up @@ -101,7 +102,7 @@ export default defineConfig({
open: false,
},
define: {
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
__APP_VERSION__: JSON.stringify((await getVersion()).version.full),
},
build: {
outDir: '../../dist/client',
Expand Down

0 comments on commit 025aa0b

Please sign in to comment.