Skip to content

Commit

Permalink
Merge pull request #2533 from snyk/refactor/get_version
Browse files Browse the repository at this point in the history
feat: Change getVersion() from async to sync
  • Loading branch information
YairZ101 authored Jan 16, 2022
2 parents c87595c + 3266c77 commit adda0a0
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/cli/commands/version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getVersion, isStandaloneBuild } from '../../lib/version';

export default async function version() {
let version = await getVersion();
let version = getVersion();
if (isStandaloneBuild()) {
version += ' (standalone)';
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/analytics/getStandardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function getStandardData(
args: ArgsOptions[],
): Promise<StandardAnalyticsData> {
const isStandalone = version.isStandaloneBuild();
const snykVersion = await version.getVersion();
const snykVersion = version.getVersion();
const seed = uuidv4();
const shasum = crypto.createHash('sha1');
const environment = isStandalone
Expand Down
2 changes: 1 addition & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function makeRequest(
process.env.HTTPS_PROXY || process.env.https_proxy;
}

const versionNumber = await getVersion();
const versionNumber = getVersion();
const body = payload.body;
let data;

Expand Down
25 changes: 2 additions & 23 deletions src/lib/version.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
import * as fs from 'fs';
import * as path from 'path';
import { executeCommand } from './exec';

export async function getVersion(): Promise<string> {
export function getVersion(): string {
const root = path.resolve(__dirname, '../..');

const { version } = JSON.parse(
fs.readFileSync(path.join(root, 'package.json'), 'utf8'),
);

if (version && version !== '0.0.0') {
return version;
}

try {
const [branchName, commitStr, filesCount] = await Promise.all([
executeCommand('git rev-parse HEAD', root),
executeCommand('git rev-parse --abbrev-ref HEAD', root),
executeCommand('git diff --shortstat', root),
]);

const dirtyCount = parseInt(filesCount as string, 10) || 0;
let curr = `${branchName}: ${commitStr}`;
if (dirtyCount !== 0) {
curr += ` (${dirtyCount} dirty files)`;
}

return curr;
} catch (_err) {
return 'unknown';
}
return version;
}

/**
Expand Down

0 comments on commit adda0a0

Please sign in to comment.