Skip to content

Commit

Permalink
adds an option to accept data directly from stdin (#59)
Browse files Browse the repository at this point in the history
* feat: accept data from stdin

Closes #54

* docs: update README to reflect new usage options
  • Loading branch information
humanchimp committed May 21, 2020
1 parent feacd1c commit 0da6b8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -13,9 +13,13 @@ Usage:
-h, --help output usage information
-v, --version output the version number
-o, --output <directory> Directory to output rendered templates, defaults to cwd
-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*
-e, --extension Output extension of generated files, defaults to html
-s, --stdout Output to standard output
-i, --stdin Receive data directly from stdin
-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 Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -22,11 +22,13 @@
"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 && npm run test:run:2 && npm run test:verify:2",
"test": "npm run test:run:1 && npm run test:verify:1 && npm run test:run:2 && npm run test:verify:2 && npm run test:run:3 && npm run test:verify:3",
"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",
"test:run:3": "cat ./test/data.json | node . -s -H handlebars-helper-br --stdin test/test-1.hbs > test/test-3.html",
"test:verify:3": "diff test/test-3.html test/verify-1.html",
"watch": "npm run prepublish -- -w"
},
"config": {
Expand Down Expand Up @@ -55,6 +57,7 @@
"babel-runtime": "^5.8.34",
"debug": "^2.2.0",
"fs-promise": "^0.3.1",
"get-stdin": "^8.0.0",
"glob-promise": "^1.0.4",
"handlebars": "^4.0.5",
"lodash.merge": "^3.3.2",
Expand Down
18 changes: 18 additions & 0 deletions src/index.js 100644 → 100755
Expand Up @@ -8,6 +8,7 @@ import resolveNode from 'resolve';
import { readFile, writeFile } from 'fs-promise';
import merge from 'lodash.merge';
import mkdirp from 'mkdirp-then';
import getStdin from 'get-stdin';
const debug = require('debug')('hbs');
function resolve(file, options) {
return new Promise((resolvePromise, reject) => resolveNode(file, options, (error, path) => {
Expand Down Expand Up @@ -86,6 +87,16 @@ export async function addObjectsToData(objects) {
return merge({}, ...dataSets.concat(fileContents));
}

export async function getStdinData() {
const text = await getStdin();
try {
debug(`Attempting to parse ${text} as JSON`);
return JSON.parse(text);
} catch (error) {
throw new Error(`stdin cannot be parsed as JSON`);
}
}

export async function renderHandlebarsTemplate(
files, outputDirectory = process.cwd(),
outputExtension = 'html', data = {}, stdout = false) {
Expand Down Expand Up @@ -117,13 +128,15 @@ if (require.main === module) {
'version',
'help',
'stdout',
'stdin',
],
alias: {
'v': 'version',
'h': 'help',
'o': 'output',
'e': 'extension',
's': 'stdout',
'i': 'stdin',
'D': 'data',
'P': 'partial',
'H': 'helper',
Expand All @@ -144,6 +157,7 @@ if (require.main === module) {
-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
-i, --stdin Receive data directly from stdin
-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)
Expand All @@ -170,6 +184,10 @@ if (require.main === module) {
debug('Setting up data', options.data);
setup.push(addObjectsToData(options.data).then((result) => data = result));
}
if (options.stdin) {
debug('Setting up stdin', options.stdin);
setup.push(getStdinData().then((stdinData) => data = stdinData));
}
Promise.all(setup)
.then(() => expandGlobList(options._))
.then((files) => renderHandlebarsTemplate(files, options.output, options.extension, data, options.stdout))
Expand Down

0 comments on commit 0da6b8e

Please sign in to comment.