-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathbundle.ts
86 lines (73 loc) · 3.04 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
85
86
import { Flags } from '@oclif/core';
import { Example } from '@oclif/core/lib/interfaces';
import Command from '../base';
import bundle from '@asyncapi/bundler';
import { promises } from 'fs';
import path from 'path';
import { Specification, load } from '../models/SpecificationFile';
const { writeFile } = promises;
export default class Bundle extends Command {
static description = 'bundle one or multiple asyncapi documents and their references together.';
static strict = false;
static examples: Example[] = [
'asyncapi bundle ./asyncapi.yaml > final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml --output final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --reference-into-components',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --base ./asyncapi.yaml --reference-into-components'
];
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.' }),
'reference-into-components': Flags.boolean({ char: 'r', description: 'Bundle the message $refs into components object.' }),
base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }),
};
async run() {
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
let baseFile;
const outputFormat = path.extname(argv[0]);
const AsyncAPIFiles = await this.loadFiles(argv);
const containsAsyncAPI3 = AsyncAPIFiles.filter((file) => {
return file.isAsyncAPI3();
});
if (containsAsyncAPI3.length > 0) {
this.error('One of the files you tried to bundle is AsyncAPI v3 format, the bundle command does not support it yet, please checkout https://github.com/asyncapi/bundler/issues/133');
}
if (flags.base) {baseFile = (await load(flags.base)).text();}
const fileContents = AsyncAPIFiles.map((file) => file.text());
const document = await bundle(fileContents,
{
referenceIntoComponents: flags['reference-into-components'],
base: baseFile
}
);
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}`);
}
}
async loadFiles(filepaths: string[]): Promise<Specification[]> {
const files = [];
for (const filepath of filepaths) {
const file = await load(filepath);
files.push(file);
}
return files;
}
}