Skip to content

Commit

Permalink
feat: Adding support for output extension (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexus49 authored and keithamus committed Aug 28, 2018
1 parent 017c5f4 commit a614a3e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/index.js
Expand Up @@ -85,10 +85,12 @@ export async function addObjectsToData(objects) {
return merge({}, ...dataSets.concat(fileContents));
}

export async function renderHandlebarsTemplate(files, outputDirectory = process.cwd(), data = {}, stdout = false) {
export async function renderHandlebarsTemplate(
files, outputDirectory = process.cwd(),
outputExtension = 'html', data = {}, stdout = false) {
await Promise.all(files.map(async function renderTemplate(file) {
debug(`Rendering template ${file} with data`, data);
const path = resolvePath(outputDirectory, `${basename(file, extname(file))}.html`);
const path = resolvePath(outputDirectory, `${basename(file, extname(file))}.${outputExtension}`);
const htmlContents = Handlebars.compile(await readFile(file, 'utf8'))(data);
if (stdout) {
await process.stdout.write(htmlContents, 'utf8');
Expand All @@ -104,6 +106,7 @@ if (require.main === module) {
const options = minimist(process.argv.slice(2), {
string: [
'output',
'extension',
'partial',
'helper',
'data',
Expand All @@ -117,6 +120,7 @@ if (require.main === module) {
'v': 'version',
'h': 'help',
'o': 'output',
'e': 'extension',
's': 'stdout',
'D': 'data',
'P': 'partial',
Expand All @@ -136,9 +140,11 @@ if (require.main === module) {
-h, --help output usage information
-v, --version output the version number
-o, --output <directory> Directory to output rendered templates, defaults to cwd
-e, --extension Output extension of generated files, defaults to html
-s, --stdout Output to standard output
-P, --partial <glob>... Register a partial (use as many of these as you want)
-H, --helper <glob>... Register a helper (use as many of these as you want)
-D, --data <glob|json>... Parse some data
Examples:
Expand All @@ -164,7 +170,7 @@ if (require.main === module) {
}
Promise.all(setup)
.then(() => expandGlobList(options._))
.then((files) => renderHandlebarsTemplate(files, options.output, data, options.stdout))
.then((files) => renderHandlebarsTemplate(files, options.output, options.extension, data, options.stdout))
.catch((error) => {
console.error(error.stack || error);
process.exit(1);
Expand Down

0 comments on commit a614a3e

Please sign in to comment.