From 32cf2aa15d49ab463f2bd7845a7f8bce36cd4345 Mon Sep 17 00:00:00 2001 From: Noah Portes Chaikin Date: Thu, 26 Mar 2015 11:19:55 -0400 Subject: [PATCH] Create configuration file in target. --- lib/configFile.js | 77 +++++++++ lib/template.js | 15 ++ test/fixtures/configFile/notDirectory.foo | 0 test/fixtures/configFile/read/noConfig/.keep | 0 test/fixtures/configFile/read/read/.sproutrc | 1 + test/fixtures/configFile/setDefaults/.keep | 0 test/fixtures/configFile/validPath/.keep | 0 test/fixtures/configFile/write/write/.keep | 0 .../template/init/configFile/src/init.js | 1 + .../template/init/configFile/src/root/.keep | 0 test/mocha.opts | 2 +- test/test.js | 150 +++++++++++++++++- 12 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 lib/configFile.js create mode 100644 test/fixtures/configFile/notDirectory.foo create mode 100644 test/fixtures/configFile/read/noConfig/.keep create mode 100644 test/fixtures/configFile/read/read/.sproutrc create mode 100644 test/fixtures/configFile/setDefaults/.keep create mode 100644 test/fixtures/configFile/validPath/.keep create mode 100644 test/fixtures/configFile/write/write/.keep create mode 100644 test/fixtures/template/init/configFile/src/init.js create mode 100644 test/fixtures/template/init/configFile/src/root/.keep diff --git a/lib/configFile.js b/lib/configFile.js new file mode 100644 index 0000000..e11dd78 --- /dev/null +++ b/lib/configFile.js @@ -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)); diff --git a/lib/template.js b/lib/template.js index 75b7d89..db24dc7 100644 --- a/lib/template.js +++ b/lib/template.js @@ -1,4 +1,5 @@ var Utils = require('./utils') + , ConfigFile = require('./configFile') , helpers = require('./helpers') , Promise = require('bluebird') , which = require('which') @@ -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 () { diff --git a/test/fixtures/configFile/notDirectory.foo b/test/fixtures/configFile/notDirectory.foo new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/configFile/read/noConfig/.keep b/test/fixtures/configFile/read/noConfig/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/configFile/read/read/.sproutrc b/test/fixtures/configFile/read/read/.sproutrc new file mode 100644 index 0000000..7c4c2f5 --- /dev/null +++ b/test/fixtures/configFile/read/read/.sproutrc @@ -0,0 +1 @@ +{"name": "foo"} diff --git a/test/fixtures/configFile/setDefaults/.keep b/test/fixtures/configFile/setDefaults/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/configFile/validPath/.keep b/test/fixtures/configFile/validPath/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/configFile/write/write/.keep b/test/fixtures/configFile/write/write/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/template/init/configFile/src/init.js b/test/fixtures/template/init/configFile/src/init.js new file mode 100644 index 0000000..f053ebf --- /dev/null +++ b/test/fixtures/template/init/configFile/src/init.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/fixtures/template/init/configFile/src/root/.keep b/test/fixtures/template/init/configFile/src/root/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/mocha.opts b/test/mocha.opts index d79e152..ed90ec0 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,2 +1,2 @@ --reporter spec ---timeout 20000 +--timeout 30000 diff --git a/test/test.js b/test/test.js index 5e0f288..7db069b 100644 --- a/test/test.js +++ b/test/test.js @@ -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') @@ -166,7 +167,7 @@ describe('sprout', function () { rimraf(target, done); } - ); + ) } ) @@ -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' @@ -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.