Skip to content

Commit

Permalink
Reorganizing library to support plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Oehlman committed Jul 10, 2012
1 parent 7417536 commit 69c32e7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
File renamed without changes.
File renamed without changes.
38 changes: 5 additions & 33 deletions lib/scaffolder.js → index.js
Expand Up @@ -4,14 +4,13 @@ var async = require('async'),
util = require('util'),
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
nopt = require('nopt'),
ncp = require('ncp').ncp,
out = require('out'),
read = require('read'),
_ = require('underscore'),
findPackage = require('./helpers/find-package'),
filters = require('./helpers/filters'),
pluginFiles = fs.readdirSync(path.resolve(__dirname, 'plugins')).filter(filters.jsOnly),
rePathDelim = /[\/\\]/,
_exists = fs.exists || path.exists,

Expand Down Expand Up @@ -89,37 +88,6 @@ function Scaffolder(opts) {

util.inherits(Scaffolder, events.EventEmitter);

Scaffolder.prototype.copy = function(src, dest, callback) {
var scaffolder = this;

// create a dummy callback if we don't have one
callback = callback || function() {};

// get the source path for the package
this.getPath(function(basePath) {
src = path.resolve(basePath, src);

debug('attempting to copy files from ' + src + ' ==> ' + dest);

// if the path exists, copy the files
_exists(src, function(exists) {
if (exists) {
mkdirp(dest, function(err) {
if (err) {
callback(err);
}
else {
ncp(src, dest, callback);
}
});
}
else {
callback(new Error('no source files'));
}
});
});
};

Scaffolder.prototype.getPath = function(callback) {
var scaffolder = this;

Expand Down Expand Up @@ -294,6 +262,10 @@ Scaffolder.prototype.run = function(name, opts, callback) {
command.run.call(this, opts, callback);
};

// patch in plugins for the prototype
pluginFiles.forEach(function(pluginFile) {
Scaffolder.prototype[path.basename(pluginFile, '.js')] = require('./plugins/' + pluginFile);
});

exports = module.exports = function(opts, initFn) {
var scaffolder,
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -6,20 +6,22 @@
],
"author": "Damon Oehlman <damon.oehlman@sidelab.com>",
"version": "0.5.8",
"main": "lib/scaffolder",
"engines": {
"node": ">= 0.6.x < 0.9.0"
},
"dependencies": {
"async": "0.1.x",
"debug": "*",
"mkdirp": "0.3.x",
"ncp": "0.2.x",
"nopt": "1.0.x",
"out": "0.4.x",
"read": "0.1.x",
"squirrel": "0.1.x",
"underscore": "1.3.x"
},
"pluginDependencies": {
"mkdirp": "0.3.x",
"ncp": "0.2.6"
},
"devDependencies": {
"mocha": "1.2.x",
"expect.js": "0.1.x"
Expand Down
36 changes: 36 additions & 0 deletions plugins/copy.js
@@ -0,0 +1,36 @@
var squirrel = require('squirrel');

module.exports = function(src, dest, callback) {
var scaffolder = this;

// create a dummy callback if we don't have one
callback = callback || function() {};

squirrel(['ncp', 'mkdirp'], { allowInstall: 'prompt' }, function(err, ncp, mkdirp) {
if (err) return callback(err);

// get the source path for the package
scaffolder.getPath(function(basePath) {
src = path.resolve(basePath, src);

debug('attempting to copy files from ' + src + ' ==> ' + dest);

// if the path exists, copy the files
_exists(src, function(exists) {
if (exists) {
mkdirp(dest, function(err) {
if (err) {
callback(err);
}
else {
ncp(src, dest, callback);
}
});
}
else {
callback(new Error('no source files'));
}
});
});
});
};

0 comments on commit 69c32e7

Please sign in to comment.