1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _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 |
|
9 | var _path = require('path');
|
10 |
|
11 | var _path2 = _interopRequireDefault(_path);
|
12 |
|
13 | var _swig = require('swig');
|
14 |
|
15 | var _swig2 = _interopRequireDefault(_swig);
|
16 |
|
17 | var _fsExtra = require('fs-extra');
|
18 |
|
19 | var _fsExtra2 = _interopRequireDefault(_fsExtra);
|
20 |
|
21 | var _config = require('./config/config');
|
22 |
|
23 | var _config2 = _interopRequireDefault(_config);
|
24 |
|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
26 |
|
27 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var TemplateGenerator = function () {
|
33 |
|
34 | |
35 |
|
36 |
|
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 |
|
48 |
|
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 |
|
68 |
|
69 |
|
70 |
|
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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
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 |
|
105 |
|
106 |
|
107 |
|
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 |
|
121 |
|
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 |
|
145 |
|
146 |
|
147 |
|
148 |
|
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 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | }, {
|
175 | key: '_getDirPath',
|
176 | value: function _getDirPath(type) {
|
177 | return this.TEMPLATES_DIR + '/' + type;
|
178 | }
|
179 |
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
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 |
|
199 | exports.default = TemplateGenerator; |
\ | No newline at end of file |