Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add versions to output versions of AsyncAPI tools used (#361) #555

Merged
56 changes: 56 additions & 0 deletions src/commands/config/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Flags } from '@oclif/core';
import Command from '../../base';

export default class Versions extends Command {
static description = 'Show versions of AsyncAPI tools used';

static flags = {
help: Flags.help({ char: 'h' }),
};

async run() {
const dependencies: string[] = [];
let dependency = '';

// Preparation of the array with all dependencies '@asyncapi/*' along with
// their versions.
for (const key in this.config.pjson.dependencies) {
// Making sure with .indexOf() that only package names which START with
// string '@asyncapi' are considered.
if (key.indexOf('@asyncapi', 0) === 0) {
// Avoiding obvious crash on manual removal or alteration of an
// '@asyncapi' package.
try {
// Goofy name `importedPJSON` is chosen to distinguish from name `pjson`
// used in `@oclif` source code.
const importedPJSON = await import(`${key}/package.json`);
dependencies.push(`${key}/${importedPJSON.default.version}`);
} catch (e) {
dependencies.push(`${key}/` + '`package.json` not found');
}
}
}

// Showing information available with `--version` flag.
this.log(this.config.userAgent);

// Iteration through the array containing all dependencies '@asyncapi/*'
// along with their versions.
for (let i = 0; i < dependencies.length; i++) {
// Minimization of the theoretical possibility of a Generic Object
// Injection Sink, at the same time disabling eslint parsing for this
// line since it is actually a false positive.
// https://github.com/eslint-community/eslint-plugin-security/issues/21#issuecomment-530184612
// https://github.com/eslint-community/eslint-plugin-security/issues/21#issuecomment-1157887653
// https://web.archive.org/web/20150430062816/https://blog.liftsecurity.io/2015/01/15/the-dangers-of-square-bracket-notation
dependency = dependencies[i]; // eslint-disable-line
if (i !== dependencies.length - 1) {
this.log(` ├${dependency}`);
} else {
this.log(` └${dependency}\n`);
}
}

this.log(`Repository: ${this.config.pjson.homepage}`);
}
}
27 changes: 27 additions & 0 deletions test/commands/config/versions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test } from '@oclif/test';

describe('versions', () => {
describe('config:versions', () => {
test
.stderr()
.stdout()
.command(['config:versions'])
.it('should show versions of AsyncAPI tools used', (ctx, done) => {
expect(ctx.stdout).toContain('@asyncapi/cli/');
expect(ctx.stdout).toContain('├@asyncapi/');
expect(ctx.stdout).toContain('└@asyncapi/');
expect(ctx.stderr).toEqual('');
done();
});

test
.stderr()
.stdout()
.command(['config:versions'])
.it('should show address of repository of AsyncAPI CLI', (ctx, done) => {
expect(ctx.stdout).toContain('https://github.com/asyncapi/cli');
expect(ctx.stderr).toEqual('');
done();
});
});
});
Loading