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