UNPKG

4.52 kBJavaScriptView Raw
1var define;
2if (typeof define === "undefined") {
3 define = function (classInstance) {
4 classInstance (require, exports, module);
5 };
6}
7
8var registry = {};
9define (function (require, exports, module) {
10
11var MODULE_NAME = 'dataflo.ws';
12var INITIATOR_PATH = 'initiator';
13
14var path,
15 fs,
16 common = require ('./common'),
17 color = require ('./color');
18
19if ($isServerSide) {
20 path = require ('path');
21 fs = require ('fs');
22}
23
24color.error = color.bind (color, "red+white_bg");
25color.path = color.cyan.bind (color);
26color.dataflows = color.green.bind (color, "dataflows");
27
28
29module.exports.common = common;
30module.exports.color = color;
31
32
33var util = require ('util');
34try {
35 util.clone = require('node-v8-clone').clone;
36} catch (e) {
37 console.log (color.dataflows(), color.path ('node-v8-clone'), 'recommended to install to fasten clone operations');
38}
39
40
41var instanceTypes = [ 'initiator', 'task' ];
42
43// - - -
44
45// TODO: add requirejs loading
46
47function registryLookup (instanceType, instanceName) {
48 var instanceClass = registry[instanceType] &&
49 registry[instanceType][instanceName];
50
51 if (!instanceClass) {
52 if ($isClientSide) {
53 console.error (
54 'you need to run dataflows.register ("'
55 +instanceType+'", "'+instanceName
56 +'", instance) before using this task'
57 );
58 return;
59 }
60
61
62 var fixedName = instanceName;
63 if (instanceType == 'initiator') {
64 fixedName = instanceName.replace(/d$/, '');
65 if (fixedName !== instanceName) {
66 console.warn(
67 '[DEPRECATED] Remove trailing "d" from "%s" in your initiator config',
68 instanceName
69 );
70 }
71 } else if (instanceType == 'task') {
72 fixedName = instanceName.replace(/^(dataflo\.ws\/)?task\//, '');
73 if (fixedName !== instanceName) {
74 console.warn(
75 '[DEPRECATED] Remove preceding "task/" from "%s" in your task config',
76 instanceName
77 );
78 }
79 }
80
81 var project;
82 var project_root;
83
84 if (common.getProject)
85 project = common.getProject();
86
87 if (project)
88 project_root = project.root;
89
90 instanceClass = getModule (instanceType, fixedName, false, project_root);
91 }
92
93 return registry[instanceType][instanceName] = instanceClass;
94};
95
96function getModule (type, name, optional, root) {
97 optional = optional || false;
98 var mod;
99
100 if (!root) {
101 if (typeof project !== "undefined") {
102 root = project.root;
103 } else {
104 root = path.dirname (require.main.filename);
105 }
106 }
107
108 var paths = [
109 path.join('dataflo.ws', type, name)
110 ];
111
112 if (root) {
113 paths.push (path.resolve(root.path ? root.path : root, type, name));
114 paths.push (path.resolve(root.path ? root.path : root, 'node_modules', type, name));
115 }
116
117 paths.push (name);
118
119 var taskFound = paths.some (function (modPath) {
120 try {
121 mod = require(modPath);
122 return true;
123 } catch (e) {
124 // assuming format: Error: Cannot find module 'csv2array' {"code":"MODULE_NOT_FOUND"}
125 if (e.toString().indexOf(name + '\'') > 0 && e.code == "MODULE_NOT_FOUND") {
126 return false;
127 } else {
128 console.error (
129 'requirement failed:',
130 color.error (e.toString()),
131 root
132 ? "in " + color.path (root.relative (modPath))
133 : ""
134 );
135 return true;
136 }
137 }
138 });
139
140 if (!mod && !optional)
141 console.error ("module " + type + " " + name + " cannot be used");
142
143 return mod;
144
145}
146
147instanceTypes.forEach(function(instanceType) {
148 registry[instanceType] = {};
149 module.exports[instanceType] = function (instanceName) {
150 return registryLookup (instanceType, instanceName);
151 };
152});
153
154// - - -
155
156/**
157 * Makes symlinks from modules to base dataflo.ws directory.
158 */
159module.exports.install = function (moduleName) {
160 var baseDir = path.dirname(require.resolve(MODULE_NAME));
161 var nodePath = path.dirname(baseDir);
162 var moduleDir = path.join(nodePath, moduleName);
163
164 instanceTypes.forEach(function (dir) {
165 var srcDir = path.join(moduleDir, dir);
166 var destDir = path.join(baseDir, dir);
167
168 if (fs.existsSync(srcDir)) {
169 var files = fs.readdirSync(srcDir);
170 files.forEach(function (fileName) {
171 var srcPath = path.join(srcDir, fileName);
172 var destPath = path.join(destDir, fileName);
173 if (!fs.existsSync(destPath)) {
174 fs.symlinkSync(srcPath, destPath);
175 }
176 });
177 }
178 });
179};
180
181/**
182 * Register base entities for dataflo.ws processing.
183 */
184module.exports.register = function (instanceType, instanceName, instanceClass) {
185 if (!registry[instanceType]) {
186 console.warn(
187 'Unexpected instance type. Predefined types is: ['+instanceTypes.join(', ')+']'
188 );
189
190 return;
191 }
192
193 registry[instanceType][instanceName] = instanceClass;
194};
195
196
197
198});