Skip to content

Commit

Permalink
feat: add option to output to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Bussieres authored and keithamus committed Jun 22, 2016
1 parent 0d73aca commit 1ac3184
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
"prepublish": "babel src -d lib",
"pretest": "npm run lint",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"test": "npm run test:run:1 && npm run test:verify:1",
"test": "npm run test:run:1 && npm run test:verify:1 && npm run test:run:2 && npm run test:verify:2",
"test:run:1": "node . -H handlebars-helper-br -D ./test/data.json -o test test/test-1.hbs",
"test:verify:1": "diff test/test-1.html test/verify-1.html",
"test:run:2": "node . -s -H handlebars-helper-br -D ./test/data.json test/test-1.hbs > test/test-2.html",
"test:verify:2": "diff test/test-2.html test/verify-1.html",
"watch": "npm run prepublish -- -w"
},
"config": {
Expand Down
22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function addHandlebarsHelpers(files) {
debug(`${file} has a register function, registering with handlebars`);
handlebarsHelper.register(Handlebars);
} else {
console.log(`WARNING: ${file} does not export a 'register' function, cannot import`);
console.error(`WARNING: ${file} does not export a 'register' function, cannot import`);
}
});
}
Expand Down Expand Up @@ -85,13 +85,18 @@ export async function addObjectsToData(objects) {
return merge({}, ...dataSets.concat(fileContents));
}

export async function renderHandlebarsTemplate(files, outputDirectory = process.cwd(), data = {}) {
export async function renderHandlebarsTemplate(files, outputDirectory = process.cwd(), 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`);
await writeFile(path, Handlebars.compile(await readFile(file, 'utf8'))(data), 'utf8');
const htmlContents = Handlebars.compile(await readFile(file, 'utf8'))(data);
if (stdout) {
await process.stdout.write(htmlContents, 'utf8');
} else {
await writeFile(path, htmlContents, 'utf8');
}
debug(`Wrote ${path}`);
console.log(`Wrote ${path} from ${file}`);
console.error(`Wrote ${path} from ${file}`);
}));
}

Expand All @@ -106,21 +111,23 @@ if (require.main === module) {
boolean: [
'version',
'help',
'stdout',
],
alias: {
'v': 'version',
'h': 'help',
'o': 'output',
's': 'stdout',
'D': 'data',
'P': 'partial',
'H': 'helper',
},
});
debug('Parsed argv', options);
if (options.version) {
console.log(packageJson.version);
console.error(packageJson.version);
} else if (options.help || !options._ || !options._.length) {
console.log(`
console.error(`
Usage:
hbs --version
hbs --help
Expand All @@ -129,6 +136,7 @@ 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
-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
Expand Down Expand Up @@ -156,7 +164,7 @@ if (require.main === module) {
}
Promise.all(setup)
.then(() => expandGlobList(options._))
.then((files) => renderHandlebarsTemplate(files, options.output, data))
.then((files) => renderHandlebarsTemplate(files, options.output, data, options.stdout))
.catch((error) => {
console.error(error.stack || error);
process.exit(1);
Expand Down

0 comments on commit 1ac3184

Please sign in to comment.