@webos-tools/cli 3.0.3 → 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 -123
  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 -301
  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 -97
  118. package/scripts/postinstall.js +24 -0
  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
@@ -1,520 +1,520 @@
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 async = require('async'),
10
- inquirer = require('inquirer'),
11
- nopt = require('nopt'),
12
- log = require('npmlog'),
13
- path = require('path'),
14
- Ssdp = require('ssdp-js'),
15
- commonTools = require('./../lib/base/common-tools'),
16
- novacom = require('./../lib/base/novacom');
17
-
18
- const version = commonTools.version,
19
- cliControl = commonTools.cliControl,
20
- help = commonTools.help,
21
- appdata = commonTools.appdata,
22
- errHndl = commonTools.errMsg,
23
- setupDevice = commonTools.setupDevice;
24
-
25
- const processName = path.basename(process.argv[1]).replace(/.js/, '');
26
-
27
- process.on('uncaughtException', function(err) {
28
- log.error('uncaughtException', err.toString());
29
- log.verbose('uncaughtException', err.stack);
30
- cliControl.end(-1);
31
- });
32
-
33
- const knownOpts = {
34
- // generic options
35
- "help": Boolean,
36
- "level": ['silly', 'verbose', 'info', 'http', 'warn', 'error'],
37
- "version": Boolean,
38
- // command-specific options
39
- "list": Boolean,
40
- "listfull": Boolean,
41
- "add": [String, null],
42
- "remove": [String, null],
43
- "modify": [String, null],
44
- "default": [String, null],
45
- "search": Boolean,
46
- "timeout": [String, null],
47
- "info": [String, Array],
48
- "reset": Boolean
49
- };
50
-
51
- const shortHands = {
52
- // generic aliases
53
- "h": ["--help"],
54
- "v": ["--level", "verbose"],
55
- "V": ["--version"],
56
- // command-specific aliases
57
- "l": ["--list"],
58
- "F": ["--listfull"],
59
- "i": ["--info"],
60
- "a": ["--add"],
61
- "r": ["--remove"],
62
- "m": ["--modify"],
63
- "f": ["--default"],
64
- "s": ["--search"],
65
- "t": ["--timeout"],
66
- "R": ["--reset"]
67
- };
68
-
69
- const argv = nopt(knownOpts, shortHands, process.argv, 2 /* drop 'node' & 'ares-*.js'*/);
70
-
71
- log.heading = processName;
72
- log.level = argv.level || 'warn';
73
- log.verbose("argv", argv);
74
-
75
- const inqChoices = ["add", "modify"],
76
- dfChoices = ["set default"],
77
- rmChoices = ["remove"],
78
- totChoices = inqChoices.concat(rmChoices, dfChoices);
79
-
80
- let questions = [],
81
- op;
82
-
83
- if (argv.list) {
84
- op = deviceList;
85
- } else if (argv.listfull) {
86
- op = deviceListFull;
87
- } else if (argv.reset) {
88
- op = reset;
89
- } else if (argv.search || argv.timeout) {
90
- op = search;
91
- } else if (argv.add || argv.modify || argv.info) {
92
- op = modifyDeviceInfo;
93
- } else if (argv.remove) {
94
- op = removeDeviceInfo;
95
- } else if (argv.default) {
96
- op = setDefaultDeviceInfo;
97
- } else if (argv.version) {
98
- version.showVersionAndExit();
99
- } else if (argv.help) {
100
- help.display(processName, appdata.getConfig(true).profile);
101
- cliControl.end();
102
- } else {
103
- op = interactiveInput;
104
- }
105
-
106
- if (op) {
107
- version.checkNodeVersion(function() {
108
- async.series([
109
- op.bind(this)
110
- ],finish);
111
- });
112
- }
113
-
114
- const _needInq = function(choice) {
115
- return function(choices) {
116
- return (choices.indexOf(choice) !== -1);
117
- };
118
- };
119
-
120
- function deviceList() {
121
- setupDevice.showDeviceList(finish);
122
- }
123
-
124
- function deviceListFull() {
125
- setupDevice.showDeviceListFull(finish);
126
- }
127
-
128
- function reset() {
129
- setupDevice.resetDeviceList(finish);
130
- }
131
-
132
- function _queryAddRemove(ssdpDevices, next) {
133
- let selDevice = {};
134
- const resolver = this.resolver || (this.resolver = new novacom.Resolver());
135
- if (typeof ssdpDevices === 'function') {
136
- next = ssdpDevices;
137
- ssdpDevices = null;
138
- }
139
- async.waterfall([
140
- resolver.load.bind(resolver),
141
- resolver.list.bind(resolver),
142
- function(devices, next) {
143
- if (!ssdpDevices) return next(null, devices, null);
144
- const ssdpDevMap = {};
145
- ssdpDevices.forEach(function(sd) {
146
- const key = sd.name + ' # '+sd.address;
147
- for (const idx in devices) {
148
- if (devices[idx].name === sd.name)
149
- {
150
- ssdpDevMap[key] = devices[idx];
151
- ssdpDevMap[key].op = 'modify';
152
- ssdpDevMap[key].host = sd.address;
153
- break;
154
- }
155
- }
156
- if (!ssdpDevMap[key]) {
157
- ssdpDevMap[key] = {
158
- name: sd.name,
159
- host: sd.address,
160
- op: 'add'
161
- };
162
- }
163
- });
164
-
165
- questions = [{
166
- type: "list",
167
- name: "discovered",
168
- message: "Select",
169
- choices: Object.keys(ssdpDevMap)
170
- }];
171
- inquirer.prompt(questions).then(function(answers) {
172
- next(null, devices, ssdpDevMap[answers.discovered]);
173
- });
174
- },
175
- function(devices, ssdpDevice, next) {
176
- const deviceNames = devices.filter(function(device) {
177
- return (device.conn.indexOf('novacom') === -1);
178
- }).map(function(device) {
179
- return (device.name);
180
- });
181
- questions = [{
182
- type: "list",
183
- name: "op",
184
- message: "Select",
185
- choices: function() {
186
- if (ssdpDevice) {
187
- if (ssdpDevice.op === 'modify') return inqChoices;
188
- else return ['add'];
189
- } else {
190
- return totChoices;
191
- }
192
- },
193
- filter: function(val) {
194
- return val.toLowerCase();
195
- },
196
- default: function() {
197
- if (ssdpDevice && ssdpDevice.op) return ssdpDevice.op;
198
- else return null;
199
- }
200
- }, {
201
- type: "input",
202
- name: "device_name",
203
- message: "Enter Device Name:",
204
- when: function(answers) {
205
- return (answers.op === "add");
206
- },
207
- default: function() {
208
- if (ssdpDevice && ssdpDevice.name) return ssdpDevice.name;
209
- else return null;
210
- },
211
- validate: function(input) {
212
- if (input.length < 1) {
213
- return errHndl.getErrStr("EMPTY_VALUE");
214
- }
215
- if (deviceNames.indexOf(input) !== -1) {
216
- return errHndl.getErrStr("EXISTING_VALUE");
217
- }
218
- if (!setupDevice.isValidDeviceName(input)) {
219
- return errHndl.getErrStr("INVALID_DEVICENAME");
220
- }
221
- return true;
222
- }
223
- }, {
224
- type: "list",
225
- name: "device_name",
226
- message: "Select a device",
227
- choices: deviceNames,
228
- when: function(answers) {
229
- return (["modify", "remove", "set default"].indexOf(answers.op) !== -1 && !ssdpDevice);
230
- }
231
- }];
232
- inquirer.prompt(questions)
233
- .then(function(answers) {
234
- devices.forEach(function(device) {
235
- if (answers.device_name === device.name) {
236
- selDevice = device;
237
- }
238
- });
239
- selDevice.name = answers.device_name || ((ssdpDevice) ? ssdpDevice.name : null);
240
- selDevice.mode = answers.op || ((ssdpDevice) ? ssdpDevice.op : null);
241
- selDevice.host = (ssdpDevice) ? ssdpDevice.host : (selDevice.host || null);
242
- next(null, selDevice, null);
243
- });
244
- }
245
- ], function(err, result) {
246
- next(err, result);
247
- });
248
- }
249
-
250
- function _queryDeviceInfo(selDevice, next) {
251
- let mode = selDevice.mode;
252
- const deviceName = selDevice.name,
253
- resolver = this.resolver || (this.resolver = new novacom.Resolver());
254
-
255
- questions = [{
256
- type: "input",
257
- name: "ip",
258
- message: "Enter Device IP address:",
259
- default: function() {
260
- return selDevice.host || "127.0.0.1";
261
- },
262
- validate: function(answers) {
263
- if (!setupDevice.isValidIpv4(answers)) {
264
- return errHndl.getErrStr("INVALID_VALUE");
265
- }
266
- return true;
267
- },
268
- when: function() {
269
- return _needInq(mode)(inqChoices);
270
- }
271
- }, {
272
- type: "input",
273
- name: "port",
274
- message: "Enter Device Port:",
275
- default: function() {
276
- return selDevice.port || "22";
277
- },
278
- validate: function(answers) {
279
- if (!setupDevice.isValidPort(answers)) {
280
- return errHndl.getErrStr("INVALID_VALUE");
281
- }
282
- return true;
283
- },
284
- when: function() {
285
- return _needInq(mode)(inqChoices);
286
- }
287
- }, {
288
- type: "input",
289
- name: "user",
290
- message: "Enter ssh user:",
291
- default: function() {
292
- return selDevice.username || "root";
293
- },
294
- when: function() {
295
- return _needInq(mode)(inqChoices);
296
- }
297
- }, {
298
- type: "input",
299
- name: "description",
300
- message: "Enter description:",
301
- default: function() {
302
- return selDevice.description || "new device";
303
- },
304
- when: function() {
305
- return _needInq(mode)(inqChoices);
306
- }
307
- }, {
308
- type: "list",
309
- name: "auth_type",
310
- message: "Select authentication",
311
- choices: ["password", "ssh key"],
312
- default: function() {
313
- let idx = 0;
314
- if (selDevice.privateKeyName) {
315
- idx = 1;
316
- }
317
- return idx;
318
- },
319
- when: function() {
320
- return _needInq(mode)(inqChoices);
321
- }
322
- }, {
323
- type: "password",
324
- name: "password",
325
- message: "Enter password:",
326
- when: function(answers) {
327
- return _needInq(mode)(inqChoices) && (answers.auth_type === "password");
328
- }
329
- }, {
330
- type: "input",
331
- name: "ssh_key",
332
- message: "Enter ssh private key file name:",
333
- default: function() {
334
- return selDevice.privateKeyName || "webos_emul";
335
- },
336
- when: function(answers) {
337
- return _needInq(mode)(inqChoices) && (answers.auth_type === "ssh key");
338
- }
339
- }, {
340
- type: "input",
341
- name: "ssh_passphrase",
342
- message: "Enter key's passphrase:",
343
- default: function() {
344
- return selDevice.passphrase || undefined;
345
- },
346
- when: function(answers) {
347
- return _needInq(mode)(inqChoices) && (answers.auth_type === "ssh key");
348
-
349
- }
350
- }, {
351
- type: "confirm",
352
- name: "default",
353
- message: "Set default ?",
354
- default: false,
355
- when: function() {
356
- return (mode === "add");
357
- }
358
- }, {
359
- type: "confirm",
360
- name: "confirm",
361
- message: "Save ?",
362
- default: true
363
- }];
364
-
365
- inquirer.prompt(questions).then(function(answers) {
366
- if (answers.confirm) {
367
- log.info("interactiveInput()#_queryDeviceInfo()", "saved");
368
- } else {
369
- log.info("interactiveInput()#_queryDeviceInfo()", "canceled");
370
- return next(null, {
371
- "msg": "Canceled"
372
- });
373
- }
374
- const inDevice = {
375
- profile: appdata.getConfig(true).profile,
376
- name: deviceName,
377
- host: answers.ip,
378
- port: answers.port,
379
- description: answers.description,
380
- username: answers.user,
381
- default: answers.default
382
- };
383
-
384
- if (mode === 'add' || mode === 'modify') {
385
- if (answers.auth_type && answers.auth_type === "password") {
386
- inDevice.password = answers.password;
387
- inDevice.privateKey = "@DELETE@";
388
- inDevice.passphrase = "@DELETE@";
389
- inDevice.privateKeyName = "@DELETE@";
390
- } else if (answers.auth_type && answers.auth_type === "ssh key") {
391
- inDevice.password = "@DELETE@";
392
- inDevice.privateKey = {
393
- "openSsh": answers.ssh_key
394
- };
395
- inDevice.passphrase = answers.ssh_passphrase || "@DELETE@";
396
- inDevice.privateKeyName = "@DELETE@";
397
- } else {
398
- return next(errHndl.getErrMsg("NOT_SUPPORT_AUTHTYPE", answers.auth_type));
399
- }
400
- }
401
-
402
- if (mode === 'set default') {
403
- inDevice.default = true;
404
- mode = 'default';
405
- }
406
-
407
- setupDevice.replaceDefaultDeviceInfo(inDevice);
408
- if (inDevice.port) {
409
- inDevice.port = Number(inDevice.port);
410
- }
411
- async.series([
412
- resolver.load.bind(resolver),
413
- resolver.modifyDeviceFile.bind(resolver, mode, inDevice),
414
- setupDevice.showDeviceList.bind(this, finish)
415
- ], function(err, results) {
416
- if (err) {
417
- return next(err);
418
- }
419
- next(null, results[1]);
420
- });
421
- });
422
- }
423
-
424
- function interactiveInput() {
425
- async.waterfall([
426
- setupDevice.showDeviceList.bind(this),
427
- function(data, next) {
428
- console.log(data.msg);
429
- console.log("** You can modify the device info in the above list, or add new device.");
430
- next();
431
- },
432
- _queryAddRemove,
433
- _queryDeviceInfo
434
- ], function(err, result) {
435
- finish(err, result);
436
- });
437
- }
438
-
439
- function search(next) {
440
- const TIMEOUT = 5000,
441
- ssdp = new Ssdp(),
442
- timeout = Number(argv.timeout) * 1000 || TIMEOUT,
443
- outterNext = next,
444
- self = this;
445
- let discovered = [],
446
- end = false;
447
-
448
- console.log("Searching...");
449
- log.verbose("search()", "timeout:", timeout);
450
- ssdp.start();
451
- ssdp.onDevice(function(device) {
452
- if (!device.headers || !device.headers.SERVER ||
453
- device.headers.SERVER.indexOf('WebOS') < 0 || end) {
454
- return finish(null, {msg: "No devices is discovered."});
455
- }
456
- log.verbose("search()# %s:%s (%s)", '[Discovered]', device.name, device.address);
457
- });
458
- async.waterfall([
459
- function(next) {
460
- setTimeout(function() {
461
- discovered = ssdp.getList().map(function(device) {
462
- end = true;
463
- return {
464
- "uuid": device.headers.USN.split(':')[1],
465
- "name": device.name.replace(/\s/g, '_'),
466
- "address": device.address,
467
- "registered": false
468
- };
469
- });
470
- // ssdp.destroy();
471
- if (discovered.length === 0) {
472
- console.log("No devices is discovered.");
473
- return outterNext();
474
- }
475
- log.verbose("search()", "discovered:", discovered.length);
476
- next(null, discovered);
477
- }, timeout);
478
- },
479
- _queryAddRemove.bind(self),
480
- _queryDeviceInfo.bind(self)
481
- ], function(err) {
482
- next(err);
483
- });
484
- }
485
-
486
- function modifyDeviceInfo() {
487
- setupDevice.modifyDeviceInfo(argv, finish);
488
- }
489
-
490
- function setDefaultDeviceInfo() {
491
- setupDevice.setDefaultDevice(argv.default, finish);
492
- }
493
-
494
- function removeDeviceInfo() {
495
- setupDevice.removeDeviceInfo(argv, finish);
496
- }
497
-
498
- function finish(err, value) {
499
- log.info("finish()");
500
- if (err) {
501
- // handle err from getErrMsg()
502
- if (Array.isArray(err) && err.length > 0) {
503
- for (const index in err) {
504
- log.error(err[index].heading, err[index].message);
505
- }
506
- log.verbose(err[0].stack);
507
- } else {
508
- // handle general err (string & object)
509
- log.error(err.toString());
510
- log.verbose(err.stack);
511
- }
512
- cliControl.end(-1);
513
- } else {
514
- log.verbose("finish()", "value:", value);
515
- if (value && value.msg) {
516
- console.log(value.msg);
517
- }
518
- cliControl.end();
519
- }
520
- }
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 async = require('async'),
10
+ inquirer = require('inquirer'),
11
+ nopt = require('nopt'),
12
+ log = require('npmlog'),
13
+ path = require('path'),
14
+ Ssdp = require('ssdp-js'),
15
+ commonTools = require('./../lib/base/common-tools'),
16
+ novacom = require('./../lib/base/novacom');
17
+
18
+ const version = commonTools.version,
19
+ cliControl = commonTools.cliControl,
20
+ help = commonTools.help,
21
+ appdata = commonTools.appdata,
22
+ errHndl = commonTools.errMsg,
23
+ setupDevice = commonTools.setupDevice;
24
+
25
+ const processName = path.basename(process.argv[1]).replace(/.js/, '');
26
+
27
+ process.on('uncaughtException', function(err) {
28
+ log.error('uncaughtException', err.toString());
29
+ log.verbose('uncaughtException', err.stack);
30
+ cliControl.end(-1);
31
+ });
32
+
33
+ const knownOpts = {
34
+ // generic options
35
+ "help": Boolean,
36
+ "level": ['silly', 'verbose', 'info', 'http', 'warn', 'error'],
37
+ "version": Boolean,
38
+ // command-specific options
39
+ "list": Boolean,
40
+ "listfull": Boolean,
41
+ "add": [String, null],
42
+ "remove": [String, null],
43
+ "modify": [String, null],
44
+ "default": [String, null],
45
+ "search": Boolean,
46
+ "timeout": [String, null],
47
+ "info": [String, Array],
48
+ "reset": Boolean
49
+ };
50
+
51
+ const shortHands = {
52
+ // generic aliases
53
+ "h": ["--help"],
54
+ "v": ["--level", "verbose"],
55
+ "V": ["--version"],
56
+ // command-specific aliases
57
+ "l": ["--list"],
58
+ "F": ["--listfull"],
59
+ "i": ["--info"],
60
+ "a": ["--add"],
61
+ "r": ["--remove"],
62
+ "m": ["--modify"],
63
+ "f": ["--default"],
64
+ "s": ["--search"],
65
+ "t": ["--timeout"],
66
+ "R": ["--reset"]
67
+ };
68
+
69
+ const argv = nopt(knownOpts, shortHands, process.argv, 2 /* drop 'node' & 'ares-*.js'*/);
70
+
71
+ log.heading = processName;
72
+ log.level = argv.level || 'warn';
73
+ log.verbose("argv", argv);
74
+
75
+ const inqChoices = ["add", "modify"],
76
+ dfChoices = ["set default"],
77
+ rmChoices = ["remove"],
78
+ totChoices = inqChoices.concat(rmChoices, dfChoices);
79
+
80
+ let questions = [],
81
+ op;
82
+
83
+ if (argv.list) {
84
+ op = deviceList;
85
+ } else if (argv.listfull) {
86
+ op = deviceListFull;
87
+ } else if (argv.reset) {
88
+ op = reset;
89
+ } else if (argv.search || argv.timeout) {
90
+ op = search;
91
+ } else if (argv.add || argv.modify || argv.info) {
92
+ op = modifyDeviceInfo;
93
+ } else if (argv.remove) {
94
+ op = removeDeviceInfo;
95
+ } else if (argv.default) {
96
+ op = setDefaultDeviceInfo;
97
+ } else if (argv.version) {
98
+ version.showVersionAndExit();
99
+ } else if (argv.help) {
100
+ help.display(processName, appdata.getConfig(true).profile);
101
+ cliControl.end();
102
+ } else {
103
+ op = interactiveInput;
104
+ }
105
+
106
+ if (op) {
107
+ version.checkNodeVersion(function() {
108
+ async.series([
109
+ op.bind(this)
110
+ ],finish);
111
+ });
112
+ }
113
+
114
+ const _needInq = function(choice) {
115
+ return function(choices) {
116
+ return (choices.indexOf(choice) !== -1);
117
+ };
118
+ };
119
+
120
+ function deviceList() {
121
+ setupDevice.showDeviceList(finish);
122
+ }
123
+
124
+ function deviceListFull() {
125
+ setupDevice.showDeviceListFull(finish);
126
+ }
127
+
128
+ function reset() {
129
+ setupDevice.resetDeviceList(finish);
130
+ }
131
+
132
+ function _queryAddRemove(ssdpDevices, next) {
133
+ let selDevice = {};
134
+ const resolver = this.resolver || (this.resolver = new novacom.Resolver());
135
+ if (typeof ssdpDevices === 'function') {
136
+ next = ssdpDevices;
137
+ ssdpDevices = null;
138
+ }
139
+ async.waterfall([
140
+ resolver.load.bind(resolver),
141
+ resolver.list.bind(resolver),
142
+ function(devices, next) {
143
+ if (!ssdpDevices) return next(null, devices, null);
144
+ const ssdpDevMap = {};
145
+ ssdpDevices.forEach(function(sd) {
146
+ const key = sd.name + ' # '+sd.address;
147
+ for (const idx in devices) {
148
+ if (devices[idx].name === sd.name)
149
+ {
150
+ ssdpDevMap[key] = devices[idx];
151
+ ssdpDevMap[key].op = 'modify';
152
+ ssdpDevMap[key].host = sd.address;
153
+ break;
154
+ }
155
+ }
156
+ if (!ssdpDevMap[key]) {
157
+ ssdpDevMap[key] = {
158
+ name: sd.name,
159
+ host: sd.address,
160
+ op: 'add'
161
+ };
162
+ }
163
+ });
164
+
165
+ questions = [{
166
+ type: "list",
167
+ name: "discovered",
168
+ message: "Select",
169
+ choices: Object.keys(ssdpDevMap)
170
+ }];
171
+ inquirer.prompt(questions).then(function(answers) {
172
+ next(null, devices, ssdpDevMap[answers.discovered]);
173
+ });
174
+ },
175
+ function(devices, ssdpDevice, next) {
176
+ const deviceNames = devices.filter(function(device) {
177
+ return (device.conn.indexOf('novacom') === -1);
178
+ }).map(function(device) {
179
+ return (device.name);
180
+ });
181
+ questions = [{
182
+ type: "list",
183
+ name: "op",
184
+ message: "Select",
185
+ choices: function() {
186
+ if (ssdpDevice) {
187
+ if (ssdpDevice.op === 'modify') return inqChoices;
188
+ else return ['add'];
189
+ } else {
190
+ return totChoices;
191
+ }
192
+ },
193
+ filter: function(val) {
194
+ return val.toLowerCase();
195
+ },
196
+ default: function() {
197
+ if (ssdpDevice && ssdpDevice.op) return ssdpDevice.op;
198
+ else return null;
199
+ }
200
+ }, {
201
+ type: "input",
202
+ name: "device_name",
203
+ message: "Enter Device Name:",
204
+ when: function(answers) {
205
+ return (answers.op === "add");
206
+ },
207
+ default: function() {
208
+ if (ssdpDevice && ssdpDevice.name) return ssdpDevice.name;
209
+ else return null;
210
+ },
211
+ validate: function(input) {
212
+ if (input.length < 1) {
213
+ return errHndl.getErrStr("EMPTY_VALUE");
214
+ }
215
+ if (deviceNames.indexOf(input) !== -1) {
216
+ return errHndl.getErrStr("EXISTING_VALUE");
217
+ }
218
+ if (!setupDevice.isValidDeviceName(input)) {
219
+ return errHndl.getErrStr("INVALID_DEVICENAME");
220
+ }
221
+ return true;
222
+ }
223
+ }, {
224
+ type: "list",
225
+ name: "device_name",
226
+ message: "Select a device",
227
+ choices: deviceNames,
228
+ when: function(answers) {
229
+ return (["modify", "remove", "set default"].indexOf(answers.op) !== -1 && !ssdpDevice);
230
+ }
231
+ }];
232
+ inquirer.prompt(questions)
233
+ .then(function(answers) {
234
+ devices.forEach(function(device) {
235
+ if (answers.device_name === device.name) {
236
+ selDevice = device;
237
+ }
238
+ });
239
+ selDevice.name = answers.device_name || ((ssdpDevice) ? ssdpDevice.name : null);
240
+ selDevice.mode = answers.op || ((ssdpDevice) ? ssdpDevice.op : null);
241
+ selDevice.host = (ssdpDevice) ? ssdpDevice.host : (selDevice.host || null);
242
+ next(null, selDevice, null);
243
+ });
244
+ }
245
+ ], function(err, result) {
246
+ next(err, result);
247
+ });
248
+ }
249
+
250
+ function _queryDeviceInfo(selDevice, next) {
251
+ let mode = selDevice.mode;
252
+ const deviceName = selDevice.name,
253
+ resolver = this.resolver || (this.resolver = new novacom.Resolver());
254
+
255
+ questions = [{
256
+ type: "input",
257
+ name: "ip",
258
+ message: "Enter Device IP address:",
259
+ default: function() {
260
+ return selDevice.host || "127.0.0.1";
261
+ },
262
+ validate: function(answers) {
263
+ if (!setupDevice.isValidIpv4(answers)) {
264
+ return errHndl.getErrStr("INVALID_VALUE");
265
+ }
266
+ return true;
267
+ },
268
+ when: function() {
269
+ return _needInq(mode)(inqChoices);
270
+ }
271
+ }, {
272
+ type: "input",
273
+ name: "port",
274
+ message: "Enter Device Port:",
275
+ default: function() {
276
+ return selDevice.port || "22";
277
+ },
278
+ validate: function(answers) {
279
+ if (!setupDevice.isValidPort(answers)) {
280
+ return errHndl.getErrStr("INVALID_VALUE");
281
+ }
282
+ return true;
283
+ },
284
+ when: function() {
285
+ return _needInq(mode)(inqChoices);
286
+ }
287
+ }, {
288
+ type: "input",
289
+ name: "user",
290
+ message: "Enter ssh user:",
291
+ default: function() {
292
+ return selDevice.username || "root";
293
+ },
294
+ when: function() {
295
+ return _needInq(mode)(inqChoices);
296
+ }
297
+ }, {
298
+ type: "input",
299
+ name: "description",
300
+ message: "Enter description:",
301
+ default: function() {
302
+ return selDevice.description || "new device";
303
+ },
304
+ when: function() {
305
+ return _needInq(mode)(inqChoices);
306
+ }
307
+ }, {
308
+ type: "list",
309
+ name: "auth_type",
310
+ message: "Select authentication",
311
+ choices: ["password", "ssh key"],
312
+ default: function() {
313
+ let idx = 0;
314
+ if (selDevice.privateKeyName) {
315
+ idx = 1;
316
+ }
317
+ return idx;
318
+ },
319
+ when: function() {
320
+ return _needInq(mode)(inqChoices);
321
+ }
322
+ }, {
323
+ type: "password",
324
+ name: "password",
325
+ message: "Enter password:",
326
+ when: function(answers) {
327
+ return _needInq(mode)(inqChoices) && (answers.auth_type === "password");
328
+ }
329
+ }, {
330
+ type: "input",
331
+ name: "ssh_key",
332
+ message: "Enter ssh private key file name:",
333
+ default: function() {
334
+ return selDevice.privateKeyName || "webos_emul";
335
+ },
336
+ when: function(answers) {
337
+ return _needInq(mode)(inqChoices) && (answers.auth_type === "ssh key");
338
+ }
339
+ }, {
340
+ type: "input",
341
+ name: "ssh_passphrase",
342
+ message: "Enter key's passphrase:",
343
+ default: function() {
344
+ return selDevice.passphrase || undefined;
345
+ },
346
+ when: function(answers) {
347
+ return _needInq(mode)(inqChoices) && (answers.auth_type === "ssh key");
348
+
349
+ }
350
+ }, {
351
+ type: "confirm",
352
+ name: "default",
353
+ message: "Set default ?",
354
+ default: false,
355
+ when: function() {
356
+ return (mode === "add");
357
+ }
358
+ }, {
359
+ type: "confirm",
360
+ name: "confirm",
361
+ message: "Save ?",
362
+ default: true
363
+ }];
364
+
365
+ inquirer.prompt(questions).then(function(answers) {
366
+ if (answers.confirm) {
367
+ log.info("interactiveInput()#_queryDeviceInfo()", "saved");
368
+ } else {
369
+ log.info("interactiveInput()#_queryDeviceInfo()", "canceled");
370
+ return next(null, {
371
+ "msg": "Canceled"
372
+ });
373
+ }
374
+ const inDevice = {
375
+ profile: appdata.getConfig(true).profile,
376
+ name: deviceName,
377
+ host: answers.ip,
378
+ port: answers.port,
379
+ description: answers.description,
380
+ username: answers.user,
381
+ default: answers.default
382
+ };
383
+
384
+ if (mode === 'add' || mode === 'modify') {
385
+ if (answers.auth_type && answers.auth_type === "password") {
386
+ inDevice.password = answers.password;
387
+ inDevice.privateKey = "@DELETE@";
388
+ inDevice.passphrase = "@DELETE@";
389
+ inDevice.privateKeyName = "@DELETE@";
390
+ } else if (answers.auth_type && answers.auth_type === "ssh key") {
391
+ inDevice.password = "@DELETE@";
392
+ inDevice.privateKey = {
393
+ "openSsh": answers.ssh_key
394
+ };
395
+ inDevice.passphrase = answers.ssh_passphrase || "@DELETE@";
396
+ inDevice.privateKeyName = "@DELETE@";
397
+ } else {
398
+ return next(errHndl.getErrMsg("NOT_SUPPORT_AUTHTYPE", answers.auth_type));
399
+ }
400
+ }
401
+
402
+ if (mode === 'set default') {
403
+ inDevice.default = true;
404
+ mode = 'default';
405
+ }
406
+
407
+ setupDevice.replaceDefaultDeviceInfo(inDevice);
408
+ if (inDevice.port) {
409
+ inDevice.port = Number(inDevice.port);
410
+ }
411
+ async.series([
412
+ resolver.load.bind(resolver),
413
+ resolver.modifyDeviceFile.bind(resolver, mode, inDevice),
414
+ setupDevice.showDeviceList.bind(this, finish)
415
+ ], function(err, results) {
416
+ if (err) {
417
+ return next(err);
418
+ }
419
+ next(null, results[1]);
420
+ });
421
+ });
422
+ }
423
+
424
+ function interactiveInput() {
425
+ async.waterfall([
426
+ setupDevice.showDeviceList.bind(this),
427
+ function(data, next) {
428
+ console.log(data.msg);
429
+ console.log("** You can modify the device info in the above list, or add new device.");
430
+ next();
431
+ },
432
+ _queryAddRemove,
433
+ _queryDeviceInfo
434
+ ], function(err, result) {
435
+ finish(err, result);
436
+ });
437
+ }
438
+
439
+ function search(next) {
440
+ const TIMEOUT = 5000,
441
+ ssdp = new Ssdp(),
442
+ timeout = Number(argv.timeout) * 1000 || TIMEOUT,
443
+ outterNext = next,
444
+ self = this;
445
+ let discovered = [],
446
+ end = false;
447
+
448
+ console.log("Searching...");
449
+ log.verbose("search()", "timeout:", timeout);
450
+ ssdp.start();
451
+ ssdp.onDevice(function(device) {
452
+ if (!device.headers || !device.headers.SERVER ||
453
+ device.headers.SERVER.indexOf('WebOS') < 0 || end) {
454
+ return finish(null, {msg: "No devices is discovered."});
455
+ }
456
+ log.verbose("search()# %s:%s (%s)", '[Discovered]', device.name, device.address);
457
+ });
458
+ async.waterfall([
459
+ function(next) {
460
+ setTimeout(function() {
461
+ discovered = ssdp.getList().map(function(device) {
462
+ end = true;
463
+ return {
464
+ "uuid": device.headers.USN.split(':')[1],
465
+ "name": device.name.replace(/\s/g, '_'),
466
+ "address": device.address,
467
+ "registered": false
468
+ };
469
+ });
470
+ // ssdp.destroy();
471
+ if (discovered.length === 0) {
472
+ console.log("No devices is discovered.");
473
+ return outterNext();
474
+ }
475
+ log.verbose("search()", "discovered:", discovered.length);
476
+ next(null, discovered);
477
+ }, timeout);
478
+ },
479
+ _queryAddRemove.bind(self),
480
+ _queryDeviceInfo.bind(self)
481
+ ], function(err) {
482
+ next(err);
483
+ });
484
+ }
485
+
486
+ function modifyDeviceInfo() {
487
+ setupDevice.modifyDeviceInfo(argv, finish);
488
+ }
489
+
490
+ function setDefaultDeviceInfo() {
491
+ setupDevice.setDefaultDevice(argv.default, finish);
492
+ }
493
+
494
+ function removeDeviceInfo() {
495
+ setupDevice.removeDeviceInfo(argv, finish);
496
+ }
497
+
498
+ function finish(err, value) {
499
+ log.info("finish()");
500
+ if (err) {
501
+ // handle err from getErrMsg()
502
+ if (Array.isArray(err) && err.length > 0) {
503
+ for (const index in err) {
504
+ log.error(err[index].heading, err[index].message);
505
+ }
506
+ log.verbose(err[0].stack);
507
+ } else {
508
+ // handle general err (string & object)
509
+ log.error(err.toString());
510
+ log.verbose(err.stack);
511
+ }
512
+ cliControl.end(-1);
513
+ } else {
514
+ log.verbose("finish()", "value:", value);
515
+ if (value && value.msg) {
516
+ console.log(value.msg);
517
+ }
518
+ cliControl.end();
519
+ }
520
+ }