@webos-tools/cli 3.0.4 → 3.0.5

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.
Files changed (159) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.js +52 -52
  3. package/APIs.js +79 -79
  4. package/CHANGELOG.md +128 -128
  5. package/LICENSE +201 -201
  6. package/README.md +218 -218
  7. package/bin/ares-config.js +199 -199
  8. package/bin/ares-device-info.js +30 -30
  9. package/bin/ares-device.js +219 -219
  10. package/bin/ares-generate.js +270 -270
  11. package/bin/ares-inspect.js +179 -179
  12. package/bin/ares-install.js +223 -223
  13. package/bin/ares-launch.js +318 -318
  14. package/bin/ares-log.js +255 -255
  15. package/bin/ares-novacom.js +223 -223
  16. package/bin/ares-package.js +336 -336
  17. package/bin/ares-pull.js +156 -156
  18. package/bin/ares-push.js +155 -155
  19. package/bin/ares-server.js +174 -174
  20. package/bin/ares-setup-device.js +520 -520
  21. package/bin/ares-shell.js +132 -132
  22. package/bin/ares.js +166 -166
  23. package/files/conf/ares.json +49 -49
  24. package/files/conf/command-service.json +73 -73
  25. package/files/conf/config.json +22 -22
  26. package/files/conf/ipk.json +30 -30
  27. package/files/conf/novacom-devices.json +35 -35
  28. package/files/conf/query/query-app.json +14 -14
  29. package/files/conf/query/query-hosted.json +18 -18
  30. package/files/conf/query/query-package.json +10 -10
  31. package/files/conf/query/query-service.json +6 -6
  32. package/files/conf/sdk.json +8 -8
  33. package/files/conf/template.json +57 -57
  34. package/files/conf/webos_emul +27 -27
  35. package/files/conf-base/env/sdk-ose.json +8 -8
  36. package/files/conf-base/env/sdk-tv.json +8 -8
  37. package/files/conf-base/profile/config-ose.json +21 -21
  38. package/files/conf-base/profile/config-tv.json +22 -22
  39. package/files/conf-base/query/query-app.json +14 -14
  40. package/files/conf-base/query/query-hosted.json +18 -18
  41. package/files/conf-base/query/query-package.json +10 -10
  42. package/files/conf-base/query/query-service.json +6 -6
  43. package/files/conf-base/template-conf/ose-templates.json +67 -67
  44. package/files/conf-base/template-conf/tv-sdk-templates.json +57 -57
  45. package/files/help/ares-config.help +43 -43
  46. package/files/help/ares-device.help +94 -94
  47. package/files/help/ares-generate.help +65 -65
  48. package/files/help/ares-inspect.help +70 -70
  49. package/files/help/ares-install.help +90 -90
  50. package/files/help/ares-launch.help +100 -100
  51. package/files/help/ares-log-pmlogd.help +84 -84
  52. package/files/help/ares-log.help +101 -101
  53. package/files/help/ares-novacom.help +68 -68
  54. package/files/help/ares-package.help +101 -101
  55. package/files/help/ares-pull.help +38 -38
  56. package/files/help/ares-push.help +38 -38
  57. package/files/help/ares-server.help +39 -39
  58. package/files/help/ares-setup-device.help +75 -75
  59. package/files/help/ares-shell.help +42 -42
  60. package/files/help/ares.help +47 -47
  61. package/files/help/readme.help +23 -23
  62. package/files/schema/ApplicationDescription.schema +319 -319
  63. package/files/schema/NovacomDevices.schema +61 -61
  64. package/files/templates/ose-sdk-templates/appinfo/appinfo.json +10 -10
  65. package/files/templates/ose-sdk-templates/bootplate-web/index.html +88 -88
  66. package/files/templates/ose-sdk-templates/hosted-webapp/index.html +13 -13
  67. package/files/templates/ose-sdk-templates/icon/icon.png +0 -0
  68. package/files/templates/ose-sdk-templates/js-service/helloclient.js +31 -31
  69. package/files/templates/ose-sdk-templates/js-service/helloworld_webos_service.js +188 -188
  70. package/files/templates/ose-sdk-templates/qml-app/main.qml +68 -68
  71. package/files/templates/ose-sdk-templates/qmlappinfo/appinfo.json +10 -10
  72. package/files/templates/ose-sdk-templates/serviceinfo/package.json +11 -11
  73. package/files/templates/ose-sdk-templates/serviceinfo/services.json +8 -8
  74. package/files/templates/tv-sdk-templates/appinfo/appinfo.json +10 -10
  75. package/files/templates/tv-sdk-templates/bootplate-web/index.html +58 -58
  76. package/files/templates/tv-sdk-templates/bootplate-web/webOSTVjs-1.2.10/LICENSE-2.0.txt +202 -202
  77. package/files/templates/tv-sdk-templates/hosted-webapp/index.html +14 -14
  78. package/files/templates/tv-sdk-templates/js-service/helloworld_service.js +39 -39
  79. package/files/templates/tv-sdk-templates/packageinfo/packageinfo.json +3 -3
  80. package/files/templates/tv-sdk-templates/serviceinfo/package.json +11 -11
  81. package/files/templates/tv-sdk-templates/serviceinfo/services.json +8 -8
  82. package/files/templates/tv-sdk-templates/webicon/icon.png +0 -0
  83. package/files/templates/tv-sdk-templates/webicon/largeIcon.png +0 -0
  84. package/lib/base/ares.html +40 -40
  85. package/lib/base/cli-appdata.js +290 -290
  86. package/lib/base/cli-control.js +44 -44
  87. package/lib/base/common-tools.js +29 -29
  88. package/lib/base/error-handler.js +265 -265
  89. package/lib/base/file-watcher.js +155 -155
  90. package/lib/base/help-format.js +147 -147
  91. package/lib/base/luna.js +178 -178
  92. package/lib/base/novacom.js +1191 -1191
  93. package/lib/base/sdkenv.js +59 -59
  94. package/lib/base/server.js +137 -137
  95. package/lib/base/setup-device.js +328 -328
  96. package/lib/base/version-tools.js +79 -79
  97. package/lib/device.js +1419 -1419
  98. package/lib/generator.js +377 -377
  99. package/lib/inspect.js +494 -494
  100. package/lib/install.js +463 -463
  101. package/lib/launch.js +605 -605
  102. package/lib/log.js +584 -584
  103. package/lib/package.js +2129 -2129
  104. package/lib/pull.js +231 -231
  105. package/lib/pusher.js +210 -210
  106. package/lib/session.js +74 -74
  107. package/lib/shell.js +193 -193
  108. package/lib/tar-filter-pack.js +62 -62
  109. package/lib/util/copy.js +31 -31
  110. package/lib/util/createFileName.js +40 -40
  111. package/lib/util/eof.js +30 -30
  112. package/lib/util/json.js +63 -63
  113. package/lib/util/merge.js +14 -14
  114. package/lib/util/objclone.js +40 -40
  115. package/lib/util/spinner.js +37 -37
  116. package/npm-shrinkwrap.json +9115 -9115
  117. package/package.json +100 -100
  118. package/scripts/postinstall.js +24 -24
  119. package/spec/helpers/reporter.js +65 -65
  120. package/spec/jsSpecs/apiTest/generator.spec.js +372 -372
  121. package/spec/jsSpecs/apiTest/inspector.spec.js +89 -89
  122. package/spec/jsSpecs/apiTest/installer.spec.js +67 -67
  123. package/spec/jsSpecs/apiTest/launcher.spec.js +150 -150
  124. package/spec/jsSpecs/apiTest/packager.spec.js +194 -194
  125. package/spec/jsSpecs/apiTest/puller.spec.js +101 -101
  126. package/spec/jsSpecs/apiTest/pusher.spec.js +103 -103
  127. package/spec/jsSpecs/apiTest/server.spec.js +115 -115
  128. package/spec/jsSpecs/apiTest/setupDevice.spec.js +93 -93
  129. package/spec/jsSpecs/apiTest/shell.spec.js +49 -49
  130. package/spec/jsSpecs/ares-config.spec.js +78 -78
  131. package/spec/jsSpecs/ares-device.spec.js +443 -443
  132. package/spec/jsSpecs/ares-generate.spec.js +397 -397
  133. package/spec/jsSpecs/ares-inspect.spec.js +252 -252
  134. package/spec/jsSpecs/ares-install.spec.js +150 -150
  135. package/spec/jsSpecs/ares-launch.spec.js +301 -301
  136. package/spec/jsSpecs/ares-log.spec.js +824 -824
  137. package/spec/jsSpecs/ares-novacom.spec.js +149 -149
  138. package/spec/jsSpecs/ares-package.spec.js +1211 -1211
  139. package/spec/jsSpecs/ares-pull.spec.js +157 -157
  140. package/spec/jsSpecs/ares-push.spec.js +146 -146
  141. package/spec/jsSpecs/ares-server.spec.js +160 -160
  142. package/spec/jsSpecs/ares-setup-device.spec.js +281 -281
  143. package/spec/jsSpecs/ares-shell.spec.js +220 -220
  144. package/spec/jsSpecs/ares.spec.js +83 -83
  145. package/spec/jsSpecs/common-spec.js +169 -169
  146. package/spec/support/jasmine.json +22 -22
  147. package/spec/tempFiles/nativeApp/auto/pkg_arm64/GLES2 +0 -0
  148. package/spec/tempFiles/nativeApp/auto/pkg_arm64/appinfo.json +9 -9
  149. package/spec/tempFiles/nativeApp/ose/pkg_arm/Hello +0 -0
  150. package/spec/tempFiles/nativeApp/ose/pkg_arm/appinfo.json +8 -8
  151. package/spec/tempFiles/nativeApp/ose/pkg_arm/package.properties +2 -2
  152. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/Hello +0 -0
  153. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/appinfo.json +9 -9
  154. package/spec/tempFiles/nativeApp/rsi/pkg_x86/GLES2 +0 -0
  155. package/spec/tempFiles/nativeApp/rsi/pkg_x86/appinfo.json +9 -9
  156. package/spec/tempFiles/sign/sign.crt +32 -32
  157. package/spec/tempFiles/sign/signPriv.key +52 -52
  158. package/spec/test_data/ares-generate.json +41 -41
  159. package/spec/test_data/ares.json +33 -33
package/lib/generator.js CHANGED
@@ -1,377 +1,377 @@
1
- #!/usr/bin/env node
2
-
3
- /*
4
- * Copyright (c) 2020-2024 LG Electronics Inc.
5
- *
6
- * SPDX-License-Identifier: Apache-2.0
7
- */
8
-
9
- const promise = require('bluebird'),
10
- Table = require('easy-table'),
11
- fs = promise.promisifyAll(require('fs-extra')),
12
- log = require('npmlog'),
13
- path = require('path'),
14
- errHndl = require('./base/error-handler'),
15
- copyToDirAsync = require('./util/copy').copyToDirAsync,
16
- readJsonSync = require('./util/json').readJsonSync,
17
- merge = require('./util/merge');
18
-
19
- const templatePath = path.join(__dirname, '/../files/conf/', 'template.json');
20
- let templates;
21
-
22
- log.heading = 'generator';
23
- log.level = 'warn';
24
-
25
- function Generator() {
26
- if (templatePath) {
27
- const cliPath = path.join(__dirname, '..');
28
- let contents = fs.readFileSync(templatePath);
29
- contents = contents.toString().replace(/\$cli-root/gi, cliPath).replace(/\\/g,'/');
30
- templates = JSON.parse(contents);
31
- } else {
32
- templates = null;
33
- }
34
- }
35
-
36
- Generator.prototype.showTemplates = function(listType, next) {
37
- const templateList = this.getTmpl(),
38
- table = new Table(),
39
- _displayType = {
40
- "webapp": "Web App",
41
- "nativeapp": "Native App",
42
- "webappinfo": "Web App Info",
43
- "nativeappinfo": "Native App Info",
44
- "jsservice": "JS Service",
45
- "nativeservice": "Native Service",
46
- "jsserviceinfo": "JS Service Info",
47
- "nativeserviceinfo": "Native Service Info",
48
- "icon": "Icon",
49
- "library": "Library",
50
- "packageinfo": "Package Info",
51
- "qmlapp": "QML App",
52
- "qmlappinfo": "QML App Info"
53
- };
54
-
55
- for (const name in templateList) {
56
- if (templateList[name].hide === true || !templateList[name].type) {
57
- continue;
58
- }
59
- const isDefault = (templateList[name].default) ? "(default) " : "",
60
- type = _displayType[templateList[name].type] || templateList[name].type;
61
-
62
- if (listType && ["true", "false", true, false].indexOf(listType) === -1) {
63
- if (templateList[name].type &&
64
- (templateList[name].type.match(new RegExp(listType+"$","gi")) === null)) {
65
- continue;
66
- }
67
- }
68
- table.cell('ID', name);
69
- table.cell('Project Type', type);
70
- table.cell('Description', isDefault + templateList[name].description);
71
- table.newRow();
72
- }
73
- return next(null, {msg: table.toString()});
74
- };
75
-
76
- Generator.prototype.generate = function(options, next) {
77
- // For API
78
- if (!options.tmplName) {
79
- return next(errHndl.getErrMsg("EMPTY_VALUE", "TEMPLATE"));
80
- }
81
- if (!options.out) {
82
- return next(errHndl.getErrMsg("EMPTY_VALUE", "APP_DIR"));
83
- }
84
-
85
- const tmplName = options.tmplName,
86
- pkginfo = options.pkginfo || {},
87
- svcinfo = options.svcinfo || {},
88
- svcName = options.svcName,
89
- out = options.out,
90
- dest = path.resolve(out),
91
- existDir = this.existOutDir(dest),
92
- templateList = this.getTmpl(),
93
- template = templateList[tmplName];
94
- let appinfo = options.appinfo || {};
95
-
96
- // For API
97
- if (!template) {
98
- return next(errHndl.getErrMsg("INVALID_VALUE", "TEMPLATE", options.tmplName));
99
- }
100
- if (!options.overwrite && existDir) {
101
- return next(errHndl.getErrMsg("NOT_OVERWRITE_DIR", dest));
102
- }
103
-
104
- if (template.metadata && template.metadata.data && typeof template.metadata.data === 'object') {
105
- appinfo = merge(appinfo, template.metadata.data);
106
- }
107
-
108
- promise.resolve()
109
- .then(function() {
110
- // If props is not exist, this input from query-mode
111
- // If props is exist, this input from props
112
- // Check argv.query, argv["no-query"], options.props and conditional statement.
113
- if (template.type.match(/(app$|appinfo$)/)) {
114
- parsePropArgs(options.props, appinfo);
115
- } else if (template.type.match(/(service$|serviceinfo$)/)) {
116
- parsePropArgs(options.props, svcinfo);
117
- } else if (template.type.match(/(package$|packageinfo$)/)) {
118
- parsePropArgs(options.props, pkginfo);
119
- }
120
- })
121
- .then(function() {
122
- if (svcName) {
123
- svcinfo.id = svcName;
124
- svcinfo.services = [{
125
- "name": svcName
126
- }];
127
- } else if (!svcName && svcinfo && !!svcinfo.id) {
128
- svcinfo.services = [{
129
- "name": svcinfo.id
130
- }];
131
- }
132
- });
133
-
134
- return promise.resolve()
135
- .then(function() {
136
- log.info("generator#generate()", "template name:" + tmplName);
137
- next(null, {msg: "Generating " + tmplName + " in " + dest});
138
-
139
- let srcs;
140
- if (tmplName.match(/(^hosted)/)) {
141
- srcs = [].concat(template.path);
142
- return promise.all(srcs.map(function(src) {
143
- return copyToDirAsync(src, dest);
144
- })).then(function() {
145
- let metaTmpl;
146
- let url;
147
- if (template.metadata && template.metadata.id) {
148
- metaTmpl = templateList[template.metadata.id];
149
- }
150
- if (metaTmpl) {
151
- if (appinfo.url) {
152
- url = appinfo.url;
153
- delete appinfo.url;
154
- const urlTmpl = {"path":path.join(srcs[0],'index.html')};
155
- _writeURLdata(urlTmpl, url);
156
- }
157
- return _writeMetadata(metaTmpl, appinfo, svcinfo, pkginfo);
158
- } else {
159
- return;
160
- }
161
- });
162
- } else if (tmplName.match(/(^qmlapp$)/)) {
163
- srcs = [].concat(template.path);
164
- return promise.all(srcs.map(function(src) {
165
- return copyToDirAsync(src, dest);
166
- })).then(function() {
167
- let metaTmpl;
168
- if (template.metadata && template.metadata.id) {
169
- metaTmpl = templateList[template.metadata.id];
170
- }
171
- if (metaTmpl) {
172
- if (appinfo.id) {
173
- const qmlTmpl = {"path":path.join(srcs[0],'main.qml')};
174
- _writeAppIDdata(qmlTmpl, appinfo.id);
175
- }
176
- return _writeMetadata(metaTmpl, appinfo, svcinfo, pkginfo);
177
- } else {
178
- return;
179
- }
180
- });
181
- } else if (template.type.match(/info$/)) {
182
- return _writeMetadata(template, appinfo, svcinfo, pkginfo);
183
- } else {
184
- srcs = [].concat(template.path);
185
- return promise.all(srcs.map(function(src) {
186
- log.info("generator#generate()", "template src:" + src);
187
- return copyToDirAsync(src, dest);
188
- })).then(function() {
189
- let metaTmpl;
190
- if (template.metadata && template.metadata.id) {
191
- metaTmpl = templateList[template.metadata.id];
192
- }
193
- if (metaTmpl) {
194
- return _writeMetadata(metaTmpl, appinfo, svcinfo, pkginfo);
195
- } else {
196
- return;
197
- }
198
- });
199
- }
200
- })
201
- .then(function() {
202
- const deps = templateList[tmplName].deps || [];
203
- return promise.all(deps.map(function(dep) {
204
- if (!templateList[dep]) {
205
- log.warn("generator#generate()", "Invalid template id:" + dep);
206
- return;
207
- } else if (!templateList[dep].path) {
208
- log.warn("generator#generate()", "Invalid template path:" + dep);
209
- return;
210
- }
211
- return copyToDirAsync(templateList[dep].path, dest);
212
- }));
213
- })
214
- .then(function() {
215
- log.info("generator#generate()", "done");
216
- return next(null, {
217
- msg: "Success"
218
- });
219
- })
220
- .catch(function(err) {
221
- log.silly("generator#generate()", "err:", err);
222
- throw err;
223
- });
224
-
225
- function _writeAppIDdata(qmlTmpl, appId) {
226
- const filePaths = [].concat(qmlTmpl.path);
227
- return promise.all(filePaths.map(function(file) {
228
- return fs.lstatAsync(file)
229
- .then(function(stats) {
230
- if (!stats.isFile()) {
231
- throw errHndl.getErrMsg("INVALID_PATH", "meta template", file);
232
- }
233
- // eslint-disable-next-line no-useless-escape
234
- const exp = /appId\s*:\s*[\'\"][\w.]*[\'\"]/g;
235
- const destFile = path.join(dest, path.basename(file));
236
- let qmlFile = fs.readFileSync(file, 'utf8');
237
- qmlFile = qmlFile.replace(exp, "appId: \"" + appId + "\"");
238
-
239
- fs.writeFileSync(destFile, qmlFile, {encoding: 'utf8'});
240
- });
241
- }))
242
- .then(function() {
243
- log.info("generator#generate()#_writeAppIDdata()", "done");
244
- return;
245
- })
246
- .catch(function(err) {
247
- log.silly("generator#generate()#_writeAppIDdata()", "err:", err);
248
- throw err;
249
- });
250
- }
251
-
252
- function _writeURLdata(urlTmpl, url) {
253
- const filePaths = [].concat(urlTmpl.path);
254
- return promise.all(filePaths.map(function(file) {
255
- return fs.lstatAsync(file)
256
- .then(function(stats) {
257
- if (!stats.isFile()) {
258
- throw errHndl.getErrMsg("INVALID_PATH", "meta template", file);
259
- }
260
- let html = fs.readFileSync(file, 'utf8');
261
- // eslint-disable-next-line no-useless-escape
262
- const exp = new RegExp("(?:[\'\"])([\:/.A-z?<_&\s=>0-9;-]+\')");
263
- // eslint-disable-next-line no-useless-escape
264
- html = html.replace(exp, "\'" + url + "\'");
265
- const destFile = path.join(dest, path.basename(file));
266
-
267
- fs.writeFileSync(destFile, html, {encoding: 'utf8'});
268
- });
269
- }))
270
- .then(function() {
271
- log.info("generator#generate()#_writeURLdata()", "done");
272
- return;
273
- })
274
- .catch(function(err) {
275
- log.silly("generator#generate()#_writeURLdata()", "err:", err);
276
- throw err;
277
- });
278
- }
279
-
280
- function _writeMetadata(metaTmpl, _appinfo, _svcinfo, _pkginfo) {
281
- const metaPaths = [].concat(metaTmpl.path),
282
- appInfo = _appinfo || {},
283
- svcInfo = _svcinfo || {},
284
- pkgInfo = _pkginfo || {};
285
-
286
- return promise.all(metaPaths.map(function(file) {
287
- return fs.lstatAsync(file)
288
- .then(function(stats) {
289
- if (!stats.isFile()) {
290
- throw errHndl.getErrMsg("INVALID_PATH", "meta template", file);
291
- }
292
- const fileName = path.basename(file);
293
- let info = readJsonSync(file);
294
-
295
- if (fileName === 'appinfo.json') {
296
- info = merge(info, appInfo);
297
- } else if (fileName === "services.json") {
298
- info = merge(info, svcInfo);
299
- } else if (fileName === "package.json" &&
300
- (metaTmpl.type === "jsserviceinfo" || metaTmpl.type === "nativeserviceinfo")) {
301
- info.name = svcInfo.id || info.name;
302
- } else if (fileName === "packageinfo.json") {
303
- info = merge(info, pkgInfo);
304
- }
305
- return info;
306
- })
307
- .then(function(info) {
308
- const destFile = path.join(dest, path.basename(file));
309
- return fs.mkdirsAsync(dest)
310
- .then(function() {
311
- return fs.writeFileSync(destFile, JSON.stringify(info, null, 2), {
312
- encoding: 'utf8'
313
- });
314
- });
315
- });
316
- }))
317
- .then(function() {
318
- log.info("generator#generate()#_writeMetadata()", "done");
319
- return;
320
- })
321
- .catch(function(err) {
322
- log.silly("generator#generate()#_writeMetadata()", "err:", err);
323
- throw err;
324
- });
325
- }
326
- };
327
-
328
- Generator.prototype.getTmpl = function() {
329
- return templates;
330
- };
331
-
332
- Generator.prototype.existOutDir = function(outDir) {
333
- log.verbose("generator#existOutDir()", outDir);
334
- try {
335
- const files = fs.readdirSync(outDir);
336
- if (files.length > 0)
337
- return true;
338
- } catch (err) {
339
- if (err && err.code === 'ENOTDIR') {
340
- throw errHndl.getErrMsg("NOT_DIRTYPE_PATH", outDir);
341
- }
342
- if (err && err.code === 'ENOENT') {
343
- log.verbose("generator#generate()", "The directory does not exist.");
344
- return false;
345
- }
346
- throw err;
347
- }
348
- };
349
-
350
- // Internal functions
351
- function parsePropArgs(property, targetInfo) {
352
- const props = property || [],
353
- info = targetInfo || {};
354
- if (props.length === 1 && props[0].indexOf('{') !== -1 && props[0].indexOf('}') !== -1 &&
355
- ((props[0].split("'").length - 1) % 2) === 0)
356
- {
357
- // eslint-disable-next-line no-useless-escape
358
- props[0] = props[0].replace(/\'/g,'"');
359
- }
360
- props.forEach(function(prop) {
361
- try {
362
- const data = JSON.parse(prop);
363
- for (const k in data) {
364
- info[k] = data[k];
365
- }
366
- } catch (err) {
367
- const tokens = prop.split('=');
368
- if (tokens.length === 2) {
369
- info[tokens[0]] = tokens[1];
370
- } else {
371
- log.warn('Ignoring invalid arguments:', prop);
372
- }
373
- }
374
- });
375
- }
376
-
377
- module.exports = Generator;
1
+ #!/usr/bin/env node
2
+
3
+ /*
4
+ * Copyright (c) 2020-2024 LG Electronics Inc.
5
+ *
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+
9
+ const promise = require('bluebird'),
10
+ Table = require('easy-table'),
11
+ fs = promise.promisifyAll(require('fs-extra')),
12
+ log = require('npmlog'),
13
+ path = require('path'),
14
+ errHndl = require('./base/error-handler'),
15
+ copyToDirAsync = require('./util/copy').copyToDirAsync,
16
+ readJsonSync = require('./util/json').readJsonSync,
17
+ merge = require('./util/merge');
18
+
19
+ const templatePath = path.join(__dirname, '/../files/conf/', 'template.json');
20
+ let templates;
21
+
22
+ log.heading = 'generator';
23
+ log.level = 'warn';
24
+
25
+ function Generator() {
26
+ if (templatePath) {
27
+ const cliPath = path.join(__dirname, '..');
28
+ let contents = fs.readFileSync(templatePath);
29
+ contents = contents.toString().replace(/\$cli-root/gi, cliPath).replace(/\\/g,'/');
30
+ templates = JSON.parse(contents);
31
+ } else {
32
+ templates = null;
33
+ }
34
+ }
35
+
36
+ Generator.prototype.showTemplates = function(listType, next) {
37
+ const templateList = this.getTmpl(),
38
+ table = new Table(),
39
+ _displayType = {
40
+ "webapp": "Web App",
41
+ "nativeapp": "Native App",
42
+ "webappinfo": "Web App Info",
43
+ "nativeappinfo": "Native App Info",
44
+ "jsservice": "JS Service",
45
+ "nativeservice": "Native Service",
46
+ "jsserviceinfo": "JS Service Info",
47
+ "nativeserviceinfo": "Native Service Info",
48
+ "icon": "Icon",
49
+ "library": "Library",
50
+ "packageinfo": "Package Info",
51
+ "qmlapp": "QML App",
52
+ "qmlappinfo": "QML App Info"
53
+ };
54
+
55
+ for (const name in templateList) {
56
+ if (templateList[name].hide === true || !templateList[name].type) {
57
+ continue;
58
+ }
59
+ const isDefault = (templateList[name].default) ? "(default) " : "",
60
+ type = _displayType[templateList[name].type] || templateList[name].type;
61
+
62
+ if (listType && ["true", "false", true, false].indexOf(listType) === -1) {
63
+ if (templateList[name].type &&
64
+ (templateList[name].type.match(new RegExp(listType+"$","gi")) === null)) {
65
+ continue;
66
+ }
67
+ }
68
+ table.cell('ID', name);
69
+ table.cell('Project Type', type);
70
+ table.cell('Description', isDefault + templateList[name].description);
71
+ table.newRow();
72
+ }
73
+ return next(null, {msg: table.toString()});
74
+ };
75
+
76
+ Generator.prototype.generate = function(options, next) {
77
+ // For API
78
+ if (!options.tmplName) {
79
+ return next(errHndl.getErrMsg("EMPTY_VALUE", "TEMPLATE"));
80
+ }
81
+ if (!options.out) {
82
+ return next(errHndl.getErrMsg("EMPTY_VALUE", "APP_DIR"));
83
+ }
84
+
85
+ const tmplName = options.tmplName,
86
+ pkginfo = options.pkginfo || {},
87
+ svcinfo = options.svcinfo || {},
88
+ svcName = options.svcName,
89
+ out = options.out,
90
+ dest = path.resolve(out),
91
+ existDir = this.existOutDir(dest),
92
+ templateList = this.getTmpl(),
93
+ template = templateList[tmplName];
94
+ let appinfo = options.appinfo || {};
95
+
96
+ // For API
97
+ if (!template) {
98
+ return next(errHndl.getErrMsg("INVALID_VALUE", "TEMPLATE", options.tmplName));
99
+ }
100
+ if (!options.overwrite && existDir) {
101
+ return next(errHndl.getErrMsg("NOT_OVERWRITE_DIR", dest));
102
+ }
103
+
104
+ if (template.metadata && template.metadata.data && typeof template.metadata.data === 'object') {
105
+ appinfo = merge(appinfo, template.metadata.data);
106
+ }
107
+
108
+ promise.resolve()
109
+ .then(function() {
110
+ // If props is not exist, this input from query-mode
111
+ // If props is exist, this input from props
112
+ // Check argv.query, argv["no-query"], options.props and conditional statement.
113
+ if (template.type.match(/(app$|appinfo$)/)) {
114
+ parsePropArgs(options.props, appinfo);
115
+ } else if (template.type.match(/(service$|serviceinfo$)/)) {
116
+ parsePropArgs(options.props, svcinfo);
117
+ } else if (template.type.match(/(package$|packageinfo$)/)) {
118
+ parsePropArgs(options.props, pkginfo);
119
+ }
120
+ })
121
+ .then(function() {
122
+ if (svcName) {
123
+ svcinfo.id = svcName;
124
+ svcinfo.services = [{
125
+ "name": svcName
126
+ }];
127
+ } else if (!svcName && svcinfo && !!svcinfo.id) {
128
+ svcinfo.services = [{
129
+ "name": svcinfo.id
130
+ }];
131
+ }
132
+ });
133
+
134
+ return promise.resolve()
135
+ .then(function() {
136
+ log.info("generator#generate()", "template name:" + tmplName);
137
+ next(null, {msg: "Generating " + tmplName + " in " + dest});
138
+
139
+ let srcs;
140
+ if (tmplName.match(/(^hosted)/)) {
141
+ srcs = [].concat(template.path);
142
+ return promise.all(srcs.map(function(src) {
143
+ return copyToDirAsync(src, dest);
144
+ })).then(function() {
145
+ let metaTmpl;
146
+ let url;
147
+ if (template.metadata && template.metadata.id) {
148
+ metaTmpl = templateList[template.metadata.id];
149
+ }
150
+ if (metaTmpl) {
151
+ if (appinfo.url) {
152
+ url = appinfo.url;
153
+ delete appinfo.url;
154
+ const urlTmpl = {"path":path.join(srcs[0],'index.html')};
155
+ _writeURLdata(urlTmpl, url);
156
+ }
157
+ return _writeMetadata(metaTmpl, appinfo, svcinfo, pkginfo);
158
+ } else {
159
+ return;
160
+ }
161
+ });
162
+ } else if (tmplName.match(/(^qmlapp$)/)) {
163
+ srcs = [].concat(template.path);
164
+ return promise.all(srcs.map(function(src) {
165
+ return copyToDirAsync(src, dest);
166
+ })).then(function() {
167
+ let metaTmpl;
168
+ if (template.metadata && template.metadata.id) {
169
+ metaTmpl = templateList[template.metadata.id];
170
+ }
171
+ if (metaTmpl) {
172
+ if (appinfo.id) {
173
+ const qmlTmpl = {"path":path.join(srcs[0],'main.qml')};
174
+ _writeAppIDdata(qmlTmpl, appinfo.id);
175
+ }
176
+ return _writeMetadata(metaTmpl, appinfo, svcinfo, pkginfo);
177
+ } else {
178
+ return;
179
+ }
180
+ });
181
+ } else if (template.type.match(/info$/)) {
182
+ return _writeMetadata(template, appinfo, svcinfo, pkginfo);
183
+ } else {
184
+ srcs = [].concat(template.path);
185
+ return promise.all(srcs.map(function(src) {
186
+ log.info("generator#generate()", "template src:" + src);
187
+ return copyToDirAsync(src, dest);
188
+ })).then(function() {
189
+ let metaTmpl;
190
+ if (template.metadata && template.metadata.id) {
191
+ metaTmpl = templateList[template.metadata.id];
192
+ }
193
+ if (metaTmpl) {
194
+ return _writeMetadata(metaTmpl, appinfo, svcinfo, pkginfo);
195
+ } else {
196
+ return;
197
+ }
198
+ });
199
+ }
200
+ })
201
+ .then(function() {
202
+ const deps = templateList[tmplName].deps || [];
203
+ return promise.all(deps.map(function(dep) {
204
+ if (!templateList[dep]) {
205
+ log.warn("generator#generate()", "Invalid template id:" + dep);
206
+ return;
207
+ } else if (!templateList[dep].path) {
208
+ log.warn("generator#generate()", "Invalid template path:" + dep);
209
+ return;
210
+ }
211
+ return copyToDirAsync(templateList[dep].path, dest);
212
+ }));
213
+ })
214
+ .then(function() {
215
+ log.info("generator#generate()", "done");
216
+ return next(null, {
217
+ msg: "Success"
218
+ });
219
+ })
220
+ .catch(function(err) {
221
+ log.silly("generator#generate()", "err:", err);
222
+ throw err;
223
+ });
224
+
225
+ function _writeAppIDdata(qmlTmpl, appId) {
226
+ const filePaths = [].concat(qmlTmpl.path);
227
+ return promise.all(filePaths.map(function(file) {
228
+ return fs.lstatAsync(file)
229
+ .then(function(stats) {
230
+ if (!stats.isFile()) {
231
+ throw errHndl.getErrMsg("INVALID_PATH", "meta template", file);
232
+ }
233
+ // eslint-disable-next-line no-useless-escape
234
+ const exp = /appId\s*:\s*[\'\"][\w.]*[\'\"]/g;
235
+ const destFile = path.join(dest, path.basename(file));
236
+ let qmlFile = fs.readFileSync(file, 'utf8');
237
+ qmlFile = qmlFile.replace(exp, "appId: \"" + appId + "\"");
238
+
239
+ fs.writeFileSync(destFile, qmlFile, {encoding: 'utf8'});
240
+ });
241
+ }))
242
+ .then(function() {
243
+ log.info("generator#generate()#_writeAppIDdata()", "done");
244
+ return;
245
+ })
246
+ .catch(function(err) {
247
+ log.silly("generator#generate()#_writeAppIDdata()", "err:", err);
248
+ throw err;
249
+ });
250
+ }
251
+
252
+ function _writeURLdata(urlTmpl, url) {
253
+ const filePaths = [].concat(urlTmpl.path);
254
+ return promise.all(filePaths.map(function(file) {
255
+ return fs.lstatAsync(file)
256
+ .then(function(stats) {
257
+ if (!stats.isFile()) {
258
+ throw errHndl.getErrMsg("INVALID_PATH", "meta template", file);
259
+ }
260
+ let html = fs.readFileSync(file, 'utf8');
261
+ // eslint-disable-next-line no-useless-escape
262
+ const exp = new RegExp("(?:[\'\"])([\:/.A-z?<_&\s=>0-9;-]+\')");
263
+ // eslint-disable-next-line no-useless-escape
264
+ html = html.replace(exp, "\'" + url + "\'");
265
+ const destFile = path.join(dest, path.basename(file));
266
+
267
+ fs.writeFileSync(destFile, html, {encoding: 'utf8'});
268
+ });
269
+ }))
270
+ .then(function() {
271
+ log.info("generator#generate()#_writeURLdata()", "done");
272
+ return;
273
+ })
274
+ .catch(function(err) {
275
+ log.silly("generator#generate()#_writeURLdata()", "err:", err);
276
+ throw err;
277
+ });
278
+ }
279
+
280
+ function _writeMetadata(metaTmpl, _appinfo, _svcinfo, _pkginfo) {
281
+ const metaPaths = [].concat(metaTmpl.path),
282
+ appInfo = _appinfo || {},
283
+ svcInfo = _svcinfo || {},
284
+ pkgInfo = _pkginfo || {};
285
+
286
+ return promise.all(metaPaths.map(function(file) {
287
+ return fs.lstatAsync(file)
288
+ .then(function(stats) {
289
+ if (!stats.isFile()) {
290
+ throw errHndl.getErrMsg("INVALID_PATH", "meta template", file);
291
+ }
292
+ const fileName = path.basename(file);
293
+ let info = readJsonSync(file);
294
+
295
+ if (fileName === 'appinfo.json') {
296
+ info = merge(info, appInfo);
297
+ } else if (fileName === "services.json") {
298
+ info = merge(info, svcInfo);
299
+ } else if (fileName === "package.json" &&
300
+ (metaTmpl.type === "jsserviceinfo" || metaTmpl.type === "nativeserviceinfo")) {
301
+ info.name = svcInfo.id || info.name;
302
+ } else if (fileName === "packageinfo.json") {
303
+ info = merge(info, pkgInfo);
304
+ }
305
+ return info;
306
+ })
307
+ .then(function(info) {
308
+ const destFile = path.join(dest, path.basename(file));
309
+ return fs.mkdirsAsync(dest)
310
+ .then(function() {
311
+ return fs.writeFileSync(destFile, JSON.stringify(info, null, 2), {
312
+ encoding: 'utf8'
313
+ });
314
+ });
315
+ });
316
+ }))
317
+ .then(function() {
318
+ log.info("generator#generate()#_writeMetadata()", "done");
319
+ return;
320
+ })
321
+ .catch(function(err) {
322
+ log.silly("generator#generate()#_writeMetadata()", "err:", err);
323
+ throw err;
324
+ });
325
+ }
326
+ };
327
+
328
+ Generator.prototype.getTmpl = function() {
329
+ return templates;
330
+ };
331
+
332
+ Generator.prototype.existOutDir = function(outDir) {
333
+ log.verbose("generator#existOutDir()", outDir);
334
+ try {
335
+ const files = fs.readdirSync(outDir);
336
+ if (files.length > 0)
337
+ return true;
338
+ } catch (err) {
339
+ if (err && err.code === 'ENOTDIR') {
340
+ throw errHndl.getErrMsg("NOT_DIRTYPE_PATH", outDir);
341
+ }
342
+ if (err && err.code === 'ENOENT') {
343
+ log.verbose("generator#generate()", "The directory does not exist.");
344
+ return false;
345
+ }
346
+ throw err;
347
+ }
348
+ };
349
+
350
+ // Internal functions
351
+ function parsePropArgs(property, targetInfo) {
352
+ const props = property || [],
353
+ info = targetInfo || {};
354
+ if (props.length === 1 && props[0].indexOf('{') !== -1 && props[0].indexOf('}') !== -1 &&
355
+ ((props[0].split("'").length - 1) % 2) === 0)
356
+ {
357
+ // eslint-disable-next-line no-useless-escape
358
+ props[0] = props[0].replace(/\'/g,'"');
359
+ }
360
+ props.forEach(function(prop) {
361
+ try {
362
+ const data = JSON.parse(prop);
363
+ for (const k in data) {
364
+ info[k] = data[k];
365
+ }
366
+ } catch (err) {
367
+ const tokens = prop.split('=');
368
+ if (tokens.length === 2) {
369
+ info[tokens[0]] = tokens[1];
370
+ } else {
371
+ log.warn('Ignoring invalid arguments:', prop);
372
+ }
373
+ }
374
+ });
375
+ }
376
+
377
+ module.exports = Generator;