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