Skip to content

Commit 3579322

Browse files
authored
Load environment resources when running help. (#1288)
1 parent a0313cd commit 3579322

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

lib/index.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,28 @@ class Generator extends Base {
189189
});
190190

191191
this.env = this.options.env;
192-
if (!this.env) {
193-
throw new Error('This generator requires an environment.');
194-
}
195192

196193
this.resolved = this.options.resolved || __dirname;
197194
this.description = this.description || '';
198195

199-
// Determine the app root
200-
this.contextRoot = this.env.cwd;
201-
this.destinationRoot(this.options.destinationRoot || this.env.cwd);
196+
if (this.env) {
197+
// Determine the app root
198+
this.contextRoot = this.env.cwd;
199+
this.destinationRoot(this.options.destinationRoot || this.env.cwd);
200+
201+
// Ensure source/destination path, can be configured from subclasses
202+
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));
202203

203-
// Ensure source/destination path, can be configured from subclasses
204-
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));
204+
this.fs = this.env.fs;
205+
}
206+
207+
// Add convenience debug object
208+
this._debug = createDebug(
209+
this.options.namespace || 'yeoman:unknownnamespace'
210+
);
211+
212+
// Expose utilities for dependency-less generators.
213+
this._ = _;
205214

206215
if (this.options.help) {
207216
return;
@@ -232,8 +241,13 @@ class Generator extends Base {
232241
this.features.uniqueBy = uniqueBy;
233242
}
234243

244+
if (!this.env) {
245+
throw new Error('This generator requires an environment.');
246+
}
247+
235248
// Ensure the environment support features this yeoman-generator version require.
236249
if (
250+
!this.env ||
237251
!this.env.adapter ||
238252
!this.env.runLoop ||
239253
!this.env.sharedFs ||
@@ -244,22 +258,15 @@ class Generator extends Base {
244258
);
245259
}
246260

247-
this.fs = this.env.fs;
248-
249-
// Place holder for run-async callback.
250-
this.async = () => () => {};
251-
252261
// Mirror the adapter log method on the generator.
253262
//
254263
// example:
255264
// this.log('foo');
256265
// this.log.error('bar');
257-
this.log = this.env.adapter.log;
266+
this.log = this.env.adapter && this.env.adapter.log;
258267

259-
// Add convenience debug object
260-
this._debug = createDebug(
261-
this.options.namespace || 'yeoman:unknownnamespace'
262-
);
268+
// Place holder for run-async callback.
269+
this.async = () => () => {};
263270

264271
this.appname = this.determineAppname();
265272

@@ -292,9 +299,6 @@ class Generator extends Base {
292299

293300
this.compose = this.options.compose;
294301

295-
// Expose utilities for dependency-less generators.
296-
this._ = _;
297-
298302
// Requires environment 3
299303
if (!this.options.skipCheckEnv) {
300304
this.checkEnvironmentVersion('3.0.0');

test/base.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,35 @@ describe('Base', () => {
130130

131131
assert(generator.fs);
132132
});
133+
134+
it('setup required fields for a working generator for help', function () {
135+
const generator = new Base([], {
136+
env: this.env,
137+
help: true,
138+
resolved: 'test'
139+
});
140+
141+
assert(generator.env);
142+
assert(generator.fs);
143+
assert(generator._debug);
144+
assert(generator._);
145+
146+
generator.option('foo');
147+
generator.argument('baz');
148+
});
149+
150+
it('should not fail without an env for help', () => {
151+
const generator = new Base([], {
152+
help: true,
153+
resolved: 'test'
154+
});
155+
156+
assert(generator._debug);
157+
assert(generator._);
158+
159+
generator.option('foo');
160+
generator.argument('baz');
161+
});
133162
});
134163

135164
describe('prototype', () => {

0 commit comments

Comments
 (0)