1 |
|
2 | var activated, fs, lstatOverride, override, sep;
|
3 |
|
4 | fs = require('fs');
|
5 |
|
6 | sep = require('path').sep;
|
7 |
|
8 | module.exports = function(list) {
|
9 | var moduleName, name, type, _results;
|
10 | module.exports.activate();
|
11 | _results = [];
|
12 | for (moduleName in list) {
|
13 | type = 'literal';
|
14 | name = moduleName;
|
15 | override[name] = {
|
16 | type: type,
|
17 | 'package.json': {
|
18 | name: name,
|
19 | version: '0.0.0',
|
20 | main: 'STUBBED.js',
|
21 | dependencies: {}
|
22 | },
|
23 | 'STUBBED.js': list[moduleName]
|
24 | };
|
25 | if (type === 'literal') {
|
26 | _results.push(require(name));
|
27 | } else {
|
28 | _results.push(void 0);
|
29 | }
|
30 | }
|
31 | return _results;
|
32 | };
|
33 |
|
34 | override = {};
|
35 |
|
36 | lstatOverride = {};
|
37 |
|
38 | activated = false;
|
39 |
|
40 | Object.defineProperty(module.exports, 'activate', {
|
41 | enumarable: 'false',
|
42 | get: function() {
|
43 | if (activated) {
|
44 | return function() {};
|
45 | }
|
46 | return function() {
|
47 | var lstatSync, readFileSync, statSync;
|
48 | activated = true;
|
49 | readFileSync = fs.readFileSync;
|
50 | fs.readFileSync = function(path, encoding) {
|
51 |
|
52 |
|
53 | var file, mod, modulePath, modulesPath, parts, scriptPath, type, _ref;
|
54 | _ref = path.split(sep).slice(-2), mod = _ref[0], file = _ref[1];
|
55 | parts = path.split(sep).slice(0, -2);
|
56 | modulesPath = parts.join(sep);
|
57 | modulePath = parts.concat([mod]).join(sep);
|
58 | scriptPath = parts.concat([mod, 'STUBBED.js']).join(sep);
|
59 | if (override.hasOwnProperty(mod)) {
|
60 | type = override[mod].type;
|
61 | switch (file) {
|
62 | case 'package.json':
|
63 | lstatOverride[modulesPath] = 1;
|
64 | lstatOverride[modulePath] = 1;
|
65 | lstatOverride[scriptPath] = 1;
|
66 | return JSON.stringify(override[mod]['package.json']);
|
67 | case 'STUBBED.js':
|
68 | if (typeof override[mod]['STUBBED.js'] === 'function') {
|
69 | if (override[mod].scriptPath != null) {
|
70 | return "module.exports = require('" + override[mod].scriptPath + "');";
|
71 | } else {
|
72 | override[mod].scriptPath = scriptPath;
|
73 | }
|
74 | return "ipso = require('ipso'); // testing catch 22\nmock = ipso.mock;\nMock = ipso.Mock;\nget = function(tag) {\n\n try { return ipso.does.getSync(tag).object }\n catch (error) { console.log('ipso: missing mock \"%s\"', tag); }\n\n}; \n\nmodule.exports = " + ((function() {
|
75 | switch (type) {
|
76 | case 'literal':
|
77 | return "(" + (override[mod]['STUBBED.js'].toString()) + ").call(this);";
|
78 | }
|
79 | })());
|
80 | } else {
|
81 | console.log("ipso.define(list) requires list of functions to be exported as modules,\nor used as module factories.".red);
|
82 | }
|
83 | }
|
84 | }
|
85 | return readFileSync(path, encoding);
|
86 | };
|
87 | statSync = fs.statSync;
|
88 | fs.statSync = function(path) {
|
89 |
|
90 |
|
91 | if (path.match(/STUBBED.js/)) {
|
92 | return {
|
93 | isDirectory: function() {
|
94 | return false;
|
95 | }
|
96 | };
|
97 | }
|
98 | return statSync(path);
|
99 | };
|
100 | lstatSync = fs.lstatSync;
|
101 | return fs.lstatSync = function(path) {
|
102 |
|
103 |
|
104 | if (lstatOverride[path] != null) {
|
105 | return {
|
106 | isSymbolicLink: function() {
|
107 | return false;
|
108 | }
|
109 | };
|
110 | }
|
111 | return lstatSync(path);
|
112 | };
|
113 | };
|
114 | }
|
115 | });
|