@webos-tools/cli 3.0.0

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 (177) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc.js +52 -0
  3. package/APIs.js +79 -0
  4. package/CHANGELOG.md +110 -0
  5. package/LICENSE +201 -0
  6. package/README.md +218 -0
  7. package/bin/ares-config.js +199 -0
  8. package/bin/ares-device-info.js +30 -0
  9. package/bin/ares-device.js +219 -0
  10. package/bin/ares-generate.js +270 -0
  11. package/bin/ares-inspect.js +179 -0
  12. package/bin/ares-install.js +223 -0
  13. package/bin/ares-launch.js +318 -0
  14. package/bin/ares-log.js +255 -0
  15. package/bin/ares-novacom.js +223 -0
  16. package/bin/ares-package.js +336 -0
  17. package/bin/ares-pull.js +156 -0
  18. package/bin/ares-push.js +155 -0
  19. package/bin/ares-server.js +174 -0
  20. package/bin/ares-setup-device.js +520 -0
  21. package/bin/ares-shell.js +132 -0
  22. package/bin/ares.js +166 -0
  23. package/files/conf/ares.json +49 -0
  24. package/files/conf/command-service.json +73 -0
  25. package/files/conf/config.json +22 -0
  26. package/files/conf/ipk.json +30 -0
  27. package/files/conf/novacom-devices.json +35 -0
  28. package/files/conf/query/query-app.json +14 -0
  29. package/files/conf/query/query-hosted.json +18 -0
  30. package/files/conf/query/query-package.json +10 -0
  31. package/files/conf/query/query-service.json +6 -0
  32. package/files/conf/sdk.json +8 -0
  33. package/files/conf/template.json +57 -0
  34. package/files/conf/webos_emul +27 -0
  35. package/files/conf-base/env/sdk-ose.json +8 -0
  36. package/files/conf-base/env/sdk-tv.json +8 -0
  37. package/files/conf-base/profile/config-ose.json +21 -0
  38. package/files/conf-base/profile/config-tv.json +22 -0
  39. package/files/conf-base/query/query-app.json +14 -0
  40. package/files/conf-base/query/query-hosted.json +18 -0
  41. package/files/conf-base/query/query-package.json +10 -0
  42. package/files/conf-base/query/query-service.json +6 -0
  43. package/files/conf-base/template-conf/ose-templates.json +67 -0
  44. package/files/conf-base/template-conf/tv-sdk-templates.json +57 -0
  45. package/files/help/ares-config.help +43 -0
  46. package/files/help/ares-device.help +94 -0
  47. package/files/help/ares-generate.help +65 -0
  48. package/files/help/ares-inspect.help +70 -0
  49. package/files/help/ares-install.help +90 -0
  50. package/files/help/ares-launch.help +100 -0
  51. package/files/help/ares-log-pmlogd.help +84 -0
  52. package/files/help/ares-log.help +101 -0
  53. package/files/help/ares-novacom.help +68 -0
  54. package/files/help/ares-package.help +101 -0
  55. package/files/help/ares-pull.help +38 -0
  56. package/files/help/ares-push.help +38 -0
  57. package/files/help/ares-server.help +39 -0
  58. package/files/help/ares-setup-device.help +75 -0
  59. package/files/help/ares-shell.help +42 -0
  60. package/files/help/ares.help +47 -0
  61. package/files/help/readme.help +23 -0
  62. package/files/schema/ApplicationDescription.schema +319 -0
  63. package/files/schema/NovacomDevices.schema +61 -0
  64. package/files/templates/ose-sdk-templates/appinfo/appinfo.json +10 -0
  65. package/files/templates/ose-sdk-templates/bootplate-web/index.html +88 -0
  66. package/files/templates/ose-sdk-templates/hosted-webapp/index.html +14 -0
  67. package/files/templates/ose-sdk-templates/icon/icon.png +0 -0
  68. package/files/templates/ose-sdk-templates/js-service/helloclient.js +31 -0
  69. package/files/templates/ose-sdk-templates/js-service/helloworld_webos_service.js +188 -0
  70. package/files/templates/ose-sdk-templates/qml-app/main.qml +68 -0
  71. package/files/templates/ose-sdk-templates/qmlappinfo/appinfo.json +10 -0
  72. package/files/templates/ose-sdk-templates/serviceinfo/package.json +11 -0
  73. package/files/templates/ose-sdk-templates/serviceinfo/services.json +8 -0
  74. package/files/templates/tv-sdk-templates/appinfo/appinfo.json +10 -0
  75. package/files/templates/tv-sdk-templates/bootplate-web/index.html +58 -0
  76. package/files/templates/tv-sdk-templates/bootplate-web/webOSTVjs-1.2.4/LICENSE-2.0.txt +202 -0
  77. package/files/templates/tv-sdk-templates/bootplate-web/webOSTVjs-1.2.4/webOSTV-dev.js +1 -0
  78. package/files/templates/tv-sdk-templates/bootplate-web/webOSTVjs-1.2.4/webOSTV.js +1 -0
  79. package/files/templates/tv-sdk-templates/hosted-webapp/index.html +14 -0
  80. package/files/templates/tv-sdk-templates/js-service/helloworld_service.js +37 -0
  81. package/files/templates/tv-sdk-templates/packageinfo/packageinfo.json +4 -0
  82. package/files/templates/tv-sdk-templates/serviceinfo/package.json +11 -0
  83. package/files/templates/tv-sdk-templates/serviceinfo/services.json +8 -0
  84. package/files/templates/tv-sdk-templates/webicon/icon.png +0 -0
  85. package/files/templates/tv-sdk-templates/webicon/largeIcon.png +0 -0
  86. package/lib/base/ares.html +40 -0
  87. package/lib/base/cli-appdata.js +289 -0
  88. package/lib/base/cli-control.js +44 -0
  89. package/lib/base/common-tools.js +29 -0
  90. package/lib/base/error-handler.js +265 -0
  91. package/lib/base/file-watcher.js +155 -0
  92. package/lib/base/help-format.js +147 -0
  93. package/lib/base/luna.js +178 -0
  94. package/lib/base/novacom.js +1191 -0
  95. package/lib/base/sdkenv.js +59 -0
  96. package/lib/base/server.js +137 -0
  97. package/lib/base/setup-device.js +324 -0
  98. package/lib/base/version-tools.js +79 -0
  99. package/lib/com.sdk.ares.hostedapp.ipk +0 -0
  100. package/lib/device.js +1419 -0
  101. package/lib/generator.js +377 -0
  102. package/lib/inspect.js +494 -0
  103. package/lib/install.js +463 -0
  104. package/lib/launch.js +605 -0
  105. package/lib/log.js +584 -0
  106. package/lib/package.js +2129 -0
  107. package/lib/pull.js +231 -0
  108. package/lib/pusher.js +210 -0
  109. package/lib/session.js +74 -0
  110. package/lib/shell.js +193 -0
  111. package/lib/tar-filter-pack.js +62 -0
  112. package/lib/util/copy.js +31 -0
  113. package/lib/util/createFileName.js +40 -0
  114. package/lib/util/eof.js +30 -0
  115. package/lib/util/json.js +63 -0
  116. package/lib/util/merge.js +14 -0
  117. package/lib/util/objclone.js +40 -0
  118. package/lib/util/spinner.js +37 -0
  119. package/npm-shrinkwrap.json +85 -0
  120. package/package.json +98 -0
  121. package/spec/helpers/reporter.js +65 -0
  122. package/spec/jsSpecs/apiTest/generator.spec.js +372 -0
  123. package/spec/jsSpecs/apiTest/inspector.spec.js +89 -0
  124. package/spec/jsSpecs/apiTest/installer.spec.js +67 -0
  125. package/spec/jsSpecs/apiTest/launcher.spec.js +150 -0
  126. package/spec/jsSpecs/apiTest/packager.spec.js +194 -0
  127. package/spec/jsSpecs/apiTest/puller.spec.js +101 -0
  128. package/spec/jsSpecs/apiTest/pusher.spec.js +103 -0
  129. package/spec/jsSpecs/apiTest/server.spec.js +115 -0
  130. package/spec/jsSpecs/apiTest/setupDevice.spec.js +93 -0
  131. package/spec/jsSpecs/apiTest/shell.spec.js +49 -0
  132. package/spec/jsSpecs/ares-config.spec.js +78 -0
  133. package/spec/jsSpecs/ares-device.spec.js +443 -0
  134. package/spec/jsSpecs/ares-generate.spec.js +397 -0
  135. package/spec/jsSpecs/ares-inspect.spec.js +252 -0
  136. package/spec/jsSpecs/ares-install.spec.js +150 -0
  137. package/spec/jsSpecs/ares-launch.spec.js +301 -0
  138. package/spec/jsSpecs/ares-log.spec.js +824 -0
  139. package/spec/jsSpecs/ares-novacom.spec.js +149 -0
  140. package/spec/jsSpecs/ares-package.spec.js +1211 -0
  141. package/spec/jsSpecs/ares-pull.spec.js +157 -0
  142. package/spec/jsSpecs/ares-push.spec.js +146 -0
  143. package/spec/jsSpecs/ares-server.spec.js +160 -0
  144. package/spec/jsSpecs/ares-setup-device.spec.js +281 -0
  145. package/spec/jsSpecs/ares-shell.spec.js +220 -0
  146. package/spec/jsSpecs/ares.spec.js +83 -0
  147. package/spec/jsSpecs/common-spec.js +169 -0
  148. package/spec/support/jasmine.json +22 -0
  149. package/spec/tempFiles/com.jasmine.native.app_0.0.1_arm.ipk +0 -0
  150. package/spec/tempFiles/com.jasmine.native.app_0.0.1_x86.ipk +0 -0
  151. package/spec/tempFiles/com.jasmine.web.app_0.0.1_all.ipk +0 -0
  152. package/spec/tempFiles/com.logtest.web.app_1.0.0_all.ipk +0 -0
  153. package/spec/tempFiles/copyFiles/helloFile.txt +1 -0
  154. package/spec/tempFiles/copyFiles/testFile.txt +1 -0
  155. package/spec/tempFiles/ipks/com.sample.echo_0.0.1_all.ipk +0 -0
  156. package/spec/tempFiles/ipks/com.web.app_1.0.0_all.ipk +0 -0
  157. package/spec/tempFiles/nativeApp/auto/pkg_arm64/GLES2 +0 -0
  158. package/spec/tempFiles/nativeApp/auto/pkg_arm64/appinfo.json +9 -0
  159. package/spec/tempFiles/nativeApp/auto/pkg_arm64/icon.png +0 -0
  160. package/spec/tempFiles/nativeApp/auto/pkg_arm64/largeIcon.png +0 -0
  161. package/spec/tempFiles/nativeApp/ose/pkg_arm/Hello +0 -0
  162. package/spec/tempFiles/nativeApp/ose/pkg_arm/appinfo.json +9 -0
  163. package/spec/tempFiles/nativeApp/ose/pkg_arm/icon.png +0 -0
  164. package/spec/tempFiles/nativeApp/ose/pkg_arm/largeIcon.png +0 -0
  165. package/spec/tempFiles/nativeApp/ose/pkg_arm/package.properties +2 -0
  166. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/Hello +0 -0
  167. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/appinfo.json +9 -0
  168. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/icon.png +0 -0
  169. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/largeIcon.png +0 -0
  170. package/spec/tempFiles/nativeApp/rsi/pkg_x86/GLES2 +0 -0
  171. package/spec/tempFiles/nativeApp/rsi/pkg_x86/appinfo.json +9 -0
  172. package/spec/tempFiles/nativeApp/rsi/pkg_x86/icon.png +0 -0
  173. package/spec/tempFiles/nativeApp/rsi/pkg_x86/largeIcon.png +0 -0
  174. package/spec/tempFiles/sign/sign.crt +32 -0
  175. package/spec/tempFiles/sign/signPriv.key +52 -0
  176. package/spec/test_data/ares-generate.json +41 -0
  177. package/spec/test_data/ares.json +33 -0
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright (c) 2020-2024 LG Electronics Inc.
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ const fs = require('fs'),
8
+ path = require('path');
9
+
10
+ (function() {
11
+ const sdkenv = {};
12
+ function SdkEnv() {
13
+ // Read SDK ENV
14
+ let sdkPath,
15
+ sdkBrowserPath;
16
+
17
+ try {
18
+ const confFile = path.join(__dirname, '../../files/conf/sdk.json'),
19
+ sdkConf = JSON.parse(fs.readFileSync(confFile, 'utf8'));
20
+ sdkPath = process.env[sdkConf.SDKPATH_ENV_NAME];
21
+ sdkBrowserPath = sdkConf.BROWSER_PATH_IN_SDK[process.platform];
22
+ } catch(e) {
23
+ // TBD. allowing exceptions...
24
+ }
25
+
26
+ const browserPath = process.env.ARES_BUNDLE_BROWSER
27
+ || (sdkPath && sdkBrowserPath) ? path.join(sdkPath, sdkBrowserPath) : null;
28
+ this.envList = {};
29
+
30
+ if (sdkPath && fs.existsSync(sdkPath)) {
31
+ this.envList.SDK = sdkPath;
32
+ }
33
+
34
+ if (browserPath && fs.existsSync(browserPath)) {
35
+ this.envList.BROWSER = browserPath;
36
+ }
37
+ }
38
+
39
+ sdkenv.Env = SdkEnv;
40
+
41
+ sdkenv.create = function() {
42
+ return new SdkEnv();
43
+ };
44
+
45
+ SdkEnv.prototype = {
46
+ getEnvList: function(next) {
47
+ const envNameList = Object.keys(this.envList);
48
+ setImmediate(next, null, envNameList);
49
+ },
50
+ getEnvValue: function(name, next) {
51
+ const envValue = this.envList[name];
52
+ setImmediate(next, null, envValue);
53
+ }
54
+ };
55
+
56
+ if (typeof module !== 'undefined' && module.exports) {
57
+ module.exports = sdkenv;
58
+ }
59
+ }());
@@ -0,0 +1,137 @@
1
+ /*
2
+ * Copyright (c) 2020-2024 LG Electronics Inc.
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ const bodyParser = require('body-parser'),
8
+ Express = require('express'),
9
+ fs = require('fs'),
10
+ http = require('http'),
11
+ npmlog = require('npmlog'),
12
+ spawn = require('child_process').spawn,
13
+ errHndl = require('./error-handler');
14
+
15
+ (function() {
16
+ const log = npmlog;
17
+ log.heading = 'server';
18
+ log.level = 'warn';
19
+
20
+ const platformOpen = {
21
+ win32: [ "cmd" , "/c", "start" ],
22
+ darwin:[ "open" ],
23
+ linux: [ "xdg-open" ]
24
+ };
25
+
26
+ const server = {},
27
+ sockets = {};
28
+ let localServer,
29
+ nextSocketId = 0;
30
+
31
+ /**
32
+ * Run a local web server based on the path
33
+ * @param {path} path where local web server indicates.
34
+ * @param {port} port number for web server. 0 means random port.
35
+ * @param {Function} next a common-JS callback invoked when the DB is ready to use.
36
+ */
37
+ server.runServer = function(path, port, reqHandlerForIFrame, next) {
38
+ log.verbose("server#runServer()");
39
+ if (typeof reqHandlerForIFrame === 'function' && !next) {
40
+ next = reqHandlerForIFrame;
41
+ reqHandlerForIFrame = null;
42
+ }
43
+ const appPath = fs.realpathSync(path),
44
+ app = new Express();
45
+
46
+ app.use("/", Express.static(appPath));
47
+ app.use("/ares_cli", Express.static(__dirname));
48
+ app.use(bodyParser.json());
49
+
50
+ if (reqHandlerForIFrame) {
51
+ app.post('/ares_cli/@@ARES_CLOSE@@', function(req, res) {
52
+ reqHandlerForIFrame("@@ARES_CLOSE@@", res);
53
+ });
54
+ app.post('/ares_cli/@@GET_URL@@', function(req, res) {
55
+ reqHandlerForIFrame("@@GET_URL@@", res);
56
+ });
57
+ }
58
+
59
+ localServer = http.createServer(app);
60
+ localServer.on('error', function(err) {
61
+ return next(errHndl.getErrMsg(err));
62
+ });
63
+ localServer.listen(port, function(err) {
64
+ if (err) {
65
+ return next(errHndl.getErrMsg(err));
66
+ }
67
+ const localServerPort = localServer.address().port,
68
+ url = 'http://localhost:' + localServerPort;
69
+
70
+ next(null, {
71
+ "url": url,
72
+ "port": localServerPort,
73
+ "openBrowserUrl": url + "/ares_cli/ares.html",
74
+ "msg":"Local server running on " + url
75
+ });
76
+ });
77
+ localServer.on('connection', function(socket) {
78
+ // Add a newly connected socket
79
+ const socketId = nextSocketId++;
80
+ sockets[socketId] = socket;
81
+ log.verbose("Socket opened: ", socketId);
82
+ });
83
+ };
84
+
85
+ /**
86
+ * Open url with a web browser
87
+ * @param {url} URL to be opened via web browser.
88
+ * @param {browserPath} browser exectable path. (optional)
89
+ * @param {Function} next a common-JS callback invoked when the DB is ready to use. (optional)
90
+ */
91
+ server.openBrowser = function(url, browserPath, next) {
92
+ log.verbose("server#openBrowser()");
93
+ let info = platformOpen[process.platform];
94
+ if (typeof browserPath === 'function') {
95
+ next = browserPath;
96
+ browserPath = null;
97
+ }
98
+ if (browserPath && fs.existsSync(browserPath)) {
99
+ if (process.platform === 'linux') {
100
+ info.splice(0, 1); // delete 'xdg-open' command
101
+ }
102
+ info = info.concat([browserPath]);
103
+
104
+ if (process.platform === 'darwin') {
105
+ info = info.concat(['--new', '--args']);
106
+ }
107
+ }
108
+
109
+ this.browserProcess = spawn(info[0], info.slice(1).concat([url]));
110
+ if (next) {
111
+ next(null, {msg:"Browser opened"});
112
+ }
113
+ };
114
+
115
+ server.stop = function(next) {
116
+ log.verbose("server#stop()");
117
+ if (!localServer) {
118
+ return next(errHndl.getErrMsg("NOT_EXIST_SERVER"));
119
+ }
120
+
121
+ if (Object.keys(sockets).length) {
122
+ for (const socketId in sockets) {
123
+ log.verbose("Socket destroyed: ", socketId);
124
+ sockets[socketId].destroy();
125
+ }
126
+ }
127
+
128
+ localServer.close(function() {
129
+ log.verbose("Local server close called");
130
+ next(null, {msg: "Local server is stopped"});
131
+ });
132
+ };
133
+
134
+ if (typeof module !== 'undefined' && module.exports) {
135
+ module.exports = server;
136
+ }
137
+ }());
@@ -0,0 +1,324 @@
1
+ /*
2
+ * Copyright (c) 2020-2024 LG Electronics Inc.
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ const async = require('async'),
8
+ chalk = require('chalk'),
9
+ Table = require('easy-table'),
10
+ npmlog = require('npmlog'),
11
+ Appdata = require('./cli-appdata'),
12
+ errHndl = require('./error-handler'),
13
+ novacom = require('./novacom');
14
+
15
+ (function() {
16
+ const log = npmlog;
17
+ log.heading = 'setup-device';
18
+ log.level = 'warn';
19
+
20
+ const appdata = new Appdata(),
21
+ devicetools = {},
22
+ defaultDeviceInfo = {
23
+ profile: appdata.getConfig(true).profile,
24
+ host: "127.0.0.1",
25
+ port: 22,
26
+ username: "root",
27
+ description: "new device description",
28
+ files: "stream",
29
+ default: false
30
+ };
31
+
32
+ if (typeof module !== 'undefined' && module.exports) {
33
+ module.exports = devicetools;
34
+ module.exports.isValidDeviceName = isValidDeviceName;
35
+ module.exports.isValidIpv4 = isValidIpv4;
36
+ module.exports.isValidPort =isValidPort;
37
+ module.exports.replaceDefaultDeviceInfo = replaceDefaultDeviceInfo;
38
+ }
39
+
40
+ function isValidDeviceName(name) {
41
+ const re = new RegExp("^[_a-zA-Z][a-zA-Z0-9#_-]*");
42
+ return (name === String(name.match(re)));
43
+ }
44
+
45
+ function isValidIpv4(host) {
46
+ return host.match(/^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$/);
47
+ }
48
+
49
+ function isValidPort(port) {
50
+ return String(port).match(/^[0-9]+$/);
51
+ }
52
+
53
+ devicetools.showDeviceList = function(next) {
54
+ async.waterfall([
55
+ _getDeviceInfo.bind(this, 'list'),
56
+ function(data, next) {
57
+ const table = new Table();
58
+ data.forEach(function(item) {
59
+ if (!isValidDeviceName(item.name)) {
60
+ return;
61
+ }
62
+ table.cell('name', (item.default === true) ? item.name + chalk.green(' (default)') : item.name);
63
+ table.cell('deviceinfo', item.info);
64
+ table.cell('connection', item.connection);
65
+ table.cell('profile', item.profile);
66
+ table.newRow();
67
+ });
68
+ next(null, table.toString());
69
+ }
70
+ ], function(err, results) {
71
+ next(err, {msg: results});
72
+ });
73
+ };
74
+
75
+ devicetools.showDeviceListFull = function(next) {
76
+ async.waterfall([
77
+ _getDeviceInfo.bind(this, 'full'),
78
+ function(data, next) {
79
+ next(null, JSON.stringify(data, null, 4));
80
+ }
81
+ ], function(err, results) {
82
+ next(err, {msg: results});
83
+ });
84
+ };
85
+
86
+ devicetools.resetDeviceList = function(next) {
87
+ async.series([
88
+ function(next) {
89
+ appdata.resetDeviceList(next);
90
+ },
91
+ this.showDeviceList.bind(this, next)
92
+ ], function(err) {
93
+ next(err);
94
+ });
95
+ };
96
+
97
+ devicetools.setDefaultDevice = function(name, next) {
98
+ try {
99
+ const resolver = this.resolver || (this.resolver = new novacom.Resolver()),
100
+ inDevice = {name: name, default: true};
101
+ async.series([
102
+ resolver.load.bind(resolver),
103
+ resolver.modifyDeviceFile.bind(resolver, 'default', inDevice),
104
+ this.showDeviceList.bind(this, next)
105
+ ], function(err) {
106
+ if (err) {
107
+ return next(err);
108
+ }
109
+ next();
110
+ });
111
+ } catch (err) {
112
+ next(err);
113
+ }
114
+ };
115
+
116
+ devicetools.removeDeviceInfo = function(options, next) {
117
+ try {
118
+ // For CLI
119
+ if (options.remove === 'true') {
120
+ next(errHndl.getErrMsg("EMPTY_VALUE", "DEVICE_NAME"));
121
+ }
122
+ // For API
123
+ if (!options.remove) {
124
+ next(errHndl.getErrMsg("INVALID_REMOVE_MODE"));
125
+ }
126
+
127
+ const resolver = this.resolver || (this.resolver = new novacom.Resolver()),
128
+ inDevice = {name: options.remove, profile: defaultDeviceInfo.profile};
129
+ async.series([
130
+ resolver.load.bind(resolver),
131
+ resolver.modifyDeviceFile.bind(resolver, 'remove', inDevice),
132
+ this.showDeviceList.bind(this, next)
133
+ ], function(err) {
134
+ if (err) {
135
+ return next(err);
136
+ }
137
+ next();
138
+ });
139
+ } catch (err) {
140
+ next(err);
141
+ }
142
+ };
143
+
144
+ devicetools.modifyDeviceInfo = function(options, next) {
145
+ try {
146
+ const mode = (options.add) ? "add" : (options.modify) ? "modify" : null;
147
+ if (!mode) {
148
+ return next(errHndl.getErrMsg("INVALID_MODE"));
149
+ }
150
+ if (options[mode].match(/^-/)) {
151
+ return next(errHndl.getErrMsg("EMPTY_VALUE", "DEVICE_NAME"));
152
+ }
153
+ const argName = (options.info) ? "info" : mode;
154
+ const inDevice = _getParams(options, argName);
155
+ if (!inDevice.name) {
156
+ if (options[mode] === "true") {
157
+ return next(errHndl.getErrMsg("EMPTY_VALUE", "DEVICE_NAME"));
158
+ }
159
+ inDevice.name = options[mode];
160
+ }
161
+
162
+ log.info("modifyDeviceInfo()", "devicename:", inDevice.name, ", mode:", mode);
163
+
164
+ if (inDevice.default !== undefined && mode === "modify") {
165
+ log.verbose("modifyDeviceInfo()", "Ignoring invalid arguments:default");
166
+ inDevice.default = undefined;
167
+ }
168
+
169
+ if (inDevice.privateKey) {
170
+ inDevice.privatekey = inDevice.privateKey;
171
+ }
172
+ if (typeof inDevice.privatekey === "string") {
173
+ inDevice.privateKey = inDevice.privatekey;
174
+ inDevice.privateKey = { "openSsh": inDevice.privateKey };
175
+ delete inDevice.privatekey;
176
+ inDevice.password = "@DELETE@";
177
+ }
178
+ if (typeof inDevice.password !== "undefined" && inDevice.password !== "@DELETE@") {
179
+ inDevice.privateKey = "@DELETE@";
180
+ inDevice.passphrase = "@DELETE@";
181
+ }
182
+
183
+ if (mode === "add") {
184
+ replaceDefaultDeviceInfo(inDevice);
185
+ if (!inDevice.privateKey && !inDevice.password) {
186
+ inDevice.password = "";
187
+ }
188
+ }
189
+ // check validation
190
+ if (!isValidDeviceName(inDevice.name)) {
191
+ return next(errHndl.getErrMsg("INVALID_DEVICENAME"));
192
+ }
193
+ if (inDevice.host && !isValidIpv4(inDevice.host)) {
194
+ return next(errHndl.getErrMsg("INVALID_VALUE", "host", inDevice.host));
195
+ }
196
+ if (inDevice.port && !isValidPort(inDevice.port)) {
197
+ return next(errHndl.getErrMsg("INVALID_VALUE", "port", inDevice.port));
198
+ }
199
+ if (inDevice.port) {
200
+ inDevice.port = Number(inDevice.port);
201
+ }
202
+ if (!inDevice.profile) {
203
+ inDevice.profile = defaultDeviceInfo.profile;
204
+ }
205
+ const resolver = this.resolver || (this.resolver = new novacom.Resolver());
206
+ async.series([
207
+ resolver.load.bind(resolver),
208
+ resolver.modifyDeviceFile.bind(resolver, mode, inDevice),
209
+ this.showDeviceList.bind(this, next)
210
+ ], function(err) {
211
+ if (err) {
212
+ return next(err);
213
+ }
214
+ next();
215
+ });
216
+ } catch (err) {
217
+ next(err);
218
+ }
219
+ };
220
+
221
+ function _getDeviceInfo(mode, next) {
222
+ const datas= [],
223
+ resolver = new novacom.Resolver();
224
+ async.waterfall([
225
+ resolver.load.bind(resolver),
226
+ resolver.list.bind(resolver),
227
+ function(devices, next) {
228
+ if (Array.isArray(devices)) {
229
+ devices.forEach(function(device) {
230
+ const conn = device.conn.concat([]),
231
+ info = (device.username && device.host && device.port) ? device.username + '@' + device.host + ':' + device.port : device.id,
232
+ item = {
233
+ profile: device.profile,
234
+ name: device.name,
235
+ default: device.default,
236
+ deviceinfo: {},
237
+ connection: device.conn || ['ssh'],
238
+ details: {
239
+ platform: device.type
240
+ }
241
+ };
242
+
243
+ if (conn.length === 1 && conn.indexOf('novacom') !== -1) {
244
+ item.deviceinfo.uid = device.id;
245
+ item.details.type = device.name.slice(0, 3);
246
+ } else {
247
+ item.deviceinfo = {
248
+ ip: device.host,
249
+ port: String(device.port),
250
+ user: device.username
251
+ };
252
+
253
+ item.details.password = device.password;
254
+ item.details.privatekey = device.privateKeyName;
255
+ item.details.passphrase = device.passphrase;
256
+ item.details.description = device.description;
257
+ }
258
+
259
+ if (device.id) {
260
+ item.deviceinfo.uid = device.id;
261
+ item.details.type = device.name.slice(0, 3);
262
+ }
263
+ const data = (mode === 'full') ? item : {name: device.name, default: device.default, info:info, connection: (device.conn || 'ssh'), profile: device.profile };
264
+ datas.push(data);
265
+ });
266
+ }
267
+ next(null, datas);
268
+ }
269
+ ], function(err, results) {
270
+ next(err, results);
271
+ });
272
+ }
273
+
274
+ function _getParams(argv, option) {
275
+ let inputParams = [];
276
+ const params = {};
277
+ if (argv[option]) {
278
+ inputParams = [].concat(argv[option]);
279
+ }
280
+
281
+ if (inputParams.length === 1 && inputParams[0].indexOf('{') !== -1 && inputParams[0].indexOf('}') !== -1 &&
282
+ ((inputParams[0].split("'").length - 1) % 2) === 0) {
283
+ // eslint-disable-next-line no-useless-escape
284
+ inputParams[0] = inputParams[0].replace(/\'/g, '"');
285
+ }
286
+ inputParams.forEach(function(strParam) {
287
+ try {
288
+ const data = JSON.parse(strParam);
289
+ for (const k in data) {
290
+ params[k] = data[k];
291
+ }
292
+ } catch (err) {
293
+ const tokens = strParam.split('=');
294
+ if (tokens.length === 2) {
295
+ params[tokens[0]] = tokens[1];
296
+ log.verbose("_getParams()", "Inserting params ", tokens[0] + " = " + tokens[1]);
297
+ } else {
298
+ log.verbose("_getParams()", "Ignoring invalid arguments:", strParam);
299
+ }
300
+ }
301
+ });
302
+
303
+ // FIXME : -i default=true is set as "true" string
304
+ if (params.default !== undefined && typeof params.default === "string") {
305
+ params.default = (params.default === "true");
306
+ }
307
+
308
+ log.silly("_getParams()", "params:", JSON.stringify(params));
309
+ return params;
310
+ }
311
+
312
+ function replaceDefaultDeviceInfo(inDevice) {
313
+ if (inDevice) {
314
+ inDevice.profile = inDevice.profile || defaultDeviceInfo.profile;
315
+ inDevice.type = inDevice.type || defaultDeviceInfo.type;
316
+ inDevice.host = inDevice.host || defaultDeviceInfo.host;
317
+ inDevice.port = inDevice.port || defaultDeviceInfo.port;
318
+ inDevice.username = inDevice.username || defaultDeviceInfo.username;
319
+ inDevice.files = inDevice.files || defaultDeviceInfo.files;
320
+ inDevice.description = inDevice.description || defaultDeviceInfo.description;
321
+ inDevice.default = inDevice.default || defaultDeviceInfo.default;
322
+ }
323
+ }
324
+ }());
@@ -0,0 +1,79 @@
1
+ /*
2
+ * Copyright (c) 2020-2024 LG Electronics Inc.
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ const fs = require('fs'),
8
+ path = require('path'),
9
+ semver = require('semver');
10
+
11
+ (function() {
12
+ const vtools = {};
13
+ let pkgInfo = null;
14
+
15
+ vtools.showVersionAndExit = function() {
16
+ getPackageVersion(function(err, version) {
17
+ console.log("Version: " + version);
18
+ process.exit(0);
19
+ });
20
+ };
21
+
22
+ vtools.checkNodeVersion = function(next) {
23
+ getAllowedNodeVersion(function(err, range) {
24
+ const expectedRange = semver.validRange(range);
25
+ if (expectedRange) {
26
+ if (semver.satisfies(process.version, expectedRange)) {
27
+ next();
28
+ } else {
29
+ console.error("This command only works on Node.js version: " + expectedRange);
30
+ process.exit(1);
31
+ }
32
+ } else {
33
+ console.error("Invalid Node.js version range: " + range);
34
+ process.exit(1);
35
+ }
36
+ });
37
+ };
38
+
39
+ // Private methods
40
+ function getAllowedNodeVersion(next) {
41
+ if (pkgInfo) {
42
+ next(null, (pkgInfo && pkgInfo.engines && pkgInfo.engines.node) || "");
43
+ } else {
44
+ loadPackageJson(function(err) {
45
+ next(err, (pkgInfo && pkgInfo.engines && pkgInfo.engines.node) || "");
46
+ });
47
+ }
48
+ }
49
+
50
+ function loadPackageJson(next) {
51
+ const filename = path.resolve(__dirname, "../..", "package.json");
52
+ fs.readFile(filename, function(err, data) {
53
+ if (err) {
54
+ return next("loadPackageJson_error");
55
+ }
56
+
57
+ try {
58
+ pkgInfo = JSON.parse(data);
59
+ next();
60
+ } catch(error) {
61
+ next(error);
62
+ }
63
+ });
64
+ }
65
+
66
+ function getPackageVersion(next) {
67
+ if (pkgInfo) {
68
+ next(null, pkgInfo.version);
69
+ } else {
70
+ loadPackageJson(function(err) {
71
+ next(err, (pkgInfo && pkgInfo.version) || "unknown");
72
+ });
73
+ }
74
+ }
75
+
76
+ if (typeof module !== 'undefined' && module.exports) {
77
+ module.exports = vtools;
78
+ }
79
+ }());
Binary file