Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Making it possible to define dynamic variables #1

Merged
merged 3 commits into from

2 participants

@Gozala

It's quite annoying to specify user / email every time I create package so I thought why not use git to read this data from config. This change makes it possible to define dynamic variables as functions that can run 'git --bare config --global user.name' to read user out and pass it back to the ngen.

Change also adds template that uses this functionality to read out user name and email from config.

@Gozala

@visionmedia can you please respond in some form, to let me know if you're willing to pull this change in ?

@tj
Owner
tj commented

hey sorry, didn't see this. i will take a look

@tj
Owner
tj commented

looks good, merging

@tj tj closed this
@tj tj reopened this
@tj tj merged commit 5b656f1 into tj:master
@Gozala

Awesome thanks @visionmedia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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);
View
5 package.json
@@ -7,8 +7,11 @@
"url": "git://github.com/visionmedia/ngen.git"
},
"author": "TJ Holowaychuk <tj@vision-media.ca>",
+ "contributors": [
+ "Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)"
+ ],
"bin": { "ngen": "./bin/ngen" },
"engines": {
"node": "*"
}
-}
+}
View
3  templates/git/content/.npmignore
@@ -0,0 +1,3 @@
+support
+test
+examples
View
5 templates/git/content/History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
View
5 templates/git/content/Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @echo "populate me"
+
+.PHONY: test
View
29 templates/git/content/Readme.md
@@ -0,0 +1,29 @@
+
+# {{project}}
+
+ {{description}}
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 {{name}} &lt;{{email}}&gt;
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
2  templates/git/content/index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/{{project}}');
View
12 templates/git/content/lib/{{project}}.js
@@ -0,0 +1,12 @@
+
+/*!
+ * {{project}}
+ * Copyright(c) 2011 {{name}} <{{email}}>
+ * MIT Licensed
+ */
+
+/**
+ * Library version.
+ */
+
+exports.version = '0.0.1';
View
10 templates/git/content/package.json
@@ -0,0 +1,10 @@
+{
+ "name": "{{project}}",
+ , "version": "0.0.1",
+ , "description": "{{description}}",
+ , "keywords": [],
+ , "author": "{{name}} <{{email}}>",
+ , "dependencies": {}
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
View
13 templates/git/content/test/{{project}}.test.js
@@ -0,0 +1,13 @@
+
+/**
+ * Module dependencies.
+ */
+
+var mini = require('{{project}}')
+ , should = require('should');
+
+module.exports = {
+ 'test .version': function(){
+ {{project}}.version.should.match(/^\d+\.\d+\.\d+$/);
+ }
+};
View
22 templates/git/index.js
@@ -0,0 +1,22 @@
+var spawn = require('child_process').spawn;
+
+exports.variables = {
+ project: 'Project name: ',
+ description: 'Project description: ',
+ name: function(values, callback) {
+ spawn('git', [
+ '--bare',
+ 'config',
+ '--global',
+ 'user.name'
+ ]).stdout.once('data', callback);
+ },
+ email: function(values, callback) {
+ spawn('git', [
+ '--bare',
+ 'config',
+ '--global',
+ 'user.email'
+ ]).stdout.once('data', callback);
+ }
+};
Something went wrong with that request. Please try again.