-
Notifications
You must be signed in to change notification settings - Fork 18
/
pristine.js
372 lines (296 loc) · 10.8 KB
/
pristine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use strict';
const path = require('path');
const debug = require('./debug');
const temp = require('./temp');
const chdir = require('./chdir');
const fs = require('fs-extra');
const findup = require('findup-sync');
const existsSync = fs.existsSync;
const moveDirectory = require('./move-directory');
const symlinkDirectory = require('./symlink-directory');
const runCommand = require('./run-command');
const runEmber = require('./run-ember');
const runNew = require('./run-new');
const semver = require('semver');
// As of Ember-CLI@2.13.0, it no longer uses Bower by default
const usesBower = semver.lt(require('ember-cli/package').version, '2.13.0');
const runCommandOptions = {
// Note: We must override the default logOnFailure logging, because we are
// not inside a test.
log() {
return; // no output for initial application build
}
};
let previousCwd;
/**
* Creates a new application with the given name. If the application was created
* previously, a fresh clone will be created.
*
* @param {String} appName
* @param {Object} [options]
* @param {String} [options.emberVersion]
* @param {String} [options.emberDataVersion]
*/
function createApp(appName, options) {
let tempDir = temp.ensureCreated();
previousCwd = process.cwd();
debug('previousCwd=%s', previousCwd);
chdir(tempDir);
let pristineAppPath = path.join(temp.pristinePath, appName);
let underTestAppPath = path.join(temp.underTestPath, appName);
// If this app was already tested, delete the copy.
// This ensures that any modifications made during testing are
// reset.
if (existsSync(underTestAppPath)) {
fs.removeSync(underTestAppPath);
}
// If a pristine version of the app doesn't exist, create one by installing it.
let appInstallation;
if (!fs.existsSync(pristineAppPath)) {
appInstallation = installPristineApp(appName, options);
} else {
appInstallation = Promise.resolve();
}
return appInstallation.then(() => {
copyUnderTestApp(pristineAppPath, underTestAppPath);
chdir(previousCwd);
return underTestAppPath;
});
}
module.exports = {
createApp
};
function installPristineApp(appName, options) {
let hasNodeModules = hasPristineNodeModules();
let hasBowerComponents = hasPristineBowerComponents();
let skipNpm = options.skipNpm;
chdir(temp.pristinePath);
// Install a vanilla app and cd into it
let args = generateArgsForEmberNew(
hasNodeModules,
hasBowerComponents,
skipNpm
);
let promise = runNew(appName, args)
.catch(handleResult)
.then(() => chdir(path.join(temp.pristinePath, appName)));
let setupOptions = {
appName,
hasNodeModules,
hasBowerComponents,
skipNpm,
emberVersion: options.emberVersion || 'canary',
emberDataVersion: options.emberDataVersion || '^3.8.0'
};
promise = promise
.then(() => nodeModulesSetup(setupOptions))
.then(() => {
if (usesBower) {
return bowerSetup(setupOptions);
}
});
// All dependencies should be installed, so symlink them into the app and
// run the addon's blueprint to finish the app creation.
return promise
.then(() => linkDependencies(appName))
.then(runAddonGenerator);
}
// Generates the arguments to pass to `ember new`. Optionally skipping the
// npm and/or bower installation phases.
function generateArgsForEmberNew(hasNodeModules, hasBowerComponents, skipNpm) {
let extraOptions = [];
if (skipNpm || hasNodeModules) {
debug('skipping npm');
extraOptions.push('--skip-npm');
}
if (hasBowerComponents) {
debug('skipping bower');
extraOptions.push('--skip-bower');
}
return extraOptions.concat(runCommandOptions);
}
function nodeModulesSetup(options) {
let appName = options.appName;
let emberVersion = options.emberVersion;
let emberDataVersion = options.emberDataVersion;
let hasNodeModules = options.hasNodeModules;
let skipNpm = options.skipNpm;
// Modifies the package.json to include correct versions of dependencies
let promise = Promise.resolve()
.then(() => addEmberDataToDependencies(appName, emberDataVersion))
.then(() => updateEmberSource(appName, emberVersion));
if (!hasNodeModules) {
if (!skipNpm) {
promise = promise
.then(() => {
debug('installing ember-disable-prototype-extensions');
return runCommand('npm', 'install', 'ember-disable-prototype-extensions');
})
.then(() => {
debug('installed ember-disable-prototype-extension');
return runCommand('npm', 'install');
})
.then(() => debug('installed ember-data ' + emberDataVersion));
}
promise = promise
.then(() => symlinkAddon(appName))
.then(() => movePristineNodeModules(appName));
}
return promise.then(() => addAddonUnderTestToDependencies(appName));
}
function bowerSetup(options) {
let appName = options.appName;
let emberVersion = options.emberVersion;
let hasBowerComponents = options.hasBowerComponents;
let promise = Promise.resolve()
.then(() => addEmberToBowerJSON(appName, emberVersion))
.then(() => removeEmberDataFromBowerJSON(appName));
if (!hasBowerComponents) {
promise = promise
.then(() => runCommand('bower', 'install'))
.then(() => debug('installed ember ' + emberVersion))
.then(() => movePristineBowerComponents(appName));
}
return promise;
}
function hasPristineNodeModules() {
return existsSync(temp.pristineNodeModulesPath);
}
function hasPristineBowerComponents() {
return existsSync(temp.pristineBowerComponentsPath);
}
function movePristineNodeModules(appName) {
let nodeModulesPath = path.join(temp.pristinePath, appName, 'node_modules');
moveDirectory(nodeModulesPath, temp.pristineNodeModulesPath);
}
function movePristineBowerComponents(appName) {
let bowerComponentsPath = path.join(temp.pristinePath, appName, 'bower_components');
moveDirectory(bowerComponentsPath, temp.pristineBowerComponentsPath);
}
function handleResult(result) {
debug('handling result');
throw result;
}
function copyUnderTestApp(pristineAppPath, underTestAppPath) {
debug('copying pristine app; from=' + pristineAppPath + '; to=' + underTestAppPath);
fs.copySync(pristineAppPath, underTestAppPath);
debug('copying complete');
}
function addEmberToBowerJSON(appName, version) {
let bowerJSONPath = path.join(temp.pristinePath, appName, 'bower.json');
let bowerJSON = fs.readJsonSync(bowerJSONPath);
bowerJSON.resolutions = {
'ember': version
};
bowerJSON.dependencies['ember'] = version;
fs.writeJsonSync(bowerJSONPath, bowerJSON);
}
function removeEmberDataFromBowerJSON(appName) {
let bowerJSONPath = path.join(temp.pristinePath, appName, 'bower.json');
let bowerJSON = fs.readJsonSync(bowerJSONPath);
delete bowerJSON.dependencies['ember-data'];
fs.writeJsonSync(bowerJSONPath, bowerJSON);
}
function symlinkAddon(appName) {
let pkg = findAddonPackageJSON();
let addonPath = findAddonPath();
let nodeModules = path.join(temp.pristinePath, appName, 'node_modules');
// if option skipNpm is supplied, node_modules doesn't exist yet
fs.ensureDirSync(nodeModules);
let addonSymlinkPath = path.join(nodeModules, pkg.name);
let pkgScope = path.dirname(pkg.name);
debug('symlinking %s', pkg.name);
if (pkgScope !== '.') {
debug('symlink: creating directory %s for scoped package name', pkgScope);
// In case the package has a scoped name, make sure the scope directoy exists before symlinking
fs.ensureDirSync(path.join(temp.pristinePath, appName, 'node_modules', pkgScope));
}
if (existsSync(addonSymlinkPath)) {
let stats = fs.lstatSync(addonSymlinkPath);
if (stats.isSymbolicLink()) {
debug('%s is already symlinked', pkg.name);
return;
}
fs.removeSync(addonSymlinkPath);
}
symlinkDirectory(addonPath, addonSymlinkPath);
}
function addEmberDataToDependencies(appName, version) {
let packageJSONPath = path.join(temp.pristinePath, appName, 'package.json');
debug('installing ember-data ' + version);
let packageJSON = fs.readJsonSync(packageJSONPath);
packageJSON.devDependencies['ember-data'] = version;
fs.writeJsonSync('package.json', packageJSON);
}
function updateEmberSource(appName, version) {
let packageJSONPath = path.join(temp.pristinePath, appName, 'package.json');
let packageJSON = fs.readJsonSync(packageJSONPath);
// If we're not using bower, but the ember version is canary, we change it to
// beta instead. This is because ember-source does not support canary releases.
if (!usesBower && version === 'canary') {
debug('ember-source cannot use canary releases, defaulting to beta');
version = 'beta';
}
if (packageJSON.devDependencies.hasOwnProperty('ember-source')) {
// If we're using bower, we need to remove ember-source from package.json,
// otherwise we update to the appropriate version.
if (usesBower) {
debug('removing ember-source from NPM ');
delete packageJSON.devDependencies['ember-source'];
} else {
debug('updating ember-source version to %s', version);
packageJSON.devDependencies['ember-source'] = version;
}
fs.writeJsonSync('package.json', packageJSON);
}
}
function addAddonUnderTestToDependencies(appName) {
let pkg = findAddonPackageJSON();
let packageJSONPath = path.join(temp.pristinePath, appName, 'package.json');
debug('installing %s as addon for application', pkg.name);
// Read the current version of the FastBoot addon under test, then add that
// to the Ember app's package.json.
let packageJSON = fs.readJsonSync(packageJSONPath);
packageJSON.devDependencies[pkg.name] = pkg.version;
fs.writeJsonSync('package.json', packageJSON);
}
function runAddonGenerator() {
let pkg = findAddonPackageJSON();
debug('running %s generator', pkg.name);
let blueprintName = pkg.name;
let emberAddon = pkg['ember-addon'];
if (emberAddon && emberAddon.defaultBlueprint) {
blueprintName = emberAddon.defaultBlueprint;
}
return runEmber('generate', [blueprintName])
.catch(() => {
debug('default blueprint failed');
});
}
function findAddonPath() {
return path.dirname(findAddonPackageJSONPath());
}
function findAddonPackageJSON() {
let pkgPath = findAddonPackageJSONPath();
let pkg = fs.readJsonSync(pkgPath);
return pkg;
}
function findAddonPackageJSONPath() {
let foundPath = findup('package.json', {
cwd: previousCwd
});
if (foundPath) {
debug('found addon package.json; path=%s', foundPath);
} else {
debug('couldn\'t find addon package.json');
}
return foundPath;
}
function linkDependencies(appName) {
let nodeModulesAppPath = path.join(temp.pristinePath, appName, 'node_modules');
symlinkDirectory(temp.pristineNodeModulesPath, nodeModulesAppPath);
if (usesBower) {
let bowerComponentsAppPath = path.join(temp.pristinePath, appName, 'bower_components');
symlinkDirectory(temp.pristineBowerComponentsPath, bowerComponentsAppPath);
}
}