Skip to content

Commit 02b1395

Browse files
committed
Implement checkEnvironmentVersion
1 parent bec0939 commit 02b1395

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed

lib/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const EventEmitter = require('events');
66
const assert = require('assert');
77
const _ = require('lodash');
88
const findUp = require('find-up');
9+
const semver = require('semver');
910
const readPkgUp = require('read-pkg-up');
1011
const chalk = require('chalk');
1112
const makeDir = require('make-dir');
@@ -20,6 +21,7 @@ const promptSuggestion = require('./util/prompt-suggestion');
2021

2122
const EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';
2223
const debug = createDebug('yeoman:generator');
24+
const ENV_VER_WITH_VER_API = '3.0.0';
2325

2426
// Ensure a prototype method is a candidate run by default
2527
const methodIsValid = function(name) {
@@ -368,6 +370,50 @@ class Generator extends EventEmitter {
368370
}
369371
}
370372

373+
checkEnvironmentVersion(packageDependency, version) {
374+
if (version === undefined) {
375+
version = packageDependency;
376+
packageDependency = 'yeoman-environment';
377+
}
378+
379+
version = version || ENV_VER_WITH_VER_API;
380+
const returnError = currentVersion => {
381+
return new Error(
382+
`This generator (${this.options.namespace}) requires ${packageDependency} at least ${version}, current version is ${currentVersion}`
383+
);
384+
};
385+
386+
if (!this.env.getVersion) {
387+
if (!this.options.ignoreVersionCheck) {
388+
throw returnError(`less than ${ENV_VER_WITH_VER_API}`);
389+
}
390+
391+
console.warn(
392+
`It's not possible to check version with running Environment less than ${ENV_VER_WITH_VER_API}`
393+
);
394+
console.warn('Some features may be missing');
395+
if (semver.lte(version, '2.8.1')) {
396+
return undefined;
397+
}
398+
399+
return false;
400+
}
401+
402+
let runningVersion = this.env.getVersion(packageDependency);
403+
if (runningVersion !== undefined && semver.lte(version, runningVersion)) {
404+
return true;
405+
}
406+
407+
if (this.options.ignoreVersionCheck) {
408+
console.warn(
409+
`Current ${packageDependency} is not compatible with current generator, min required: ${version} current version: ${runningVersion}. Some features may be missing.`
410+
);
411+
return false;
412+
}
413+
414+
throw returnError(runningVersion);
415+
}
416+
371417
/**
372418
* Convenience debug method
373419
*

test/environment.js

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
'use strict';
2+
const os = require('os');
3+
const path = require('path');
4+
const sinon = require('sinon');
5+
const Environment = require('yeoman-environment');
6+
const assert = require('assert');
7+
const helpers = require('yeoman-test');
8+
const { TestAdapter } = require('yeoman-test/lib/adapter');
9+
10+
const Base = require('..');
11+
12+
const tmpdir = path.join(os.tmpdir(), 'yeoman-generator-environment');
13+
14+
describe('Generator with environment version', () => {
15+
before(helpers.setUpTestDirectory(tmpdir));
16+
describe('mocked 3.0.0', () => {
17+
before(function() {
18+
this.timeout(100000);
19+
this.env = Environment.createEnv([], { 'skip-install': true }, new TestAdapter());
20+
this.env.getVersion = this.env.getVersion || (() => {});
21+
this.getVersionStub = sinon.stub(this.env, 'getVersion');
22+
23+
this.Dummy = class extends Base {};
24+
this.dummy = new this.Dummy(['bar', 'baz', 'bom'], {
25+
foo: false,
26+
something: 'else',
27+
namespace: 'dummy',
28+
env: this.env,
29+
'skip-install': true
30+
});
31+
});
32+
33+
after(function() {
34+
this.getVersionStub.restore();
35+
});
36+
37+
describe('#checkEnvironmentVersion', () => {
38+
describe('without args', () => {
39+
it('returns true', function() {
40+
this.getVersionStub.returns('3.0.0');
41+
assert.equal(this.dummy.checkEnvironmentVersion(), true);
42+
});
43+
});
44+
45+
describe('with required environment', () => {
46+
before(function() {
47+
this.getVersionStub.returns('3.0.1');
48+
});
49+
50+
it('returns true', function() {
51+
assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true);
52+
});
53+
54+
describe('with ignoreVersionCheck', () => {
55+
before(function() {
56+
this.dummy.options.ignoreVersionCheck = true;
57+
});
58+
59+
after(function() {
60+
this.dummy.options.ignoreVersionCheck = false;
61+
});
62+
63+
it('returns true', function() {
64+
this.getVersionStub.returns('3.0.1');
65+
assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true);
66+
});
67+
});
68+
});
69+
70+
describe('with greater than required environment', () => {
71+
it('returns true', function() {
72+
this.getVersionStub.returns('3.0.2');
73+
assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true);
74+
});
75+
});
76+
77+
describe('with less than required environment', () => {
78+
before(function() {
79+
this.getVersionStub.returns('3.0.0');
80+
});
81+
82+
it('returns true', function() {
83+
assert.throws(
84+
() => this.dummy.checkEnvironmentVersion('3.0.1'),
85+
/requires yeoman-environment at least 3.0.1, current version is 3.0.0$/
86+
);
87+
});
88+
89+
describe('with ignoreVersionCheck', () => {
90+
before(function() {
91+
this.dummy.options.ignoreVersionCheck = true;
92+
});
93+
94+
after(function() {
95+
this.dummy.options.ignoreVersionCheck = false;
96+
});
97+
98+
it('returns false', function() {
99+
assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), false);
100+
});
101+
});
102+
});
103+
104+
describe('with required inquirer', () => {
105+
it('returns true', function() {
106+
this.getVersionStub.withArgs('inquirer').returns('7.1.0');
107+
assert.equal(this.dummy.checkEnvironmentVersion('inquirer', '7.1.0'), true);
108+
});
109+
});
110+
111+
describe('with greater than required inquirer', () => {
112+
it('returns true', function() {
113+
this.getVersionStub.withArgs('inquirer').returns('7.1.1');
114+
assert.equal(this.dummy.checkEnvironmentVersion('inquirer', '7.1.0'), true);
115+
});
116+
});
117+
118+
describe('with less than required inquirer', () => {
119+
before(function() {
120+
this.getVersionStub.withArgs('inquirer').returns('7.1.0');
121+
});
122+
123+
it('throws exception', function() {
124+
assert.throws(
125+
() => this.dummy.checkEnvironmentVersion('inquirer', '7.1.1'),
126+
/requires inquirer at least 7.1.1, current version is 7.1.0$/
127+
);
128+
});
129+
130+
describe('with ignoreVersionCheck', () => {
131+
before(function() {
132+
this.dummy.options.ignoreVersionCheck = true;
133+
});
134+
135+
after(function() {
136+
this.dummy.options.ignoreVersionCheck = false;
137+
});
138+
139+
it('returns false', function() {
140+
assert.equal(this.dummy.checkEnvironmentVersion('inquirer', '7.1.1'), false);
141+
});
142+
});
143+
});
144+
});
145+
});
146+
147+
describe('mocked 2.8.1', () => {
148+
before(function() {
149+
this.timeout(100000);
150+
this.env = Environment.createEnv([], { 'skip-install': true }, new TestAdapter());
151+
this.getVersion = Environment.prototype.getVersion;
152+
delete Environment.prototype.getVersion;
153+
154+
this.Dummy = class extends Base {};
155+
this.dummy = new this.Dummy(['bar', 'baz', 'bom'], {
156+
foo: false,
157+
something: 'else',
158+
namespace: 'dummy',
159+
env: this.env,
160+
'skip-install': true
161+
});
162+
});
163+
164+
after(function() {
165+
Environment.prototype.getVersion = this.getVersion;
166+
});
167+
168+
describe('#checkEnvironmentVersion', () => {
169+
describe('without args', () => {
170+
it('throws exception', function() {
171+
assert.throws(
172+
() => this.dummy.checkEnvironmentVersion(),
173+
/requires yeoman-environment at least 3.0.0, current version is less than 3.0.0$/
174+
);
175+
});
176+
});
177+
178+
describe('with ignoreVersionCheck', () => {
179+
before(function() {
180+
this.dummy.options.ignoreVersionCheck = true;
181+
});
182+
183+
after(function() {
184+
this.dummy.options.ignoreVersionCheck = false;
185+
});
186+
187+
describe('without args', () => {
188+
it('returns false', function() {
189+
assert.equal(this.dummy.checkEnvironmentVersion(), false);
190+
});
191+
});
192+
193+
describe('without less then 3.0.0', () => {
194+
it('returns undefined', function() {
195+
assert.equal(this.dummy.checkEnvironmentVersion('2.9.0'), false);
196+
});
197+
});
198+
});
199+
});
200+
});
201+
});

0 commit comments

Comments
 (0)