Skip to content

Commit

Permalink
Merge pull request #63 from carrot/configFile
Browse files Browse the repository at this point in the history
Create configuration file in target
  • Loading branch information
nporteschaikin committed Mar 26, 2015
2 parents 6132628 + 32cf2aa commit bdd9f08
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 2 deletions.
77 changes: 77 additions & 0 deletions lib/configFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Promise = require('bluebird')
, fs = Promise.promisifyAll(require('fs'))
, path = require('path')
, _ = require('lodash');

module.exports = (function () {

/*
* Given a target path, returns a ConfigFile instance.
* @param {String} target - the target path
* @return {Function} - ConfigFile instance.
*/

var ConfigFile = function (target, defaults) {
var lstat;
if (!fs.existsSync(target)) {
throw new Error(target + ' does not exist');
} else {
lstat = fs.lstatSync(target);
if (!lstat.isDirectory()) {
throw new Error(target + ' is not a directory');
}
}
this.path = path.join(target, '.sproutrc');
this.config = _.extend({}, defaults);
}

ConfigFile.prototype = {

/*
* Read `configFile.path`
* @return {Promise} - a promise which returns the read configuration.
*/

read: function () {
var self = this;
return new Promise(
function (resolve, reject) {

/*
* If the config file exists, read it and
* resolve with the parsed output. if the
* file doesn't exist, simply resolve with
* the existing configuration.
*/

if (fs.existsSync(self.path)) {
return fs.readFileAsync(self.path, 'utf8').then(
function (output) {
self.config = JSON.parse(output);
return resolve(self.config);
}
)
} else {
return resolve(self.config);
}

}
);
},

/*
* Write `configFile.path` with the JSON
* set in `configFile.config`.
* @return {Promise} - a promise which returns the configuration.
*/

write: function () {
return fs.writeFileAsync(this.path, JSON.stringify(this.config))
.return(this.config);
}

}

return ConfigFile;

}.call(this));
15 changes: 15 additions & 0 deletions lib/template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Utils = require('./utils')
, ConfigFile = require('./configFile')
, helpers = require('./helpers')
, Promise = require('bluebird')
, which = require('which')
Expand Down Expand Up @@ -449,6 +450,20 @@ module.exports = (function () {
self.emitter.emit('msg', 'copying files in root to target');
return ncp(self.root, target);

}
).then(
function () {

var configFile = new ConfigFile(target, {name: self.name});

/*
* Write configuration file with
* the template's name.
*/

self.emitter.emit('msg', 'creating sprout config file at target');
return configFile.write();

}
).then(
function () {
Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/configFile/read/read/.sproutrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "foo"}
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/template/init/configFile/src/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
Empty file.
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--reporter spec
--timeout 20000
--timeout 30000
150 changes: 149 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Sprout = require('./../lib')
, apiRemove = require('./../lib/api/remove')
, Template = require('./../lib/template')
, Utils = require('./../lib/utils')
, ConfigFile = require('./../lib/configFile')
, CLI = require('./../lib/cli')
, helpers = require('./../lib/helpers')
, chai = require('chai')
Expand Down Expand Up @@ -166,7 +167,7 @@ describe('sprout',
function () {
rimraf(target, done);
}
);
)
}
)

Expand Down Expand Up @@ -1094,6 +1095,35 @@ describe('template',
}
)

it('should create target configuration file',
function (done) {
var name = 'configFile'
, fixture = path.join(initTemplateFixturesPath, name)
, src = path.join(fixture, 'src')
, target = path.join(fixture, 'target')
, template = new Template(sprout, name, src);
return gitInit(src).then(
function () {
return template.save();
}
).then(
function (template) {
fs.existsSync(template.path).should.be.true;
return template.init(target);
}
).then(
function (template) {
JSON.parse(fs.readFileSync(path.join(target, '.sproutrc'), 'utf8')).name.should.eq(name);
return template.remove();
}
).then(
function () {
return rimraf(target, done);
}
)
}
)

it('should ask questions if questionnaire is passed',
function (done) {
var name = 'questionnaire'
Expand Down Expand Up @@ -1994,6 +2024,124 @@ describe('helpers',
}
)

describe('configFile',
function () {

var configFileFixturesPath;

before(
function () {
configFileFixturesPath = path.join(fixturesPath, 'configFile');
}
)

it('should construct with a valid path',
function (done) {
var p = path.join(configFileFixturesPath, 'validPath')
, configFile = new ConfigFile(p);
configFile.path.should.eq(path.join(p, '.sproutrc'));
configFile.config.should.be.instanceof(Object);
done();
}
)

it('should set defaults',
function (done) {
var p = path.join(configFileFixturesPath, 'setDefaults')
, configFile = new ConfigFile(p, {foo: 'bar'});
configFile.path.should.eq(path.join(p, '.sproutrc'));
configFile.config.foo.should.eq('bar');
done();
}
)

it('should throw an error if the path doesn\'t exist',
function (done) {
var p = 'foo/bar/foo/bar/foo/bar/doge';
(function () { return new ConfigFile(p) }).should.throw(p + ' does not exist');
done();
}
)

it('should throw if path is not a directory',
function (done) {
var p = path.join(configFileFixturesPath, 'notDirectory.foo');
(function () { return new ConfigFile(p) }).should.throw(p + ' is not a directory');
done();
}
)

describe('read',
function () {

var configFileReadFixturesPath;

before(
function () {
configFileReadFixturesPath = path.join(configFileFixturesPath, 'read');
}
)

it('should read a configuration file',
function (done) {
var fixture = path.join(configFileReadFixturesPath, 'read')
, configFile = new ConfigFile(fixture);
return configFile.read().then(
function (config) {
config.name.should.eq('foo');
done();
}
)
}
)

it('should resolve with empty config if config file doesn\'t exist',
function (done) {
var fixture = path.join(configFileReadFixturesPath, 'noConfig')
, configFile = new ConfigFile(fixture);
return configFile.read().then(
function (config) {
config.should.be.empty;
done();
}
)
}
)

}
)

describe('write',
function () {

var configFileWriteFixturesPath;

before(
function () {
configFileWriteFixturesPath = path.join(configFileFixturesPath, 'write');
}
)

it('should write a configuration file',
function (done) {
var fixture = path.join(configFileWriteFixturesPath, 'write')
, configFile = new ConfigFile(fixture, {foo: 'bar'});
return configFile.write().then(
function () {
JSON.parse(fs.readFileSync(configFile.path, 'utf8')).foo.should.eq('bar');
fs.unlinkSync(configFile.path);
done();
}
)
}
)

}
)

}
)

/*
* Helper function for initializing a git repository
* in the specified directory.
Expand Down

0 comments on commit bdd9f08

Please sign in to comment.