Skip to content

Commit

Permalink
feat: file-templates as array (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Mar 4, 2021
1 parent 86df05b commit b105d53
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Restrictions:
- HTML tags at the moment is not supported.
- React internal components like Fragments, Suspense etc. are skipped.

### The debug flag
## The debug flag

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

Expand Down
12 changes: 12 additions & 0 deletions src/renderer/__tests__/file-tests/file-templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable */

const React = require('react');
const { File } = require('../../../components');

module.exports = function() {
return [
React.createElement(File, { name: 'file1.html' }, ['Content1']),
undefined,
React.createElement(File, { name: 'file2.html' }, ['Content2'])
];
};
8 changes: 8 additions & 0 deletions src/renderer/__tests__/file-tests/single-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */

const React = require('react');
const { File } = require('../../../components');

module.exports = function() {
return React.createElement(File, { name: 'file.html' }, ['Content']);
};
27 changes: 27 additions & 0 deletions src/renderer/__tests__/template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from "path";
import { renderTemplate } from "../template";
import { TemplateRenderResult } from "../../types";

describe('renderTemplate', () => {
test('should render a single File template', async () => {
const filePath = path.resolve(__dirname, './file-tests/single-template.js');
const renderedContent = await renderTemplate(filePath, {} as any) as TemplateRenderResult;

expect(typeof renderedContent).toEqual('object');
expect(renderedContent.content).toEqual('Content');
expect(renderedContent.metadata.fileName).toEqual('file.html');
});

test('should render an array of File templates', async () => {
const filePath = path.resolve(__dirname, './file-tests/file-templates.js');
const renderedContent = await renderTemplate(filePath, {} as any) as TemplateRenderResult[];

expect(Array.isArray(renderedContent)).toEqual(true);
expect(typeof renderedContent[0]).toEqual('object');
expect(renderedContent[0].content).toEqual('Content1');
expect(renderedContent[0].metadata.fileName).toEqual('file1.html');
expect(typeof renderedContent[1]).toEqual('object');
expect(renderedContent[1].content).toEqual('Content2');
expect(renderedContent[1].metadata.fileName).toEqual('file2.html');
});
});
6 changes: 5 additions & 1 deletion src/renderer/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TemplateContext, TemplateRenderResult } from "../types";
*
* @param filepath the path to file to render
*/
export async function renderTemplate(filepath: string, context: TemplateContext): Promise<TemplateRenderResult | undefined> {
export async function renderTemplate(filepath: string, context: TemplateContext): Promise<TemplateRenderResult[] | TemplateRenderResult | undefined> {
if (!isJsFile(filepath)) {
return undefined;
}
Expand All @@ -25,6 +25,10 @@ export async function renderTemplate(filepath: string, context: TemplateContext)
if (!data) {
return undefined;
}

if (Array.isArray(data)) {
return data.map(file => file && renderFile(file)).filter(Boolean);
}
return renderFile(data);
}

Expand Down
14 changes: 8 additions & 6 deletions src/transpiler/__tests__/transpiler.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { transpileFiles } from "../";
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import { renderTemplate } from "../../renderer/index";
import { AsyncAPIDocument } from "@asyncapi/parser";
const readFile = promisify(fs.readFile);

import { transpileFiles } from "../transpiler";
import { renderTemplate } from "../../renderer";
import { TemplateRenderResult } from "../../types";

const readFile = promisify(fs.readFile);

describe('Transpiler', () => {
const testFiles = path.resolve(__dirname, './testfiles');
Expand Down Expand Up @@ -34,7 +36,7 @@ describe('Transpiler', () => {
});
test('and render correctly', async () => {
const content = await renderTemplate(commonjs_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect(content?.content).toBe("hello Test");
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});
Expand All @@ -51,7 +53,7 @@ describe('Transpiler', () => {
});
test('and render correctly', async () => {
const content = await renderTemplate(es5_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect(content?.content).toBe("hello Test");
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});
Expand All @@ -68,7 +70,7 @@ describe('Transpiler', () => {
});
test('and render correctly', async () => {
const content = await renderTemplate(es6_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect(content?.content).toBe("hello Test");
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});
Expand Down

0 comments on commit b105d53

Please sign in to comment.