Skip to content

Commit

Permalink
Merge pull request #228 from jaredwray/adding-in-template-data
Browse files Browse the repository at this point in the history
adding in template data
  • Loading branch information
jaredwray committed Jan 4, 2024
2 parents d5a9310 + cf9e706 commit e142878
Show file tree
Hide file tree
Showing 9 changed files with 1,325 additions and 4 deletions.
55 changes: 51 additions & 4 deletions src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import {Ecto} from 'ecto';
import fs from 'fs-extra';
import {WritrOptions} from './options.js';
import {type GithubData, Github, type GithubOptions} from './github.js';

export type WritrData = {
github: GithubData;
github?: GithubData;
templates?: WritrTemplates;
};

export type WritrTemplates = {
index: string;
releases: string;
};

export class WritrBuilder {
Expand All @@ -25,10 +32,13 @@ export class WritrBuilder {
// Get data from github
const githubData = await this.getGithubData(this.options.githubPath);
// Get data of the site
const writrData: WritrData = {
github: githubData,
};
// Get the templates to use
writrData.templates = await this.getTemplates(this.options);

// get the templates to use

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

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

Expand Down Expand Up @@ -67,4 +77,41 @@ export class WritrBuilder {
const github = new Github(options);
return github.getData();
}

public async getTemplates(options: WritrOptions): Promise<WritrTemplates> {
const templates: WritrTemplates = {
index: '',
releases: '',
};

if (await fs.pathExists(options.templatePath)) {
const index = await this.getTemplateFile(options.templatePath, 'index');
if (index) {
templates.index = index;
}

const releases = await this.getTemplateFile(options.templatePath, 'releases');
if (releases) {
templates.releases = releases;
}
} else {
throw new Error('No template path found');
}

return templates;
}

public async getTemplateFile(path: string, name: string): Promise<string | undefined> {
let result;
const files = await fs.readdir(path);
for (const file of files) {
const fileName = file.split('.');
if (fileName[0].toString().toLowerCase() === name.toLowerCase()) {
result = file.toString();
break;
}
}

return result;
}
}
27 changes: 27 additions & 0 deletions test/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,31 @@ describe('WritrBuilder', () => {
const githubData = await builder.getGithubData('jaredwray/writr');
expect(githubData).toBeTruthy();
});
it('should get the file without extension', async () => {
const builder = new WritrBuilder();
const file = await builder.getTemplateFile('test/fixtures/template-example/', 'index');
expect(file).toBe('index.hbs');
});
it('should not get the file without extension', async () => {
const builder = new WritrBuilder();
const file = await builder.getTemplateFile('test/fixtures/template-example/', 'foo');
expect(file).toBe(undefined);
});
it('should get the template data', async () => {
const builder = new WritrBuilder();
const options = new WritrOptions();
options.templatePath = 'test/fixtures/template-example/';
const templateData = await builder.getTemplates(options);
expect(templateData.releases).toBe('releases.handlebars');
});
it('should throw error when template path doesnt exist', async () => {
const builder = new WritrBuilder();
const options = new WritrOptions();
options.templatePath = 'test/fixtures/template-example1/';
try {
await builder.getTemplates(options);
} catch (error: any) {
expect(error.message).toBe('No template path found');
}
});
});
Loading

0 comments on commit e142878

Please sign in to comment.