Skip to content

Commit

Permalink
Merge pull request tj#7 from uber/testability
Browse files Browse the repository at this point in the history
Removed hard coded stuff from index.js for more testability
  • Loading branch information
David Ellis committed Jan 9, 2014
2 parents 4fd1e80 + 77fd029 commit 201b6ee
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
16 changes: 16 additions & 0 deletions Readme.md
Expand Up @@ -54,6 +54,22 @@ structure:
./package.json
./README.md

## Docs

`uber-ngen` can also be called directly

```js
var Template = require('uber-ngen')

var t = Template('name-of-template', {
templates: 'folder location of templates'
})
t.init('target location to write on disk', function (err) {
// scaffolded the template to the location
// calling init() will prompt on STDIN.
})
```

## License

(The MIT License)
Expand Down
12 changes: 10 additions & 2 deletions bin/ngen
Expand Up @@ -20,6 +20,14 @@ var description = argv._[1];
// create template
var tmpl = new Template(template, {
templates: templates,
description: description
description: description,
logger: console
});
tmpl.init(dest, function (err) {
if (err) {
console.error(err.message);
return process.exit(1);
}

process.stdin.destroy();
});
tmpl.init(dest);
75 changes: 40 additions & 35 deletions index.js
Expand Up @@ -3,6 +3,8 @@ var join = require('path').join;
var prompt = require('promptly').prompt;
var chalk = require('chalk');

function noop() {}

/**
* Initialize a new `Template` with the given `name`.
*
Expand All @@ -18,6 +20,9 @@ function Template(name, opts) {
this.templates = opts.templates;
this.description = opts.description;
this.name = name;
this.logger = opts.logger || {
log: noop
};
this.path = join(this.templates, name);
this.contentPath = this.path + '/content';
this.mod = require(this.path);
Expand All @@ -34,25 +39,24 @@ function Template(name, opts) {
* @api private
*/

Template.prototype.init = function(dest){
Template.prototype.init = function(dest, callback) {
var self = this;
var vars = this.mod;
var vars = self.mod;
var keys = Object.keys(vars);

self.values.project = dest;
self.values.description = this.description;
self.values.description = self.description;
self.dest = dest;
// print new line for pretties.
console.log();
self.logger.log();

function parseLocal() {
var desc;
var key = keys.shift();

function done(err, value) {
if (err) {
console.error(err.message);
return process.exit(1);
return callback(err);
}

self.values[key] = String(value).trim();
Expand All @@ -67,11 +71,11 @@ Template.prototype.init = function(dest){
prompt(chalk.gray(' ' + desc.trim()), done);
}
} else if (key === undefined) {
process.stdin.destroy();
if (!self.dest) {
self.dest = self.values.project;
}
self.create();
if (callback) callback();
} else {
done(null, self.values[key]);
}
Expand All @@ -87,44 +91,41 @@ Template.prototype.init = function(dest){
* @api private
*/

Object.defineProperty(Template.prototype, 'files', {
get: function() {
var self = this;
var files = [];

(function readdirs(dir) {
fs.readdirSync(dir).forEach(function(file){
files.push(file = dir + '/' + file);
var stat = fs.statSync(file);
if (stat.isDirectory()) {
self.directories[file] = true;
readdirs(file);
}
});
})(this.contentPath);

return files;
}
});
Template.prototype.files = function() {
var self = this;
var files = [];

(function readdirs(dir) {
fs.readdirSync(dir).forEach(function(file){
files.push(file = dir + '/' + file);
var stat = fs.statSync(file);
if (stat.isDirectory()) {
self.directories[file] = true;
readdirs(file);
}
});
})(self.contentPath);

return files;
};

/**
* Create the template files.
*
* @api private
*/

Template.prototype.create = function(){
// dest
Template.prototype.create = function() {
var self = this;

try {
fs.mkdirSync(this.dest, 0775);
fs.mkdirSync(self.dest, 0775);
} catch (err) {
// ignore
}

var self = this;
console.log();
self.files.forEach(function(file){
self.logger.log();
self.files().forEach(function(file){
var uri = self.parse(file);
var out = join(self.dest, uri.replace(self.contentPath, ''));

Expand All @@ -134,7 +135,9 @@ Template.prototype.create = function(){
if (self.directories[file]) {
try {
fs.mkdirSync(out, 0775);
console.log(' \033[90mcreate :\033[0m \033[36m%s\033[0m', out);
self.logger.log(
chalk.gray(' create :'),
chalk.cyan(out));
} catch (err) {
// ignore
}
Expand All @@ -143,11 +146,13 @@ Template.prototype.create = function(){
if (!fs.existsSync(out)) {
var str = self.parse(fs.readFileSync(file, 'utf8'));
fs.writeFileSync(out, str);
console.log(' \033[90mcreate :\033[0m \033[36m%s\033[0m', out);
self.logger.log(
chalk.gray(' create :'),
chalk.cyan(out));
}
}
});
console.log();
self.logger.log();
};

/**
Expand Down

0 comments on commit 201b6ee

Please sign in to comment.