Skip to content

Commit

Permalink
fix(*): extract distribution from parsed package.json (#444)
Browse files Browse the repository at this point in the history
relates-to: #442
  • Loading branch information
lsndr authored Aug 24, 2023
1 parent b51134a commit 02b1eb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/Config/CliInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import { readFileSync } from 'fs';
export class CliInfo {
public readonly cwd: string;
public readonly version: string;
private _pkgPath: string;
public readonly distribution: string | undefined;

constructor(cwd: string = process.cwd()) {
this._pkgPath = this.getPkgPath(cwd);
this.cwd = this._pkgPath ? path.dirname(this._pkgPath) : cwd;
this.version = process.env.VERSION ?? this.getVersion();
const packagePath = this.getPackagePath(cwd);
const packageData = this.getPackageData(packagePath);

this.cwd = packagePath ? path.dirname(packagePath) : cwd;
this.version = process.env.VERSION ?? packageData?.version;
this.distribution = packageData.brightCli?.distribution;
}

private getVersion(): string | undefined {
private getPackageData(packagePath: string) {
try {
const pkg = readFileSync(this._pkgPath, 'utf8');
const { version } = JSON.parse(pkg);
const pkg = readFileSync(packagePath, 'utf8');

return version;
return JSON.parse(pkg);
} catch {
// noop
}
}

private getPkgPath(cwd?: string): string {
private getPackagePath(cwd?: string): string {
return sync('package.json', {
cwd: cwd || process.env.BRIGHT_CWD || process.cwd()
});
Expand Down
10 changes: 8 additions & 2 deletions src/Repeater/DefaultRuntimeDetector.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import packageInfo from '../../package.json';
import { CliInfo } from '../Config';
import { RuntimeDetector } from './RuntimeDetector';
import arch from 'arch';
import { delay, inject, injectable } from 'tsyringe';
import { execSync } from 'child_process';
import os from 'os';

@injectable()
export class DefaultRuntimeDetector implements RuntimeDetector {
constructor(
@inject(delay(() => CliInfo)) private readonly cliInfo: CliInfo
) {}

public distribution(): string | undefined {
return packageInfo.brightCli.distribution;
return this.cliInfo.distribution;
}

public isInsideDocker(): boolean {
Expand Down

0 comments on commit 02b1eb7

Please sign in to comment.