UNPKG

16.5 kBJavaScriptView Raw
1var Base, BaseModel, ClassInfo, ESCodeGenerator, EntryGenerator, EntryGeneratorInput, Facade, JSCodeGenerator, MasterDataResource, Path, camelize, fs, requireFile,
2 indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
3 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
4 hasProp = {}.hasOwnProperty;
5
6fs = require('fs');
7
8Path = require('path');
9
10if (Path.isAbsolute == null) {
11 Path.isAbsolute = function(str) {
12 return str.charAt(0) === '/';
13 };
14}
15
16camelize = require('./util').camelize;
17
18Facade = require('./main');
19
20requireFile = Facade.requireFile;
21
22Base = Facade.Base, BaseModel = Facade.BaseModel;
23
24MasterDataResource = require('./master-data-resource');
25
26ClassInfo = (function() {
27 function ClassInfo(name1, relPath1, className1, moduleName1) {
28 this.name = name1;
29 this.relPath = relPath1;
30 this.className = className1;
31 this.moduleName = moduleName1;
32 }
33
34 Object.defineProperties(ClassInfo.prototype, {
35 modFullName: {
36 get: function() {
37 if (this.moduleName) {
38 return this.moduleName + '/' + this.name;
39 } else {
40 return this.name;
41 }
42 }
43 },
44 fullClassName: {
45 get: function() {
46 return camelize(this.moduleName) + this.className;
47 }
48 }
49 });
50
51 return ClassInfo;
52
53})();
54
55EntryGeneratorInput = (function() {
56 function EntryGeneratorInput(facadePath, dirname, outfile) {
57 this.validate(facadePath, dirname, outfile);
58 this.absDirname = this.absolutePath(dirname);
59 this.absOutfilePath = this.absolutePath(outfile);
60 this.facadePath = this.relativePath(facadePath);
61 this.coreClasses = this.getClassInfoList(this.absDirname);
62 this.modules = this.getModulesClasses();
63 this.facadeClassName = requireFile(this.absolutePath(facadePath)).name;
64 this.facade = this.createFacade();
65 this.masterJSONStr = JSON.stringify(this.getMasterJSON());
66 this.factories = this.getPreferredFactoryNames();
67 }
68
69 EntryGeneratorInput.prototype.createFacade = function() {
70 var allModules, i, len, moduleName, ref;
71 allModules = {};
72 ref = this.getModuleNames();
73 for (i = 0, len = ref.length; i < len; i++) {
74 moduleName = ref[i];
75 allModules[moduleName] = Path.join(this.absDirname, moduleName);
76 }
77 return Facade.createInstance({
78 dirname: this.absDirname,
79 modules: allModules,
80 master: true
81 });
82 };
83
84
85 /**
86 @return {Array(ClassInfo)}
87 */
88
89 EntryGeneratorInput.prototype.getClassInfoList = function(dirPath, moduleName) {
90 var className, filename, i, len, name, ref, relDirname, relPath, results;
91 if (moduleName == null) {
92 moduleName = '';
93 }
94 relDirname = this.relativePath(dirPath);
95 ref = this.getClassFiles(dirPath);
96 results = [];
97 for (i = 0, len = ref.length; i < len; i++) {
98 filename = ref[i];
99 name = filename.split('.')[0];
100 relPath = relDirname + '/' + name;
101 className = requireFile(Path.resolve(dirPath, name)).name;
102 results.push(new ClassInfo(name, relPath, className, moduleName));
103 }
104 return results;
105 };
106
107
108 /**
109 @return {{[string]: Array(ClassInfo)}}
110 */
111
112 EntryGeneratorInput.prototype.getModulesClasses = function() {
113 var i, len, moduleName, modulePath, modules, ref;
114 modules = {};
115 ref = this.getModuleNames();
116 for (i = 0, len = ref.length; i < len; i++) {
117 moduleName = ref[i];
118 modulePath = Path.join(this.absDirname, moduleName);
119 modules[moduleName] = this.getClassInfoList(modulePath, moduleName);
120 }
121 return modules;
122 };
123
124
125 /**
126 @method getMasterJSON
127 @private
128 @return {Object} master data
129 */
130
131 EntryGeneratorInput.prototype.getMasterJSON = function() {
132 var e, error, masterJSONPath;
133 try {
134 masterJSONPath = this.facade.master.masterJSONPath;
135 if (!fs.existsSync(masterJSONPath)) {
136 return null;
137 }
138 return require(masterJSONPath);
139 } catch (error) {
140 e = error;
141 return null;
142 }
143 };
144
145
146 /**
147 @return {Array(string)} array of module names
148 */
149
150 EntryGeneratorInput.prototype.getModuleNames = function() {
151 return fs.readdirSync(this.absDirname).filter(function(subDirName) {
152 return subDirName !== 'master-data';
153 }).filter(function(subDirName) {
154 return subDirName !== 'custom-roles';
155 }).map((function(_this) {
156 return function(subDirname) {
157 return Path.join(_this.absDirname, subDirname);
158 };
159 })(this)).filter(function(subDirPath) {
160 return fs.statSync(subDirPath).isDirectory();
161 }).filter(function(subDirPath) {
162 return fs.readdirSync(subDirPath).some(function(filename) {
163 var klass;
164 klass = requireFile(Path.join(subDirPath, filename));
165 return klass.isBaseDomainClass;
166 });
167 }).map(function(subDirPath) {
168 return Path.basename(subDirPath);
169 });
170 };
171
172
173 /**
174 get domain files to load
175
176 @method getClassFiles
177 @private
178 @return {Array(string)} filenames
179 */
180
181 EntryGeneratorInput.prototype.getClassFiles = function(path) {
182 var ParentClass, ext, fileInfo, fileInfoDict, filename, files, i, klass, len, name, pntFileName, ref, ref1, ref2;
183 fileInfoDict = {};
184 ref = fs.readdirSync(path);
185 for (i = 0, len = ref.length; i < len; i++) {
186 filename = ref[i];
187 ref1 = filename.split('.'), name = ref1[0], ext = ref1[1];
188 if (ext !== 'js' && ext !== 'coffee') {
189 continue;
190 }
191 klass = requireFile(path + '/' + filename);
192 fileInfoDict[name] = {
193 filename: filename,
194 klass: klass
195 };
196 }
197 files = [];
198 for (name in fileInfoDict) {
199 fileInfo = fileInfoDict[name];
200 klass = fileInfo.klass, filename = fileInfo.filename;
201 if (indexOf.call(files, filename) >= 0) {
202 continue;
203 }
204 ParentClass = Object.getPrototypeOf(klass.prototype).constructor;
205 if (ParentClass.className && (pntFileName = (ref2 = fileInfoDict[ParentClass.getName()]) != null ? ref2.filename : void 0)) {
206 if (indexOf.call(files, pntFileName) < 0) {
207 files.push(pntFileName);
208 }
209 }
210 files.push(filename);
211 }
212 return files;
213 };
214
215
216 /**
217 get entities with no factory
218 */
219
220 EntryGeneratorInput.prototype.getPreferredFactoryNames = function() {
221 var classInfo, classes, factories, i, j, k, len, len1, modName, ref, ref1, v;
222 factories = {};
223 ref = this.coreClasses;
224 for (i = 0, len = ref.length; i < len; i++) {
225 classInfo = ref[i];
226 factories[classInfo.modFullName] = this.getPreferredFactoryName(classInfo);
227 }
228 ref1 = this.modules;
229 for (modName in ref1) {
230 classes = ref1[modName];
231 for (j = 0, len1 = classes.length; j < len1; j++) {
232 classInfo = classes[j];
233 factories[classInfo.modFullName] = this.getPreferredFactoryName(classInfo);
234 }
235 }
236 for (k in factories) {
237 v = factories[k];
238 if (v == null) {
239 delete factories[k];
240 }
241 }
242 return factories;
243 };
244
245 EntryGeneratorInput.prototype.getPreferredFactoryName = function(classInfo) {
246 var ModelClass, e, error, factory;
247 ModelClass = this.facade.require(classInfo.modFullName);
248 if (!(ModelClass.prototype instanceof BaseModel)) {
249 return;
250 }
251 try {
252 factory = this.facade.createPreferredFactory(classInfo.modFullName);
253 return "'" + factory.constructor.className + "'";
254 } catch (error) {
255 e = error;
256 return 'null';
257 }
258 };
259
260
261 /**
262 validate input data
263 */
264
265 EntryGeneratorInput.prototype.validate = function(facadePath, dirname, outfile) {
266 var absDirname, absFacadePath, outDir;
267 absFacadePath = this.absolutePath(facadePath);
268 absDirname = this.absolutePath(dirname);
269 outDir = Path.dirname(this.absolutePath(outfile));
270 if (!fs.existsSync(absFacadePath)) {
271 throw new Error("'" + absFacadePath + "' is not found.");
272 }
273 if (!fs.existsSync(absDirname)) {
274 throw new Error("dirname: '" + absDirname + "' is not found.");
275 }
276 if (!fs.existsSync(outDir)) {
277 throw new Error("output directory: '" + outDir + "' is not found.");
278 }
279 };
280
281 EntryGeneratorInput.prototype.absolutePath = function(path) {
282 if (Path.isAbsolute(path)) {
283 return Path.resolve(path);
284 }
285 return Path.resolve(process.cwd(), path);
286 };
287
288 EntryGeneratorInput.prototype.relativePath = function(path) {
289 var relPath;
290 relPath = Path.relative(Path.dirname(this.absOutfilePath), path);
291 if (relPath.charAt(0) !== '.') {
292 relPath = './' + relPath;
293 }
294 return relPath;
295 };
296
297 return EntryGeneratorInput;
298
299})();
300
301EntryGenerator = (function() {
302 EntryGenerator.generate = function(facadePath, dirname, outfile, esCode) {
303 var generator, input;
304 if (esCode == null) {
305 esCode = false;
306 }
307 input = new EntryGeneratorInput(facadePath, dirname, outfile);
308 if (esCode) {
309 generator = new ESCodeGenerator(input);
310 } else {
311 generator = new JSCodeGenerator(input);
312 }
313 return generator.generate();
314 };
315
316 function EntryGenerator(input1) {
317 this.input = input1;
318 }
319
320 EntryGenerator.prototype.generate = function() {
321 var code;
322 code = [this.getPragmas(), this.getImportStatements(), this.getPackedData(), this.getExportStatements()].join('\n') + '\n';
323 return fs.writeFileSync(this.input.absOutfilePath, code);
324 };
325
326 EntryGenerator.prototype.getPragmas = function() {
327 return '';
328 };
329
330 EntryGenerator.prototype.getPackedData = function() {
331 var coreClasses, facadeClassName, factories, masterJSONStr, modules, ref;
332 ref = this.input, factories = ref.factories, coreClasses = ref.coreClasses, modules = ref.modules, masterJSONStr = ref.masterJSONStr, facadeClassName = ref.facadeClassName;
333 return "const packedData = {\n // eslint-disable-next-line quotes, key-spacing, object-curly-spacing, comma-spacing\n masterData : " + masterJSONStr + ",\n core: {\n" + (this.getPackedCode(coreClasses, 2)) + ",\n },\n modules: {\n" + (this.getModulesPackedData(modules)) + "\n },\n factories: {\n" + (this.getFactoriesPackedData(factories, 2)) + "\n }\n}\n" + facadeClassName + ".prototype.init = function init() { return this.initWithPacked(packedData) }";
334 };
335
336 EntryGenerator.prototype.getPackedCode = function(classes, indent) {
337 var i, ref, results, spaces;
338 spaces = (function() {
339 results = [];
340 for (var i = 0, ref = indent * 4; 0 <= ref ? i < ref : i > ref; 0 <= ref ? i++ : i--){ results.push(i); }
341 return results;
342 }).apply(this).map(function(x) {
343 return ' ';
344 }).join('');
345 return spaces + classes.map(function(classInfo) {
346 return "'" + classInfo.name + "': " + classInfo.fullClassName;
347 }).join(',\n' + spaces);
348 };
349
350 EntryGenerator.prototype.getModulesPackedData = function(modules) {
351 var _;
352 _ = ' ';
353 return Object.keys(modules).map((function(_this) {
354 return function(modName) {
355 var modClasses;
356 modClasses = modules[modName];
357 return _ + "'" + modName + "': {\n" + (_this.getPackedCode(modClasses, 3)) + "\n" + _ + "}";
358 };
359 })(this)).join(',\n');
360 };
361
362 EntryGenerator.prototype.getFactoriesPackedData = function(factories, indent) {
363 var i, ref, results, spaces;
364 spaces = (function() {
365 results = [];
366 for (var i = 0, ref = indent * 4; 0 <= ref ? i < ref : i > ref; 0 <= ref ? i++ : i--){ results.push(i); }
367 return results;
368 }).apply(this).map(function(x) {
369 return ' ';
370 }).join('');
371 return spaces + Object.keys(factories).map((function(_this) {
372 return function(modelName) {
373 var factoryName;
374 factoryName = factories[modelName];
375 return "'" + modelName + "': " + factoryName;
376 };
377 })(this)).join(',\n' + spaces);
378 };
379
380 return EntryGenerator;
381
382})();
383
384JSCodeGenerator = (function(superClass) {
385 extend(JSCodeGenerator, superClass);
386
387 function JSCodeGenerator() {
388 return JSCodeGenerator.__super__.constructor.apply(this, arguments);
389 }
390
391 JSCodeGenerator.prototype.getPragmas = function() {
392 return "/* eslint quote-props: 0, object-shorthand: 0, no-underscore-dangle: 0 */\nconst __ = function __(m) { return m.default ? m.default : m }";
393 };
394
395 JSCodeGenerator.prototype.getImportStatements = function() {
396 var classInfo, code, coreClasses, facadeClassName, facadePath, i, j, len, len1, modClasses, modName, modules, ref;
397 ref = this.input, coreClasses = ref.coreClasses, modules = ref.modules, facadePath = ref.facadePath, facadeClassName = ref.facadeClassName;
398 code = this.getRequireStatement(facadeClassName, facadePath);
399 for (i = 0, len = coreClasses.length; i < len; i++) {
400 classInfo = coreClasses[i];
401 code += this.getRequireStatement(classInfo.className, classInfo.relPath);
402 }
403 for (modName in modules) {
404 modClasses = modules[modName];
405 for (j = 0, len1 = modClasses.length; j < len1; j++) {
406 classInfo = modClasses[j];
407 code += this.getRequireStatement(classInfo.fullClassName, classInfo.relPath);
408 }
409 }
410 return code;
411 };
412
413 JSCodeGenerator.prototype.getExportStatements = function() {
414 var classNames, coreClasses, facadeClassName, keyValues, modClasses, modName, modules, ref;
415 ref = this.input, coreClasses = ref.coreClasses, modules = ref.modules, facadeClassName = ref.facadeClassName;
416 classNames = coreClasses.map(function(coreClass) {
417 return coreClass.className;
418 });
419 for (modName in modules) {
420 modClasses = modules[modName];
421 classNames = classNames.concat(modClasses.map(function(modClass) {
422 return modClass.fullClassName;
423 }));
424 }
425 keyValues = classNames.map(function(className) {
426 return className + ": " + className;
427 });
428 return "module.exports = {\n " + facadeClassName + ": " + facadeClassName + ",\n " + (keyValues.join(',\n ')) + "\n}";
429 };
430
431 JSCodeGenerator.prototype.getRequireStatement = function(className, path) {
432 return "const " + className + " = __(require('" + path + "'))\n";
433 };
434
435 return JSCodeGenerator;
436
437})(EntryGenerator);
438
439ESCodeGenerator = (function(superClass) {
440 extend(ESCodeGenerator, superClass);
441
442 function ESCodeGenerator() {
443 return ESCodeGenerator.__super__.constructor.apply(this, arguments);
444 }
445
446 ESCodeGenerator.prototype.getPragmas = function() {
447 return "// @flow\n/* eslint quote-props: 0, max-len: 0 */";
448 };
449
450 ESCodeGenerator.prototype.getImportStatements = function() {
451 var classInfo, code, coreClasses, facadeClassName, facadePath, i, j, len, len1, modClasses, modName, modules, ref;
452 ref = this.input, coreClasses = ref.coreClasses, modules = ref.modules, facadePath = ref.facadePath, facadeClassName = ref.facadeClassName;
453 code = this.getImportStatement(facadeClassName, facadePath);
454 for (i = 0, len = coreClasses.length; i < len; i++) {
455 classInfo = coreClasses[i];
456 code += this.getImportStatement(classInfo.className, classInfo.relPath);
457 }
458 for (modName in modules) {
459 modClasses = modules[modName];
460 for (j = 0, len1 = modClasses.length; j < len1; j++) {
461 classInfo = modClasses[j];
462 code += this.getImportStatement(classInfo.fullClassName, classInfo.relPath);
463 }
464 }
465 return code;
466 };
467
468 ESCodeGenerator.prototype.getExportStatements = function() {
469 var classNames, coreClasses, facadeClassName, modClasses, modName, modules, ref;
470 ref = this.input, coreClasses = ref.coreClasses, modules = ref.modules, facadeClassName = ref.facadeClassName;
471 classNames = coreClasses.map(function(coreClass) {
472 return coreClass.className;
473 });
474 for (modName in modules) {
475 modClasses = modules[modName];
476 classNames = classNames.concat(modClasses.map(function(modClass) {
477 return modClass.fullClassName;
478 }));
479 }
480 return "export default " + facadeClassName + "\nexport {\n " + facadeClassName + ",\n " + (classNames.join(',\n ')) + "\n}";
481 };
482
483
484 /**
485 get import statement from className and path
486 */
487
488 ESCodeGenerator.prototype.getImportStatement = function(className, path) {
489 return "import " + className + " from '" + path + "'\n";
490 };
491
492 return ESCodeGenerator;
493
494})(EntryGenerator);
495
496module.exports = EntryGenerator;