Skip to content

Commit

Permalink
Merge pull request #146 from carrot/es6
Browse files Browse the repository at this point in the history
Convert to ES6
  • Loading branch information
Jeff Escalante committed Mar 17, 2016
2 parents 1172e7a + ef32b86 commit 232d337
Show file tree
Hide file tree
Showing 15 changed files with 1,201 additions and 1,520 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-2"]
}
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
build:
cp -R lib src
babel src --out-dir lib

unbuild:
rm -rf lib
mv src lib

publish:
make build
npm publish .
make unbuild
4 changes: 2 additions & 2 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ If you are opening an issue about a bug, make sure that you include clear steps
### Getting Set Up

- Clone the project down
- Make sure [nodejs](http://nodejs.org) has been installed and is above version `0.10.x`
- Make sure [nodejs](http://nodejs.org) has been installed and is above version `4.x`
- Run `npm install`
- Put in work

Expand All @@ -21,7 +21,7 @@ To run the test suite, make sure you have installed mocha (`npm install mocha -g

### Code Style

To keep a consistant coding style in the project, we're using [Polar Mobile's guide](https://github.com/polarmobile/coffeescript-style-guide), with one difference begin that much of this project uses `under_scores` rather than `camelCase` for variable naming. For any inline documentation in the code, we're using [JSDoc](http://usejsdoc.org/).
To keep a consistent coding style in the project, we use [standard](http://standardjs.com). For any inline documentation in the code, we're using [JSDoc](http://usejsdoc.org/).

### Commit Cleanliness

Expand Down
15 changes: 6 additions & 9 deletions lib/api/add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Template = require('./../template')
import Template from '../template'

/*
* Create a new template.
Expand All @@ -7,12 +7,9 @@ var Template = require('./../template')
* @param {String} src - path or URL to template source.
* @return {Promise} - Promise for Template instance.
*/

module.exports = function (sprout, name, src) {
var template = new Template(sprout, name, src)
return template.save().then(
function (template) {
sprout.templates[name] = template
}
)
export default function (sprout, name, src) {
const template = new Template(sprout, name, src)
return template.save().then((template) => {
sprout.templates[name] = template
})
}
11 changes: 11 additions & 0 deletions lib/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import add from './add'
import remove from './remove'
import init from './init'
import run from './run'

export default {
add: add,
remove: remove,
init: init,
run: run
}
11 changes: 5 additions & 6 deletions lib/api/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Promise = require('bluebird')
import Promise from 'bluebird'

/*
* Initialize an existing template at a
Expand All @@ -8,9 +8,8 @@ var Promise = require('bluebird')
* @param {String} target - The path to save the template to.
* @return {Promise} - Promise for Template instance.
*/

module.exports = function (sprout, name, target, options) {
var template = sprout.templates[name]
if (template) return template.init(target, options)
return Promise.reject(new Error('template ' + name + ' does not exist'))
export default function (sprout, name, target, options) {
const template = sprout.templates[name]
if (template) { return template.init(target, options) }
return Promise.reject(new Error(`template ${name} does not exist`))
}
17 changes: 7 additions & 10 deletions lib/api/remove.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
var Promise = require('bluebird')
import Promise from 'bluebird'

/*
* Remove a template.
* @param {Function} sprout - Sprout instance.
* @param {String} name - name of template to remove.
* @return {Promise} - Promise for Template instance.
*/

module.exports = function (sprout, name) {
var template = sprout.templates[name]
export default function (sprout, name) {
const template = sprout.templates[name]
if (template) {
return template.remove().then(
function () {
delete sprout.templates[name]
}
)
return template.remove().then(() => {
delete sprout.templates[name]
})
}
return Promise.reject(new Error('template ' + name + ' does not exist'))
return Promise.reject(new Error(`template ${name} does not exist`))
}
11 changes: 5 additions & 6 deletions lib/api/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Promise = require('bluebird')
import Promise from 'bluebird'

/*
* Run a generator in an existing template
Expand All @@ -10,9 +10,8 @@ var Promise = require('bluebird')
* @param {Array} args - An array of arguments to pass to the generator.
* @return {Promise} - Promise for Template instance.
*/

module.exports = function (sprout, name, target, generator, args) {
var template = sprout.templates[name]
if (template) return template.run(target, generator, args)
return Promise.reject(new Error('template ' + name + ' does not exist'))
export default function (sprout, name, target, generator, args) {
const template = sprout.templates[name]
if (template) { return template.run(target, generator, args) }
return Promise.reject(new Error(`template ${name} does not exist`))
}
18 changes: 7 additions & 11 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
var isBinaryFile = require('isbinaryfile')
import isBinary from 'isbinaryfile'

/*
* Determines whether a string is a
* valid Git URL tests against a
* regular expression.
* Determines whether a string is a valid Git URL tests against a regular
* expression.
* @param {String} str - String to test
* @return {Boolean} - is `str` a git URL
*/

exports.isGitURL = function (str) {
export function isGitUrl (str) {
return /(?:[A-Za-z0-9]+@|https?:\/\/)[A-Za-z0-9.]+(?::|\/)[A-Za-z0-9\/]+(?:\.git)?/.test(str)
}

/*
* A decent check to determine whether
* a given file is a binary.
* A decent check to determine whether a given file is a binary.
* @param {Array} src - Path the file
* @return {Boolean} - whether given file is binary
*/

exports.isBinaryFile = function (src) {
return isBinaryFile.sync(src)
export function isBinaryFile (src) {
return isBinary.sync(src)
}
82 changes: 37 additions & 45 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
var Template = require('./template')
var Emitter = require('events').EventEmitter
var fs = require('fs')
var path = require('path')
import Template from './template'
import {EventEmitter} from 'events'
import fs from 'fs'
import path from 'path'
import api from './api'

/*
* Given a base path, returns a Sprout instance.
* @param {String} basePath - Path to directory containing Sprout templates.
* @return {Function} - Sprout instance.
*/
class Sprout {

var Sprout = function (basePath) {
this.templates = {}
this.path = basePath
this.emitter = new Emitter()
constructor (basePath) {
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')
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')
}
}
}

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)
if (lstat.isDirectory() || lstat.isSymbolicLink()) {
this.templates[name] = new Template(this, name)
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)
if (lstat.isDirectory() || lstat.isSymbolicLink()) {
this.templates[name] = new Template(this, name)
}
}
}
}

Sprout.prototype = {

/*
* Create a new template.
* @param {String} name - name to save template as.
* @param {String} src - path or URL to template source.
* @return {Promise} - Promise for Sprout instance.
*/

add: function (name, src) {
return require('./api/add')(this, name, src)
.return(this)
},
add (name, src) {
return api.add(this, name, src).return(this)
}

/*
* Remove a template.
* @param {String} name - name of template to remove.
* @return {Promise} - Promise for Sprout instance.
*/

remove: function (name) {
return require('./api/remove')(this, name)
.return(this)
},
remove (name) {
return api.remove(this, name).return(this)
}

/*
* Initialize an existing template at a
Expand All @@ -68,11 +64,9 @@ Sprout.prototype = {
* @param {Object} options - Initialization options.
* @return {Promise} - Promise for Sprout instance.
*/

init: function (name, target, options) {
return require('./api/init')(this, name, target, options)
.return(this)
},
init (name, target, options) {
return api.init(this, name, target, options).return(this)
}

/*
* Run a generator in an existing template
Expand All @@ -83,12 +77,10 @@ Sprout.prototype = {
* @param {Array} args - An array of arguments to pass to the generator.
* @return {Promise} - Promise for Sprout instance.
*/

run: function (name, target, generator, args) {
return require('./api/run')(this, name, target, generator, args)
run (name, target, generator, args) {
return api.run(this, name, target, generator, args)
.return(this)
}

}

module.exports = Sprout
Loading

0 comments on commit 232d337

Please sign in to comment.