diff --git a/app/generator.js b/app/generator.js index 517773bc8..7751af8fe 100644 --- a/app/generator.js +++ b/app/generator.js @@ -2,9 +2,9 @@ import fs from 'fs'; import path from 'path'; -import genUtils from '../util.js'; -import {Base} from 'yeoman-generator'; import chalk from 'chalk'; +import {Base} from 'yeoman-generator'; +import * as genUtils from '../util'; export default class Generator extends Base { @@ -12,10 +12,12 @@ export default class Generator extends Base { super(...args); this.argument('name', { type: String, required: false }); + this.option('app-suffix', { desc: 'Allow a custom suffix to be added to the module name', type: String, - required: 'false' + required: 'false', + defaults: 'App' }); } @@ -26,8 +28,7 @@ export default class Generator extends Base { this.appname = this.name || path.basename(process.cwd()); this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname))); - this.scriptAppName = this.appname + genUtils.appName(this); - console.log(this.scriptAppName); + this.scriptAppName = this.appname + genUtils.appSuffix(this); this.appPath = this.env.options.appPath; this.pkg = require('../package.json'); diff --git a/endpoint/generator.js b/endpoint/generator.js index b4757d743..0d6d3217d 100644 --- a/endpoint/generator.js +++ b/endpoint/generator.js @@ -1,8 +1,8 @@ 'use strict'; import path from 'path'; -import ngUtil from '../util'; -import ScriptBase from '../script-base.js'; +import ScriptBase from '../script-base'; +import * as genUtils from '../util'; export default class Generator extends ScriptBase { @@ -103,7 +103,7 @@ export default class Generator extends ScriptBase { writing() { this.sourceRoot(path.join(__dirname, './templates')); - ngUtil.processDirectory(this, '.', this.routeDest); + genUtils.processDirectory(this, '.', this.routeDest); } end() { @@ -117,7 +117,7 @@ export default class Generator extends ScriptBase { "app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));" ] }; - ngUtil.rewriteFile(routeConfig); + genUtils.rewriteFile(routeConfig); } if (this.filters.socketio && this.config.get('insertSockets')) { @@ -131,7 +131,7 @@ export default class Generator extends ScriptBase { "require(\'" + reqPath + "\').register(socket);" ] }; - ngUtil.rewriteFile(socketConfig); + genUtils.rewriteFile(socketConfig); } if (this.filters.sequelize && this.config.get('insertModels')) { @@ -145,7 +145,7 @@ export default class Generator extends ScriptBase { "db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');" ] }; - ngUtil.rewriteFile(modelConfig); + genUtils.rewriteFile(modelConfig); } } } diff --git a/script-base.js b/script-base.js index 288a4e4f4..0c9fc299e 100644 --- a/script-base.js +++ b/script-base.js @@ -1,44 +1,45 @@ 'use strict'; -var util = require('util'); -var path = require('path'); -var yeoman = require('yeoman-generator'); -var angularUtils = require('./util.js'); - -var Generator = module.exports = function Generator() { - yeoman.generators.NamedBase.apply(this, arguments); - - try { - this.appname = require(path.join(process.cwd(), 'bower.json')).name; - } catch (e) { - this.appname = path.basename(process.cwd()); - } - this.appname = this._.slugify(this._.humanize(this.appname)); - this.scriptAppName = this._.camelize(this.appname) + angularUtils.appName(this); - - var name = this.name.replace(/\//g, '-'); - - this.cameledName = this._.camelize(name); - this.classedName = this._.classify(name); - - this.basename = path.basename(this.name); - this.dirname = (this.name.indexOf('/') >= 0) ? path.dirname(this.name) : this.name; - // dynamic assertion statements - this.expect = function() { - return this.filters.expect ? 'expect(' : ''; - }.bind(this); - this.to = function() { - return this.filters.expect ? ').to' : '.should'; - }.bind(this); - - // dynamic relative require path - this.relativeRequire = function(to, fr) { - fr = fr || this.filePath; - return angularUtils.relativeRequire(this, to, fr); - }.bind(this); - - this.filters = this.config.get('filters'); - this.sourceRoot(path.join(__dirname, '/templates')); -}; - -util.inherits(Generator, yeoman.generators.NamedBase); +import util from 'util'; +import path from 'path'; +import {NamedBase} from 'yeoman-generator'; +import * as genUtils from './util'; + +export default class ScriptBase extends NamedBase { + constructor(...args) { + super(...args); + + try { + this.appname = require(path.join(process.cwd(), 'bower.json')).name; + } catch (e) { + this.appname = path.basename(process.cwd()); + } + this.appname = this._.slugify(this._.humanize(this.appname)); + this.scriptAppName = this._.camelize(this.appname) + genUtils.appSuffix(this); + + var name = this.name.replace(/\//g, '-'); + + this.cameledName = this._.camelize(name); + this.classedName = this._.classify(name); + + this.basename = path.basename(this.name); + this.dirname = (this.name.indexOf('/') >= 0) ? path.dirname(this.name) : this.name; + + // dynamic assertion statements + this.expect = function() { + return this.filters.expect ? 'expect(' : ''; + }.bind(this); + this.to = function() { + return this.filters.expect ? ').to' : '.should'; + }.bind(this); + + // dynamic relative require path + this.relativeRequire = function(to, fr) { + fr = fr || this.filePath; + return genUtils.relativeRequire(this, to, fr); + }.bind(this); + + this.filters = this.config.get('filters'); + this.sourceRoot(path.join(__dirname, '/templates')); + } +} diff --git a/util.js b/util.js index 0490a228a..64cf5c647 100644 --- a/util.js +++ b/util.js @@ -1,16 +1,9 @@ 'use strict'; -var path = require('path'); -var fs = require('fs'); - -module.exports = { - rewrite: rewrite, - rewriteFile: rewriteFile, - appName: appName, - processDirectory: processDirectory, - relativeRequire: relativeRequire -}; - -function rewriteFile (args) { + +import path from 'path'; +import fs from 'fs'; + +export function rewriteFile(args) { args.path = args.path || process.cwd(); var fullPath = path.join(args.path, args.file); @@ -20,13 +13,13 @@ function rewriteFile (args) { fs.writeFileSync(fullPath, body); } -function escapeRegExp (str) { +function escapeRegExp(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } -function rewrite (args) { +export function rewrite(args) { // check if splicable is already in the body text - var re = new RegExp(args.splicable.map(function (line) { + var re = new RegExp(args.splicable.map(function(line) { return '\s*' + escapeRegExp(line); }).join('\n')); @@ -54,28 +47,19 @@ function rewrite (args) { spaceStr += ' '; } - lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { + lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function(line) { return spaceStr + line; }).join('\n')); return lines.join('\n'); } -function appName (self) { - var counter = 0, suffix = self.options['app-suffix']; - // Have to check this because of generator bug #386 - process.argv.forEach(function(val) { - if (val.indexOf('--app-suffix') > -1) { - counter++; - } - }); - if (counter === 0 || (typeof suffix === 'boolean' && suffix)) { - suffix = 'App'; - } - return suffix ? self._.classify(suffix) : ''; +export function appSuffix(self) { + var suffix = self.options['app-suffix']; + return (typeof suffix === 'string') ? self._.classify(suffix) : ''; } -function destinationPath (self, filepath) { +function destinationPath(self, filepath) { filepath = path.normalize(filepath); if (!path.isAbsolute(filepath)) { filepath = path.join(self.destinationRoot(), filepath); @@ -84,7 +68,7 @@ function destinationPath (self, filepath) { return filepath; } -function relativeRequire (self, to, fr) { +export function relativeRequire(self, to, fr) { fr = destinationPath(self, fr); to = destinationPath(self, to); return path.relative(path.dirname(fr), to) @@ -93,7 +77,7 @@ function relativeRequire (self, to, fr) { .replace(/[\/\\]index\.js$/, ''); // strip index.js suffix from path } -function filterFile (template) { +function filterFile(template) { // Find matches for parans var filterMatches = template.match(/\(([^)]+)\)/g); var filters = []; @@ -107,7 +91,7 @@ function filterFile (template) { return { name: template, filters: filters }; } -function templateIsUsable (self, filteredFile) { +function templateIsUsable(self, filteredFile) { var filters = self.filters || self.config.get('filters'); var enabledFilters = []; for(var key in filters) { @@ -121,7 +105,7 @@ function templateIsUsable (self, filteredFile) { return true; } -function processDirectory (self, source, destination) { +export function processDirectory(self, source, destination) { var root = self.isPathAbsolute(source) ? source : path.join(self.sourceRoot(), source); var files = self.expandFiles('**', { dot: true, cwd: root }); var dest, src;