Repository navigation
feat: add bundle command #391
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
Changes from all commits
eedd602
b639236
9a6881d
06b09c9
ceb0f79
8882d2b
264ea5e
3683063
81f017f
822014c
1e7745d
1c14ea8
2d1fc87
9fff8b5
8c3e813
7d97a5b
74b31d8
a2abaa3
6576201
3b1d25c
d8d9aef
6952446
276f04c
ac9e5a7
169d425
b4470aa
f11decf
027465b
63d89c9
7891c2c
d5c0c33
bef60e6
588468f
b4e7f13
e45c40f
b6e3d5d
39b809e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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.' }), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
What
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when I read explanation, I understand that 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 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; | ||||||
| } | ||||||
| } | ||||||
| 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" |
| 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(); | ||
| }); | ||
| }); |
| 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' | ||
|
|
| 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 | ||
|
|
| 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 | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.