-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathbundle.ts
78 lines (66 loc) · 2.58 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
import Command from '../core/base';
import bundle from '@asyncapi/bundler';
import { promises as fsPromises } from 'fs';
import path from 'path';
import { Specification } from '../core/models/SpecificationFile';
import { Document } from '@asyncapi/bundler/lib/document';
import { bundleFlags } from '../core/flags/bundle.flags';
const { writeFile } = fsPromises;
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 = bundleFlags();
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}`);
}
}
}
}