Skip to content

Commit

Permalink
Merge pull request #231 from jaredwray/building-the-index-page
Browse files Browse the repository at this point in the history
building the index page
  • Loading branch information
jaredwray committed Jan 5, 2024
2 parents 93ad04f + ac90659 commit 76e32b0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export class WritrBuilder {
writrData.templates = await this.getTemplates(this.options);

// Build the home page (index.html)
await this.buildIndexPage(writrData);

// build the releases page (/releases/index.html)
// Build the releases page (/releases/index.html)

// build the sitemap (/sitemap.xml)
await this.buildSiteMapPage(writrData);
Expand Down Expand Up @@ -154,4 +155,22 @@ export class WritrBuilder {

await fs.writeFile(sitemapPath, xml, 'utf8');
}

public async buildIndexPage(data: WritrData): Promise<void> {
const {templatePath} = data.options;
const {outputPath} = data.options;

if (data.templates) {
const indexPath = `${outputPath}/index.html`;
const indexOutputPath = `${outputPath}/index.html`;

await fs.ensureDir(outputPath);

const indexTemplate = `${templatePath}/${data.templates.index}`;
const indexContent = await this._ecto.renderFromFile(indexTemplate, data, templatePath, indexOutputPath);
await fs.writeFile(indexPath, indexContent, 'utf8');
} else {
throw new Error('No templates found');
}
}
}
1 change: 1 addition & 0 deletions template/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<title>{{ options.siteTitle }}</title>
35 changes: 35 additions & 0 deletions test/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,39 @@ describe('WritrBuilder', () => {
await fs.remove(data.options.outputPath);
}
});
it('should build the index.html (/index.html)', async () => {
const builder = new WritrBuilder();
const data: WritrData = {
options: new WritrOptions(),
templates: {
index: 'index.hbs',
releases: 'releases.hbs',
},
};
data.options.sitePath = 'template';
data.options.outputPath = 'test/temp-index-test';

await fs.remove(data.options.outputPath);
try {
await builder.buildIndexPage(data);
const index = await fs.readFile(`${data.options.outputPath}/index.html`, 'utf8');
expect(index).toContain('<title>Writr</title>');
} finally {
await fs.remove(data.options.outputPath);
}
});
it('should throw an error build the index.html (/index.html)', async () => {
const builder = new WritrBuilder();
const data: WritrData = {
options: new WritrOptions(),
};
data.options.sitePath = 'template';
data.options.outputPath = 'test/temp-index-test';

try {
await builder.buildIndexPage(data);
} catch (error: any) {
expect(error.message).toBe('No templates found');
}
});
});

0 comments on commit 76e32b0

Please sign in to comment.