1 | 'use strict';
|
2 | var crypto = require('crypto');
|
3 | var path = require('path');
|
4 | var os = require('os');
|
5 | var assert = require('assert');
|
6 | var _ = require('lodash');
|
7 | var Promise = require('pinkie-promise');
|
8 | var yeoman = require('yeoman-environment');
|
9 | var util = require('util');
|
10 | var rimraf = require('rimraf');
|
11 | var EventEmitter = require('events').EventEmitter;
|
12 | var helpers = require('./');
|
13 | var TestAdapter = require('./adapter').TestAdapter;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function RunContext(Generator, settings) {
|
31 | this._asyncHolds = 0;
|
32 | this.ran = false;
|
33 | this.inDirSet = false;
|
34 | this.args = [];
|
35 | this.options = {};
|
36 | this.answers = {};
|
37 | this.localConfig = null;
|
38 | this.dependencies = [];
|
39 | this.Generator = Generator;
|
40 | this.settings = _.extend(
|
41 | { tmpdir: true, namespace: 'gen:test', compatibility: false },
|
42 | settings
|
43 | );
|
44 | this.withOptions({
|
45 | force: true,
|
46 | skipCache: true,
|
47 | skipInstall: true
|
48 | });
|
49 | setTimeout(this._run.bind(this), 10);
|
50 | }
|
51 |
|
52 | util.inherits(RunContext, EventEmitter);
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | RunContext.prototype.async = function() {
|
60 | this._asyncHolds++;
|
61 |
|
62 | return function() {
|
63 | this._asyncHolds--;
|
64 | this._run();
|
65 | }.bind(this);
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | RunContext.prototype._run = function() {
|
74 | if (!this.inDirSet && this.settings.tmpdir) {
|
75 | this.inTmpDir();
|
76 | }
|
77 |
|
78 | if (this._asyncHolds !== 0 || this.ran) {
|
79 | return;
|
80 | }
|
81 |
|
82 | this.ran = true;
|
83 | var namespace;
|
84 | this.env = (this.envCB || (env => env)).call(
|
85 | this,
|
86 | yeoman.createEnv([], {}, new TestAdapter())
|
87 | );
|
88 |
|
89 | helpers.registerDependencies(this.env, this.dependencies);
|
90 |
|
91 | if (_.isString(this.Generator)) {
|
92 | namespace = this.env.namespace(this.Generator);
|
93 | this.env.register(this.Generator);
|
94 | } else {
|
95 | namespace = this.settings.namespace;
|
96 | this.env.registerStub(this.Generator, namespace, this.settings.resolved);
|
97 | }
|
98 |
|
99 | this.generator = this.env.create(namespace, {
|
100 | arguments: this.args,
|
101 | options: this.options
|
102 | });
|
103 |
|
104 | helpers.mockPrompt(this.generator, this.answers);
|
105 |
|
106 | if (this.localConfig) {
|
107 |
|
108 | helpers.mockLocalConfig(this.generator, this.localConfig);
|
109 | }
|
110 |
|
111 | this.generator.on(
|
112 | 'error',
|
113 | function(err) {
|
114 | if (!this.emit('error', err)) {
|
115 | throw err;
|
116 | }
|
117 | }.bind(this)
|
118 | );
|
119 | this.generator.once(
|
120 | 'end',
|
121 | function() {
|
122 | helpers.restorePrompt(this.generator);
|
123 | this.emit('end');
|
124 | this.completed = true;
|
125 | }.bind(this)
|
126 | );
|
127 |
|
128 | this.emit('ready', this.generator);
|
129 |
|
130 | const generatorPromise = this.generator.run();
|
131 | if (!this.settings.compatibility) {
|
132 | generatorPromise.catch(err => this.emit('error', err));
|
133 | }
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | RunContext.prototype.toPromise = function() {
|
141 | return new Promise(
|
142 | function(resolve, reject) {
|
143 | this.on(
|
144 | 'end',
|
145 | function() {
|
146 | resolve(this.targetDirectory);
|
147 | }.bind(this)
|
148 | );
|
149 | this.on('error', reject);
|
150 | }.bind(this)
|
151 | );
|
152 | };
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | RunContext.prototype.then = function() {
|
159 | var promise = this.toPromise();
|
160 | return promise.then.apply(promise, arguments);
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | RunContext.prototype.catch = function() {
|
168 | var promise = this.toPromise();
|
169 | return promise.catch.apply(promise, arguments);
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | RunContext.prototype.inDir = function(dirPath, cb) {
|
181 | this.inDirSet = true;
|
182 | this.targetDirectory = dirPath;
|
183 | var release = this.async();
|
184 | var callBackThenRelease = _.flowRight(
|
185 | release,
|
186 | (cb || _.noop).bind(this, path.resolve(dirPath))
|
187 | );
|
188 | helpers.testDirectory(dirPath, callBackThenRelease);
|
189 | return this;
|
190 | };
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | RunContext.prototype.cd = function(dirPath) {
|
199 | this.inDirSet = true;
|
200 | this.targetDirectory = dirPath;
|
201 | dirPath = path.resolve(dirPath);
|
202 | try {
|
203 | process.chdir(dirPath);
|
204 | } catch (err) {
|
205 | throw new Error(err.message + ' ' + dirPath);
|
206 | }
|
207 |
|
208 | return this;
|
209 | };
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | RunContext.prototype.inTmpDir = function(cb) {
|
221 | var tmpdir = path.join(os.tmpdir(), crypto.randomBytes(20).toString('hex'));
|
222 | return this.inDir(tmpdir, cb);
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | RunContext.prototype.withEnvironment = function(cb) {
|
235 | this.envCB = cb;
|
236 | return this;
|
237 | };
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | RunContext.prototype.cleanTestDirectory = function() {
|
243 | if (this.targetDirectory) {
|
244 | rimraf.sync(this.targetDirectory);
|
245 | }
|
246 | };
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | RunContext.prototype.withArguments = function(args) {
|
255 | var argsArray = _.isString(args) ? args.split(' ') : args;
|
256 | assert(
|
257 | _.isArray(argsArray),
|
258 | 'args should be either a string separated by spaces or an array'
|
259 | );
|
260 | this.args = this.args.concat(argsArray);
|
261 | return this;
|
262 | };
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | RunContext.prototype.withOptions = function(options) {
|
271 |
|
272 |
|
273 | Object.keys(options).forEach(function(key) {
|
274 | options[_.camelCase(key)] = options[key];
|
275 | options[_.kebabCase(key)] = options[key];
|
276 | });
|
277 |
|
278 | this.options = _.extend(this.options, options);
|
279 | return this;
|
280 | };
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | RunContext.prototype.withPrompts = function(answers) {
|
289 | this.answers = _.extend(this.answers, answers);
|
290 | return this;
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | RunContext.prototype.withGenerators = function(dependencies) {
|
311 | assert(_.isArray(dependencies), 'dependencies should be an array');
|
312 | this.dependencies = this.dependencies.concat(dependencies);
|
313 | return this;
|
314 | };
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | RunContext.prototype.withLocalConfig = function(localConfig) {
|
322 | assert(_.isObject(localConfig), 'config should be an object');
|
323 | this.localConfig = localConfig;
|
324 | return this;
|
325 | };
|
326 |
|
327 | module.exports = RunContext;
|