Skip to content

Commit

Permalink
Load environment resources when running help. (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 27, 2021
1 parent a0313cd commit 3579322
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
46 changes: 25 additions & 21 deletions lib/index.js
Expand Up @@ -189,19 +189,28 @@ class Generator extends Base {
});

this.env = this.options.env;
if (!this.env) {
throw new Error('This generator requires an environment.');
}

this.resolved = this.options.resolved || __dirname;
this.description = this.description || '';

// Determine the app root
this.contextRoot = this.env.cwd;
this.destinationRoot(this.options.destinationRoot || this.env.cwd);
if (this.env) {
// Determine the app root
this.contextRoot = this.env.cwd;
this.destinationRoot(this.options.destinationRoot || this.env.cwd);

// Ensure source/destination path, can be configured from subclasses
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));

// Ensure source/destination path, can be configured from subclasses
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));
this.fs = this.env.fs;
}

// Add convenience debug object
this._debug = createDebug(
this.options.namespace || 'yeoman:unknownnamespace'
);

// Expose utilities for dependency-less generators.
this._ = _;

if (this.options.help) {
return;
Expand Down Expand Up @@ -232,8 +241,13 @@ class Generator extends Base {
this.features.uniqueBy = uniqueBy;
}

if (!this.env) {
throw new Error('This generator requires an environment.');
}

// Ensure the environment support features this yeoman-generator version require.
if (
!this.env ||
!this.env.adapter ||
!this.env.runLoop ||
!this.env.sharedFs ||
Expand All @@ -244,22 +258,15 @@ class Generator extends Base {
);
}

this.fs = this.env.fs;

// Place holder for run-async callback.
this.async = () => () => {};

// Mirror the adapter log method on the generator.
//
// example:
// this.log('foo');
// this.log.error('bar');
this.log = this.env.adapter.log;
this.log = this.env.adapter && this.env.adapter.log;

// Add convenience debug object
this._debug = createDebug(
this.options.namespace || 'yeoman:unknownnamespace'
);
// Place holder for run-async callback.
this.async = () => () => {};

this.appname = this.determineAppname();

Expand Down Expand Up @@ -292,9 +299,6 @@ class Generator extends Base {

this.compose = this.options.compose;

// Expose utilities for dependency-less generators.
this._ = _;

// Requires environment 3
if (!this.options.skipCheckEnv) {
this.checkEnvironmentVersion('3.0.0');
Expand Down
29 changes: 29 additions & 0 deletions test/base.js
Expand Up @@ -130,6 +130,35 @@ describe('Base', () => {

assert(generator.fs);
});

it('setup required fields for a working generator for help', function () {
const generator = new Base([], {
env: this.env,
help: true,
resolved: 'test'
});

assert(generator.env);
assert(generator.fs);
assert(generator._debug);
assert(generator._);

generator.option('foo');
generator.argument('baz');
});

it('should not fail without an env for help', () => {
const generator = new Base([], {
help: true,
resolved: 'test'
});

assert(generator._debug);
assert(generator._);

generator.option('foo');
generator.argument('baz');
});
});

describe('prototype', () => {
Expand Down

0 comments on commit 3579322

Please sign in to comment.