Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 5147106

Browse files
committed
Added plugin/preprocessor support
1 parent e37c99c commit 5147106

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

lib/cli.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var util = require('util');
2-
var path = require('path')
2+
var path = require('path');
33
var fs = require('fs');
44
var webdriver = require('selenium-webdriver');
55
var remote = require('selenium-webdriver/remote');
@@ -25,7 +25,7 @@ var config = {
2525
showColors: true,
2626
includeStackTrace: true
2727
}
28-
}
28+
};
2929

3030
var originalOnComplete = config.jasmineNodeOpts.onComplete;
3131

@@ -59,11 +59,57 @@ var printVersion = function () {
5959
process.exit(0);
6060
};
6161

62+
var requirePlugin = function(name) {
63+
util.puts('Loading plugin: ' + name);
64+
try {
65+
return require(name);
66+
} catch (e) {
67+
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(name) !== -1) {
68+
throw new Error('Cannot find plugin: ' + name);
69+
} else {
70+
throw new Error('Error during loading ' + name + ' plugin: ' + e.message);
71+
}
72+
}
73+
};
74+
75+
var preprocess = function (originalPath, preprocessors) {
76+
var env = process.env;
77+
var tmp = env.TMPDIR || env.TMP || env.TEMP || '/tmp';
78+
var contentPath = tmp + '/' + path.basename(originalPath) + '.js';
79+
80+
var nextPreprocessor = function(content) {
81+
if (!preprocessors.length) {
82+
return fs.writeFileSync(contentPath, content);
83+
}
84+
85+
preprocessors.shift()(originalPath, content, nextPreprocessor);
86+
};
87+
88+
nextPreprocessor(fs.readFileSync(originalPath).toString());
89+
90+
return contentPath;
91+
};
92+
6293
var run = function() {
6394
if (config.jasmineNodeOpts.specFolders) {
6495
throw new Error('Using config.jasmineNodeOpts.specFolders is deprecated ' +
6596
'in Protractor 0.6.0. Please switch to config.specs.');
6697
}
98+
99+
var plugins = [];
100+
var preprocessors = [];
101+
if (config.plugins) {
102+
plugins = config.plugins;
103+
for (var i = 0; i < plugins.length; ++i) {
104+
var plugin = requirePlugin(plugins[i]);
105+
for (key in plugin) {
106+
if (key.substring(0, 13) == "preprocessor:") {
107+
preprocessors.push(plugin[key]);
108+
}
109+
}
110+
}
111+
}
112+
67113
// Check the specs.
68114
// TODO(ralphj): Interpret patterns from the specs, e.g.
69115
// 'specs/*.js'
@@ -74,7 +120,10 @@ var run = function() {
74120
if (!fs.existsSync(specs[i])) {
75121
throw new Error('Test file ' + specs[i] + ' not found.');
76122
}
123+
124+
specs[i] = preprocess(specs[i], preprocessors);
77125
}
126+
78127
minijn.addSpecs(specs);
79128

80129
if (config.sauceUser && config.sauceKey) {

0 commit comments

Comments
 (0)