Skip to content

Commit 3debe1c

Browse files
committed
feat(gen): use new base classes
1 parent 77bf967 commit 3debe1c

File tree

3 files changed

+79
-45
lines changed

3 files changed

+79
-45
lines changed

Diff for: src/generators/app/index.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
'use strict';
2-
31
import fs from 'fs';
42
import path from 'path';
53
import Promise from 'bluebird';
64
import { runCmd } from '../util';
75
import chalk from 'chalk';
8-
import { Base } from 'yeoman-generator';
9-
import { genBase } from '../generator-base';
6+
import { Base } from '../generator-base';
107
import insight from '../insight-init';
118
import { exec } from 'child_process';
129
import babelStream from 'gulp-babel';
@@ -28,17 +25,18 @@ export class Generator extends Base {
2825
defaults: false
2926
});
3027

28+
// This is mainly for development purposes
3129
this.option('skip-config', {
3230
desc: 'Always use existing .yo-rc.json',
3331
type: Boolean,
3432
defaults: false
3533
});
3634

37-
this.option('app-suffix', {
38-
desc: 'Allow a custom suffix to be added to the module name',
39-
type: String,
40-
defaults: 'App'
41-
});
35+
// this.option('app-suffix', {
36+
// desc: 'Allow a custom suffix to be added to the module name',
37+
// type: String,
38+
// defaults: 'App'
39+
// });
4240

4341
this.option('dev-port', {
4442
desc: 'Port to use for the development HTTP server',
@@ -65,9 +63,7 @@ export class Generator extends Base {
6563
this.config.set('generatorVersion', this.rootGeneratorVersion());
6664
this.filters = {};
6765

68-
// init shared generator properies and methods
69-
const genBasePromise = genBase(this);
70-
let promises = [genBasePromise];
66+
let promises = [];
7167

7268
if(process.env.CI) {
7369
insight.optOut = true;
@@ -96,7 +92,7 @@ export class Generator extends Base {
9692
},
9793
info: function () {
9894
this.log(this.yoWelcome);
99-
this.log('Out of the box I create an AngularJS app with an Express server.\n');
95+
this.log('Out of the box I create an Angular app with an Express server.\n');
10096
},
10197
checkForConfig: function() {
10298
var existingFilters = this.config.get('filters');
@@ -390,6 +386,7 @@ export class Generator extends Base {
390386
this.config.set('filters', this.filters);
391387
this.config.forceSave();
392388
},
389+
// TODO: switch to ng2 component generator
393390
// ngComponent: function() {
394391
// if(this.skipConfig) return;
395392
// var appPath = 'client/app/';

Diff for: src/generators/endpoint/index.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict';
22

33
import path from 'path';
4-
import {Base} from 'yeoman-generator';
5-
import {genNamedBase} from '../generator-base';
4+
import { NamedBase } from '../generator-base';
65

7-
export class Generator extends Base {
6+
export class Generator extends NamedBase {
87
constructor(...args) {
98
super(...args);
109

11-
this.argument('name', { type: String, required: true });
12-
1310
this.option('route', {
1411
desc: 'URL for the endpoint',
1512
type: String
@@ -26,11 +23,6 @@ export class Generator extends Base {
2623
});
2724
}
2825

29-
initializing() {
30-
// init shared generator properies and methods
31-
return genNamedBase(this);
32-
}
33-
3426
prompting() {
3527
let promptCb = props => {
3628
if(props.route.charAt(0) !== '/') {

Diff for: src/generators/generator-base.js

+67-22
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,77 @@
22

33
import util from 'util';
44
import path from 'path';
5-
import lodash from 'lodash';
5+
import _ from 'lodash';
66
import s from 'underscore.string';
7-
import semver from 'semver';
7+
import { Base as YoBase } from 'yeoman-generator';
88
import yoWelcome from 'yeoman-welcome';
99
import * as genUtils from './util';
1010

1111
// extend lodash with underscore.string
12-
lodash.mixin(s.exports());
12+
_.mixin(s.exports());
1313

14-
export function genBase(self) {
15-
self = self || this;
14+
export class Base extends YoBase {
15+
constructor(...args) {
16+
super(...args);
17+
18+
this.lodash = _;
19+
this.yoWelcome = yoWelcome;
20+
21+
this.appname = _.camelize(_.slugify(_.humanize(this.determineAppname())));
22+
23+
this.scriptAppName = this.appname + this.appSuffix();
24+
25+
this.filters = this.filters || this.config.get('filters');
26+
27+
// dynamic relative require path
28+
this.relativeRequire = genUtils.relativeRequire.bind(this);
29+
// process template directory
30+
this.processDirectory = genUtils.processDirectory.bind(this);
31+
// rewrite a file in place
32+
this.rewriteFile = genUtils.rewriteFile;
33+
}
34+
35+
appSuffix() {
36+
var suffix = this.options['app-suffix'];
37+
return (typeof suffix === 'string') ? this.lodash.classify(suffix) : '';
38+
}
39+
40+
determineAppname() {
41+
if(this.name) return this.name;
42+
else return super.determineAppname();
43+
}
1644

17-
let yoCheckPromise;
18-
if(!process.env.CI) {
19-
yoCheckPromise = genUtils.runCmd('yo --version').then(stdout => {
20-
if(!semver.satisfies(semver.clean(stdout), '>= 1.7.1')) {
21-
throw new Error(`ERROR: You need to update yo to at least 1.7.1 (npm i -g yo)
22-
'yo --version' output: ${stdout}`);
23-
}
24-
});
25-
} else {
26-
// CI won't have yo installed
27-
yoCheckPromise = Promise.resolve();
45+
// dynamic assertion statements
46+
expect() {
47+
return this.filters.expect ? 'expect(' : '';
48+
}
49+
to() {
50+
return this.filters.expect ? ').to' : '.should';
51+
}
52+
}
53+
54+
export class NamedBase extends Base {
55+
constructor(...args) {
56+
super(...args);
57+
58+
this.argument('name', { type: String, required: true });
59+
60+
var name = this.name.replace(/\//g, '-');
61+
62+
this.cameledName = _.camelize(name);
63+
this.classedName = _.classify(name);
64+
65+
this.basename = path.basename(this.name);
66+
this.dirname = this.name.includes('/')
67+
? path.dirname(this.name)
68+
: this.name;
2869
}
70+
}
71+
72+
export function genBase(self) {
73+
self = self || this;
2974

30-
self.lodash = lodash;
75+
self.lodash = _;
3176
self.yoWelcome = yoWelcome;
3277

3378
let baseDetermineAppname = self.determineAppname.bind(self);
@@ -39,8 +84,8 @@ export function genBase(self) {
3984
}
4085
}
4186

42-
self.appname = lodash.camelize(lodash.slugify(
43-
lodash.humanize(self.determineAppname())
87+
self.appname = _.camelize(_.slugify(
88+
_.humanize(self.determineAppname())
4489
));
4590
self.scriptAppName = self.appname + genUtils.appSuffix(self);
4691

@@ -61,7 +106,7 @@ export function genBase(self) {
61106
// rewrite a file in place
62107
self.rewriteFile = genUtils.rewriteFile;
63108

64-
return yoCheckPromise;
109+
return Promise.resolve();
65110
}
66111

67112
export function genNamedBase(self) {
@@ -71,8 +116,8 @@ export function genNamedBase(self) {
71116
return genBase(self).then(() => {
72117
var name = self.name.replace(/\//g, '-');
73118

74-
self.cameledName = lodash.camelize(name);
75-
self.classedName = lodash.classify(name);
119+
self.cameledName = _.camelize(name);
120+
self.classedName = _.classify(name);
76121

77122
self.basename = path.basename(self.name);
78123
self.dirname = (self.name.indexOf('/') >= 0) ? path.dirname(self.name) : self.name;

0 commit comments

Comments
 (0)