Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit b105d53

Browse files
feat: file-templates as array (#38)
1 parent 86df05b commit b105d53

File tree

6 files changed

+61
-8
lines changed

6 files changed

+61
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Restrictions:
5252
- HTML tags at the moment is not supported.
5353
- React internal components like Fragments, Suspense etc. are skipped.
5454

55-
### The debug flag
55+
## The debug flag
5656

5757
When rendering you have the option of passing a `debug` flag which does not remove the transpiled files after the rendering process is done.
5858

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable */
2+
3+
const React = require('react');
4+
const { File } = require('../../../components');
5+
6+
module.exports = function() {
7+
return [
8+
React.createElement(File, { name: 'file1.html' }, ['Content1']),
9+
undefined,
10+
React.createElement(File, { name: 'file2.html' }, ['Content2'])
11+
];
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable */
2+
3+
const React = require('react');
4+
const { File } = require('../../../components');
5+
6+
module.exports = function() {
7+
return React.createElement(File, { name: 'file.html' }, ['Content']);
8+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import path from "path";
2+
import { renderTemplate } from "../template";
3+
import { TemplateRenderResult } from "../../types";
4+
5+
describe('renderTemplate', () => {
6+
test('should render a single File template', async () => {
7+
const filePath = path.resolve(__dirname, './file-tests/single-template.js');
8+
const renderedContent = await renderTemplate(filePath, {} as any) as TemplateRenderResult;
9+
10+
expect(typeof renderedContent).toEqual('object');
11+
expect(renderedContent.content).toEqual('Content');
12+
expect(renderedContent.metadata.fileName).toEqual('file.html');
13+
});
14+
15+
test('should render an array of File templates', async () => {
16+
const filePath = path.resolve(__dirname, './file-tests/file-templates.js');
17+
const renderedContent = await renderTemplate(filePath, {} as any) as TemplateRenderResult[];
18+
19+
expect(Array.isArray(renderedContent)).toEqual(true);
20+
expect(typeof renderedContent[0]).toEqual('object');
21+
expect(renderedContent[0].content).toEqual('Content1');
22+
expect(renderedContent[0].metadata.fileName).toEqual('file1.html');
23+
expect(typeof renderedContent[1]).toEqual('object');
24+
expect(renderedContent[1].content).toEqual('Content2');
25+
expect(renderedContent[1].metadata.fileName).toEqual('file2.html');
26+
});
27+
});

src/renderer/template.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { TemplateContext, TemplateRenderResult } from "../types";
99
*
1010
* @param filepath the path to file to render
1111
*/
12-
export async function renderTemplate(filepath: string, context: TemplateContext): Promise<TemplateRenderResult | undefined> {
12+
export async function renderTemplate(filepath: string, context: TemplateContext): Promise<TemplateRenderResult[] | TemplateRenderResult | undefined> {
1313
if (!isJsFile(filepath)) {
1414
return undefined;
1515
}
@@ -25,6 +25,10 @@ export async function renderTemplate(filepath: string, context: TemplateContext)
2525
if (!data) {
2626
return undefined;
2727
}
28+
29+
if (Array.isArray(data)) {
30+
return data.map(file => file && renderFile(file)).filter(Boolean);
31+
}
2832
return renderFile(data);
2933
}
3034

src/transpiler/__tests__/transpiler.spec.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { transpileFiles } from "../";
21
import fs from 'fs';
32
import path from 'path';
43
import { promisify } from 'util';
5-
import { renderTemplate } from "../../renderer/index";
64
import { AsyncAPIDocument } from "@asyncapi/parser";
7-
const readFile = promisify(fs.readFile);
85

6+
import { transpileFiles } from "../transpiler";
7+
import { renderTemplate } from "../../renderer";
8+
import { TemplateRenderResult } from "../../types";
9+
10+
const readFile = promisify(fs.readFile);
911

1012
describe('Transpiler', () => {
1113
const testFiles = path.resolve(__dirname, './testfiles');
@@ -34,7 +36,7 @@ describe('Transpiler', () => {
3436
});
3537
test('and render correctly', async () => {
3638
const content = await renderTemplate(commonjs_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
37-
expect(content?.content).toBe("hello Test");
39+
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
3840
});
3941
});
4042
});
@@ -51,7 +53,7 @@ describe('Transpiler', () => {
5153
});
5254
test('and render correctly', async () => {
5355
const content = await renderTemplate(es5_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
54-
expect(content?.content).toBe("hello Test");
56+
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
5557
});
5658
});
5759
});
@@ -68,7 +70,7 @@ describe('Transpiler', () => {
6870
});
6971
test('and render correctly', async () => {
7072
const content = await renderTemplate(es6_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
71-
expect(content?.content).toBe("hello Test");
73+
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
7274
});
7375
});
7476
});

0 commit comments

Comments
 (0)