Skip to content

Commit d153140

Browse files
committed
refactor(gen): remove legacy argument fix, and ES2015ify more
1 parent 8f1c82c commit d153140

File tree

4 files changed

+72
-86
lines changed

4 files changed

+72
-86
lines changed

Diff for: app/generator.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
import fs from 'fs';
44
import path from 'path';
5-
import genUtils from '../util.js';
6-
import {Base} from 'yeoman-generator';
75
import chalk from 'chalk';
6+
import {Base} from 'yeoman-generator';
7+
import * as genUtils from '../util';
88

99
export default class Generator extends Base {
1010

1111
constructor(...args) {
1212
super(...args);
1313

1414
this.argument('name', { type: String, required: false });
15+
1516
this.option('app-suffix', {
1617
desc: 'Allow a custom suffix to be added to the module name',
1718
type: String,
18-
required: 'false'
19+
required: 'false',
20+
defaults: 'App'
1921
});
2022
}
2123

@@ -26,8 +28,7 @@ export default class Generator extends Base {
2628
this.appname = this.name || path.basename(process.cwd());
2729
this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname)));
2830

29-
this.scriptAppName = this.appname + genUtils.appName(this);
30-
console.log(this.scriptAppName);
31+
this.scriptAppName = this.appname + genUtils.appSuffix(this);
3132
this.appPath = this.env.options.appPath;
3233
this.pkg = require('../package.json');
3334

Diff for: endpoint/generator.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
import path from 'path';
4-
import ngUtil from '../util';
5-
import ScriptBase from '../script-base.js';
4+
import ScriptBase from '../script-base';
5+
import * as genUtils from '../util';
66

77
export default class Generator extends ScriptBase {
88

@@ -103,7 +103,7 @@ export default class Generator extends ScriptBase {
103103

104104
writing() {
105105
this.sourceRoot(path.join(__dirname, './templates'));
106-
ngUtil.processDirectory(this, '.', this.routeDest);
106+
genUtils.processDirectory(this, '.', this.routeDest);
107107
}
108108

109109
end() {
@@ -117,7 +117,7 @@ export default class Generator extends ScriptBase {
117117
"app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));"
118118
]
119119
};
120-
ngUtil.rewriteFile(routeConfig);
120+
genUtils.rewriteFile(routeConfig);
121121
}
122122

123123
if (this.filters.socketio && this.config.get('insertSockets')) {
@@ -131,7 +131,7 @@ export default class Generator extends ScriptBase {
131131
"require(\'" + reqPath + "\').register(socket);"
132132
]
133133
};
134-
ngUtil.rewriteFile(socketConfig);
134+
genUtils.rewriteFile(socketConfig);
135135
}
136136

137137
if (this.filters.sequelize && this.config.get('insertModels')) {
@@ -145,7 +145,7 @@ export default class Generator extends ScriptBase {
145145
"db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');"
146146
]
147147
};
148-
ngUtil.rewriteFile(modelConfig);
148+
genUtils.rewriteFile(modelConfig);
149149
}
150150
}
151151
}

Diff for: script-base.js

+43-42
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
'use strict';
2-
var util = require('util');
3-
var path = require('path');
4-
var yeoman = require('yeoman-generator');
5-
var angularUtils = require('./util.js');
6-
7-
var Generator = module.exports = function Generator() {
8-
yeoman.generators.NamedBase.apply(this, arguments);
9-
10-
try {
11-
this.appname = require(path.join(process.cwd(), 'bower.json')).name;
12-
} catch (e) {
13-
this.appname = path.basename(process.cwd());
14-
}
15-
this.appname = this._.slugify(this._.humanize(this.appname));
16-
this.scriptAppName = this._.camelize(this.appname) + angularUtils.appName(this);
17-
18-
var name = this.name.replace(/\//g, '-');
19-
20-
this.cameledName = this._.camelize(name);
21-
this.classedName = this._.classify(name);
22-
23-
this.basename = path.basename(this.name);
24-
this.dirname = (this.name.indexOf('/') >= 0) ? path.dirname(this.name) : this.name;
252

26-
// dynamic assertion statements
27-
this.expect = function() {
28-
return this.filters.expect ? 'expect(' : '';
29-
}.bind(this);
30-
this.to = function() {
31-
return this.filters.expect ? ').to' : '.should';
32-
}.bind(this);
33-
34-
// dynamic relative require path
35-
this.relativeRequire = function(to, fr) {
36-
fr = fr || this.filePath;
37-
return angularUtils.relativeRequire(this, to, fr);
38-
}.bind(this);
39-
40-
this.filters = this.config.get('filters');
41-
this.sourceRoot(path.join(__dirname, '/templates'));
42-
};
43-
44-
util.inherits(Generator, yeoman.generators.NamedBase);
3+
import util from 'util';
4+
import path from 'path';
5+
import {NamedBase} from 'yeoman-generator';
6+
import * as genUtils from './util';
7+
8+
export default class ScriptBase extends NamedBase {
9+
constructor(...args) {
10+
super(...args);
11+
12+
try {
13+
this.appname = require(path.join(process.cwd(), 'bower.json')).name;
14+
} catch (e) {
15+
this.appname = path.basename(process.cwd());
16+
}
17+
this.appname = this._.slugify(this._.humanize(this.appname));
18+
this.scriptAppName = this._.camelize(this.appname) + genUtils.appSuffix(this);
19+
20+
var name = this.name.replace(/\//g, '-');
21+
22+
this.cameledName = this._.camelize(name);
23+
this.classedName = this._.classify(name);
24+
25+
this.basename = path.basename(this.name);
26+
this.dirname = (this.name.indexOf('/') >= 0) ? path.dirname(this.name) : this.name;
27+
28+
// dynamic assertion statements
29+
this.expect = function() {
30+
return this.filters.expect ? 'expect(' : '';
31+
}.bind(this);
32+
this.to = function() {
33+
return this.filters.expect ? ').to' : '.should';
34+
}.bind(this);
35+
36+
// dynamic relative require path
37+
this.relativeRequire = function(to, fr) {
38+
fr = fr || this.filePath;
39+
return genUtils.relativeRequire(this, to, fr);
40+
}.bind(this);
41+
42+
this.filters = this.config.get('filters');
43+
this.sourceRoot(path.join(__dirname, '/templates'));
44+
}
45+
}

Diff for: util.js

+17-33
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
'use strict';
2-
var path = require('path');
3-
var fs = require('fs');
4-
5-
module.exports = {
6-
rewrite: rewrite,
7-
rewriteFile: rewriteFile,
8-
appName: appName,
9-
processDirectory: processDirectory,
10-
relativeRequire: relativeRequire
11-
};
12-
13-
function rewriteFile (args) {
2+
3+
import path from 'path';
4+
import fs from 'fs';
5+
6+
export function rewriteFile(args) {
147
args.path = args.path || process.cwd();
158
var fullPath = path.join(args.path, args.file);
169

@@ -20,13 +13,13 @@ function rewriteFile (args) {
2013
fs.writeFileSync(fullPath, body);
2114
}
2215

23-
function escapeRegExp (str) {
16+
function escapeRegExp(str) {
2417
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
2518
}
2619

27-
function rewrite (args) {
20+
export function rewrite(args) {
2821
// check if splicable is already in the body text
29-
var re = new RegExp(args.splicable.map(function (line) {
22+
var re = new RegExp(args.splicable.map(function(line) {
3023
return '\s*' + escapeRegExp(line);
3124
}).join('\n'));
3225

@@ -54,28 +47,19 @@ function rewrite (args) {
5447
spaceStr += ' ';
5548
}
5649

57-
lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) {
50+
lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function(line) {
5851
return spaceStr + line;
5952
}).join('\n'));
6053

6154
return lines.join('\n');
6255
}
6356

64-
function appName (self) {
65-
var counter = 0, suffix = self.options['app-suffix'];
66-
// Have to check this because of generator bug #386
67-
process.argv.forEach(function(val) {
68-
if (val.indexOf('--app-suffix') > -1) {
69-
counter++;
70-
}
71-
});
72-
if (counter === 0 || (typeof suffix === 'boolean' && suffix)) {
73-
suffix = 'App';
74-
}
75-
return suffix ? self._.classify(suffix) : '';
57+
export function appSuffix(self) {
58+
var suffix = self.options['app-suffix'];
59+
return (typeof suffix === 'string') ? self._.classify(suffix) : '';
7660
}
7761

78-
function destinationPath (self, filepath) {
62+
function destinationPath(self, filepath) {
7963
filepath = path.normalize(filepath);
8064
if (!path.isAbsolute(filepath)) {
8165
filepath = path.join(self.destinationRoot(), filepath);
@@ -84,7 +68,7 @@ function destinationPath (self, filepath) {
8468
return filepath;
8569
}
8670

87-
function relativeRequire (self, to, fr) {
71+
export function relativeRequire(self, to, fr) {
8872
fr = destinationPath(self, fr);
8973
to = destinationPath(self, to);
9074
return path.relative(path.dirname(fr), to)
@@ -93,7 +77,7 @@ function relativeRequire (self, to, fr) {
9377
.replace(/[\/\\]index\.js$/, ''); // strip index.js suffix from path
9478
}
9579

96-
function filterFile (template) {
80+
function filterFile(template) {
9781
// Find matches for parans
9882
var filterMatches = template.match(/\(([^)]+)\)/g);
9983
var filters = [];
@@ -107,7 +91,7 @@ function filterFile (template) {
10791
return { name: template, filters: filters };
10892
}
10993

110-
function templateIsUsable (self, filteredFile) {
94+
function templateIsUsable(self, filteredFile) {
11195
var filters = self.filters || self.config.get('filters');
11296
var enabledFilters = [];
11397
for(var key in filters) {
@@ -121,7 +105,7 @@ function templateIsUsable (self, filteredFile) {
121105
return true;
122106
}
123107

124-
function processDirectory (self, source, destination) {
108+
export function processDirectory(self, source, destination) {
125109
var root = self.isPathAbsolute(source) ? source : path.join(self.sourceRoot(), source);
126110
var files = self.expandFiles('**', { dot: true, cwd: root });
127111
var dest, src;

0 commit comments

Comments
 (0)