-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
bundle.ts
84 lines (72 loc) · 3.2 KB
/
bundle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Flags } from '@oclif/core';
import Command from '../base';
import bundle from '@asyncapi/bundler';
import { promises } from 'fs';
import path from 'path';
import { Specification } from '../models/SpecificationFile';
import { Document } from '@asyncapi/bundler/lib/document';
const { writeFile } = promises;
export default class Bundle extends Command {
static readonly description = 'Bundle one or multiple AsyncAPI Documents and their references together.';
static strict = false;
static examples = [
'asyncapi bundle ./asyncapi.yaml > final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml --output final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --base ./main.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --base ./main.yaml --xOrigin',
'asyncapi bundle ./asyncapi.yaml -o final-asyncapi.yaml --base ../public-api/main.yaml --baseDir ./social-media/comments-service',
];
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.' }),
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.' }),
baseDir: Flags.string({ char: 'd', description: 'One relative/absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.' }),
xOrigin: Flags.boolean({ char: 'x', description: 'Pass this switch to generate properties "x-origin" that will contain historical values of dereferenced "$ref"s.' }),
};
async run() {
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
const outputFormat = path.extname(argv[0] as string);
const AsyncAPIFiles = argv as string[];
this.metricsMetadata.files = AsyncAPIFiles.length;
const document = await bundle(AsyncAPIFiles,
{
base: flags.base,
baseDir: flags.baseDir,
xOrigin: flags.xOrigin,
}
);
await this.collectMetricsData(document);
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.string(), {
encoding: 'utf-8',
});
}
this.log(`Check out your shiny new bundled files at ${output}`);
}
}
private async collectMetricsData(document: Document) {
try {
// We collect the metadata from the final output so it contains all the files
this.specFile = new Specification(document.string() ?? '');
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}
}