Skip to content

Commit

Permalink
Merge pull request #149 from carrot/refactors
Browse files Browse the repository at this point in the history
Refactors
  • Loading branch information
Jeff Escalante committed Mar 24, 2016
2 parents 907a5dc + f888089 commit 8cc2e1f
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 598 deletions.
4 changes: 2 additions & 2 deletions lib/api/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Template from '../template'
* @return {Promise} - Promise for Template instance.
*/
export default function (sprout, name, src) {
const template = new Template(sprout, name, src)
return template.save().then((template) => {
const template = new Template({ sprout: sprout, name: name, src: src })
return template.save().then(function (template) {
sprout.templates[name] = template
})
}
4 changes: 1 addition & 3 deletions lib/api/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import W from 'when'
export default function (sprout, name) {
const template = sprout.templates[name]
if (template) {
return template.remove().then(() => {
delete sprout.templates[name]
})
return template.remove().then((_) => delete sprout.templates[name])
}
return W.reject(new Error(`template ${name} does not exist`))
}
29 changes: 12 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,27 @@ import api from './api'
* @param {String} basePath - Path to directory containing Sprout templates.
* @return {Function} - Sprout instance.
*/
class Sprout {
class Sprout extends EventEmitter {

constructor (basePath) {
super()
this.templates = {}
this.path = basePath
this.emitter = new EventEmitter()

var lstat, dirs, name, dir
if (!fs.existsSync(this.path)) {
throw new Error(this.path + ' does not exist')
} else {
lstat = fs.lstatSync(this.path)
if (!lstat.isDirectory()) {
throw new Error(this.path + ' is not a directory')
}
try { fs.accessSync(this.path) } catch (err) {
throw new Error(`${this.path} does not exist`)
}

if (!fs.lstatSync(this.path).isDirectory()) {
throw new Error(`${this.path} is not a directory`)
}

dirs = fs.readdirSync(this.path)
for (var i = 0; i < dirs.length; i++) {
name = dirs[i]
dir = path.join(this.path, name)
lstat = fs.lstatSync(dir)
fs.readdirSync(this.path).map((name) => {
const lstat = fs.lstatSync(path.join(this.path, name))
if (lstat.isDirectory() || lstat.isSymbolicLink()) {
this.templates[name] = new Template(this, name)
this.templates[name] = new Template({ sprout: this, name: name })
}
}
})
}

/*
Expand Down
Loading

0 comments on commit 8cc2e1f

Please sign in to comment.