1 | var minimatch = require("minimatch");
|
2 | var through = require("through");
|
3 | var path = require("path");
|
4 |
|
5 | var __get__ = require("rewire/lib/__get__").toString();
|
6 | var __set__ = require("rewire/lib/__set__").toString();
|
7 |
|
8 | console.warn("Injecting Rewireify into modules");
|
9 |
|
10 | module.exports = function rewireify(file, options) {
|
11 | options = {
|
12 | ignore: options.ignore || ""
|
13 | };
|
14 |
|
15 | var ignore = ["__get__.js", "__set__.js", "**/*.json"].concat(options.ignore.split(","));
|
16 | var relativeFile = file.replace(process.cwd(), "");
|
17 | var fileName = path.basename(file);
|
18 |
|
19 | var matches = ignore.filter(function(pattern) {
|
20 | return ignore.indexOf(fileName) > -1 || minimatch(relativeFile, pattern);
|
21 | });
|
22 |
|
23 | if (matches.length) {
|
24 | return through();
|
25 | }
|
26 |
|
27 | var data = "";
|
28 | var post = "";
|
29 |
|
30 | function write(buffer) {
|
31 | data += buffer;
|
32 | }
|
33 |
|
34 | function end() {
|
35 | post += "/* This code was injected by Rewireify */\n";
|
36 | post += "if ((typeof module.exports).match(/object|function/) && \n"
|
37 | post += "Object.isExtensible(module.exports)) {\n";
|
38 | post += "Object.defineProperty(module.exports, '__get__', { value: " + __get__ + ", writable: true });\n";
|
39 | post += "Object.defineProperty(module.exports, '__set__', { value: " + __set__ + ", writable: true });\n";
|
40 | post += "}\n";
|
41 |
|
42 | this.queue(data);
|
43 | this.queue(post);
|
44 | this.queue(null);
|
45 | }
|
46 |
|
47 | return through(write, end);
|
48 | };
|