-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathproject.js
184 lines (134 loc) · 4.96 KB
/
project.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";
var path = require ('path'),
fs = require ('fs'),
os = require ('os'),
util = require ('util'),
cluster = require ('cluster'),
io = require ('fsobject'),
confFu = require ('conf-fu');
var MODULE_NAME = 'dataflo.ws';
var EventEmitter = require ('events').EventEmitter;
var dataflows = require ('./');
var paint = dataflows.color;
var Project = function (rootPath) {
// ask master for the config file
if (cluster.isWorker) {
process.send ({request: 'config'});
// response must be instantiated after fork.
// take a look bin/daemon.js
process.on ('message', function configHandler (msg) {
if (msg.request && msg.request === 'config') {
this.configReady (msg.response, new io (msg.response.root));
process.removeListener ('message', configHandler);
}
}.bind (this));
return;
}
var rootSearch = new io (rootPath || process.env.PROJECT_ROOT || process.cwd());
this.configDir = process.env.PROJECT_CONF || '.dataflows';
this.varDir = process.env.PROJECT_VAR || '.dataflows';
this.confParams = {
projectRoot: rootSearch.path,
configRoot: this.configDir,
instanceFile: 'instance',
configName: 'project',
fixupName: '<$instance>/fixup'
};
rootSearch.findUp (this.configDir, function (err, projectRoot, stats) {
// TODO: handle err
// console.log ('config root', '.dataflows');
// console.log ('project root', projectRoot.path);
this.root = projectRoot;
this.confParams.projectRoot = projectRoot.path;
var conf = new confFu (this.confParams);
this.fixes = 0;
this.fatal = false;
conf.on ('ready', this.configReady.bind (this, conf, projectRoot));
conf.on ('notReady', this.configNotReady.bind (this, conf, projectRoot));
conf.on ('error', this.configError.bind (this, conf, projectRoot));
}.bind (this));
};
module.exports = Project;
util.inherits (Project, EventEmitter);
Project.prototype.configReady = function (conf, projectRoot) {
// console.log (conf, conf.configFile.path, conf.fixupFile.path);
// console.log (conf.config.initiator.http.flows);
this.config = conf.config;
this.root = projectRoot;
dataflows.config = this.config;
dataflows.root = this.root;
this.emit ('ready');
}
function logVariables (conf) {
console.log (paint.error ("those config variables is unpopulated:"));
conf.logVariables ();
var messageChunks = [
paint.error ((conf.fixupFile? "" : "you must define fixup path, then") + "you can execute"),
paint.yellow ("<variable> <value>"),
"to define individual variables\n or edit",
conf.fixupFile ? paint.path (conf.fixupFile.shortPath ()) : "fixup file",
"to define all those vars at once"
];
console.log.apply (console, messageChunks);
this.emit ('failed');
}
Project.prototype.configNotReady = function (conf) {
if (!this.fixes) {
logVariables.call (this, conf);
return;
}
var confFix = new confFu (this.confParams);
// after we have written instance file, config can exists, so just return it
confFix.on ("ready", this.configReady.bind (this, confFix));
// otherwise, notify user
confFix.on ("notReady", function () {
// console.log ("not ready");
logVariables.call (this, confFix);
}.bind (this));
}
Project.prototype.configError = function (conf, eOrigin, eType, eData, eFile) {
// console.log (conf, conf.configFile.path, conf.fixupFile.path);
//
if (eOrigin === "instance" && eType === "file") {
// create instance file, directory for fixup and relaunch to generate fixup
conf.instanceFile.writeFile (generatedInstance ());
conf.configRoot.mkpath (generatedInstance ());
this.fixes ++;
} else if (eOrigin === "fixup" && eType === "file") {
// make sure directory is created or fixup file can be read
conf.configRoot.mkpath (conf.instance);
this.fixes ++;
} else if (eType === "parse") {
console.log ("cannot parse file", eFile, eData);
} else if (eOrigin === "core" && eType === "file") {
console.log ("core config file must be present", eFile, eData);
} else if (eOrigin === "config" && eType === "variables") {
// nothing to do, conf-fu will emit not ready and unpopulated variables will be logged
} else {
console.log ("error origin: %s, type: %s, data: %s, file: %s", eOrigin, eType, eData, eFile);
}
}
function generatedInstance () {
return [
(process.env.USER || process.env.USERNAME),
(process.env.HOSTNAME || process.env.COMPUTERNAME || os.hostname())
].join ('@');
}
Project.prototype.connectors = {};
Project.prototype.connections = {};
// TODO: remove this in favor of dataflows
Project.prototype.getModule = function (type, name, optional) {
return dataflows.getModule (type, name, optional, this.root);
};
Project.prototype.getService = function (name) {
return this.getModule ('service', name);
};
Project.prototype.getInitiator = function (name) {
return this.getModule ('initiator', name);
};
Project.prototype.getTask = function (name) {
return this.getModule ('task', name);
};
Project.prototype.require = function (name, optional) {
return this.getModule ('', name, optional);
};