-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge master into dependabot/npm_and_yarn/cross-spawn-7.0.6
- Loading branch information
Showing
7 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Args } from '@oclif/core'; | ||
import { promises as fs } from 'fs'; | ||
import * as yaml from 'yaml'; | ||
import Command from '../core/base'; | ||
import { load , retrieveFileFormat} from '../core/models/SpecificationFile'; | ||
import { ValidationError } from '../core/errors/validation-error'; | ||
import { prettyFlags } from '../core/flags/pretty.flags'; | ||
|
||
export default class Pretty extends Command { | ||
static readonly description = 'Format AsyncAPI specification file'; | ||
|
||
static readonly examples = [ | ||
'asyncapi pretty ./asyncapi.yaml', | ||
'asyncapi pretty ./asyncapi.yaml --output formatted-asyncapi.yaml', | ||
]; | ||
|
||
static readonly flags = prettyFlags(); | ||
|
||
static readonly args = { | ||
'spec-file': Args.string({description: 'spec path, url, or context-name', required: true}), | ||
}; | ||
|
||
async run() { | ||
const { args, flags } = await this.parse(Pretty); | ||
const filePath = args['spec-file']; | ||
const outputPath = flags.output; | ||
|
||
try { | ||
this.specFile = await load(filePath); | ||
} catch (err) { | ||
this.error( | ||
new ValidationError({ | ||
type: 'invalid-file', | ||
filepath: filePath, | ||
}) | ||
); | ||
} | ||
|
||
const content = this.specFile.text(); | ||
let formatted: string; | ||
|
||
try { | ||
const fileFormat = retrieveFileFormat(this.specFile.text()); | ||
if (fileFormat === 'yaml' || fileFormat === 'yml') { | ||
const yamlDoc = yaml.parseDocument(content); | ||
formatted = yamlDoc.toString({ | ||
lineWidth: 0, | ||
}); | ||
} else if (fileFormat === 'json') { | ||
const jsonObj = JSON.parse(content); | ||
formatted = JSON.stringify(jsonObj, null, 2); | ||
} else { | ||
throw new Error('Unsupported file format'); | ||
} | ||
} catch (err) { | ||
this.error(`Error formatting file: ${err}`); | ||
} | ||
|
||
if (outputPath) { | ||
await fs.writeFile(outputPath, formatted, 'utf8'); | ||
this.log(`Asyncapi document has been beautified ${outputPath}`); | ||
} else { | ||
await fs.writeFile(filePath, formatted, 'utf8'); | ||
this.log(`Asyncapi document ${filePath} has been beautified in-place.`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Flags } from '@oclif/core'; | ||
|
||
export const prettyFlags = () => { | ||
return { | ||
output: Flags.string({ | ||
char: 'o', | ||
description: 'Output file path', | ||
}), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
asyncapi: "2.1.0" | ||
info: | ||
title: Streetlights API | ||
version: "1.0.0" | ||
description: | | ||
The Smartylighting Streetlights API allows you | ||
to remotely manage the city lights. | ||
license: | ||
name: Apache 2.0 | ||
url: "https://www.apache.org/licenses/LICENSE-2.0" | ||
servers: | ||
mosquitto: | ||
url: mqtt://test.mosquitto.org | ||
protocol: mqtt | ||
channels: | ||
light/measured: | ||
publish: | ||
summary: Inform about environmental lighting conditions for a particular streetlight. | ||
operationId: onLightMeasured | ||
message: | ||
name: LightMeasured | ||
payload: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
minimum: 0 | ||
description: Id of the streetlight. | ||
lumens: | ||
type: integer | ||
minimum: 0 | ||
description: Light intensity measured in lumens. | ||
sentAt: | ||
type: string | ||
format: date-time | ||
description: Date and time when the message was sent. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"asyncapi": "2.2.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" | ||
} | ||
} | ||
} | ||
}, | ||
"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" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { test } from '@oclif/test'; | ||
import TestHelper, { createMockServer, stopMockServer } from '../helpers'; | ||
import { expect } from '@oclif/test'; | ||
|
||
const testHelper = new TestHelper(); | ||
const badFormatPath = './test/fixtures/asyncapi_v1.yml'; | ||
const validFormatPath = './test/fixtures/asyncapiValid_v1.yml'; | ||
const badFormatPathJson = './test/fixtures/badFormatAsyncapi.json'; | ||
|
||
describe('pretty', () => { | ||
describe('with file paths', () => { | ||
beforeEach(() => { | ||
testHelper.createDummyContextFile(); | ||
}); | ||
|
||
afterEach(() => { | ||
testHelper.deleteDummyContextFile(); | ||
}); | ||
|
||
before(() => { | ||
createMockServer(); | ||
}); | ||
|
||
after(() => { | ||
stopMockServer(); | ||
}); | ||
|
||
test | ||
.stderr() | ||
.stdout() | ||
.command(['pretty', badFormatPath]) | ||
.it('should log the information file has been beautified', (ctx, done) => { | ||
expect(ctx.stdout).to.contain( | ||
`Asyncapi document ${badFormatPath} has been beautified in-place`, | ||
); | ||
expect(ctx.stderr).to.equal(''); | ||
done(); | ||
}); | ||
|
||
test | ||
.stderr() | ||
.stdout() | ||
.command(['pretty', badFormatPath ,'-o', validFormatPath]) | ||
.it('should log the information file has been beautified', (ctx, done) => { | ||
expect(ctx.stdout).to.contain( | ||
`Asyncapi document has been beautified ${validFormatPath}`, | ||
); | ||
expect(ctx.stderr).to.equal(''); | ||
done(); | ||
}); | ||
|
||
test | ||
.stderr() | ||
.stdout() | ||
.command(['pretty', badFormatPathJson]) | ||
.it('should log the information file has been beautified json file', (ctx, done) => { | ||
expect(ctx.stdout).to.contain( | ||
`Asyncapi document ${badFormatPathJson} has been beautified in-place`, | ||
); | ||
expect(ctx.stderr).to.equal(''); | ||
done(); | ||
}); | ||
}); | ||
}); |