Skip to content

Commit

Permalink
Merge pull request angular-fullstack#1241 from kingcody/refactor/unif…
Browse files Browse the repository at this point in the history
…y-script-base

refactor(gen): improve `generator-base` and `util` wiring
  • Loading branch information
Awk34 committed Sep 5, 2015
2 parents 3aac05f + b81292f commit 1b0c49d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
19 changes: 4 additions & 15 deletions app/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 () {
Expand Down Expand Up @@ -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() {
Expand Down
19 changes: 12 additions & 7 deletions endpoint/generator.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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')) {
Expand All @@ -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')) {
Expand All @@ -145,7 +150,7 @@ export default class Generator extends ScriptBase {
"db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');"
]
};
genUtils.rewriteFile(modelConfig);
this.rewriteFile(modelConfig);
}
}
}
49 changes: 49 additions & 0 deletions generator-base.js
Original file line number Diff line number Diff line change
@@ -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;
}
45 changes: 0 additions & 45 deletions script-base.js

This file was deleted.

8 changes: 5 additions & 3 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1b0c49d

Please sign in to comment.