-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
122 lines (112 loc) · 3.54 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { readFileSync } from 'fs';
import { toJS, resolve, versionCheck } from './util';
import { Document } from './document';
import { parse } from './parser';
import type { AsyncAPIObject } from './spec-types';
/**
*
* @param {string | string[]} files One or more relative/absolute paths to
* AsyncAPI Documents that should be bundled.
* @param {Object} [options]
* @param {string} [options.base] One relative/absolute path to base object whose
* properties will be retained.
* @param {string} [options.baseDir] One relative/absolute path to directory
* relative to which paths to AsyncAPI Documents that should be bundled will be
* resolved.
* @param {boolean} [options.xOrigin] Pass `true` to generate properties
* `x-origin` that will contain historical values of dereferenced `$ref`s.
*
* @return {Document}
*
* @example
*
***TypeScript**
*```ts
*import { writeFileSync } from 'fs';
*import bundle from '@asyncapi/bundler';
*
*async function main() {
* const document = await bundle(['social-media/comments-service/main.yaml'], {
* baseDir: 'example-data',
* xOrigin: true,
* });
*
* console.log(document.yml()); // the complete bundled AsyncAPI document
* writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
*}
*
*main().catch(e => console.error(e));
*```
*
***JavaScript CJS module system**
*```js
*'use strict';
*
*const { writeFileSync } = require('fs');
*const bundle = require('@asyncapi/bundler');
*
*async function main() {
* const document = await bundle(['social-media/comments-service/main.yaml'], {
* baseDir: 'example-data',
* xOrigin: true,
* });
* writeFileSync('asyncapi.yaml', document.yml());
*}
*
*main().catch(e => console.error(e));
*```
*
***JavaScript ESM module system**
*```js
*'use strict';
*
*import { writeFileSync } from 'fs';
*import bundle from '@asyncapi/bundler';
*
*async function main() {
* const document = await bundle(['social-media/comments-service/main.yaml'], {
* baseDir: 'example-data',
* xOrigin: true,
* });
* writeFileSync('asyncapi.yaml', document.yml());
*}
*
*main().catch(e => console.error(e));
*```
*
*/
export default async function bundle(
files: string[] | string,
options: any = {}
) {
// if one string was passed, convert it to an array
if (typeof files === 'string') {
files = Array.from(files.split(' '));
}
if (options.baseDir && typeof options.baseDir === 'string') {
process.chdir(options.baseDir);
} else if (options.baseDir && Array.isArray(options.baseDir)) {
process.chdir(String(options.baseDir[0])); // guard against passing an array
}
const readFiles = files.map(file => readFileSync(file, 'utf-8')); // eslint-disable-line
const parsedJsons = readFiles.map(file => toJS(file)) as AsyncAPIObject[];
const majorVersion = versionCheck(parsedJsons);
if (typeof options.base !== 'undefined') {
if (typeof options.base === 'string') {
options.base = readFileSync(options.base, 'utf-8'); // eslint-disable-line
} else if (Array.isArray(options.base)) {
options.base = readFileSync(String(options.base[0]), 'utf-8'); // eslint-disable-line
}
options.base = toJS(options.base);
await parse(options.base, majorVersion, options);
}
const resolvedJsons: AsyncAPIObject[] = await resolve(
parsedJsons,
majorVersion,
options
);
return new Document(resolvedJsons, options.base);
}
// 'module.exports' is added to maintain backward compatibility with Node.js
// projects, that use CJS module system.
module.exports = bundle;