-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.js
executable file
·217 lines (197 loc) · 7.33 KB
/
plugin.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var kitchen = require("meteor-kitchen");
var util = require('util');
var mkdirp = require('mkdirp');
var beautify_js = require('js-beautify').js;
var beautify_html = require('js-beautify').html;
var beautify_css = require('js-beautify').css;
var component = kitchen.getInput();
var dots = require("dot");
var path = require("path");
var fs = require('fs');
var _ = require('underscore');
var _s = require('underscore.string');
var env = process.env;
var default_templates_dirs = [
path.join(env['PWD']),
path.join(env['PWD'], 'templates'),
path.join(env['PWD'], 'files', 'templates'),
path.join(env['HOME'], '.meteor-kitchen', 'plugins', 'template', 'ui'),
path.join(env['HOME'], '.meteor-kitchen', 'templates', 'ui', 'bootstrap3', 'components')
];
var helpers = {};
helpers = _.extend(helpers, _s);
var loadfile = function (file_path) {
var mydir;
if (!fs.existsSync(file_path)) {
mydir = _.find(default_templates_dirs, function (template_dir) {
return fs.existsSync(path.join(template_dir, file_path));
});
}
if (mydir) {
mypath = path.join(mydir, file_path);
console.log('process file:' + mypath);
return fs.readFileSync(mypath, "utf8");
}
else
return null;
};
var defs = {
loadfile: function (mypath) {
var mydir = _.find(default_templates_dirs, function (template_dir) {
return fs.existsSync(path.join(template_dir, mypath));
});
if (mydir) {
mypath = path.join(mydir, mypath);
console.log('process file:' + mypath);
return fs.readFileSync(mypath);
} else {
return '';
}
}
};
dots.templateSettings = {
evaluate: /\[\[([\s\S]+?)]]/g,
interpolate: /\[\[=([\s\S]+?)]]/g,
encode: /\[\[!([\s\S]+?)]]/g,
use: /\[\[#([\s\S]+?)]]/g,
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\[\[##\s*([\w\.$]+)\s*(:|=)([\s\S]+?)#]]/g,
defineParams: /^\s*([\w$]+):([\s\S]+)/,
conditional: /\[\[\?(\?)?\s*([\s\S]*?)\s*]]/g,
iterate: /\[\[~\s*(?:]]|([\s\S]+?)\s*:\s*([\w$]+)\s*(?::\s*([\w$]+))?\s*]])/g,
varname: 'it,helpers',
strip: false,
append: true,
selfcontained: false,
doNotSkipEncoded: false
};
var beautifier_options =
{
"indent_size": 4,
"indent_char": " ",
"eol": "\n",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": false,
"max_preserve_newlines": 1,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
"break_chained_methods": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 0,
"wrap_attributes": "auto",
"wrap_attributes_indent_size": 4,
"end_with_newline": false
};
var compileFromFile = function (template, data) {
var out = '';
if (template) {
var view = loadfile(template);
if (view) out = dots.template(view, null, defs)(data,helpers);
}
return out;
}
function processComponent(component) {
//console.log(util.inspect(component, false, null));
var htmlArr = [];
var jsArr = [];
var cssArr = [];
var config_file = component['template'] + '.json';
var config = compileFromFile(config_file, component);
if (config) {
config = JSON.parse(config);
// console.log('------------before------------');
// console.log(util.inspect(config, false, null));
// console.log('------------config------------');
// console.log(config);
// console.log('------------after------------');
component = _.defaults(component, config);
//console.log(util.inspect(component, false, null));
// console.log('------------------------');
}
// console.log(util.inspect(component, false, null));
if (component.components) {
component.components = _.map(component.components, function (child_component) {
if (component.root)
child_component.root = path.join(component.root, child_component.name);
var child = processComponent(child_component);
//console.log('child:' + child);
if (child.html) htmlArr.push(child.html);
if (child.js) jsArr.push(child.js);
if (child.css) cssArr.push(child.css);
return child;
})
}
if (!component.html) {
var template_file = component['template'] + '.html';
component.html = compileFromFile(template_file, component);
}
if (!component.js) {
var template_file = component['template'] + '.js';
component.js = compileFromFile(template_file, component);
}
if (!component.css) {
var template_file = component['template'] + '.css';
component.css = compileFromFile(template_file, component);
}
if (!_.isEmpty(htmlArr)) {
component.html = component.html + '\n' + htmlArr.join('\n');
}
if (!_.isEmpty(jsArr)) {
component.js = component.js + '\n' + jsArr.join('\n');
}
if (!_.isEmpty(cssArr)) {
component.css = component.css + '\n' + cssArr.join('\n');
}
if (component.fileout) {
var outdir = path.join(process.env['PWD'], component.fileout);
//console.log('outdir:' + outdir);
if (component.html && component.html.length > 0) {
component.html = beautify_html(component.html,beautifier_options);
fs.writeFile(path.join(outdir, component.name + '.html'), component.html, {flags: 'w'}, function (err) {
if (err) return console.log(err);
});
}
if (component.js && component.js.length > 0) {
component.js = beautify_js(component.js,beautifier_options);
fs.writeFile(path.join(outdir, component.name + '.js'), component.js, {flags: 'w'}, function (err) {
if (err) return console.log(err);
});
}
if (component.css && component.css.length > 0) {
component.css = beautify_css(component.css,beautifier_options);
fs.writeFile(path.join(outdir, component.name + '.css'), component.css, {flags: 'w'}, function (err) {
if (err) return console.log(err);
});
}
component.html = component.js = component.css = '';
}
return component;
}
if (component['template_data']) {
var mycomponent = component['template_data'];
mycomponent = processComponent(mycomponent);
component.js = mycomponent.js;
component.html = beautify_html(mycomponent.html,beautifier_options);
component.js = beautify_js(mycomponent.js, beautifier_options);
component.css = beautify_css(mycomponent.css,beautifier_options);
if (mycomponent.css && mycomponent.css.length > 0) {
var cssdir = mycomponent.root ? mycomponent.root : path.join(process.env['PWD'], 'client/styles');
mkdirp.sync(cssdir);
fs.writeFile(path.join(cssdir, mycomponent.name + '.css'), component.css, {flags: 'w'}, function (err) {
if (err) return console.log(err);
});
}
} else {
component.html = '';
component.js = '';
component.css = '';
}
// console.log(util.inspect(component, false, null));
// write output
kitchen.setOutput(component);