Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
eedd602
feat: add bundler command
Souvikns Nov 16, 2022
b639236
feat: add tests
Souvikns Nov 16, 2022
9a6881d
refactor: lint fixes
Souvikns Nov 16, 2022
06b09c9
feat: add all the remaining tests
Souvikns Nov 17, 2022
ceb0f79
Merge branch 'master' into add-bundler-command
Souvikns Nov 17, 2022
8882d2b
docs: update readme
Souvikns Nov 21, 2022
264ea5e
Merge branch 'master' into add-bundler-command
Souvikns Nov 21, 2022
3683063
fix: test file not syncing issue
Souvikns Nov 22, 2022
81f017f
refactor: make requested changes.
Souvikns Nov 28, 2022
822014c
feat: update bundler library to fix base option
Souvikns Nov 29, 2022
1e7745d
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Nov 29, 2022
1c14ea8
refactor: lint fixes
Souvikns Nov 29, 2022
2d1fc87
Update bundle.ts
Souvikns Dec 1, 2022
9fff8b5
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Dec 1, 2022
8c3e813
refactor: lint fixes
Souvikns Dec 1, 2022
7d97a5b
Merge branch 'master' into add-bundler-command
Souvikns Dec 6, 2022
74b31d8
feat: make suggested changes
Souvikns Dec 9, 2022
a2abaa3
fix: bug with mismatch file type
Souvikns Dec 9, 2022
6576201
Merge branch 'master' into add-bundler-command
Souvikns Dec 9, 2022
3b1d25c
refactor: renaming test spec files
Souvikns Dec 9, 2022
d8d9aef
refactor: update help message
Souvikns Dec 13, 2022
6952446
Merge branch 'master' into add-bundler-command
Souvikns Dec 13, 2022
276f04c
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Jan 18, 2023
ac9e5a7
fix: update dependencies
Souvikns Jan 18, 2023
169d425
Update package-lock.json
Souvikns Jan 20, 2023
b4470aa
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Feb 1, 2023
f11decf
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Feb 9, 2023
027465b
Merge branch 'master' into add-bundler-command
Souvikns Feb 9, 2023
63d89c9
Merge branch 'master' into add-bundler-command
Souvikns Feb 9, 2023
7891c2c
Merge branch 'master' into add-bundler-command
Souvikns Feb 13, 2023
d5c0c33
chore: update package lock
Souvikns Feb 15, 2023
bef60e6
Merge branch 'master' into add-bundler-command
Souvikns Feb 15, 2023
588468f
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Feb 22, 2023
b4e7f13
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Mar 20, 2023
e45c40f
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Mar 27, 2023
b6e3d5d
Merge branch 'master' into add-bundler-command
Souvikns Mar 29, 2023
39b809e
Merge remote-tracking branch 'upstream/master' into add-bundler-command
Souvikns Mar 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56,425 changes: 38,840 additions & 17,585 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"bugs": "https://github.com/asyncapi/cli/issues",
"dependencies": {
"@asyncapi/bundler": "^0.3.8",
"@asyncapi/converter": "^1.2.0",
"@asyncapi/diff": "^0.4.1",
"@asyncapi/generator": "^1.9.17",
Expand Down
76 changes: 76 additions & 0 deletions src/commands/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Flags } from '@oclif/core';
import { Example } from '@oclif/core/lib/interfaces';
import Command from '../base';
import bundle from '@asyncapi/bundler';
import { promises } from 'fs';
import path from 'path';
import { load } from '../models/SpecificationFile';

const { writeFile } = promises;

export default class Bundle extends Command {
static description = 'bundle one or multiple asyncapi documents and their references together.';
static strict = false;

static examples: Example[] = [
'asyncapi bundle ./asyncapi.yaml > final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml --output final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --reference-into-components',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --base ./asyncapi.yaml --reference-into-components'
];

static flags = {
help: Flags.help({ char: 'h' }),
output: Flags.string({ char: 'o', description: 'The output file name. Omitting this flag the result will be printed in the console.' }),
'reference-into-components': Flags.boolean({ char: 'r', description: 'Bundle the message $refs into components object.' }),
base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }),
Comment thread
Souvikns marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }),
base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties need to be overwritten.' }),

What This is required when some properties need to be overwritten means? I tried to figure from bundle project history why it was added, also looked into test case, but in test cases we do not evaluate bundle against some real result, so I could not figure quickly what is the difference between bundling with and without base

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this could help asyncapi/bundler#101 , this is something most people wouldn't be interested in, but it is like nice to have feature. You can use base flag to overwrite or retain info object when you are merging different asyncapi files.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I read explanation, I understand that base is basically something that holds basic information that should stay in asyncapi.yml and all the other files are added to it, cannot override what is in base already

but then I see the linked issue and the expected behavior section. Looks like what is there in base is lost and expected result is This service is in charge of processing user logouts and not what is in base This service is in charge of processing user signups

sorry for all these questions :(

};

async run() {
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
let baseFile;
const outputFormat = path.extname(argv[0]);
const AsyncAPIFiles = await this.loadFiles(argv);
if (flags.base) {baseFile = (await load(flags.base)).text();}

const document = await bundle(AsyncAPIFiles,
{
referenceIntoComponents: flags['reference-into-components'],
base: baseFile
}
);

if (!output) {
if (outputFormat === '.yaml' || outputFormat === '.yml') {
this.log(document.yml());
} else {
this.log(JSON.stringify(document.json()));
}
} else {
const format = path.extname(output);

if (format === '.yml' || format === '.yaml') {
await writeFile(path.resolve(process.cwd(), output), document.yml(), {
encoding: 'utf-8',
});
}

if (format === '.json') {
await writeFile(path.resolve(process.cwd(), output), document.json(), {
encoding: 'utf-8',
});
}
this.log(`Check out your shiny new bundled files at ${output}`);
}
}

async loadFiles(filepaths: string[]): Promise<string[]> {
const files = [];
for (const filepath of filepaths) {
const file = await load(filepath);
files.push(file.text());
}
return files;
}
}
10 changes: 10 additions & 0 deletions test/commands/bundle/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
asyncapi: "2.5.0"
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup:
subscribe:
message:
$ref: "./test/commands/bundle/messages.yaml#/messages/UserSignedUp"
72 changes: 72 additions & 0 deletions test/commands/bundle/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { test } from '@oclif/test';
import fs from 'fs';
import path from 'path';
import { fileCleanup } from '../../testHelper';

const spec = fs.readFileSync('./test/commands/bundle/final-asyncapi.yaml', {encoding: 'utf-8'});

function validateGeneratedSpec(filePath, spec) {
const generatedSPec = fs.readFileSync(path.resolve(filePath), { encoding: 'utf-8' });
return generatedSPec === spec;
}

describe('bundle', () => {
test
.stdout()
.command([
'bundle', './test/commands/bundle/asyncapi.yaml',
'--output=./test/commands/bundle/final.yaml',
])
.it('should successfully bundle specification', (ctx, done) => {
expect(ctx.stdout).toContain(
'Check out your shiny new bundled files at ./test/commands/bundle/final.yaml'
);
fileCleanup('./test/commands/bundle/final.yaml');
done();
});

test
.stderr()
.command([
'bundle', './test/commands/bundle/asyncapi.yml'
])
.it('should throw error message if the file path is wrong', (ctx, done) => {
expect(ctx.stderr).toContain('error loading AsyncAPI document from file: ./test/commands/bundle/asyncapi.yml file does not exist.\n');
done();
});

test
.stdout()
.command([
'bundle', './test/commands/bundle/asyncapi.yaml', '--reference-into-components', '--output=test/commands/bundle/final.yaml'
])
.it('should be able to refence messages into components', (ctx, done) => {
expect(ctx.stdout).toContain('Check out your shiny new bundled files at test/commands/bundle/final.yaml\n');
fileCleanup('./test/commands/bundle/final.yaml');
done();
});

test
.stdout()
.command([
'bundle', './test/commands/bundle/asyncapi.yaml', './test/commands/bundle/feature.yaml', '--reference-into-components', '--output=test/commands/bundle/final.yaml'
])
.it('should be able to bundle multiple specs along with custom reference', (ctx, done) => {
expect(ctx.stdout).toContain('Check out your shiny new bundled files at test/commands/bundle/final.yaml\n');
expect(validateGeneratedSpec('test/commands/bundle/final.yaml', spec));
fileCleanup('./test/commands/bundle/final.yaml');
done();
});

test
.stdout()
.command([
'bundle', './test/commands/bundle/asyncapi.yaml', './test/commands/bundle/feature.yaml', '--reference-into-components', '--output=test/commands/bundle/final.yaml', '--base=./test/commands/bundle/asyncapi.yaml'
])
.it('should be able to bundle correctly with overwriting base file', (ctx, done) => {
expect(ctx.stdout).toContain('Check out your shiny new bundled files at test/commands/bundle/final.yaml\n');
expect(validateGeneratedSpec('test/commands/bundle/final-asyncapi.yaml', spec));
fileCleanup('./test/commands/bundle/final.yaml');
done();
});
});
11 changes: 11 additions & 0 deletions test/commands/bundle/feature.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
asyncapi: "2.5.0"
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user logouts
channels:
user/loggedOut:
subcribe:
message:
$ref: 'test/commands/bundle/messages.yaml#/messages/UserLoggedOut'

41 changes: 41 additions & 0 deletions test/commands/bundle/final-asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
asyncapi: 2.5.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup:
subscribe:
message:
$ref: '#/components/messages/UserSignedUp'
user/loggedOut:
subcribe:
message:
$ref: '#/components/messages/UserLoggedOut'
components:
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
UserLoggedOut:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
userId:
type: string
description: Id the user
timestamp:
type: number
descriptio: Time stamp when the user logged out

26 changes: 26 additions & 0 deletions test/commands/bundle/messages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
UserLoggedOut:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
userId:
type: string
description: Id the user
timestamp:
type: number
description: Time stamp when the user logged out

36 changes: 18 additions & 18 deletions test/commands/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--type=all',
'--format=json',
])
Expand All @@ -42,8 +42,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--type=breaking',
'--format=json',
])
Expand All @@ -62,8 +62,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--type=non-breaking',
'--format=json',
])
Expand All @@ -82,8 +82,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--type=unclassified',
'--format=json',
])
Expand All @@ -102,8 +102,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--format=json',
])
// eslint-disable-next-line sonarjs/no-identical-functions
Expand All @@ -122,8 +122,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--overrides=./test/fixtures/overrides.json',
'--format=json',
])
Expand All @@ -142,8 +142,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--overrides=./overrides-wrong.json',
'--format=json',
])
Expand All @@ -162,8 +162,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--overrides=./test/fixtures/invalid-overrides.json',
])
.it((ctx, done) => {
Expand All @@ -181,8 +181,8 @@ describe('diff', () => {
.stdout()
.command([
'diff',
'./test/fixtures/specification_v1.yml',
'./test/fixtures/specification_v2.yml',
'./test/fixtures/asyncapi_v1.yml',
'./test/fixtures/asyncapi_v2.yml',
'--type=all',
])
.it('works when file path is passed', (ctx, done) => {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions test/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ export default class ContextTestingHelper {
};
}
}

export function fileCleanup(filepath: string) {
unlinkSync(filepath);
}