Skip to content

Commit

Permalink
feat: migrate convert command
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurtu-gal committed May 1, 2024
1 parent ffcba6c commit 7e80488
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ export default class Convert extends Command {
try {
// LOAD FILE
this.specFile = await load(filePath);
// eslint-disable-next-line sonarjs/no-duplicate-string
this.metricsMetadata.to_version = flags['target-version'];

// CONVERSION
convertedFile = convert(this.specFile.text(), flags['target-version'] as ConvertVersion);
if (convertedFile) {
if (this.specFile.getFilePath()) {
this.log(`File ${this.specFile.getFilePath()} successfully converted!`);
this.log(`🎉 The ${this.specFile.getFilePath()} file has been successfully converted to version ${flags['target-version']}!!`);
} else if (this.specFile.getFileURL()) {
this.log(`URL ${this.specFile.getFileURL()} successfully converted!`);
this.log(`🎉 The URL ${this.specFile.getFileURL()} has been successfully converted to version ${flags['target-version']}!!`);
}
}

Expand All @@ -64,9 +65,11 @@ export default class Convert extends Command {
type: 'invalid-file',
filepath: filePath
}));
} else if (this.specFile?.toJson().asyncapi > flags['target-version']) {
this.error(`The ${filePath} file cannot be converted to an older version. Downgrading is not supported.`);
} else {
this.error(err as Error);
}
}
}
}
}
10 changes: 5 additions & 5 deletions test/integration/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('convert', () => {
.stdout()
.command(['convert', filePath])
.it('works when file path is passed', (ctx, done) => {
expect(ctx.stdout).to.contain('File ./test/fixtures/specification.yml successfully converted!\n');
expect(ctx.stdout).to.contain('The ./test/fixtures/specification.yml file has been successfully converted to version 3.0.0!!');
expect(ctx.stderr).to.equal('');
done();
});
Expand All @@ -52,7 +52,7 @@ describe('convert', () => {
.stdout()
.command(['convert', 'http://localhost:8080/dummySpec.yml'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).to.contain('URL http://localhost:8080/dummySpec.yml successfully converted!\n');
expect(ctx.stdout).to.contain('The URL http://localhost:8080/dummySpec.yml has been successfully converted to version 3.0.0!!');
expect(ctx.stderr).to.equal('');
done();
});
Expand All @@ -73,7 +73,7 @@ describe('convert', () => {
.stdout()
.command(['convert'])
.it('converts from current context', (ctx, done) => {
expect(ctx.stdout).to.contain(`File ${path.resolve(__dirname, '../fixtures/specification.yml')} successfully converted!\n`);
expect(ctx.stdout).to.contain(`The ${path.resolve(__dirname, '../fixtures/specification.yml')} file has been successfully converted to version 3.0.0!!\n`);
expect(ctx.stderr).to.equal('');
done();
});
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('convert', () => {
.stdout()
.command(['convert', filePath, '-o=./test/fixtures/specification_output.yml'])
.it('works when .yml file is passed', (ctx, done) => {
expect(ctx.stdout).to.equal(`File ${filePath} successfully converted!\n`);
expect(ctx.stdout).to.contain(`The ${filePath} file has been successfully converted to version 3.0.0!!`);
expect(fs.existsSync('./test/fixtures/specification_output.yml')).to.equal(true);
expect(ctx.stderr).to.equal('');
fs.unlinkSync('./test/fixtures/specification_output.yml');
Expand All @@ -171,7 +171,7 @@ describe('convert', () => {
.stdout()
.command(['convert', JSONFilePath, '-o=./test/fixtures/specification_output.json'])
.it('works when .json file is passed', (ctx, done) => {
expect(ctx.stdout).to.equal(`File ${JSONFilePath} successfully converted!\n`);
expect(ctx.stdout).to.contain(`The ${JSONFilePath} file has been successfully converted to version 3.0.0!!`);
expect(fs.existsSync('./test/fixtures/specification_output.json')).to.equal(true);
expect(ctx.stderr).to.equal('');
fs.unlinkSync('./test/fixtures/specification_output.json');
Expand Down

0 comments on commit 7e80488

Please sign in to comment.