@speedcubing/cookie
Version:
Speedcubing Online's UI design.
47 lines (37 loc) • 1.27 kB
text/typescript
import * as ejs from 'ejs';
import * as fs from 'fs';
import * as path from 'path';
/**
* Renders a component from the code base.
* @param file the filename/path of the file to render
* @param values the values to use while templating
*/
export function render(file: string, values: { [key: string]: any }): string {
file = file.endsWith('.ejs') ? file : file + '.ejs';
const filepath: string = path.resolve(path.join(__dirname, '../views', file));
// Check if file exists
if (fs.existsSync(filepath)) {
// Render file
const body: string = fs.readFileSync(filepath).toString();
// Add oreo library to values
values.oreo = {
render
};
return ejs.render(body, values);
}
return null;
}
export function getStaticFolder(): string {
return path.resolve(path.join(__dirname, '../public'));
}
export function getViewFolder(): string {
return path.resolve(path.join(__dirname, '../views'));
}
export function engine(filepath, data, callback) {
filepath = path.resolve(path.join(__dirname, '../views', filepath.endsWith('.ejs') ? filepath : filepath + '.ejs'));
fs.readFile(filepath, (err, contents) => {
if (err)
return callback(err, null);
callback(null, ejs.render(contents), data);
});
}