UNPKG

5.32 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _path = require('path');
10
11var _path2 = _interopRequireDefault(_path);
12
13var _swig = require('swig');
14
15var _swig2 = _interopRequireDefault(_swig);
16
17var _fsExtra = require('fs-extra');
18
19var _fsExtra2 = _interopRequireDefault(_fsExtra);
20
21var _config = require('./config/config');
22
23var _config2 = _interopRequireDefault(_config);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29/**
30 * TemplateGenerator
31 */
32var TemplateGenerator = function () {
33
34 /**
35 * Todo: Inject swig, fs and config to mock them in the future tests
36 * @param options
37 */
38 function TemplateGenerator(options) {
39 _classCallCheck(this, TemplateGenerator);
40
41 this.TEMPLATES_DIR = __dirname + '/blueprints';
42 this._create(options);
43 }
44
45 /**
46 *
47 * @param options
48 * @private
49 */
50
51
52 _createClass(TemplateGenerator, [{
53 key: '_create',
54 value: function _create() {
55 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
56 var name = options.name,
57 type = options.type,
58 actions = options.actions,
59 container = options.container;
60
61 var filesType = _config2.default.getConfigFile().filesType;
62 this._createDirectory(this._getDirPath(type), { name: name, actions: actions, filesType: filesType, container: container }, filesType);
63 }
64
65 /**
66 *
67 * @param file
68 * @param data
69 * @returns {*}
70 * @private
71 */
72
73 }, {
74 key: '_compileTpl',
75 value: function _compileTpl(file, _ref) {
76 var name = _ref.name,
77 actions = _ref.actions,
78 filesType = _ref.filesType,
79 container = _ref.container;
80
81 var compiled = _swig2.default.compileFile(file);
82 return compiled({ name: name, actions: actions, filesType: filesType, container: container });
83 }
84
85 /**
86 *
87 * @param name
88 * @param fileType
89 * @param type
90 * @param tpl
91 * @private
92 */
93
94 }, {
95 key: '_createFile',
96 value: function _createFile(name, type, fileType, tpl) {
97 _fsExtra2.default.outputFile(this._createFilePath(name, type, fileType), tpl, function (err) {
98 if (err) console.log(err);
99 });
100 }
101
102 /**
103 *
104 * @param dirPath
105 * @param fileType
106 * @param data
107 * @private
108 */
109
110 }, {
111 key: '_createDirectory',
112 value: function _createDirectory(dirPath, data, fileTypes, container) {
113 var _this = this;
114
115 _fsExtra2.default.readdir(dirPath, function (err, dir) {
116 var name = data.name;
117 var folder = _path2.default.join(process.cwd(), name);
118 var filePath = void 0;
119
120 // If container option is not provided we must not create the Container
121 // component, thus we remove it from the blueprints array.
122 if (!data.container) {
123 dir = dir.filter(function (d) {
124 return !d.includes("Container");
125 });
126 }
127
128 dir.forEach(function (tempFile) {
129 var compiled = _this._compileTpl(dirPath + '/' + tempFile, data);
130
131 var fileName = _this._createFileName(tempFile, name, fileTypes);
132
133 filePath = _path2.default.join(folder, fileName);
134
135 _fsExtra2.default.outputFile(filePath, compiled, function (err) {
136 if (err) console.log(err);
137 });
138 });
139 });
140 }
141
142 /**
143 *
144 * @param tempFile
145 * @param name
146 * @param fileTypes
147 * @returns {*}
148 * @private
149 */
150
151 }, {
152 key: '_createFileName',
153 value: function _createFileName(tempFile, name, fileTypes) {
154 var newName = tempFile.replace(/temp/, name);
155
156 if (newName.indexOf('tpl') > -1) {
157 newName = newName.replace(/tpl/, 'component').replace(/extension/, fileTypes.html);
158 }
159
160 if (newName.indexOf('sty') > -1) {
161 newName = newName.replace(/sty/, 'component').replace(/extension/, fileTypes.style);
162 }
163
164 return newName;
165 }
166
167 /**
168 *
169 * @param type
170 * @returns {*}
171 * @private
172 */
173
174 }, {
175 key: '_getDirPath',
176 value: function _getDirPath(type) {
177 return this.TEMPLATES_DIR + '/' + type;
178 }
179
180 /**
181 *
182 * @param name
183 * @param type
184 * @param fileType
185 * @returns {*}
186 * @private
187 */
188
189 }, {
190 key: '_createFilePath',
191 value: function _createFilePath(name, type, fileType) {
192 return _path2.default.join(process.cwd(), name + '.' + type + '.' + fileType);
193 }
194 }]);
195
196 return TemplateGenerator;
197}();
198
199exports.default = TemplateGenerator;
\No newline at end of file