Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding build function #226

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {Ecto} from 'ecto';
import {WritrOptions} from './options.js';
import {type GithubData} from './github.js';

export type WritrData = {
github: GithubData;
};

export class WritrBuilder {
private readonly _options: WritrOptions = new WritrOptions();
private readonly _ecto: Ecto = new Ecto();
constructor(options?: WritrOptions) {
if (options) {
this._options = options;
}
}

public get options(): WritrOptions {
return this._options;
}

public async build(): Promise<void> {
// Validate the options
this.validateOptions(this.options);
// Get data from github

// get data of the site

// get the templates to use

// build the home page (index.html)

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

// build the rss feed (/rss.xml)

// build the sitemap (/sitemap.xml)

// build the robots.txt (/robots.txt)
console.log('build');
}

public validateOptions(options: WritrOptions): void {
if (options.githubPath.length < 3) {
throw new Error('No github options provided');
}

if (options.siteDescription.length < 3) {
throw new Error('No site description options provided');
}

if (!options.siteTitle) {
throw new Error('No site title options provided');
}

if (!options.siteUrl) {
throw new Error('No site url options provided');
}
}
}
9 changes: 7 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export type GithubOptions = {
repo: string;
};

export type GithubData = {
releases: Record<string, unknown>;
contributors: Record<string, unknown>;
};

export class Github {
options = {
api: 'https://api.github.com',
Expand All @@ -17,7 +22,7 @@ export class Github {
this.parseOptions(options);
}

async getData(): Promise<Record<string, unknown>> {
async getData(): Promise<GithubData> {
const data = {
releases: {},
contributors: {},
Expand All @@ -27,7 +32,7 @@ export class Github {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.contributors = await this.getContributors();

return data;
return data as GithubData;
}

async getReleases(): Promise<any> {
Expand Down
70 changes: 70 additions & 0 deletions test/builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {expect, it, describe} from 'vitest';
import {WritrBuilder} from '../src/builder.js';
import {WritrOptions} from '../src/options.js';

describe('WritrBuilder', () => {
it('should initiate', () => {
const builder = new WritrBuilder();
expect(builder).toBeTruthy();
});
it('should initiate with options', () => {
const options = new WritrOptions();
const builder = new WritrBuilder(options);
expect(builder).toBeTruthy();
expect(builder.options).toBe(options);
});
it('should build', async () => {
const builder = new WritrBuilder();
const consoleLog = console.log;
let consoleMessage = '';
console.log = message => {
consoleMessage = message as string;
};

await builder.build();

expect(consoleMessage).toBe('build');

console.log = consoleLog;
});
it('should validate githubPath options', async () => {
const builder = new WritrBuilder();
const options = new WritrOptions();
try {
options.githubPath = '';
builder.validateOptions(options);
} catch (error: any) {
expect(error.message).toBe('No github options provided');
}
});
it('should validate siteDescription options', async () => {
const builder = new WritrBuilder();
const options = new WritrOptions();
try {
options.siteDescription = '';
builder.validateOptions(options);
} catch (error: any) {
expect(error.message).toBe('No site description options provided');
}
});
it('should validate site title options', async () => {
const builder = new WritrBuilder();
const options = new WritrOptions();
try {
options.siteTitle = '';
builder.validateOptions(options);
} catch (error: any) {
expect(error.message).toBe('No site title options provided');
}
});
it('should validate site url options', async () => {
const builder = new WritrBuilder();
const options = new WritrOptions();
try {
options.siteUrl = '';
builder.validateOptions(options);
} catch (error: any) {
expect(error.message).toBe('No site url options provided');
}
});
});
Loading