-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
475 lines (399 loc) · 16.8 KB
/
index.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* eslint-disable */
// Has to be written without use of Babel / ES2015+
// Taken from: https://github.com/xolvio/automated-testing-best-practices/blob/master/wallaby_server.js
// How to run:
//
// 1. Start your Meteor app and wait until it has built.
// 2. Start Wallaby with this configuration.
var fs = require('fs');
var path = require('path');
module.exports = function(options) {
if (! options) {
options = {}
}
var meteorPort = options.meteorPort || 3000
// Only way we can pass data to child processes is through environment
process.env.NODE_ENV = 'test'
process.env.ROOT_URL = options.rootUrl || 'http://localhost:' + meteorPort + '/'
process.env.MONGO_URL = options.mongoUrl || 'mongodb://127.0.0.1:' + (meteorPort + 1) + '/wallaby'
return config
}
var nodePath = require('child_process')
.execSync('meteor node -e "process.stdout.write(process.execPath)"', { encoding: 'utf8' });
var relativeAppPath = 'src'; // Note, this is intentionally repeated below
function config(wallaby) {
process.env.NODE_PATH += path.delimiter +
path.join(wallaby.localProjectDir, relativeAppPath, '.meteor/local/build/programs/server/node_modules');
// REVISIT: would have thought with Wallaby changes this wouldn't be needed anymore
process.env.NODE_PATH += path.delimiter + path.join(wallaby.localProjectDir, relativeAppPath, 'node_modules');
var compiler
if (fs.existsSync(path.join(wallaby.localProjectDir, '.babelrc'))) {
compiler = wallaby.compilers.babel()
} else {
var meteorNodeModules = path.join(wallaby.localProjectDir, relativeAppPath, '.meteor/local/dev_bundle/lib/node_modules')
var babelConfig = require(path.join(meteorNodeModules, 'meteor-babel/options'))
.getDefaults({ react: true, jscript: true })
babelConfig.babel = require(path.join(meteorNodeModules, 'babel-core'))
function pushBabelOpts(key, opts) {
if (! babelConfig[key]) {
babelConfig[key] = []
}
Array.prototype.push.apply(babelConfig[key], opts)
}
var appBabelRcPath = path.join(wallaby.localProjectDir, relativeAppPath, '.babelrc')
if (fs.existsSync(appBabelRcPath)) {
var appBabelConfig = JSON.parse(fs.readFileSync(appBabelRcPath));
for (k in appBabelConfig) {
if (! Array.isArray(appBabelConfig[k])) {
throw new Error('Not implemented yet')
}
pushBabelOpts(k, appBabelConfig[k])
}
}
pushBabelOpts('plugins', [
['@mindhive/babel-plugin-root-import', {
rootPathPrefix: '/',
rootPathSuffix: 'src'
}],
])
compiler = wallaby.compilers.babel(babelConfig)
}
return {
files: [
'src/**/*.@(js|jsx)',
'src/.specs/**/*.js',
'!src/.specs/**/*.spec.js',
'!src/**/*.spec.@(js|jsx)',
'!src/**/*.story.@(js|jsx)',
'!src/node_modules/**/*',
],
tests: [
'src/**/*.spec.@(js|jsx)',
'src/.specs/**/*.spec.js',
'!src/node_modules/**/*',
],
compilers: {
'src/**/*.@(js|jsx)': compiler,
'src/.specs/**/*.js': compiler,
},
env: {
type: 'node',
runner: nodePath,
},
reportConsoleErrorAsError: true,
testFramework: 'mocha',
workers: {
recycle: true, // REVISIT: without this Fibers break, but is probably slowing us down
},
debug: false,
bootstrap: function (wallaby) {
var relativeAppPath = 'src';
wallaby.delayStart();
process.on('unhandledRejection', function(reason, promise) {
var exception = reason.stack ? reason.stack.replace(/\(\/.*?\/instrumented\//g, '(') : reason
console.error('Unhandled promise rejection', exception)
});
var path = require('path');
var appPath = path.resolve(wallaby.localProjectDir, relativeAppPath);
var serverPath = path.resolve(appPath, '.meteor/local/build/programs/server');
var meteorModulesPath = path.resolve(appPath,'.meteor/local/dev_bundle/lib/node_modules')
var meteorServerModulesPath = path.resolve(appPath,'.meteor/local/dev_bundle/server-lib/node_modules')
process.argv.splice(2, 0, 'program.json');
try {
process.chdir(serverPath);
} catch (error) {
if (error.message.match(/^ENOENT/)) {
throw new Error('You need to run the Meteor app before you start Wallaby!');
} else {
throw error;
}
}
var Fiber = require("fibers");
require('babel-polyfill')
require(path.join(meteorModulesPath, 'reify/lib/runtime'))
.enable(module.constructor.prototype)
// This should allow Fibers to work in across an await point in an async function but doesn't seem to work
require(path.join(meteorServerModulesPath, 'meteor-promise'))
.makeCompatible(Promise, Fiber)
// The below is Meteor's boot code
//
// https://github.com/meteor/meteor/blob/devel/tools/static-assets/server/boot.js
//
// Modifications:
// - Only load packages
var fs = require("fs");
// var path = require("path");
var Future = require("fibers/future");
var _ = require('underscore');
var sourcemap_support = require('source-map-support');
// var bootUtils = require(path.resolve(serverPath, './boot-utils.js'));
var files = require(path.resolve(serverPath, './mini-files.js'));
var npmRequire = require(path.resolve(serverPath, './npm-require.js')).require;
var Profile = require(path.resolve(serverPath, './profile.js')).Profile;
// This code is duplicated in tools/main.js.
var MIN_NODE_VERSION = 'v0.10.41';
var hasOwn = Object.prototype.hasOwnProperty;
if (require('semver').lt(process.version, MIN_NODE_VERSION)) {
process.stderr.write(
'Meteor requires Node ' + MIN_NODE_VERSION + ' or later.\n');
process.exit(1);
}
// read our control files
var serverJsonPath = path.resolve(process.argv[2]);
var serverDir = path.dirname(serverJsonPath);
var serverJson = require(path.resolve(serverPath, "./server-json.js"));
var configJson =
JSON.parse(fs.readFileSync(path.resolve(serverDir, 'config.json'), 'utf8'));
// Set up environment
__meteor_bootstrap__ = {
startupHooks: [],
serverDir: serverDir,
configJson: configJson };
__meteor_runtime_config__ = { meteorRelease: configJson.meteorRelease };
if (!process.env.APP_ID) {
process.env.APP_ID = configJson.appId;
}
// Map from load path to its source map.
var parsedSourceMaps = {};
// Read all the source maps into memory once.
_.each(serverJson.load, function (fileInfo) {
if (fileInfo.sourceMap) {
var rawSourceMap = fs.readFileSync(
path.resolve(serverDir, fileInfo.sourceMap), 'utf8');
// Parse the source map only once, not each time it's needed. Also remove
// the anti-XSSI header if it's there.
var parsedSourceMap = JSON.parse(rawSourceMap.replace(/^\)\]\}'/, ''));
// source-map-support doesn't ever look at the sourcesContent field, so
// there's no point in keeping it in memory.
delete parsedSourceMap.sourcesContent;
var url;
if (fileInfo.sourceMapRoot) {
// Add the specified root to any root that may be in the file.
parsedSourceMap.sourceRoot = path.join(
fileInfo.sourceMapRoot, parsedSourceMap.sourceRoot || '');
}
parsedSourceMaps[path.resolve(__dirname, fileInfo.path)] = parsedSourceMap;
}
});
var retrieveSourceMap = function (pathForSourceMap) {
if (_.has(parsedSourceMaps, pathForSourceMap))
return { map: parsedSourceMaps[pathForSourceMap] };
return null;
};
var origWrapper = sourcemap_support.wrapCallSite;
var wrapCallSite = function (frame) {
var frame = origWrapper(frame);
var wrapGetter = function (name) {
var origGetter = frame[name];
frame[name] = function (arg) {
// replace a custom location domain that we set for better UX in Chrome
// DevTools (separate domain group) in source maps.
var source = origGetter(arg);
if (! source)
return source;
return source.replace(/(^|\()meteor:\/\/..app\//, '$1');
};
};
wrapGetter('getScriptNameOrSourceURL');
wrapGetter('getEvalOrigin');
return frame;
};
sourcemap_support.install({
// Use the source maps specified in program.json instead of parsing source
// code for them.
retrieveSourceMap: retrieveSourceMap,
// For now, don't fix the source line in uncaught exceptions, because we
// haven't fixed handleUncaughtExceptions in source-map-support to properly
// locate the source files.
handleUncaughtExceptions: false,
wrapCallSite: wrapCallSite
});
var specialArgPaths = {
"packages/modules-runtime.js": function () {
return {
npmRequire: npmRequire,
Profile: Profile
};
},
"packages/dynamic-import.js": function (file) {
var dynamicImportInfo = {};
Object.keys(configJson.clientPaths).map(function (key) {
var programJsonPath = path.resolve(configJson.clientPaths[key]);
var programJson = require(programJsonPath);
dynamicImportInfo[key] = {
dynamicRoot: path.join(path.dirname(programJsonPath), "dynamic")
};
});
dynamicImportInfo.server = {
dynamicRoot: path.join(serverDir, "dynamic")
};
return { dynamicImportInfo: dynamicImportInfo };
}
};
Fiber(function () {
_.each(serverJson.load, function (fileInfo) {
// Modification: Only load packages
if (fileInfo.path.indexOf('packages/') !== 0) {
return;
}
var code = fs.readFileSync(path.resolve(serverDir, fileInfo.path));
var nonLocalNodeModulesPaths = [];
function addNodeModulesPath(path) {
nonLocalNodeModulesPaths.push(
files.pathResolve(serverDir, path)
);
}
if (typeof fileInfo.node_modules === "string") {
addNodeModulesPath(fileInfo.node_modules);
} else if (fileInfo.node_modules) {
_.each(fileInfo.node_modules, function (info, path) {
if (! info.local) {
addNodeModulesPath(path);
}
});
}
function statOrNull(path) {
try {
return fs.statSync(path);
} catch (e) {
return null;
}
}
var Npm = {
/**
* @summary Require a package that was specified using
* `Npm.depends()`.
* @param {String} name The name of the package to require.
* @locus Server
* @memberOf Npm
*/
require: function (name) {
if (nonLocalNodeModulesPaths.length === 0) {
return require(name);
}
var fullPath;
nonLocalNodeModulesPaths.some(function (nodeModuleBase) {
var packageBase = files.convertToOSPath(files.pathResolve(
nodeModuleBase,
name.split("/", 1)[0]
));
if (statOrNull(packageBase)) {
return fullPath = files.convertToOSPath(
files.pathResolve(nodeModuleBase, name)
);
}
});
if (fullPath) {
return require(fullPath);
}
try {
return require(name);
} catch (e) {
// Try to guess the package name so we can print a nice
// error message
// fileInfo.path is a standard path, use files.pathSep
var filePathParts = fileInfo.path.split(files.pathSep);
var packageName = filePathParts[1].replace(/\.js$/, '');
// XXX better message
throw new Error(
"Can't find npm module '" + name +
"'. Did you forget to call 'Npm.depends' in package.js " +
"within the '" + packageName + "' package?");
}
}
};
var getAsset = function (assetPath, encoding, callback) {
var fut;
if (! callback) {
fut = new Future();
callback = fut.resolver();
}
// This assumes that we've already loaded the meteor package, so meteor
// itself can't call Assets.get*. (We could change this function so that
// it doesn't call bindEnvironment if you don't pass a callback if we need
// to.)
var _callback = Package.meteor.Meteor.bindEnvironment(function (err, result) {
if (result && ! encoding)
// Sadly, this copies in Node 0.10.
result = new Uint8Array(result);
callback(err, result);
}, function (e) {
console.log("Exception in callback of getAsset", e.stack);
});
// Convert a DOS-style path to Unix-style in case the application code was
// written on Windows.
assetPath = files.convertToStandardPath(assetPath);
// Unicode normalize the asset path to prevent string mismatches when
// using this string elsewhere.
assetPath = files.unicodeNormalizePath(assetPath);
if (!fileInfo.assets || !_.has(fileInfo.assets, assetPath)) {
_callback(new Error("Unknown asset: " + assetPath));
} else {
var filePath = path.join(serverDir, fileInfo.assets[assetPath]);
fs.readFile(files.convertToOSPath(filePath), encoding, _callback);
}
if (fut)
return fut.wait();
};
var Assets = {
getText: function (assetPath, callback) {
return getAsset(assetPath, "utf8", callback);
},
getBinary: function (assetPath, callback) {
return getAsset(assetPath, undefined, callback);
},
/**
* @summary Get the absolute path to the static server asset. Note that assets are read-only.
* @locus Server [Not in build plugins]
* @memberOf Assets
* @param {String} assetPath The path of the asset, relative to the application's `private` subdirectory.
*/
absoluteFilePath: function (assetPath) {
// Unicode normalize the asset path to prevent string mismatches when
// using this string elsewhere.
assetPath = files.unicodeNormalizePath(assetPath);
if (!fileInfo.assets || !_.has(fileInfo.assets, assetPath)) {
throw new Error("Unknown asset: " + assetPath);
}
assetPath = files.convertToStandardPath(assetPath);
var filePath = path.join(serverDir, fileInfo.assets[assetPath]);
return files.convertToOSPath(filePath);
},
};
var wrapParts = ["(function(Npm,Assets"];
var specialArgs =
hasOwn.call(specialArgPaths, fileInfo.path) &&
specialArgPaths[fileInfo.path](fileInfo);
var specialKeys = Object.keys(specialArgs || {});
specialKeys.forEach(function (key) {
wrapParts.push("," + key);
});
// \n is necessary in case final line is a //-comment
wrapParts.push("){", code, "\n})");
var wrapped = wrapParts.join("");
// It is safer to use the absolute path when source map is present as
// different tooling, such as node-inspector, can get confused on relative
// urls.
// fileInfo.path is a standard path, convert it to OS path to join with
// __dirname
var fileInfoOSPath = files.convertToOSPath(fileInfo.path);
var absoluteFilePath = path.resolve(__dirname, fileInfoOSPath);
var scriptPath =
parsedSourceMaps[absoluteFilePath] ? absoluteFilePath : fileInfoOSPath;
// The final 'true' is an undocumented argument to runIn[Foo]Context that
// causes it to print out a descriptive error message on parse error. It's
// what require() uses to generate its errors.
var func = require('vm').runInThisContext(wrapped, scriptPath, true);
var args = [Npm, Assets];
specialKeys.forEach(function (key) {
args.push(specialArgs[key]);
});
func.apply(global, args);
});
// A lot of boot.js removed
process.chdir(appPath);
wallaby.start();
}).run();
},
};
}