Skip to content

refactor(gen): remove legacy argument fix, and ES2015ify more #1238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

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 {

constructor(...args) {
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'
});
}

Expand All @@ -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');

Expand Down
12 changes: 6 additions & 6 deletions endpoint/generator.js
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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() {
Expand All @@ -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')) {
Expand All @@ -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')) {
Expand All @@ -145,7 +145,7 @@ export default class Generator extends ScriptBase {
"db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');"
]
};
ngUtil.rewriteFile(modelConfig);
genUtils.rewriteFile(modelConfig);
}
}
}
85 changes: 43 additions & 42 deletions script-base.js
Original file line number Diff line number Diff line change
@@ -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'));
}
}
50 changes: 17 additions & 33 deletions util.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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'));

Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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 = [];
Expand All @@ -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) {
Expand All @@ -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;
Expand Down