gm3 1.1.2 → 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/README.md CHANGED
@@ -4,7 +4,8 @@
4
4
 
5
5
  ## Getting Started
6
6
 
7
- 使用gm3需要现在全局安装npm包
7
+
8
+ 可以使用gm3全局安装npm包,直接执行 gm3 生成对应内容
8
9
 
9
10
  ### 环境基础
10
11
 
@@ -12,9 +13,14 @@
12
13
 
13
14
  2.安装gm3
14
15
 
16
+ * 通过代码调用: `require('gm3')`
17
+ ```
18
+ npm install gm3
15
19
  ```
16
- npm install -g gm3 # or [url of this repository]
17
20
 
21
+ * 全局使用 `gm3`
22
+ ```
23
+ npm install -g gm3 # or [url of this repository]
18
24
  ```
19
25
 
20
26
  ### 开发指南
@@ -152,10 +158,11 @@ partOne,partTwo,partThree都是html文本。采用<%-%>,语法参考 [EJS]
152
158
  ***template*** 由包名和版本号组成。固定格式引用。
153
159
  ***data*** 填充该template的json数据,会覆盖模板的默认数据。可以为空,为空则用默认数据。
154
160
 
155
- ## 指令
161
+ ## 指令 -- gm3
156
162
 
157
163
  ```
158
164
  Usage: gm3 [options]
165
+ default: gm3 = gm3 -b
159
166
 
160
167
  Gm help.
161
168
 
@@ -168,6 +175,12 @@ Gm help.
168
175
  -a, --append-array Appends intead of replaces an array
169
176
  -b, --build Compile && build file
170
177
  -i, --install[=MODULES] Install templates
178
+ -v, --version Output version information and exit
179
+
180
+ Report bugs to <wyyxdgm@163.com>.
181
+
182
+ ```
183
+ <!--
171
184
  -A, --auth[=USER:PASS] User auth by name and password
172
185
  -s, --search[=key1:key2:...]
173
186
  Search by keys
@@ -175,11 +188,7 @@ Gm help.
175
188
  -I, --info Show local infos
176
189
  -V, --verbose Makes output more verbose
177
190
  -h, --help Display this help message and exit
178
- -v, --version Output version information and exit
179
-
180
- Report bugs to <wyyxdgm@163.com>.
181
-
182
- ```
191
+ -->
183
192
 
184
193
  **举例**
185
194
 
@@ -254,3 +263,24 @@ console.log(gm3Str);
254
263
 
255
264
  * EJS: https://github.com/mde/ejs
256
265
 
266
+ ## TODO
267
+
268
+ cli support completely
269
+
270
+ ```
271
+ -d, --directory[=DIR] The directory to be builded, default is current
272
+ directory
273
+ -o, --output[=PATH] Write the builded content to the target file
274
+ -m, --main[=PATH] defind the entry file
275
+ -a, --append-array Appends intead of replaces an array
276
+ -b, --build Compile && build file
277
+ -i, --install[=MODULES] Install templates
278
+ -A, --auth[=USER:PASS] User auth by name and password
279
+ -s, --search[=key1:key2:...]
280
+ Search by keys
281
+ -p, --publish Publish package
282
+ -I, --info Show local infos
283
+ -V, --verbose Makes output more verbose
284
+ -h, --help Display this help message and exit
285
+ -v, --version Output version information and exit
286
+ ```
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.buildStr(conf.dir || '', conf.appendArray, conf);
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.2",
3
+ "version": "1.1.4",
4
4
  "description": "constructing your world by snippets",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -9,16 +9,31 @@
9
9
  "bin": {
10
10
  "gm3": "./bin/gm.js"
11
11
  },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/wyyxdgm/gm3.git"
15
+ },
16
+ "keywords": [
17
+ "ejs",
18
+ "template",
19
+ "json2html",
20
+ "gm3"
21
+ ],
12
22
  "author": "wyyxdgm@163.com",
13
23
  "license": "ISC",
24
+ "bugs": {
25
+ "url": "https://github.com/wyyxdgm/gm3/issues"
26
+ },
27
+ "homepage": "https://github.com/wyyxdgm/gm3#readme",
14
28
  "dependencies": {
15
- "ejs": "^2.6.1",
16
- "underscore": "^1.9.1",
17
29
  "argp": "^1.0.4",
18
30
  "colors": "^1.3.2",
31
+ "ejs": "^2.6.1",
19
32
  "ejs-lint": "^0.3.0",
20
33
  "git-clone-repo": "^1.0.0",
34
+ "js-beautify": "^1.15.4",
21
35
  "ncp": "^2.0.0",
22
- "pretty": "^2.0.0"
36
+ "pretty": "^2.0.0",
37
+ "underscore": "^1.9.1"
23
38
  }
24
39
  }