gm3 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -1
- package/lib/util.js +95 -1
- package/package.json +5 -4
package/lib/index.js
CHANGED
|
@@ -4,4 +4,4 @@ module.exports.auth = util.auth;
|
|
|
4
4
|
module.exports.info = util.info;
|
|
5
5
|
module.exports.publish = util.publish;
|
|
6
6
|
module.exports.search = util.search;
|
|
7
|
-
module.exports.build = (conf) => util.
|
|
7
|
+
module.exports.build = (conf) => util.buildJsStr(conf.dir || '', conf.appendArray, conf);
|
package/lib/util.js
CHANGED
|
@@ -254,6 +254,99 @@ let buildStr = (baseDir, appendArray = true, conf) => {
|
|
|
254
254
|
return require('pretty')(htmlStr);
|
|
255
255
|
}
|
|
256
256
|
|
|
257
|
+
/**
|
|
258
|
+
* buildJsStr - 使用 js-beautify 格式化 JavaScript 代码
|
|
259
|
+
*/
|
|
260
|
+
let buildJsStr = (baseDir, appendArray = true, conf) => {
|
|
261
|
+
let {
|
|
262
|
+
htmlContent,
|
|
263
|
+
json,
|
|
264
|
+
filePath,
|
|
265
|
+
conf2
|
|
266
|
+
} = load(baseDir, undefined, conf) || {};
|
|
267
|
+
/**
|
|
268
|
+
* init gmComponents by load gm_components
|
|
269
|
+
*/
|
|
270
|
+
let gmComponents = {};
|
|
271
|
+
let _gm_path = path.join(baseDir, 'gm_components');
|
|
272
|
+
if (fs.existsSync(_gm_path)) {
|
|
273
|
+
fs.readdirSync(_gm_path).forEach((moduleDir) => {
|
|
274
|
+
let _p = path.join(baseDir, 'gm_components', moduleDir);
|
|
275
|
+
if (__verbose) console.log(LOG_TITLE, 'Load'.yellow, baseDir ? path.relative(baseDir, _p) : _p);
|
|
276
|
+
gmComponents[moduleDir] = load(_p, baseDir) || {};
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
let resolveKey = (o, k) => {
|
|
281
|
+
let v = o[k];
|
|
282
|
+
if (_.isObject(v) && v.$template) { //template
|
|
283
|
+
let moduleComponent = gmComponents[v.$template];
|
|
284
|
+
if (!moduleComponent) return console.log(`no template: ${v.$template}`.red);
|
|
285
|
+
let data = deepExtend(moduleComponent.json, v.$data, appendArray);
|
|
286
|
+
for (key in data) {
|
|
287
|
+
data[key] = resolveKey(data, key);
|
|
288
|
+
}
|
|
289
|
+
if (__verbose) console.log(LOG_TITLE, 'Render'.yellow, v.$template);
|
|
290
|
+
try {
|
|
291
|
+
return ejs.render(moduleComponent.htmlContent, data, {
|
|
292
|
+
root: baseDir,
|
|
293
|
+
filename: moduleComponent.filePath
|
|
294
|
+
});
|
|
295
|
+
} catch (e) {
|
|
296
|
+
let lint = ejsLint(moduleComponent.htmlContent, data);
|
|
297
|
+
console.log('RenderError'.red, e, lint);
|
|
298
|
+
if (__verbose) console.error(moduleComponent.htmlContent, data);
|
|
299
|
+
}
|
|
300
|
+
} else if (_.isArray(v)) {
|
|
301
|
+
return _.map(v, e => resolveKey({
|
|
302
|
+
k: e
|
|
303
|
+
}, 'k'));
|
|
304
|
+
} else {
|
|
305
|
+
return v;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
for (key in json) {
|
|
309
|
+
json[key] = resolveKey(json, key);
|
|
310
|
+
}
|
|
311
|
+
let jsStr;
|
|
312
|
+
try {
|
|
313
|
+
jsStr = ejs.render(htmlContent, json, {
|
|
314
|
+
root: baseDir,
|
|
315
|
+
filename: filePath
|
|
316
|
+
});
|
|
317
|
+
} catch (e) {
|
|
318
|
+
let lint = ejsLint(htmlContent, json);
|
|
319
|
+
console.log('RenderError'.red, e, lint);
|
|
320
|
+
if (__verbose) console.error(htmlContent, json);
|
|
321
|
+
}
|
|
322
|
+
// 使用 js-beautify 格式化 JavaScript 代码
|
|
323
|
+
return require('js-beautify').js(jsStr, {
|
|
324
|
+
indent_size: 4,
|
|
325
|
+
indent_char: ' ',
|
|
326
|
+
indent_with_tabs: false,
|
|
327
|
+
editorconfig: false,
|
|
328
|
+
eol: '\n',
|
|
329
|
+
end_with_newline: false,
|
|
330
|
+
indent_level: 0,
|
|
331
|
+
preserve_newlines: true,
|
|
332
|
+
max_preserve_newlines: 2,
|
|
333
|
+
space_in_paren: false,
|
|
334
|
+
space_in_empty_paren: false,
|
|
335
|
+
jslint_happy: false,
|
|
336
|
+
space_after_anon_function: false,
|
|
337
|
+
space_after_named_function: false,
|
|
338
|
+
brace_style: 'collapse',
|
|
339
|
+
unindent_chained_methods: false,
|
|
340
|
+
break_chained_methods: false,
|
|
341
|
+
keep_array_indentation: false,
|
|
342
|
+
unescape_strings: false,
|
|
343
|
+
wrap_line_length: 150,
|
|
344
|
+
e4x: false,
|
|
345
|
+
comma_first: false,
|
|
346
|
+
operator_position: 'before-newline'
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
257
350
|
let build = (baseDir, target, appendArray = true, main) => {
|
|
258
351
|
let firstConf = {
|
|
259
352
|
main: main
|
|
@@ -321,5 +414,6 @@ module.exports = {
|
|
|
321
414
|
deepExtend,
|
|
322
415
|
init,
|
|
323
416
|
build,
|
|
324
|
-
buildStr
|
|
417
|
+
buildStr,
|
|
418
|
+
buildJsStr
|
|
325
419
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gm3",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "constructing your world by snippets",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,13 +26,14 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/wyyxdgm/gm3#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"ejs": "^2.6.1",
|
|
30
|
-
"underscore": "^1.9.1",
|
|
31
29
|
"argp": "^1.0.4",
|
|
32
30
|
"colors": "^1.3.2",
|
|
31
|
+
"ejs": "^2.6.1",
|
|
33
32
|
"ejs-lint": "^0.3.0",
|
|
34
33
|
"git-clone-repo": "^1.0.0",
|
|
34
|
+
"js-beautify": "^1.15.4",
|
|
35
35
|
"ncp": "^2.0.0",
|
|
36
|
-
"pretty": "^2.0.0"
|
|
36
|
+
"pretty": "^2.0.0",
|
|
37
|
+
"underscore": "^1.9.1"
|
|
37
38
|
}
|
|
38
39
|
}
|