Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
refactor: parse node and ts-node path (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Wolff authored Jun 26, 2020
1 parent 6fb7df3 commit 13328b9
Showing 1 changed file with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,29 @@ export class Context {

public static nodeVersion: string = '';

/**
* Add extra ../ to access on environments not using ts-node
*/
private static readonly JS_NODE_PREFIX = '../';

/**
* Path to azure-opentelemetry-exporter
*/
private static readonly ROOT_PATH = '../../../../';

constructor(
private _logger: Logger = new ConsoleLogger(LogLevel.WARN),
private _exporterPrefix = '../',
private _appPrefix = '../../../',
/**
* Path to this module's `package.json` relative to
* `Context.ROOT_PATH`
*/
private _exporterPrefix = './',
/**
* Path to end user application folder which contains `package.json`
* relative to `Context.ROOT_PATH`
* @default `../../`
*/
private _appPrefix = '../../',
) {
this.keys = new Contracts.ContextTagKeys();
this.tags = <{ [key: string]: string }>{};
Expand All @@ -39,19 +58,38 @@ export class Context {
private _loadApplicationContext() {
if (Object.keys(Context.appVersion).length === 0) {
// note: this should return the host package.json
// note: this does not require this._prefix
const packageJsonPath = path.resolve(__dirname, `${this._appPrefix}../../../../package.json`);
let packageJson: { version: string } | null = null;
const packageJsonPath = path.resolve(
__dirname,
Context.JS_NODE_PREFIX,
this._appPrefix,
Context.ROOT_PATH,
'./package.json',
);
const packageJsonPathTsNode = path.resolve(
__dirname,
this._appPrefix,
Context.ROOT_PATH,
'./package.json',
);

Context.appVersion[packageJsonPath] = 'unknown';

try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson && typeof packageJson.version === 'string') {
Context.appVersion[packageJsonPath] = packageJson.version;
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
} catch (_) {
try {
packageJson = JSON.parse(fs.readFileSync(packageJsonPathTsNode, 'utf8'));
} catch (exception) {
this._logger.warn('Failed to load Application version', exception);
}
}

this.tags[this.keys.applicationVersion] = Context.appVersion[packageJsonPath];
} catch (exception) {
this._logger.warn('Failed to load Application version');
if (packageJson && typeof packageJson.version === 'string') {
Context.appVersion[packageJsonPath] = packageJson.version;
}

this.tags[this.keys.applicationVersion] = Context.appVersion[packageJsonPath];
}
}

Expand All @@ -68,20 +106,39 @@ export class Context {

private _loadInternalContext() {
if (!Context.sdkVersion) {
let packageJson: { version: string } | null = null;
const { node } = process.versions;
[Context.nodeVersion] = node.split('.');

// note: this should return the sdk package.json
const packageJsonPath = path.resolve(__dirname, `${this._exporterPrefix}../../../../package.json`);
const packageJsonPath = path.resolve(
__dirname,
Context.JS_NODE_PREFIX,
this._exporterPrefix,
Context.ROOT_PATH,
'./package.json',
);
const packageJsonPathTsNode = path.resolve(
__dirname,
this._exporterPrefix,
Context.ROOT_PATH,
'./package.json',
);

Context.sdkVersion = 'unknown';
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson && typeof packageJson.version === 'string') {
Context.sdkVersion = packageJson.version;
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
} catch (_) {
try {
packageJson = JSON.parse(fs.readFileSync(packageJsonPathTsNode, 'utf8'));
} catch (exception) {
this._logger.warn('Failed to load Exporter version', exception);
throw exception;
}
} catch (exception) {
this._logger.warn('Failed to load Exporter version');
}

if (packageJson && typeof packageJson.version === 'string') {
Context.sdkVersion = packageJson.version;
}
}

Expand Down

0 comments on commit 13328b9

Please sign in to comment.