Skip to content

Commit

Permalink
Merge pull request #85 from nporteschaikin/cleanup
Browse files Browse the repository at this point in the history
@nporteschaikin's cleanup and additions
  • Loading branch information
nporteschaikin committed Jun 9, 2015
2 parents e27ee70 + f5c3b7a commit 2a23530
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ module.exports = (function () {

if (init.before) {
self.emitter.emit('msg', 'running before hook');
return Promise.method(init.before)(utils);
return Promise.method(init.before)(utils, config);
}

}
Expand Down
25 changes: 15 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Promise = require('bluebird')
, fs = Promise.promisifyAll(require('fs'))
, exec = Promise.promisify(require('child_process').exec)
, ncp = Promise.promisify(require('ncp').ncp)
, ejs = require('ejs')
, path = require('path')
, _ = require('lodash')
Expand All @@ -18,24 +19,26 @@ module.exports = (function () {

var Utils = function (src, target) {

// set proxy values of src and target
this._src = src;
this._target = target;


/*
* Copy a file from one path relative to the src, to another relative to the target
* @param {String} from - the path to copy from, relative to the src.
* @param {String} to - the path to copy to.
* @return {Promise} - promise to copy
*/

this.copy = function (from, to) {
return copy(path.resolve(src, from), path.resolve(target, to));
}


this.src = {

/*
* The source path.
*/

path: _.clone(src),

/*
* Read a file, relative to the source.
* @param {String} from - the path to read from, relative to the source.
Expand All @@ -50,6 +53,12 @@ module.exports = (function () {

this.target = {

/*
* The target path.
*/

path: _.clone(target),

/*
* Copy a file from one path to another, relative
* to the target.
Expand Down Expand Up @@ -130,11 +139,7 @@ module.exports = (function () {
*/

function copy (from, to) {
return fs.readFileAsync(from).then(
function (buffer) {
return fs.writeFileAsync(to, buffer);
}
)
return ncp(from, to);
}

/*
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ So between this config file and the root folder, you should be able to make anyt
### Hooks
Sprout comes with the following events for you to write custom logic for. Each hook is passed a utilities object for manipulating files in your template. Each of these hooks accept A+ promises as return values. To use these events, export a function for the hook (or multiple hooks) of your choosing in your `init.js`:

- `before` - run before prompting for user input
- `before` - run before prompting for user input; is passed a configuration object as the second argument.
- `beforeRender` - run after the project configuration is set; is passed a configuration object as the second argument.
- `after` - run after rendering has completed; is passed a configuration object as the second argument.

The utilities object passed to each hook contains the following functions (each returns a promise):

function | description
:------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------
`utils.copy(from, to)` | copy a file at `from` (relative to the template's _base_ directory) to the path at `to` (relative to the template's _target_ directory).
`utils.copy(from, to)` | copy a file or directory recursively at `from` (relative to the template's _base_ directory) to the path at `to` (relative to the template's _target_ directory).
`utils.src.read(from)` | read a file at `from` (relative to the template's _base_ directory).
`utils.target.copy(from, to)` | copy a file at `from` (relative to the template's _target_ directory) to the path at `to` (relative to the template's _target_ directory).
`utils.target.read(from)` | read a file at `from` (relative to the template's _target_ directory).
Expand Down
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,15 @@ describe('utils',
}
)

it('should return the source path',
function (done) {
var fixture = path.join(utilsSrcFixturesPath, 'path')
, utils = new Utils(fixture, null);
utils.src.path.should.eq(fixture);
done();
}
)

}
)

Expand All @@ -2044,6 +2053,15 @@ describe('utils',
}
)

it('should return the target path',
function (done) {
var fixture = path.join(utilsTargetFixturesPath, 'path')
, utils = new Utils(fixture, null);
utils.src.path.should.eq(fixture);
done();
}
)

it('should copy from one path to another, relative to the target',
function (done) {
var fixture = path.join(utilsTargetFixturesPath, 'copy')
Expand Down

0 comments on commit 2a23530

Please sign in to comment.