1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const path = require("path");
|
9 | const klaw = require("klaw");
|
10 |
|
11 |
|
12 | const util = require("./util");
|
13 | const build = require("./build");
|
14 |
|
15 |
|
16 | const resolveAndCall = function() {
|
17 | if(this.context.configuration.script) {
|
18 | var scriptPath = path.resolve(
|
19 | this.context.root,
|
20 | this.context.configuration.script
|
21 | );
|
22 |
|
23 | try {
|
24 | var script = require(scriptPath);
|
25 | if(script.call) {
|
26 | script.call(this, script);
|
27 | } else {
|
28 | console.error("\n\n\x1b[31mserver, mountPoints: not a module", scriptPath);
|
29 | }
|
30 | } catch (err) {
|
31 | console.error("\n\n\x1b[31m", err);
|
32 | }
|
33 | } else {
|
34 | console.error(
|
35 | "\n\n\x1b[31mserver, mountPoints: script undefined",
|
36 | this.context.configurationFilePath
|
37 | );
|
38 | }
|
39 | };
|
40 |
|
41 | const mount = function (klawItem, confDone) {
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const configurationFilePath = klawItem.path;
|
49 |
|
50 | const mountPoint = path.dirname(configurationFilePath).split(path.sep).pop();
|
51 |
|
52 | var mountType;
|
53 | var typeHandler;
|
54 |
|
55 | switch(path.basename(configurationFilePath)) {
|
56 | case "app.json":
|
57 | mountType = "app";
|
58 | typeHandler = build;
|
59 | break;
|
60 | case "io.json":
|
61 | mountType = "io";
|
62 | typeHandler = resolveAndCall;
|
63 | break;
|
64 | case "socket.json":
|
65 | mountType = "socket";
|
66 | break;
|
67 | case "extention.json":
|
68 | mountType = "extention";
|
69 | break;
|
70 | }
|
71 |
|
72 |
|
73 | if(mountType !== undefined) {
|
74 | util.localJson(
|
75 | configurationFilePath
|
76 | ).then((configuration) => {
|
77 |
|
78 | if(
|
79 | configuration.server &&
|
80 | configuration.server.indexOf &&
|
81 | (
|
82 | configuration.server.indexOf(this.conf.name) >= 0 ||
|
83 | configuration.server === "*"
|
84 | )
|
85 | ) {
|
86 |
|
87 | var context = this.mount[mountType][mountPoint] = {
|
88 | configuration: configuration,
|
89 | configurationFilePath: configurationFilePath,
|
90 | mountType: mountType,
|
91 | mountPoint: mountPoint,
|
92 | root: path.parse(configurationFilePath).dir,
|
93 | util: util
|
94 | };
|
95 |
|
96 |
|
97 | if(typeHandler && typeHandler.call) {
|
98 | var mountPath = "/" + mountType + "/" + mountPoint;
|
99 | this.router.use(
|
100 | mountPath,
|
101 | function (req, res, next) {
|
102 | typeHandler.call({
|
103 | context: context,
|
104 | express: {
|
105 | req: req,
|
106 | res: res,
|
107 | next: next
|
108 | }
|
109 | });
|
110 | }
|
111 | );
|
112 | }
|
113 |
|
114 |
|
115 | else if(mountType === "extention") {
|
116 | resolveAndCall.call({
|
117 | context: context,
|
118 | runtime: this
|
119 | });
|
120 | }
|
121 |
|
122 | process.stdout.write("\n\x1b[35m" + mountType + " \x1b[36m" + mountPoint + "\n");
|
123 | } else {
|
124 | process.stdout.write("\x1b[35m.");
|
125 | }
|
126 |
|
127 | confDone();
|
128 | }, (err) => {
|
129 | console.error(`mountPoints, mount, ${configurationFilePath}: ${err}`);
|
130 |
|
131 | confDone();
|
132 | });
|
133 | } else {
|
134 | confDone();
|
135 | }
|
136 | };
|
137 |
|
138 |
|
139 | module.exports = function(done) {
|
140 | const configurationQue = util.all();
|
141 | process.stdout.write("\n");
|
142 |
|
143 |
|
144 | klaw(this.conf.root)
|
145 |
|
146 | .on("data", (klawItem)=> {
|
147 | configurationQue.add((confDone)=> {
|
148 | mount.call(this, klawItem, confDone);
|
149 | });
|
150 | })
|
151 |
|
152 | .on("end", ()=> {
|
153 | configurationQue.then(()=> {
|
154 | process.stdout.write("\n");
|
155 | done();
|
156 | });
|
157 | });
|
158 | };
|
159 |
|