diff --git a/package.json b/package.json index 7234a3f26..0dacd2205 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/generators/app/index.js b/src/generators/app/index.js index 8c3b55b2f..96439c536 100644 --- a/src/generators/app/index.js +++ b/src/generators/app/index.js @@ -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'; @@ -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', @@ -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; @@ -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'); @@ -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/'; diff --git a/src/generators/endpoint/index.js b/src/generators/endpoint/index.js index 04fcd2be5..7075d172c 100644 --- a/src/generators/endpoint/index.js +++ b/src/generators/endpoint/index.js @@ -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 @@ -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) !== '/') { diff --git a/src/generators/generator-base.js b/src/generators/generator-base.js index 0d398a88b..d4a13c314 100644 --- a/src/generators/generator-base.js +++ b/src/generators/generator-base.js @@ -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); @@ -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); @@ -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) { @@ -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;