1 | #!/usr/bin/env node
|
2 |
|
3 | var path = require('path');
|
4 | var fs = require('fs');
|
5 | var resolve = require('resolve');
|
6 |
|
7 | var NODE_PATH = process.env.NODE_PATH;
|
8 |
|
9 | // This wrapper only resolves which instance of Karma should be run.
|
10 | // All the logic is in the main Karma module.
|
11 | // The resolving algorithm checks (in this order):
|
12 | // - a local module (using `require`, so it will traverse up, looking for `node_modules/karma`),
|
13 | // - a siblink module to karma-cli (that loads the global karma, when using NVM),
|
14 | // - a global module (NODE_PATH)
|
15 |
|
16 |
|
17 | var requireCliAndRun = function(karmaPath) {
|
18 | if (process.argv.indexOf('--which') !== -1) {
|
19 | console.log(karmaPath);
|
20 | } else {
|
21 | require(karmaPath + '/lib/cli').run();
|
22 | }
|
23 | };
|
24 |
|
25 | resolve('karma', {basedir: process.cwd()}, function(err, pathToKarma) {
|
26 | // There is a local version, let's use it.
|
27 | if (!err) {
|
28 | return requireCliAndRun(pathToKarma.replace(/\/lib\/index\.js/, ''));
|
29 | }
|
30 |
|
31 | // We can't load a global one, since NODE_PATH is not defined.
|
32 | if (!NODE_PATH) {
|
33 | // Let's try a siblink to karma-cli, that is a global module with NVM.
|
34 | var siblinkKarma = path.normalize(__dirname + '/../../karma');
|
35 | if (fs.existsSync(siblinkKarma)) {
|
36 | return requireCliAndRun(siblinkKarma);
|
37 | }
|
38 |
|
39 | console.error('Cannot find local Karma!\n' +
|
40 | ' Please install Karma by `npm install karma --save-dev`.\n' +
|
41 | ' If you wanna use a global instance, please set NODE_PATH env variable.\n');
|
42 | return;
|
43 | }
|
44 |
|
45 | // Let's try global paths.
|
46 | var globalPaths = NODE_PATH.split(path.delimiter);
|
47 | var globalKarma;
|
48 |
|
49 | while (globalPaths.length) {
|
50 | globalKarma = path.normalize(globalPaths.shift() + '/karma');
|
51 | if (fs.existsSync(globalKarma)) {
|
52 | return requireCliAndRun(globalKarma);
|
53 | }
|
54 | }
|
55 |
|
56 | console.error('Cannot find local Karma!\n' +
|
57 | ' Please install karma by `npm install karma --save-dev`.\n');
|
58 | });
|