From b9661b44347eda00d048957045f4463c6c0709bf Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Sun, 19 Apr 2015 12:58:18 +0200 Subject: [PATCH] refactor(core): core deps, fix deprecations And remove method calls in templates. --- anim/index.js | 6 +++--- api/index.js | 10 ++++++---- api/templates/mongo/controller.js | 10 +++++----- app/index.js | 12 ++++++++---- .../server/config/environment/development.js | 2 +- .../server/config/environment/production.js | 2 +- app/templates/server/config/environment/test.js | 2 +- directive/index.js | 6 ++++-- factory/index.js | 6 +++--- filter/index.js | 5 +++-- font/index.js | 10 ++++++++-- package.json | 6 +++++- route/index.js | 8 +++++--- service/index.js | 6 +++--- style/index.js | 1 + test/app.js | 3 ++- util.js | 17 ++++++++++++----- 17 files changed, 71 insertions(+), 41 deletions(-) diff --git a/anim/index.js b/anim/index.js index 3da46ed..b9da80b 100644 --- a/anim/index.js +++ b/anim/index.js @@ -1,13 +1,13 @@ 'use strict'; var yeoman = require('yeoman-generator'); +var _ = require('underscore.string'); var BangularGenerator = yeoman.generators.NamedBase.extend({ initializing: function () { - this.camelName = this._.camelize(this.name, true); - // TODO use _.decapitalize instead of this trick when yeoman-generator will update its undercore.string dependency... - this.dashName = this._.dasherize(this.name.substr(0, 1).toLowerCase() + this.name.substr(1)); + this.camelName = _.camelize(this.name, true); + this.dashName = _.dasherize(_.decapitalize(this.name)); this.events = {}; }, diff --git a/api/index.js b/api/index.js index c7909ed..ab5bb35 100644 --- a/api/index.js +++ b/api/index.js @@ -2,6 +2,8 @@ var chalk = require('chalk'); var yeoman = require('yeoman-generator'); +var _ = require('underscore.string'); + var utils = require('../util'); var BangularGenerator = yeoman.generators.NamedBase.extend({ @@ -9,13 +11,13 @@ var BangularGenerator = yeoman.generators.NamedBase.extend({ prompting: function () { var done = this.async(); - this.instanceName = this._.camelize(this.name); - this.instancesName = this._.camelize(this.name) + 's'; + this.instanceName = _.camelize(this.name); + this.instancesName = _.camelize(this.name) + 's'; - this.fileName = this._.dasherize(this.name.substr(0, 1).toLowerCase() + this.name.substr(1)); + this.fileName = _.decapitalize(_.dasherize(this.name)); this.routeName = this.fileName + 's'; - this.objectName = this.name.substr(0, 1).toUpperCase() + this._.camelize(this.name).substr(1); + this.objectName = _.capitalize(_.camelize(this.name)); this.objectsName = this.objectName + 's'; var prompts = [{ diff --git a/api/templates/mongo/controller.js b/api/templates/mongo/controller.js index 1b11742..a5677b4 100644 --- a/api/templates/mongo/controller.js +++ b/api/templates/mongo/controller.js @@ -14,7 +14,7 @@ function handleError (res, err) { * @param res */ exports.index = function (req, res) { - <%= _.capitalize(objectName) %>.find(function (err, <%= instancesName %>) { + <%= objectName %>.find(function (err, <%= instancesName %>) { if (err) { return handleError(res, err); } return res.status(200).json(<%= instancesName %>); }); @@ -27,7 +27,7 @@ exports.index = function (req, res) { * @param res */ exports.show = function (req, res) { - <%= _.capitalize(objectName) %>.findById(req.params.id, function (err, <%= instanceName %>) { + <%= objectName %>.findById(req.params.id, function (err, <%= instanceName %>) { if (err) { return handleError(res, err); } if (!<%= instanceName %>) { return res.status(404).end(); } return res.status(200).json(<%= instanceName %>); @@ -41,7 +41,7 @@ exports.show = function (req, res) { * @param res */ exports.create = function (req, res) { - <%= _.capitalize(objectName) %>.create(req.body, function (err, <%= instanceName %>) { + <%= objectName %>.create(req.body, function (err, <%= instanceName %>) { if (err) { return handleError(res, err); } return res.status(201).json(<%= instanceName %>); }); @@ -55,7 +55,7 @@ exports.create = function (req, res) { */ exports.update = function (req, res) { if (req.body._id) { delete req.body._id; } - <%= _.capitalize(objectName) %>.findById(req.params.id, function (err, <%= instanceName %>) { + <%= objectName %>.findById(req.params.id, function (err, <%= instanceName %>) { if (err) { return handleError(res, err); } if (!<%= instanceName %>) { return res.status(404).end(); } var updated = _.merge(<%= instanceName %>, req.body); @@ -73,7 +73,7 @@ exports.update = function (req, res) { * @param res */ exports.destroy = function (req, res) { - <%= _.capitalize(objectName) %>.findById(req.params.id, function (err, <%= instanceName %>) { + <%= objectName %>.findById(req.params.id, function (err, <%= instanceName %>) { if (err) { return handleError(res, err); } if (!<%= instanceName %>) { return res.status(404).end(); } <%= instanceName %>.remove(function (err) { diff --git a/app/index.js b/app/index.js index e80babb..7be8a11 100644 --- a/app/index.js +++ b/app/index.js @@ -1,8 +1,11 @@ 'use strict'; -var utils = require('../util'); var path = require('path'); +var mkdir = require('mkdirp'); var yeoman = require('yeoman-generator'); +var _ = require('underscore.string'); + +var utils = require('../util'); var bangAscii = require('./ascii'); var BangularGenerator = yeoman.generators.Base.extend({ @@ -10,6 +13,7 @@ var BangularGenerator = yeoman.generators.Base.extend({ initializing: { getVars: function () { this.appname = this.appname || path.basename(process.cwd()); + this.slugName = _.slugify(this.appname); this.filters = {}; this.pkg = require('../package.json'); }, @@ -126,7 +130,7 @@ var BangularGenerator = yeoman.generators.Base.extend({ checked: false }] }], function (props) { - self.appname = self._.camelize(self._.slugify(self._.humanize(props.name))); + self.appname = _.camelize(_.slugify(_.humanize(props.name))); self.filters.backend = props.backend; self.filters.reload = props.reload; @@ -189,8 +193,8 @@ var BangularGenerator = yeoman.generators.Base.extend({ this.sourceRoot(path.join(__dirname, './templates')); utils.processDirectory(this, '.', '.'); - this.mkdir('client/assets/fonts'); - this.mkdir('client/assets/images'); + mkdir.sync('client/assets/fonts'); + mkdir.sync('client/assets/images'); }, end: function () { diff --git a/app/templates/server/config/environment/development.js b/app/templates/server/config/environment/development.js index abb8198..5ddbf5f 100644 --- a/app/templates/server/config/environment/development.js +++ b/app/templates/server/config/environment/development.js @@ -2,6 +2,6 @@ module.exports = {<% if (filters.backend === 'mongo') { %> mongo: { - uri: 'mongodb://localhost/<%= _.slugify(appname) %>-dev' + uri: 'mongodb://localhost/<%= slugName %>-dev' }<% } %> }; diff --git a/app/templates/server/config/environment/production.js b/app/templates/server/config/environment/production.js index 1bde437..33da306 100644 --- a/app/templates/server/config/environment/production.js +++ b/app/templates/server/config/environment/production.js @@ -3,6 +3,6 @@ module.exports = { ip: process.env.IP || undefined<% if (filters.backend === 'mongo') { %>, mongo: { - uri: 'mongodb://localhost/<%= _.slugify(appname) %>' + uri: 'mongodb://localhost/<%= slugName %>' }<% } %> }; diff --git a/app/templates/server/config/environment/test.js b/app/templates/server/config/environment/test.js index 5963174..398bebf 100644 --- a/app/templates/server/config/environment/test.js +++ b/app/templates/server/config/environment/test.js @@ -2,6 +2,6 @@ module.exports = {<% if (filters.backend === 'mongo') { %> mongo: { - uri: 'mongodb://localhost/<%= _.slugify(appname) %>-test' + uri: 'mongodb://localhost/<%= slugName %>-test' }<% } %> }; diff --git a/directive/index.js b/directive/index.js index 20d68ee..46c72e1 100644 --- a/directive/index.js +++ b/directive/index.js @@ -2,13 +2,15 @@ var yeoman = require('yeoman-generator'); var chalk = require('chalk'); +var _ = require('underscore.string'); + var utils = require('../util'); var BangularGenerator = yeoman.generators.NamedBase.extend({ initializing: function () { - this.camelName = this._.camelize(this.name); - this.dashName = this._.dasherize(this.name); + this.camelName = _.camelize(this.name); + this.dashName = _.dasherize(this.name); }, prompting: function () { diff --git a/factory/index.js b/factory/index.js index 768b1fb..3c393d7 100644 --- a/factory/index.js +++ b/factory/index.js @@ -1,13 +1,13 @@ 'use strict'; var yeoman = require('yeoman-generator'); +var _ = require('underscore.string'); var BangularGenerator = yeoman.generators.NamedBase.extend({ initializing: function () { - this.camelName = this._.capitalize(this._.camelize(this.name, true)); - // TODO use _.decapitalize instead of this trick when yeoman-generator will update its undercore.string dependency... - this.dashName = this._.dasherize(this.name.substr(0, 1).toLowerCase() + this.name.substr(1)); + this.camelName = _.capitalize(_.camelize(this.name, true)); + this.dashName = _.dasherize(_.decapitalize(this.name)); }, writing: function () { diff --git a/filter/index.js b/filter/index.js index 4002cf3..ab54943 100644 --- a/filter/index.js +++ b/filter/index.js @@ -1,12 +1,13 @@ 'use strict'; var yeoman = require('yeoman-generator'); +var _ = require('underscore.string'); var BangularGenerator = yeoman.generators.NamedBase.extend({ initializing: function () { - this.dashName = this._.dasherize(this.name); - this.camelName = this._.camelize(this.name); + this.dashName = _.dasherize(this.name); + this.camelName = _.camelize(this.name); }, writing: function () { diff --git a/font/index.js b/font/index.js index 9f91347..6af892c 100644 --- a/font/index.js +++ b/font/index.js @@ -2,15 +2,21 @@ var async = require('async'); var yeoman = require('yeoman-generator'); +var mkdir = require('mkdirp'); +var _ = require('underscore.string'); + var utils = require('../util'); var BangularGenerator = yeoman.generators.NamedBase.extend({ + initializing: function () { + this.name = _.slugify(this.name); + }, + prompting: function () { var done = this.async(); - this.name = this._.slugify(this.name); this.types = {}; this.prompt([{ @@ -50,7 +56,7 @@ var BangularGenerator = yeoman.generators.NamedBase.extend({ writing: function () { - this.mkdir('client/assets/fonts/' + this.name); + mkdir.sync('client/assets/fonts/' + this.name); if (!utils.fileExists('client/styles/fonts.scss')) { this.template('fonts.scss', 'client/styles/fonts.scss'); diff --git a/package.json b/package.json index c0ae33a..bf9a02f 100644 --- a/package.json +++ b/package.json @@ -37,9 +37,13 @@ "dependencies": { "async": "^0.9.0", "chalk": "^1.0.0", + "glob": "^5.0.5", "gulp-jshint": "^1.9.2", "jshint-stylish": "^1.0.1", - "yeoman-generator": "^0.18.10" + "lodash": "^3.7.0", + "mkdirp": "^0.5.0", + "underscore.string": "^3.0.3", + "yeoman-generator": "^0.19.2" }, "devDependencies": { "conventional-changelog": "0.0.17", diff --git a/route/index.js b/route/index.js index e211676..b360d84 100644 --- a/route/index.js +++ b/route/index.js @@ -2,14 +2,16 @@ var yeoman = require('yeoman-generator'); var chalk = require('chalk'); +var _ = require('underscore.string'); + var utils = require('../util'); var BangularGenerator = yeoman.generators.NamedBase.extend({ initializing: function () { - this.appName = this._.camelize(this.appname); - this.controllerName = this._.capitalize(this._.camelize(this.name)) + 'Ctrl'; - this.dashName = this._.dasherize(this.name); + this.appName = _.camelize(this.appname); + this.controllerName = _.capitalize(_.camelize(this.name)) + 'Ctrl'; + this.dashName = _.dasherize(this.name); }, prompting: function () { diff --git a/service/index.js b/service/index.js index 25c2d97..e59215d 100644 --- a/service/index.js +++ b/service/index.js @@ -1,13 +1,13 @@ 'use strict'; var yeoman = require('yeoman-generator'); +var _ = require('underscore.string'); var BangularGenerator = yeoman.generators.NamedBase.extend({ initializing: function () { - this.camelName = this._.capitalize(this._.camelize(this.name, true)); - // TODO use _.decapitalize instead of this trick when yeoman-generator will update its undercore.string dependency... - this.dashName = this._.dasherize(this.name.substr(0, 1).toLowerCase() + this.name.substr(1)); + this.camelName = _.capitalize(_.camelize(this.name, true)); + this.dashName = _.dasherize(_.decapitalize(this.name)); }, writing: function () { diff --git a/style/index.js b/style/index.js index 76d951b..2bdce5a 100644 --- a/style/index.js +++ b/style/index.js @@ -2,6 +2,7 @@ var chalk = require('chalk'); var yeoman = require('yeoman-generator'); + var utils = require('../util'); var BangularGenerator = yeoman.generators.NamedBase.extend({ diff --git a/test/app.js b/test/app.js index ce5c5e5..7157359 100644 --- a/test/app.js +++ b/test/app.js @@ -397,7 +397,8 @@ describe('Launching app generator tests', function () { .inDir(path.join(__dirname, './mock'), function (d) { dir = d; }) - .withPrompt({ + .withOptions({ skipInstall: false }) + .withPrompts({ name: 'Test', backend: 'mongo', reload: 'livereload', diff --git a/util.js b/util.js index 85d58b8..4a17ec6 100644 --- a/util.js +++ b/util.js @@ -1,15 +1,22 @@ 'use strict'; -var path = require('path'); var os = require('os'); var fs = require('fs'); +var path = require('path'); +var glob = require('glob'); var chalk = require('chalk'); +var _ = require('lodash'); var helpers = require('yeoman-generator').test; function escapeRegExp (str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } +function isPathAbsolute () { + var filepath = path.join.apply(path, arguments); + return path.resolve(filepath) === filepath; +} + function filterFile (template) { // Find matches for params var filterMatches = template.match(/\(([^)]+)\)/g); @@ -31,7 +38,7 @@ function templateIsUsable (self, filteredFile) { enabledFilters.push(key); } } - var matchedFilters = self._.intersection(filteredFile.filters, enabledFilters); + var matchedFilters = _.intersection(filteredFile.filters, enabledFilters); // check that all filters on file are matched if (filteredFile.filters.length && matchedFilters.length !== filteredFile.filters.length) { return false; @@ -48,7 +55,7 @@ var out = { helpers.run(path.join(__dirname, './app')) .inDir(path.join(os.tmpdir(), './tmp')) .withOptions(opts || { skipInstall: true }) - .withPrompt(prompts) + .withPrompts(prompts) .on('end', cb); }, fileExists: function (path) { @@ -147,8 +154,8 @@ var out = { }); }, processDirectory: function (self, source, destination) { - var root = self.isPathAbsolute(source) ? source : path.join(self.sourceRoot(), source); - var files = self.expandFiles('**', { dot: true, cwd: root }); + var root = isPathAbsolute(source) ? source : path.join(self.sourceRoot(), source); + var files = glob.sync('**', { dot: true, nodir: true, cwd: root }); var dest, src; files.forEach(function (f) {