Skip to content

Commit

Permalink
feat: analytics- enable getting a command version
Browse files Browse the repository at this point in the history
fix: add async to postAnalytics

fix: add async to promise chain

style: run prettier

fix: update analytics test

feat: analytics- enable getting a command version
  • Loading branch information
Avishag Israeli committed Dec 3, 2020
1 parent 7606a5b commit 8097297
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/lib/analytics-sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

const debug = require('debug')('snyk');
import * as fs from 'fs';
import * as semver from 'semver';
import { ArgsOptions } from '../cli/args';
import { join } from 'path';
const { exec } = require('child_process');

export const INTEGRATION_NAME_ENVVAR = 'SNYK_INTEGRATION_NAME';
export const INTEGRATION_VERSION_ENVVAR = 'SNYK_INTEGRATION_VERSION';
Expand Down Expand Up @@ -149,3 +151,53 @@ export function validateHomebrew(snykExecutablePath: string): boolean {
}
return false;
}

function runCommand(cmd: string): Promise<string> {
return new Promise((resolve) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.warn(error);
}
return resolve(stdout ? stdout : stderr);
});
});
}

export async function isInstalled(commandToCheck: string): Promise<boolean> {
let whichCommand = 'which';
const os = process.platform;
if (os === 'win32') {
whichCommand = 'where';
} else if (os === 'android') {
whichCommand = 'adb shell which';
}

try {
await runCommand(`${whichCommand} ${commandToCheck}`);
} catch (error) {
return false;
}
return true;
}

export async function getCommandVersion(
commandToCheck: string,
): Promise<string | null> {
const isCommandInstalled = await isInstalled(commandToCheck);

if (isCommandInstalled) {
try {
let version = await runCommand(`${commandToCheck} --version`);
version = version.replace('\n', '');
if (semver.valid(version) !== null) {
if (version.charAt(0) === 'v') {
version = version.substring(1);
}
return version;
}
} catch (error) {
return null;
}
}
return null;
}
7 changes: 6 additions & 1 deletion src/lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
getIntegrationVersion,
getIntegrationEnvironment,
getIntegrationEnvironmentVersion,
getCommandVersion,
} = require('./analytics-sources');
const isCI = require('./is-ci').isCI;
const debug = require('debug')('snyk');
Expand Down Expand Up @@ -63,7 +64,7 @@ function postAnalytics(data) {
// get snyk version
return version
.getVersion()
.then((version) => {
.then(async (version) => {
data.version = version;
data.os = osName(os.platform(), os.release());
data.nodeVersion = process.version;
Expand All @@ -85,6 +86,10 @@ function postAnalytics(data) {
}

data.ci = isCI();

data.environment = {};
data.environment.npmVersion = isStandalone ? null : await getCommandVersion('npm');

data.durationMs = Date.now() - startTime;

try {
Expand Down
3 changes: 3 additions & 0 deletions test/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ test('analytics', (t) => {
'version',
'id',
'ci',
'environment',
'metadata',
'metrics',
'args',
Expand Down Expand Up @@ -120,6 +121,7 @@ test('analytics with args', (t) => {
'version',
'id',
'ci',
'environment',
'metadata',
'metrics',
'args',
Expand Down Expand Up @@ -161,6 +163,7 @@ test('analytics with args and org', (t) => {
'version',
'id',
'ci',
'environment',
'metadata',
'metrics',
'args',
Expand Down

0 comments on commit 8097297

Please sign in to comment.