Skip to content

Commit

Permalink
Remove class-extend (isn't necessary with ES6), clean jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Mar 5, 2017
1 parent 2cab46e commit 00912ce
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 123 deletions.
74 changes: 30 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ const promptSuggestion = require('./util/prompt-suggestion');
const EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';

/**
* The `Base` class provides the common API shared by all generators.
* The `Generator` class provides the common API shared by all generators.
* It define options, arguments, file, prompt, log, API, etc.
*
* It mixes into its prototype all the methods found in the `actions/` mixins.
*
* Every generator should extend this base class.
*
* @constructor
* @mixes actions/help
* @mixes actions/install
* @mixes actions/spawn-command
* @mixes actions/user
* @mixes actions/help
* @mixes nodejs/EventEmitter
*
* @param {String|Array} args
Expand All @@ -57,7 +57,7 @@ const EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';
* };
*/

const Base = function (args, options) {
const Generator = function (args, options) {
EventEmitter.call(this);

if (!Array.isArray(args)) {
Expand Down Expand Up @@ -141,13 +141,13 @@ const Base = function (args, options) {
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));
};

util.inherits(Base, EventEmitter);
util.inherits(Generator, EventEmitter);

// Mixin the actions modules
_.extend(Base.prototype, require('./actions/install'));
_.extend(Base.prototype, require('./actions/help'));
_.extend(Base.prototype, require('./actions/spawn-command'));
Base.prototype.user = require('./actions/user');
_.extend(Generator.prototype, require('./actions/install'));
_.extend(Generator.prototype, require('./actions/help'));
_.extend(Generator.prototype, require('./actions/spawn-command'));
Generator.prototype.user = require('./actions/user');

/*
* Prompt user to answer questions. The signature of this method is the same as {@link https://github.com/SBoudrias/Inquirer.js Inquirer.js}
Expand All @@ -160,7 +160,7 @@ Base.prototype.user = require('./actions/user');
* @return {Promise}
*/

Base.prototype.prompt = function (questions) {
Generator.prototype.prompt = function (questions) {
questions = promptSuggestion.prefillQuestions(this._globalConfig, questions);
questions = promptSuggestion.prefillQuestions(this.config, questions);

Expand Down Expand Up @@ -191,7 +191,7 @@ Base.prototype.prompt = function (questions) {
* @param {Object} config
*/

Base.prototype.option = function (name, config) {
Generator.prototype.option = function (name, config) {
config = config || {};

// Alias default to defaults for backward compatibility.
Expand Down Expand Up @@ -249,7 +249,7 @@ Base.prototype.option = function (name, config) {
* @param {Object} config
*/

Base.prototype.argument = function (name, config) {
Generator.prototype.argument = function (name, config) {
config = config || {};

// Alias default to defaults for backward compatibility.
Expand All @@ -270,7 +270,7 @@ Base.prototype.argument = function (name, config) {
return this;
};

Base.prototype.parseOptions = function () {
Generator.prototype.parseOptions = function () {
const minimistDef = {
string: [],
boolean: [],
Expand Down Expand Up @@ -346,7 +346,7 @@ Base.prototype.parseOptions = function () {
this.checkRequiredArgs();
};

Base.prototype.checkRequiredArgs = function () {
Generator.prototype.checkRequiredArgs = function () {
// If the help option was provided, we don't want to check for required
// arguments, since we're only going to print the help message anyway.
if (this.options.help) {
Expand Down Expand Up @@ -381,7 +381,7 @@ Base.prototype.checkRequiredArgs = function () {
* @param {Function} [cb]
*/

Base.prototype.run = function (cb) {
Generator.prototype.run = function (cb) {
cb = cb || function () {};

const self = this;
Expand Down Expand Up @@ -496,7 +496,7 @@ Base.prototype.run = function (cb) {
* this.composeWith(require.resolve('generator-bootstrap/app/main.js'), { sass: true });
*/

Base.prototype.composeWith = function (modulePath, options) {
Generator.prototype.composeWith = function (modulePath, options) {
let generator;
options = options || {};

Expand Down Expand Up @@ -537,21 +537,21 @@ Base.prototype.composeWith = function (modulePath, options) {
};

/**
* Determine the root generator name (the one who's extending Base).
* Determine the root generator name (the one who's extending Generator).
* @return {String} The name of the root generator
*/

Base.prototype.rootGeneratorName = function () {
Generator.prototype.rootGeneratorName = function () {
const pkg = readPkgUp.sync({cwd: this.resolved}).pkg;
return pkg ? pkg.name : '*';
};

/**
* Determine the root generator version (the one who's extending Base).
* Determine the root generator version (the one who's extending Generator).
* @return {String} The version of the root generator
*/

Base.prototype.rootGeneratorVersion = function () {
Generator.prototype.rootGeneratorVersion = function () {
const pkg = readPkgUp.sync({cwd: this.resolved}).pkg;
return pkg ? pkg.version : '0.0.0';
};
Expand All @@ -562,7 +562,7 @@ Base.prototype.rootGeneratorVersion = function () {
* @private
*/

Base.prototype._getStorage = function () {
Generator.prototype._getStorage = function () {
const storePath = path.join(this.destinationRoot(), '.yo-rc.json');
return new Storage(this.rootGeneratorName(), this.fs, storePath);
};
Expand All @@ -573,7 +573,7 @@ Base.prototype._getStorage = function () {
* @private
*/

Base.prototype._getGlobalStorage = function () {
Generator.prototype._getGlobalStorage = function () {
const storePath = path.join(os.homedir(), '.yo-rc-global.json');
const storeName = util.format('%s:%s', this.rootGeneratorName(), this.rootGeneratorVersion());
return new Storage(storeName, this.fs, storePath);
Expand All @@ -587,7 +587,7 @@ Base.prototype._getGlobalStorage = function () {
* @return {String} destination root path
*/

Base.prototype.destinationRoot = function (rootPath) {
Generator.prototype.destinationRoot = function (rootPath) {
if (_.isString(rootPath)) {
this._destinationRoot = path.resolve(rootPath);

Expand All @@ -612,7 +612,7 @@ Base.prototype.destinationRoot = function (rootPath) {
* @return {String} source root path
*/

Base.prototype.sourceRoot = function (rootPath) {
Generator.prototype.sourceRoot = function (rootPath) {
if (_.isString(rootPath)) {
this._sourceRoot = path.resolve(rootPath);
}
Expand All @@ -626,7 +626,7 @@ Base.prototype.sourceRoot = function (rootPath) {
* @return {String} joined path
*/

Base.prototype.templatePath = function () {
Generator.prototype.templatePath = function () {
let filepath = path.join.apply(path, arguments);

if (!path.isAbsolute(filepath)) {
Expand All @@ -642,7 +642,7 @@ Base.prototype.templatePath = function () {
* @return {String} joined path
*/

Base.prototype.destinationPath = function () {
Generator.prototype.destinationPath = function () {
let filepath = path.join.apply(path, arguments);

if (!path.isAbsolute(filepath)) {
Expand All @@ -660,7 +660,7 @@ Base.prototype.destinationPath = function () {
* Finally defaults to the name of the current directory.
* @return {String} The name of the application
*/
Base.prototype.determineAppname = function () {
Generator.prototype.determineAppname = function () {
let appname = this.fs.readJSON(this.destinationPath('bower.json'), {}).name;

if (!appname) {
Expand All @@ -683,7 +683,7 @@ Base.prototype.determineAppname = function () {
* or a single one.
* @return {this}
*/
Base.prototype.registerTransformStream = function (streams) {
Generator.prototype.registerTransformStream = function (streams) {
assert(streams, 'expected to receive a transform stream as parameter');
if (!_.isArray(streams)) {
streams = [streams];
Expand All @@ -695,8 +695,9 @@ Base.prototype.registerTransformStream = function (streams) {
/**
* Write memory fs file to disk and logging results
* @param {Function} done - callback once files are written
* @private
*/
Base.prototype._writeFiles = function (done) {
Generator.prototype._writeFiles = function (done) {
const self = this;

const conflictChecker = through.obj(function (file, enc, cb) {
Expand Down Expand Up @@ -738,19 +739,4 @@ Base.prototype._writeFiles = function (done) {
});
};

/**
* Extend the Generator class to create a new one inherithing the base one. This
* method is useful if your environment do not suport the ES6 classes.
* @param {Object} protoProps Prototype properties (available on the instances)
* @param {Object} staticProps Static properties (available on the contructor)
* @return {Object} New sub class
* @example
* var Generator = require('yeoman-generator');
* module.exports = Generator.extend({
* writing: function () {}
* // ...
* });
*/
Base.extend = require('class-extend').extend;

module.exports = Base;
module.exports = Generator;
4 changes: 2 additions & 2 deletions lib/util/prompt-suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const assert = require('assert');
const _ = require('lodash');

/**
* @mixin
* @alias util/prompt-suggestion
* @module promptSuggestion
* @description Utilities to allow remembering answers to Inquirer.js prompts
*/
const promptSuggestion = module.exports;

Expand Down
11 changes: 6 additions & 5 deletions lib/util/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const _ = require('lodash');
/**
* Storage instances handle a json file where Generator authors can store data.
*
* `Base` instantiate the storage as `config` by default.
* The `Generator` class instantiate the storage named `config` by default.
*
* @constructor
* @param {String} name The name of the new storage (this is a namespace)
* @param {mem-fs-editor} fs A mem-fs editor instance
* @param {String} configPath The filepath used as a storage.
*
* @example
* var MyGenerator = yeoman.base.extend({
* config: function() {
* class extend Generator {
* writing: function() {
* this.config.set('coffeescript', false);
* }
* });
* }
*/
class Storage {
constructor(name, fs, configPath) {
Expand All @@ -32,8 +32,8 @@ class Storage {

/**
* Return the current store as JSON object
* @private
* @return {Object} the store content
* @private
*/
get _store() {
return this.fs.readJSON(this.path, {})[this.name] || {};
Expand All @@ -42,6 +42,7 @@ class Storage {
/**
* Persist a configuration to disk
* @param {Object} val - current configuration values
* @private
*/
_persist(val) {
const fullStore = this.fs.readJSON(this.path, {});
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"async": "^2.0.0",
"chalk": "^1.0.0",
"class-extend": "^0.1.0",
"cli-table": "^0.3.1",
"cross-spawn": "^5.0.1",
"dargs": "^5.1.0",
Expand Down
Loading

0 comments on commit 00912ce

Please sign in to comment.