Skip to content

Commit

Permalink
feat: add ability to process multiple partial folders (#28)
Browse files Browse the repository at this point in the history
* add the ability to process multiple partial folders

* fix test

* Better test

* Update src/index.ts

* Apply suggestions from code review

* Update src/partials.ts

* internally convert to array

* feedback changes

Co-authored-by: Alex LaFroscia <alex@lafroscia.com>
  • Loading branch information
Bohreromir and alexlafroscia authored Mar 18, 2021
1 parent e4c6885 commit fbcb39f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export default {
};
```

Each file in this directory (`.html` or `.hbs`) will become registered as a partial. The name of the file is used to invoke it. So, with the above configuration and the following files:
If you want to use multiple partial folders, an array can be submitted.

Each file in these directories (`.html` or `.hbs`) will become registered as a partial. The name of the file is used to invoke it. So, with the above configuration and the following files:

```html
<!-- partials/header.hbs -->
Expand Down
20 changes: 19 additions & 1 deletion __tests__/partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ test('it allows for injecting an HBS partial', async () => {
expect(html).toContain('<h1>Title</h1>');
});

test('it allows for injecting an HBS partial with multiple partial folders', async () => {
const temp = await factory.createStructure({
'index.html': '{{> header }}{{> header2 }}',
partials1: {
'header.hbs': '<h1>Title</h1>',
},
partials2: {
'header2.hbs': '<h1>Different title</h1>',
},
});
const result = await build(temp.dir, {
partialDirectory: [temp.path('partials1'), temp.path('partials2')],
});
const html = getHtmlSource(result);

expect(html).toContain('<h1>Title</h1><h1>Different title</h1>');
});

test('it handles no partial directory existing', async () => {
const temp = await factory.createStructure({
'index.html': '<h1>Title</h1>',
Expand All @@ -60,7 +78,7 @@ test('it handles the partial directory being empty', async () => {
});

const result = await build(temp.dir, {
partialDirectory: temp.path('partials'),
partialDirectory: [temp.path('partials')],
});
const html = getHtmlSource(result);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/resolve-from-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('it can resolve a path from the root', async () => {
},
});
const result = await build(temp.dir, {
partialDirectory: temp.path('partials'),
partialDirectory: [temp.path('partials')],
});
const html = getHtmlSource(result);

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface HandlebarsPluginConfig {
context?: Record<string, unknown>;
compileOptions?: CompileOptions;
runtimeOptions?: RuntimeOptions;
partialDirectory?: string;
partialDirectory?: string | Array<string>;
}

export default function handlebars({
Expand Down
32 changes: 18 additions & 14 deletions src/partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import { resolve, parse } from 'path';
/**
* Registers each HTML file in a directory as Handlebars partial
*/
export async function registerPartials(directoryPath: string): Promise<void> {
let dir: Dir;
export async function registerPartials(directoryPath: string | Array<string>): Promise<void> {
const pathArray: Array<string> = Array.isArray(directoryPath) ? directoryPath : [directoryPath];

try {
dir = await opendir(directoryPath);
} catch (e) {
// No-op if the directory does not exist
return;
}
for await (const path of pathArray) {
let dir: Dir;

try {
dir = await opendir(path);
} catch (e) {
// No-op if the directory does not exist
return;
}

for await (const entry of dir) {
if (entry.isFile) {
const fullPath = resolve(directoryPath, entry.name);
const partialName = parse(entry.name).name;
const content = await readFile(fullPath);
for await (const entry of dir) {
if (entry.isFile) {
const fullPath = resolve(path, entry.name);
const partialName = parse(entry.name).name;
const content = await readFile(fullPath);

registerPartial(partialName, content.toString());
registerPartial(partialName, content.toString());
}
}
}
}

0 comments on commit fbcb39f

Please sign in to comment.