Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding ability to read variables dynamically.

  • Loading branch information...
commit 744bd0a746a5472e51b439964f53cdcb21c00f18 1 parent 4e0ee2a
@Gozala Gozala authored
Showing with 21 additions and 7 deletions.
  1. +21 −7 bin/ngen
View
28 bin/ngen
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+// vim:ft=javascript:
/**
* Module dependencies.
@@ -87,14 +88,21 @@ Template.prototype.init = function(dest){
function next() {
var desc
, key = keys.shift();
+
+ function yield(value) {
+ self.values[key] = String(value).trim();
+ next();
+ }
+
if (key) {
desc = vars[key];
- process.stdout.write(' \033[90m' + desc + '\033[0m');
- process.stdin.setEncoding('utf8');
- process.stdin.once('data', function(str){
- self.values[key] = str.trim();
- next();
- }).resume();
+ // If `desc` is a function executing it so that it can read a value.
+ if (typeof desc === "function") {
+ desc(self.values, yield);
+ // Otherwise falling back to CLI.
+ } else {
+ ask(desc, yield);
+ }
} else {
process.stdin.destroy();
self.create();
@@ -188,6 +196,12 @@ function requireArgument(flag) {
}
}
+function ask(desc, callback) {
+ process.stdout.write(' \033[90m' + desc + '\033[0m');
+ process.stdin.setEncoding('utf8');
+ process.stdin.once('data', callback).resume();
+}
+
// parse arguments
var arg;
@@ -219,4 +233,4 @@ try {
// ignore
}
-tmpl.init(dest);
+tmpl.init(dest);
Please sign in to comment.
Something went wrong with that request. Please try again.