Skip to content

Commit

Permalink
feat: add x-origin property
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Apr 18, 2024
1 parent 92987d6 commit 72aafe3
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
6 changes: 3 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ console.log(document.string()); // get JSON string

| Param | Type | Description |
| --- | --- | --- |
| files | <code>Array.&lt;string&gt;</code> | <p>Array of relative or absolute paths to AsyncAPI Documents that should be bundled.</p> |
| files | <code>string</code> \| <code>Array.&lt;string&gt;</code> | <p>One or more relative/absolute paths to AsyncAPI Documents that should be bundled.</p> |
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | <p>Base object whose properties will be retained.</p> |
| [options.baseDir] | <code>string</code> | <p>Relative or absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.</p> |
| [options.base] | <code>string</code> | <p>One relative/absolute path to base object whose properties will be retained.</p> |
| [options.baseDir] | <code>string</code> | <p>One relative/absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.</p> |
| [options.xOrigin] | <code>boolean</code> | <p>Pass <code>true</code> to generate properties <code>x-origin</code> that will contain historical values of dereferenced <code>$ref</code>s.</p> |

**Example**
Expand Down
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Installation](#installation)
- [Usage](#usage)
* [Dereference of the external references](#dereference-of-the-external-references)
* [Option `baseDir`](#option-basedir)
* [Property `x-origin`](#property-x-origin)
* [Movement of components to `components`](#movement-of-components-to-components)
* [Code examples](#code-examples)
Expand Down Expand Up @@ -214,6 +215,25 @@ Regexes of internal references that MUST be `Reference Object`s:
```


### Option `baseDir`

Option `baseDir` represents the main working directory of the program, "root directory," relatively to which will be resolved all paths of AsyncAPI Documents passed to the `Bundler`.

Starting from `Bundler` v0.5.0, option `baseDir` is reimplemented with changed logic, and `Bundler` accepts only **paths** of AsyncAPI Documents, which will be read with `readFileSync()` internally.

In a nutshell, the process looks like this:

- Paths of AsyncAPI Documents are passed as `'main.yaml'` | `'./main.yaml'` | `'../main.yaml'` | `['./main.yaml']` | `['main.yaml', 'audio.yaml']`, etc.

- Path/paths are assured to have an `Array` type with `Array.from()` to make them iterable.

- Working directory of the program is changed to the `baseDir` with `process.chdir()`.

- **And only then** are the paths of the AsyncAPI Documents starting to be read from the array the are currently in, one by one, resolving paths and `$ref`s relatively to the `baseDir`.

Take a look at `./example/bundle-cjs.cjs`, which demonstrates working with `baseDir` and `$ref`s of different levels of nesting.


### Property `x-origin`

Property `x-origin` is used for origin tracing in `Bundler` and component naming in `Optimizer`.
Expand Down Expand Up @@ -302,10 +322,10 @@ main().catch(e => console.error(e));
| Param | Type | Description |
| --- | --- | --- |
| files | <code>Array.&lt;string&gt;</code> | <p>Array of relative or absolute paths to AsyncAPI Documents that should be bundled.</p> |
| files | <code>string</code> \| <code>Array.&lt;string&gt;</code> | <p>One or more relative/absolute paths to AsyncAPI Documents that should be bundled.</p> |
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | <p>Base object whose properties will be retained.</p> |
| [options.baseDir] | <code>string</code> | <p>Relative or absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.</p> |
| [options.base] | <code>string</code> | <p>One relative/absolute path to base object whose properties will be retained.</p> |
| [options.baseDir] | <code>string</code> | <p>One relative/absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.</p> |
| [options.xOrigin] | <code>boolean</code> | <p>Pass <code>true</code> to generate properties <code>x-origin</code> that will contain historical values of dereferenced <code>$ref</code>s.</p> |
Expand Down
2 changes: 1 addition & 1 deletion example/bundle-cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');

async function main() {
const document = await bundle(['social-media/comments-service/main.yaml'], {
const document = await bundle(['./social-media/comments-service/main.yaml', '../main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
Expand Down
2 changes: 1 addition & 1 deletion example/bundle-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const bundle = require('@asyncapi/bundler');
async function main() {
const filePaths = ['./camera.yml', './audio.yml'];
const document = await bundle(filePaths, {
base: readFileSync('./base.yml', 'utf-8'),
base: ['./base.yml'],
xOrigin: true,
});
if (document.yml()) {
Expand Down
29 changes: 22 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import type { AsyncAPIObject } from './spec-types';

/**
*
* @param {string[]} files Array of relative or absolute paths to AsyncAPI
* Documents that should be bundled.
* @param {string | string[]} files One or more relative/absolute paths to
* AsyncAPI Documents that should be bundled.
* @param {Object} [options]
* @param {string | object} [options.base] Base object whose properties will be
* retained.
* @param {string} [options.baseDir] Relative or absolute path to directory
* @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
Expand Down Expand Up @@ -77,9 +77,19 @@ import type { AsyncAPIObject } from './spec-types';
*```
*
*/
export default async function bundle(files: string[], options: any = {}) {
if (options.baseDir) {
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
Expand All @@ -89,6 +99,11 @@ export default async function bundle(files: string[], options: any = {}) {
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);
}
Expand Down
11 changes: 2 additions & 9 deletions tests/lib/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { describe, expect, test } from '@jest/globals';
import bundle from '../../src';
import { isExternalReference } from '../../src/util';
import fs from 'fs';
import path from 'path';

describe('[integration testing] bundler should ', () => {
test('should return bundled doc', async () => {
const files = ['./tests/camera.yml', './tests/audio.yml'];
const response = await bundle(files, {
base: fs.readFileSync(
path.resolve(process.cwd(), './tests/base.yml'),
'utf-8'
),
base: path.resolve(process.cwd(), './tests/base.yml'),
noValidation: true,
});
expect(response).toBeDefined();
Expand Down Expand Up @@ -54,10 +50,7 @@ describe('[integration testing] bundler should ', () => {
expect(
await bundle(files, {
xOrigin: true,
base: fs.readFileSync(
path.resolve(process.cwd(), './tests/base-option/base.yaml'),
'utf-8'
),
base: path.resolve(process.cwd(), './tests/base-option/base.yaml'),
noValidation: true,
})
).resolves;
Expand Down

0 comments on commit 72aafe3

Please sign in to comment.