From b81292f0e9c4a9fcd96eaef5fc55283a6ccaa180 Mon Sep 17 00:00:00 2001 From: kingcody Date: Wed, 2 Sep 2015 06:10:08 -0400 Subject: [PATCH] refactor(gen): improve `generator-base` and `util` wiring --- app/generator.js | 19 ++++------------- endpoint/generator.js | 19 ++++++++++------- generator-base.js | 49 +++++++++++++++++++++++++++++++++++++++++++ script-base.js | 45 --------------------------------------- util.js | 8 ++++--- 5 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 generator-base.js delete mode 100644 script-base.js diff --git a/app/generator.js b/app/generator.js index 7751af8fe..591984d98 100644 --- a/app/generator.js +++ b/app/generator.js @@ -4,7 +4,7 @@ import fs from 'fs'; import path from 'path'; import chalk from 'chalk'; import {Base} from 'yeoman-generator'; -import * as genUtils from '../util'; +import {genBase} from '../generator-base'; export default class Generator extends Base { @@ -25,22 +25,11 @@ export default class Generator extends Base { return { init: function () { - this.appname = this.name || path.basename(process.cwd()); - this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname))); - - this.scriptAppName = this.appname + genUtils.appSuffix(this); - this.appPath = this.env.options.appPath; this.pkg = require('../package.json'); - this.filters = {}; - // dynamic assertion statements - this.expect = function() { - return this.filters.expect ? 'expect(' : ''; - }.bind(this); - this.to = function() { - return this.filters.expect ? ').to' : '.should'; - }.bind(this); + // init shared generator properies and methods + genBase(this); }, info: function () { @@ -394,7 +383,7 @@ export default class Generator extends Base { generateProject: function() { this.sourceRoot(path.join(__dirname, './templates')); - genUtils.processDirectory(this, '.', '.'); + this.processDirectory('.', '.'); }, generateEndpoint: function() { diff --git a/endpoint/generator.js b/endpoint/generator.js index 0d6d3217d..169dec3d8 100644 --- a/endpoint/generator.js +++ b/endpoint/generator.js @@ -1,10 +1,10 @@ 'use strict'; import path from 'path'; -import ScriptBase from '../script-base'; -import * as genUtils from '../util'; +import {NamedBase} from 'yeoman-generator'; +import {genNamedBase} from '../generator-base'; -export default class Generator extends ScriptBase { +export default class Generator extends NamedBase { constructor(...args) { super(...args); @@ -25,6 +25,11 @@ export default class Generator extends ScriptBase { }); } + initializing() { + // init shared generator properies and methods + genNamedBase(this); + } + prompting() { var done = this.async(); var promptCb = function (props) { @@ -103,7 +108,7 @@ export default class Generator extends ScriptBase { writing() { this.sourceRoot(path.join(__dirname, './templates')); - genUtils.processDirectory(this, '.', this.routeDest); + this.processDirectory('.', this.routeDest); } end() { @@ -117,7 +122,7 @@ export default class Generator extends ScriptBase { "app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));" ] }; - genUtils.rewriteFile(routeConfig); + this.rewriteFile(routeConfig); } if (this.filters.socketio && this.config.get('insertSockets')) { @@ -131,7 +136,7 @@ export default class Generator extends ScriptBase { "require(\'" + reqPath + "\').register(socket);" ] }; - genUtils.rewriteFile(socketConfig); + this.rewriteFile(socketConfig); } if (this.filters.sequelize && this.config.get('insertModels')) { @@ -145,7 +150,7 @@ export default class Generator extends ScriptBase { "db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');" ] }; - genUtils.rewriteFile(modelConfig); + this.rewriteFile(modelConfig); } } } diff --git a/generator-base.js b/generator-base.js new file mode 100644 index 000000000..fbe4be437 --- /dev/null +++ b/generator-base.js @@ -0,0 +1,49 @@ +'use strict'; + +import util from 'util'; +import path from 'path'; +import * as genUtils from './util'; + +export function genBase(self) { + self = self || this; + + try { + self.appname = require(path.join(process.cwd(), 'bower.json')).name; + } catch (e) { + self.appname = self.name || path.basename(process.cwd()); + } + self.appname = self._.camelize(self._.slugify(self._.humanize(self.appname))); + self.scriptAppName = self.appname + genUtils.appSuffix(self); + + self.filters = self.filters || self.config.get('filters'); + + // dynamic assertion statements + self.expect = function() { + return self.filters.expect ? 'expect(' : ''; + }; + self.to = function() { + return self.filters.expect ? ').to' : '.should'; + }; + + // dynamic relative require path + self.relativeRequire = genUtils.relativeRequire.bind(self); + // process template directory + self.processDirectory = genUtils.processDirectory.bind(self); + // rewrite a file in place + self.rewriteFile = genUtils.rewriteFile; +} + +export function genNamedBase(self) { + self = self || this; + + // extend genBase + genBase(self); + + var name = self.name.replace(/\//g, '-'); + + self.cameledName = self._.camelize(name); + self.classedName = self._.classify(name); + + self.basename = path.basename(self.name); + self.dirname = (self.name.indexOf('/') >= 0) ? path.dirname(self.name) : self.name; +} diff --git a/script-base.js b/script-base.js deleted file mode 100644 index 0c9fc299e..000000000 --- a/script-base.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -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 64cf5c647..d23e7f693 100644 --- a/util.js +++ b/util.js @@ -68,8 +68,9 @@ function destinationPath(self, filepath) { return filepath; } -export function relativeRequire(self, to, fr) { - fr = destinationPath(self, fr); +export function relativeRequire(to, fr) { + var self = this; + fr = destinationPath(self, fr || self.filePath); to = destinationPath(self, to); return path.relative(path.dirname(fr), to) .replace(/\\/g, '/') // convert win32 separator to posix @@ -105,7 +106,8 @@ function templateIsUsable(self, filteredFile) { return true; } -export function processDirectory(self, source, destination) { +export function processDirectory(source, destination) { + var self = this; var root = self.isPathAbsolute(source) ? source : path.join(self.sourceRoot(), source); var files = self.expandFiles('**', { dot: true, cwd: root }); var dest, src;