Skip to content

Commit

Permalink
feat: add versions to output versions of AsyncAPI tools used (#361) (
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet authored May 29, 2023
1 parent 4714cc3 commit f345194
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
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();
});
});
});

0 comments on commit f345194

Please sign in to comment.