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: create new command to format the asyncapi document #1573

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 62 additions & 0 deletions src/commands/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Args } from '@oclif/core';
import { promises as fs } from 'fs';
import * as yaml from 'js-yaml';
import Command from '../core/base';
import { load } from '../core/models/SpecificationFile';
import { ValidationError } from '../core/errors/validation-error';
import { formatFlags } from '../core/flags/format.flags';

export default class Format extends Command {
static description = 'Format AsyncAPI specification file';

static examples = [
'asyncapi format ./asyncapi.yaml',
'asyncapi format ./asyncapi.yaml --output formatted-asyncapi.yaml',
];

static flags = formatFlags();

static args = {
'spec-file': Args.string({description: 'spec path, url, or context-name', required: true}),
};

async run() {
const { args, flags } = await this.parse(Format);
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 parsed = yaml.load(content);
formatted = yaml.dump(parsed, {
indent: 2,
lineWidth: -1,
noRefs: true,
sortKeys: true,
});
} catch (err) {
this.error(`Error formatting file: ${err}`);
}

if (outputPath) {
await fs.writeFile(outputPath, formatted, 'utf8');
this.log(`The Asyncapi document has been formatted, the new formatted document is present in ${outputPath}`);
} else {
await fs.writeFile(filePath, formatted, 'utf8');
this.log(`Asyncapi document ${filePath} has been formatted in-place.`);
}
}
}

Check failure on line 62 in src/commands/format.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Newline required at end of file but not found
10 changes: 10 additions & 0 deletions src/core/flags/format.flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Flags } from '@oclif/core';

export const formatFlags = () => {
return {
output: Flags.string({
char: 'o',
description: 'Output file path',
}),
};
};

Check failure on line 10 in src/core/flags/format.flags.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Newline required at end of file but not found
Loading