Skip to content

gen base refactor #2425

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 2 commits into from
Dec 25, 2016
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
"yeoman-assert": "^2.0.0",
"yeoman-test": "~1.5.0"
},
"peerDependencies": {
"yo": ">= 1.7.1"
},
"engines": {
"node": "^6.2.2",
"npm": "^3.9.5"
Expand Down
23 changes: 10 additions & 13 deletions src/generators/app/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict';

import fs from 'fs';
import path from 'path';
import Promise from 'bluebird';
import { runCmd } from '../util';
import chalk from 'chalk';
import { Base } from 'yeoman-generator';
import { genBase } from '../generator-base';
import { Base } from '../generator-base';
import insight from '../insight-init';
import { exec } from 'child_process';
import babelStream from 'gulp-babel';
Expand All @@ -28,17 +25,18 @@ export class Generator extends Base {
defaults: false
});

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

this.option('app-suffix', {
desc: 'Allow a custom suffix to be added to the module name',
type: String,
defaults: 'App'
});
// this.option('app-suffix', {
// desc: 'Allow a custom suffix to be added to the module name',
// type: String,
// defaults: 'App'
// });

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

// init shared generator properies and methods
const genBasePromise = genBase(this);
let promises = [genBasePromise];
let promises = [];

if(process.env.CI) {
insight.optOut = true;
Expand Down Expand Up @@ -96,7 +92,7 @@ export class Generator extends Base {
},
info: function () {
this.log(this.yoWelcome);
this.log('Out of the box I create an AngularJS app with an Express server.\n');
this.log('Out of the box I create an Angular app with an Express server.\n');
},
checkForConfig: function() {
var existingFilters = this.config.get('filters');
Expand Down Expand Up @@ -390,6 +386,7 @@ export class Generator extends Base {
this.config.set('filters', this.filters);
this.config.forceSave();
},
// TODO: switch to ng2 component generator
// ngComponent: function() {
// if(this.skipConfig) return;
// var appPath = 'client/app/';
Expand Down
12 changes: 2 additions & 10 deletions src/generators/endpoint/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict';

import path from 'path';
import {Base} from 'yeoman-generator';
import {genNamedBase} from '../generator-base';
import { NamedBase } from '../generator-base';

export class Generator extends Base {
export class Generator extends NamedBase {
constructor(...args) {
super(...args);

this.argument('name', { type: String, required: true });

this.option('route', {
desc: 'URL for the endpoint',
type: String
Expand All @@ -26,11 +23,6 @@ export class Generator extends Base {
});
}

initializing() {
// init shared generator properies and methods
return genNamedBase(this);
}

prompting() {
let promptCb = props => {
if(props.route.charAt(0) !== '/') {
Expand Down
89 changes: 67 additions & 22 deletions src/generators/generator-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,77 @@

import util from 'util';
import path from 'path';
import lodash from 'lodash';
import _ from 'lodash';
import s from 'underscore.string';
import semver from 'semver';
import { Base as YoBase } from 'yeoman-generator';
import yoWelcome from 'yeoman-welcome';
import * as genUtils from './util';

// extend lodash with underscore.string
lodash.mixin(s.exports());
_.mixin(s.exports());

export function genBase(self) {
self = self || this;
export class Base extends YoBase {
constructor(...args) {
super(...args);

this.lodash = _;
this.yoWelcome = yoWelcome;

this.appname = _.camelize(_.slugify(_.humanize(this.determineAppname())));

this.scriptAppName = this.appname + this.appSuffix();

this.filters = this.filters || this.config.get('filters');

// dynamic relative require path
this.relativeRequire = genUtils.relativeRequire.bind(this);
// process template directory
this.processDirectory = genUtils.processDirectory.bind(this);
// rewrite a file in place
this.rewriteFile = genUtils.rewriteFile;
}

appSuffix() {
var suffix = this.options['app-suffix'];
return (typeof suffix === 'string') ? this.lodash.classify(suffix) : '';
}

determineAppname() {
if(this.name) return this.name;
else return super.determineAppname();
}

let yoCheckPromise;
if(!process.env.CI) {
yoCheckPromise = genUtils.runCmd('yo --version').then(stdout => {
if(!semver.satisfies(semver.clean(stdout), '>= 1.7.1')) {
throw new Error(`ERROR: You need to update yo to at least 1.7.1 (npm i -g yo)
'yo --version' output: ${stdout}`);
}
});
} else {
// CI won't have yo installed
yoCheckPromise = Promise.resolve();
// dynamic assertion statements
expect() {
return this.filters.expect ? 'expect(' : '';
}
to() {
return this.filters.expect ? ').to' : '.should';
}
}

export class NamedBase extends Base {
constructor(...args) {
super(...args);

this.argument('name', { type: String, required: true });

var name = this.name.replace(/\//g, '-');

this.cameledName = _.camelize(name);
this.classedName = _.classify(name);

this.basename = path.basename(this.name);
this.dirname = this.name.includes('/')
? path.dirname(this.name)
: this.name;
}
}

export function genBase(self) {
self = self || this;

self.lodash = lodash;
self.lodash = _;
self.yoWelcome = yoWelcome;

let baseDetermineAppname = self.determineAppname.bind(self);
Expand All @@ -39,8 +84,8 @@ export function genBase(self) {
}
}

self.appname = lodash.camelize(lodash.slugify(
lodash.humanize(self.determineAppname())
self.appname = _.camelize(_.slugify(
_.humanize(self.determineAppname())
));
self.scriptAppName = self.appname + genUtils.appSuffix(self);

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

return yoCheckPromise;
return Promise.resolve();
}

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

self.cameledName = lodash.camelize(name);
self.classedName = lodash.classify(name);
self.cameledName = _.camelize(name);
self.classedName = _.classify(name);

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