@webos-tools/cli 3.2.0 → 3.2.1

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 (152) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.js +52 -52
  3. package/.vscode/c_cpp_properties.json +21 -0
  4. package/.vscode/launch.json +97 -0
  5. package/.vscode/settings.json +13 -0
  6. package/.vscode/tasks.json +32 -0
  7. package/APIs.js +79 -79
  8. package/CHANGELOG.md +11 -5
  9. package/LICENSE +201 -201
  10. package/bin/ares-config.js +1 -1
  11. package/bin/ares-device-info.js +30 -30
  12. package/bin/ares-generate.js +274 -274
  13. package/bin/ares-inspect.js +179 -179
  14. package/bin/ares-install.js +223 -223
  15. package/bin/ares-log.js +259 -259
  16. package/bin/ares-pull.js +156 -156
  17. package/bin/ares-push.js +155 -155
  18. package/bin/ares-server.js +174 -174
  19. package/bin/ares-setup-device.js +6 -10
  20. package/bin/ares-shell.js +132 -132
  21. package/bin/ares.js +166 -166
  22. package/files/conf/ares.json +1 -2
  23. package/files/conf/ipk.json +30 -30
  24. package/files/conf/novacom-devices.json +2 -2
  25. package/files/conf/query/query-app.json +14 -14
  26. package/files/conf/query/query-hosted.json +18 -18
  27. package/files/conf/query/query-package.json +10 -10
  28. package/files/conf/query/query-service.json +6 -6
  29. package/files/conf/webos_emul +27 -27
  30. package/files/conf-base/env/sdk-apollo.json +8 -8
  31. package/files/conf-base/env/sdk-ose.json +8 -8
  32. package/files/conf-base/env/sdk-tv.json +8 -8
  33. package/files/conf-base/profile/config-apollo.json +29 -29
  34. package/files/conf-base/profile/config-ose.json +29 -29
  35. package/files/conf-base/profile/config-tv.json +31 -31
  36. package/files/conf-base/query/query-app.json +14 -14
  37. package/files/conf-base/query/query-hosted.json +18 -18
  38. package/files/conf-base/query/query-package.json +10 -10
  39. package/files/conf-base/query/query-service.json +6 -6
  40. package/files/conf-base/query/signage/query-app.json +1 -1
  41. package/files/conf-base/query/signage/query-service.json +1 -1
  42. package/files/conf-base/template-conf/ose-templates.json +67 -67
  43. package/files/conf-base/template-conf/tv-sdk-templates.json +57 -57
  44. package/files/help/ares-device.help +109 -109
  45. package/files/help/ares-install.help +95 -95
  46. package/files/help/ares-launch.help +1 -1
  47. package/files/help/ares-log-pmlogd.help +84 -84
  48. package/files/help/ares-log.help +101 -101
  49. package/files/help/ares-novacom.help +68 -68
  50. package/files/help/ares-pull.help +38 -38
  51. package/files/help/ares-push.help +38 -38
  52. package/files/help/ares-server.help +44 -44
  53. package/files/help/ares-shell.help +42 -42
  54. package/files/help/ares.help +52 -52
  55. package/files/help/readme.help +23 -23
  56. package/files/schema/ApplicationDescription.schema +319 -319
  57. package/files/templates/apollo-sdk-templates/appinfo/appinfo.json +10 -10
  58. package/files/templates/apollo-sdk-templates/bootplate-web/index.html +88 -88
  59. package/files/templates/apollo-sdk-templates/hosted-webapp/index.html +13 -13
  60. package/files/templates/apollo-sdk-templates/js-service/helloclient.js +31 -31
  61. package/files/templates/apollo-sdk-templates/js-service/helloworld_webos_service.js +188 -188
  62. package/files/templates/apollo-sdk-templates/serviceinfo/package.json +11 -11
  63. package/files/templates/apollo-sdk-templates/serviceinfo/services.json +8 -8
  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/js-service/helloclient.js +31 -31
  68. package/files/templates/ose-sdk-templates/js-service/helloworld_webos_service.js +188 -188
  69. package/files/templates/ose-sdk-templates/qml-app/main.qml +68 -68
  70. package/files/templates/ose-sdk-templates/qmlappinfo/appinfo.json +10 -10
  71. package/files/templates/ose-sdk-templates/serviceinfo/package.json +11 -11
  72. package/files/templates/ose-sdk-templates/serviceinfo/services.json +8 -8
  73. package/files/templates/signage-sdk-templates/Backlight_Scheduling/index.html +1 -1
  74. package/files/templates/signage-sdk-templates/Backlight_Scheduling/scheduler.js +2 -3
  75. package/files/templates/signage-sdk-templates/File_Explorer/index.html +1 -33
  76. package/files/templates/signage-sdk-templates/File_Explorer/js/Root.js +0 -9
  77. package/files/templates/tv-sdk-templates/appinfo/appinfo.json +10 -10
  78. package/files/templates/tv-sdk-templates/bootplate-web/index.html +58 -58
  79. package/files/templates/tv-sdk-templates/bootplate-web/webOSTVjs-1.2.10/LICENSE-2.0.txt +202 -202
  80. package/files/templates/tv-sdk-templates/hosted-webapp/index.html +14 -14
  81. package/files/templates/tv-sdk-templates/js-service/helloworld_service.js +39 -39
  82. package/files/templates/tv-sdk-templates/packageinfo/packageinfo.json +3 -3
  83. package/files/templates/tv-sdk-templates/serviceinfo/package.json +11 -11
  84. package/files/templates/tv-sdk-templates/serviceinfo/services.json +8 -8
  85. package/lib/base/ares.html +40 -40
  86. package/lib/base/cli-appdata.js +290 -289
  87. package/lib/base/cli-control.js +44 -44
  88. package/lib/base/common-tools.js +29 -29
  89. package/lib/base/error-handler.js +265 -265
  90. package/lib/base/file-watcher.js +155 -155
  91. package/lib/base/help-format.js +147 -147
  92. package/lib/base/luna.js +178 -178
  93. package/lib/base/novacom.js +4 -4
  94. package/lib/base/sdkenv.js +59 -59
  95. package/lib/base/server.js +137 -137
  96. package/lib/base/setup-device.js +2 -2
  97. package/lib/base/version-tools.js +79 -79
  98. package/lib/device.js +1419 -1419
  99. package/lib/generator.js +1 -1
  100. package/lib/inspect.js +493 -493
  101. package/lib/log.js +584 -584
  102. package/lib/package.js +61 -7
  103. package/lib/pull.js +231 -231
  104. package/lib/pusher.js +210 -210
  105. package/lib/session.js +74 -74
  106. package/lib/shell.js +193 -193
  107. package/lib/tar-filter-pack.js +62 -62
  108. package/lib/util/copy.js +31 -31
  109. package/lib/util/createFileName.js +40 -40
  110. package/lib/util/eof.js +30 -30
  111. package/lib/util/json.js +63 -63
  112. package/lib/util/merge.js +14 -14
  113. package/lib/util/objclone.js +40 -40
  114. package/lib/util/spinner.js +37 -37
  115. package/npm-shrinkwrap.json +2 -2
  116. package/package.json +1 -1
  117. package/scripts/postinstall.js +24 -24
  118. package/spec/helpers/reporter.js +65 -65
  119. package/spec/jsSpecs/apiTest/generator.spec.js +372 -372
  120. package/spec/jsSpecs/apiTest/inspector.spec.js +89 -89
  121. package/spec/jsSpecs/apiTest/installer.spec.js +67 -67
  122. package/spec/jsSpecs/apiTest/launcher.spec.js +150 -150
  123. package/spec/jsSpecs/apiTest/packager.spec.js +194 -194
  124. package/spec/jsSpecs/apiTest/puller.spec.js +101 -101
  125. package/spec/jsSpecs/apiTest/pusher.spec.js +103 -103
  126. package/spec/jsSpecs/apiTest/server.spec.js +115 -115
  127. package/spec/jsSpecs/apiTest/setupDevice.spec.js +93 -93
  128. package/spec/jsSpecs/apiTest/shell.spec.js +49 -49
  129. package/spec/jsSpecs/ares-config.spec.js +88 -88
  130. package/spec/jsSpecs/ares-device.spec.js +443 -443
  131. package/spec/jsSpecs/ares-generate.spec.js +397 -401
  132. package/spec/jsSpecs/ares-inspect.spec.js +252 -252
  133. package/spec/jsSpecs/ares-install.spec.js +150 -150
  134. package/spec/jsSpecs/ares-log.spec.js +824 -824
  135. package/spec/jsSpecs/ares-novacom.spec.js +149 -149
  136. package/spec/jsSpecs/ares-pull.spec.js +157 -157
  137. package/spec/jsSpecs/ares-push.spec.js +146 -146
  138. package/spec/jsSpecs/ares-setup-device.spec.js +1 -0
  139. package/spec/jsSpecs/ares-shell.spec.js +220 -220
  140. package/spec/jsSpecs/ares.spec.js +83 -83
  141. package/spec/support/jasmine.json +22 -22
  142. package/spec/tempFiles/nativeApp/auto/pkg_arm64/appinfo.json +9 -9
  143. package/spec/tempFiles/nativeApp/ose/pkg_arm/appinfo.json +8 -8
  144. package/spec/tempFiles/nativeApp/ose/pkg_arm/package.properties +2 -2
  145. package/spec/tempFiles/nativeApp/oseEmul/pkg_x86/appinfo.json +9 -9
  146. package/spec/tempFiles/nativeApp/rsi/pkg_x86/appinfo.json +9 -9
  147. package/spec/tempFiles/sign/sign.crt +32 -32
  148. package/spec/tempFiles/sign/signPriv.key +52 -52
  149. package/spec/test_data/ares-generate.json +58 -91
  150. package/spec/test_data/ares.json +50 -50
  151. package/webos-tools-cli-3.2.1.tgz +0 -0
  152. package/webos-tools-cli-3.2.0.tgz +0 -0
@@ -1,289 +1,290 @@
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
- Validator = require('jsonschema').Validator,
9
- mkdirp = require('mkdirp'),
10
- log = require('npmlog'),
11
- path = require('path'),
12
- shelljs = require('shelljs'),
13
- errHndl = require('./error-handler.js'),
14
- clone = require('./../util/objclone').deepCopy;
15
-
16
- const Cli = (function() {
17
- let instance;
18
-
19
- function finish(err, returnValue, next) {
20
- if (next && typeof next === 'function') {
21
- if (!returnValue) {
22
- return setImmediate(next, err);
23
- }
24
- return setImmediate(next, err, returnValue);
25
- }
26
- return err || returnValue;
27
- }
28
-
29
- function init() {
30
- const
31
- workParentDir = path.resolve(process.env.APPDATA || process.env.HOME || process.env.USERPROFILE),
32
- builtinFiles = {
33
- "config": {
34
- "dir": path.join(__dirname, "../../files", "conf"),
35
- "file": "config.json"
36
- },
37
- "commands": {
38
- "dir": path.join(__dirname, "../../files", "conf"),
39
- "file": "command-service.json"
40
- },
41
- "deviceList": {
42
- "dir": path.join(__dirname, "../../files", "conf"),
43
- "file": "novacom-devices.json"
44
- },
45
- "deviceListSchema": {
46
- "dir": path.join(__dirname, "../../files", "schema"),
47
- "file": "NovacomDevices.schema"
48
- },
49
- "template": {
50
- "dir": path.join(__dirname, "../../files", "conf"),
51
- "file": "template.json"
52
- },
53
- "env": {
54
- "dir": path.join(__dirname, "../../files", "conf"),
55
- "file": "sdk.json"
56
- }
57
- },
58
- queryPath = path.join(__dirname, "../../files/conf/query");
59
-
60
- function _store(store, n, file) {
61
- store[n] = JSON.parse(fs.readFileSync(file));
62
- store[n + 'File'] = file;
63
- }
64
-
65
- for (const key in builtinFiles) {
66
- const filePath = path.join(builtinFiles[key].dir, builtinFiles[key].file);
67
- _store(this, key, filePath);
68
- }
69
-
70
- const
71
- workDir = (this.config.dataDir) ? path.join(workParentDir, this.config.dataDir) : path.join(workParentDir, '.webos'),
72
- builtinDeviceListFile = path.join(builtinFiles.deviceList.dir, builtinFiles.deviceList.file),
73
- userDeviceListFile = path.join(workDir, builtinFiles.deviceList.file);
74
-
75
- if (!fs.existsSync(workDir)) {
76
- mkdirp.sync(workDir);
77
- }
78
-
79
- try {
80
- const userDeviceList = JSON.parse(fs.readFileSync(userDeviceListFile)),
81
- result = validateDevice(this.deviceListSchema, userDeviceList);
82
-
83
- if (result && result.resultValue) {
84
- this.deviceList = userDeviceList;
85
- } else {
86
- log.verbose("cli-appdata#init()", "schema check failure:", userDeviceListFile);
87
- log.verbose("cli-appdata#init()", "result:", result);
88
- log.verbose("cli-appdata#init()", "copy " + builtinDeviceListFile + " to " + userDeviceListFile);
89
- shelljs.cp('-f', builtinDeviceListFile, userDeviceListFile);
90
- }
91
- } catch (err) {
92
- log.verbose("cli-appdata#init()", "err:", err);
93
- log.verbose("cli-appdata#init()", "copy " + builtinDeviceListFile + " to " + userDeviceListFile);
94
- shelljs.cp('-f', builtinDeviceListFile, userDeviceListFile);
95
- }
96
-
97
- this.builtinDeviceListFile = builtinDeviceListFile;
98
- this.deviceListFile = userDeviceListFile;
99
- this.workDir = workDir;
100
- this.queryPath = queryPath;
101
- if (this.config.simulatorConfig) {
102
- this.simulatorConfigPath = path.join(workDir, this.config.simulatorConfig);
103
- }
104
- return true;
105
- }
106
-
107
- function validateDevice(schema, data) {
108
- let result = {
109
- resultValue: false,
110
- err: null
111
- };
112
- const schemaArray = {
113
- "id": "test",
114
- "type": "array",
115
- "items": {
116
- "$ref": "/deviceSchema"
117
- }
118
- };
119
- const v = new Validator();
120
-
121
- try {
122
- v.addSchema(schema, "/deviceSchema");
123
- result = v.validate(data, schemaArray);
124
- if (result && result.errors.length > 0) {
125
- let errMsg = "Invalid device info.";
126
- for (const idx in result.errors) {
127
- errMsg = errMsg.concat("\n");
128
- let res,
129
- errMsgLine = result.errors[idx].property + " " + result.errors[idx].message;
130
- const regex = /instance\[*.*\]*\./g;
131
- if ((res = regex.exec(errMsgLine)) !== null) {
132
- errMsgLine = errMsgLine.substring(res[0].length);
133
- }
134
- errMsg = errMsg.concat(errMsgLine);
135
- }
136
- result.err = new Error(errMsg);
137
- } else {
138
- result.resultValue = true;
139
- }
140
- } catch (err) {
141
- // ignore exception
142
- result.err = err;
143
- }
144
- return result;
145
- }
146
-
147
- function CliAppData() {
148
- if (instance) {
149
- return instance;
150
- }
151
- instance = this;
152
- if (!this.inialized) {
153
- this.inialized = init.call(this);
154
- }
155
- }
156
-
157
- CliAppData.prototype = {
158
- getSimulatorConfigPath: function(next) {
159
- return finish(null, this.simulatorConfigPath, next);
160
- },
161
-
162
- getPath: function(next) {
163
- return finish(null, this.workDir, next);
164
- },
165
-
166
- getAppDir: function(next) {
167
- return finish(null, path.join(__dirname, '..'), next);
168
- },
169
-
170
- getConfig: function(next) {
171
- return finish(null, clone(this.config), next);
172
- },
173
-
174
- compareProfile: function(query, next) {
175
- next(null, query.toLowerCase() === this.config.profile.toLowerCase());
176
- },
177
-
178
- compareProfileSync: function(query) {
179
- return query.toLowerCase() === this.config.profile.toLowerCase();
180
- },
181
-
182
- getDeviceList: function(renewal, next) {
183
- return finish(null, clone(this.deviceList), next);
184
- },
185
-
186
- getCommandService: function(next) {
187
- return finish(null, clone(this.commands), next);
188
- },
189
-
190
- setDeviceList: function(deviceListData, next) {
191
- log.info("cli-appdata#CliAppData#setDeviceList()");
192
- try {
193
- const result = validateDevice(this.deviceListSchema, deviceListData);
194
- if (result && !result.resultValue) {
195
- const err = result.err || "invalid data";
196
- return finish(err, this.deviceList, next);
197
- }
198
- this.deviceList = deviceListData;
199
- fs.writeFileSync(this.deviceListFile, JSON.stringify(this.deviceList, null, Number(4)));
200
- } catch (err) {
201
- return finish(err, this.deviceList, next);
202
- }
203
- return finish(null, this.deviceList, next);
204
- },
205
-
206
- setConfig: function(configData, next) {
207
- try {
208
- this.config = configData;
209
- fs.writeFileSync(this.configFile, JSON.stringify(this.config, null, Number(4)) + "\n");
210
- } catch (err) {
211
- return finish(err, this.config, next);
212
- }
213
- return finish(null, this.config, next);
214
- },
215
-
216
- setTemplate: function(templateData, next) {
217
- try {
218
- this.template = templateData;
219
- fs.writeFileSync(this.templateFile, JSON.stringify(this.template, null, Number(4)) + "\n");
220
- } catch (err) {
221
- return finish(err, this.template, next);
222
- }
223
- },
224
-
225
- setEnv: function(envData, next) {
226
- try {
227
- this.env = envData;
228
- fs.writeFileSync(this.envFile, JSON.stringify(this.env, null, Number(4)) + "\n");
229
- } catch (err) {
230
- return finish(err, this.env, next);
231
- }
232
- },
233
-
234
- setKey: function(keyPath, next) {
235
- try {
236
- const dstPubkey = path.join(__dirname, "../../files/conf", "pubkey.pem");
237
- if (fs.existsSync(dstPubkey)) {
238
- shelljs.rm(dstPubkey);
239
- }
240
- if (keyPath) {
241
- shelljs.cp(keyPath, dstPubkey);
242
- }
243
- } catch (err) {
244
- return finish(errHndl.getErrMsg(err), keyPath, next);
245
- }
246
- return finish(null, keyPath, next);
247
- },
248
-
249
- setQuery: function(queryBasePath, next) {
250
- try {
251
- if (fs.existsSync(this.queryPath)) {
252
- shelljs.ls(this.queryPath).forEach(function(file) {
253
- shelljs.rm(path.join(__dirname, "../../files/conf/query",file));
254
- });
255
- } else {
256
- fs.mkdirSync(this.queryPath);
257
- }
258
-
259
- shelljs.ls(queryBasePath+'/*.json').forEach(function(file) {
260
- shelljs.cp(file, path.join(__dirname, "../../files/conf", "query"));
261
- });
262
- } catch (err) {
263
- return finish(err, this.queryPath, next);
264
- }
265
- return finish(null, this.queryPath, next);
266
- },
267
-
268
- resetDeviceList: function(next) {
269
- log.info("cli-appdata#CliAppData#resetDeviceList()");
270
- try {
271
- if (fs.existsSync(this.deviceListFile)) {
272
- shelljs.rm(this.deviceListFile);
273
- }
274
-
275
- shelljs.cp(this.builtinDeviceListFile, this.deviceListFile);
276
- this.deviceList = JSON.parse(fs.readFileSync(this.deviceListFile));
277
- } catch (err) {
278
- log.verbose("cli-appdata#CliAppData#resetDeviceList()", "err:", err);
279
- return finish(err, clone(this.deviceList), next);
280
- }
281
- return finish(null, clone(this.deviceList), next);
282
- }
283
- };
284
- return CliAppData;
285
- }());
286
-
287
- if (typeof module !== 'undefined' && module.exports) {
288
- module.exports = Cli;
289
- }
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
+ Validator = require('jsonschema').Validator,
9
+ mkdirp = require('mkdirp'),
10
+ log = require('npmlog'),
11
+ path = require('path'),
12
+ shelljs = require('shelljs'),
13
+ errHndl = require('./error-handler.js'),
14
+ clone = require('./../util/objclone').deepCopy;
15
+
16
+ const Cli = (function() {
17
+ let instance;
18
+
19
+ function finish(err, returnValue, next) {
20
+ if (next && typeof next === 'function') {
21
+ if (!returnValue) {
22
+ return setImmediate(next, err);
23
+ }
24
+ return setImmediate(next, err, returnValue);
25
+ }
26
+ return err || returnValue;
27
+ }
28
+
29
+ function init() {
30
+ const
31
+ workParentDir = path.resolve(process.env.APPDATA || process.env.HOME || process.env.USERPROFILE),
32
+ builtinFiles = {
33
+ "config": {
34
+ "dir": path.join(__dirname, "../../files", "conf"),
35
+ "file": "config.json"
36
+ },
37
+ "commands": {
38
+ "dir": path.join(__dirname, "../../files", "conf"),
39
+ "file": "command-service.json"
40
+ },
41
+ "deviceList": {
42
+ "dir": path.join(__dirname, "../../files", "conf"),
43
+ "file": "novacom-devices.json"
44
+ },
45
+ "deviceListSchema": {
46
+ "dir": path.join(__dirname, "../../files", "schema"),
47
+ "file": "NovacomDevices.schema"
48
+ },
49
+ "template": {
50
+ "dir": path.join(__dirname, "../../files", "conf"),
51
+ "file": "template.json"
52
+ },
53
+ "env": {
54
+ "dir": path.join(__dirname, "../../files", "conf"),
55
+ "file": "sdk.json"
56
+ }
57
+ },
58
+ queryPath = path.join(__dirname, "../../files/conf/query");
59
+
60
+ function _store(store, n, file) {
61
+ store[n] = JSON.parse(fs.readFileSync(file));
62
+ store[n + 'File'] = file;
63
+ }
64
+
65
+ for (const key in builtinFiles) {
66
+ const filePath = path.join(builtinFiles[key].dir, builtinFiles[key].file);
67
+ _store(this, key, filePath);
68
+ }
69
+
70
+ const
71
+ workDir = (this.config.dataDir) ? path.join(workParentDir, this.config.dataDir) : path.join(workParentDir, '.webos'),
72
+ builtinDeviceListFile = path.join(builtinFiles.deviceList.dir, builtinFiles.deviceList.file),
73
+ userDeviceListFile = path.join(workDir, builtinFiles.deviceList.file);
74
+
75
+ if (!fs.existsSync(workDir)) {
76
+ mkdirp.sync(workDir);
77
+ }
78
+
79
+ try {
80
+ const userDeviceList = JSON.parse(fs.readFileSync(userDeviceListFile)),
81
+ result = validateDevice(this.deviceListSchema, userDeviceList);
82
+
83
+ if (result && result.resultValue) {
84
+ this.deviceList = userDeviceList;
85
+ } else {
86
+ log.verbose("cli-appdata#init()", "schema check failure:", userDeviceListFile);
87
+ log.verbose("cli-appdata#init()", "result:", result);
88
+ log.verbose("cli-appdata#init()", "copy " + builtinDeviceListFile + " to " + userDeviceListFile);
89
+ shelljs.cp('-f', builtinDeviceListFile, userDeviceListFile);
90
+ }
91
+ } catch (err) {
92
+ log.verbose("cli-appdata#init()", "err:", err);
93
+ log.verbose("cli-appdata#init()", "copy " + builtinDeviceListFile + " to " + userDeviceListFile);
94
+ shelljs.cp('-f', builtinDeviceListFile, userDeviceListFile);
95
+ }
96
+
97
+ this.builtinDeviceListFile = builtinDeviceListFile;
98
+ this.deviceListFile = userDeviceListFile;
99
+ this.workDir = workDir;
100
+ this.queryPath = queryPath;
101
+ if (this.config.simulatorConfig) {
102
+ this.simulatorConfigPath = path.join(workDir, this.config.simulatorConfig);
103
+ }
104
+
105
+ return true;
106
+ }
107
+
108
+ function validateDevice(schema, data) {
109
+ let result = {
110
+ resultValue: false,
111
+ err: null
112
+ };
113
+ const schemaArray = {
114
+ "id": "test",
115
+ "type": "array",
116
+ "items": {
117
+ "$ref": "/deviceSchema"
118
+ }
119
+ };
120
+ const v = new Validator();
121
+
122
+ try {
123
+ v.addSchema(schema, "/deviceSchema");
124
+ result = v.validate(data, schemaArray);
125
+ if (result && result.errors.length > 0) {
126
+ let errMsg = "Invalid device info.";
127
+ for (const idx in result.errors) {
128
+ errMsg = errMsg.concat("\n");
129
+ let res,
130
+ errMsgLine = result.errors[idx].property + " " + result.errors[idx].message;
131
+ const regex = /instance\[*.*\]*\./g;
132
+ if ((res = regex.exec(errMsgLine)) !== null) {
133
+ errMsgLine = errMsgLine.substring(res[0].length);
134
+ }
135
+ errMsg = errMsg.concat(errMsgLine);
136
+ }
137
+ result.err = new Error(errMsg);
138
+ } else {
139
+ result.resultValue = true;
140
+ }
141
+ } catch (err) {
142
+ // ignore exception
143
+ result.err = err;
144
+ }
145
+ return result;
146
+ }
147
+
148
+ function CliAppData() {
149
+ if (instance) {
150
+ return instance;
151
+ }
152
+ instance = this;
153
+ if (!this.inialized) {
154
+ this.inialized = init.call(this);
155
+ }
156
+ }
157
+
158
+ CliAppData.prototype = {
159
+ getSimulatorConfigPath: function(next) {
160
+ return finish(null, this.simulatorConfigPath, next);
161
+ },
162
+
163
+ getPath: function(next) {
164
+ return finish(null, this.workDir, next);
165
+ },
166
+
167
+ getAppDir: function(next) {
168
+ return finish(null, path.join(__dirname, '..'), next);
169
+ },
170
+
171
+ getConfig: function(next) {
172
+ return finish(null, clone(this.config), next);
173
+ },
174
+
175
+ compareProfile: function(query, next) {
176
+ next(null, query.toLowerCase() === this.config.profile.toLowerCase());
177
+ },
178
+
179
+ compareProfileSync: function(query) {
180
+ return query.toLowerCase() === this.config.profile.toLowerCase();
181
+ },
182
+
183
+ getDeviceList: function(renewal, next) {
184
+ return finish(null, clone(this.deviceList), next);
185
+ },
186
+
187
+ getCommandService: function(next) {
188
+ return finish(null, clone(this.commands), next);
189
+ },
190
+
191
+ setDeviceList: function(deviceListData, next) {
192
+ log.info("cli-appdata#CliAppData#setDeviceList()");
193
+ try {
194
+ const result = validateDevice(this.deviceListSchema, deviceListData);
195
+ if (result && !result.resultValue) {
196
+ const err = result.err || "invalid data";
197
+ return finish(err, this.deviceList, next);
198
+ }
199
+ this.deviceList = deviceListData;
200
+ fs.writeFileSync(this.deviceListFile, JSON.stringify(this.deviceList, null, Number(4)));
201
+ } catch (err) {
202
+ return finish(err, this.deviceList, next);
203
+ }
204
+ return finish(null, this.deviceList, next);
205
+ },
206
+
207
+ setConfig: function(configData, next) {
208
+ try {
209
+ this.config = configData;
210
+ fs.writeFileSync(this.configFile, JSON.stringify(this.config, null, Number(4)) + "\n");
211
+ } catch (err) {
212
+ return finish(err, this.config, next);
213
+ }
214
+ return finish(null, this.config, next);
215
+ },
216
+
217
+ setTemplate: function(templateData, next) {
218
+ try {
219
+ this.template = templateData;
220
+ fs.writeFileSync(this.templateFile, JSON.stringify(this.template, null, Number(4)) + "\n");
221
+ } catch (err) {
222
+ return finish(err, this.template, next);
223
+ }
224
+ },
225
+
226
+ setEnv: function(envData, next) {
227
+ try {
228
+ this.env = envData;
229
+ fs.writeFileSync(this.envFile, JSON.stringify(this.env, null, Number(4)) + "\n");
230
+ } catch (err) {
231
+ return finish(err, this.env, next);
232
+ }
233
+ },
234
+
235
+ setKey: function(keyPath, next) {
236
+ try {
237
+ const dstPubkey = path.join(__dirname, "../../files/conf", "pubkey.pem");
238
+ if (fs.existsSync(dstPubkey)) {
239
+ shelljs.rm(dstPubkey);
240
+ }
241
+ if (keyPath) {
242
+ shelljs.cp(keyPath, dstPubkey);
243
+ }
244
+ } catch (err) {
245
+ return finish(errHndl.getErrMsg(err), keyPath, next);
246
+ }
247
+ return finish(null, keyPath, next);
248
+ },
249
+
250
+ setQuery: function(queryBasePath, next) {
251
+ try {
252
+ if (fs.existsSync(this.queryPath)) {
253
+ shelljs.ls(this.queryPath).forEach(function(file) {
254
+ shelljs.rm(path.join(__dirname, "../../files/conf/query",file));
255
+ });
256
+ } else {
257
+ fs.mkdirSync(this.queryPath);
258
+ }
259
+
260
+ shelljs.ls(queryBasePath+'/*.json').forEach(function(file) {
261
+ shelljs.cp(file, path.join(__dirname, "../../files/conf", "query"));
262
+ });
263
+ } catch (err) {
264
+ return finish(err, this.queryPath, next);
265
+ }
266
+ return finish(null, this.queryPath, next);
267
+ },
268
+
269
+ resetDeviceList: function(next) {
270
+ log.info("cli-appdata#CliAppData#resetDeviceList()");
271
+ try {
272
+ if (fs.existsSync(this.deviceListFile)) {
273
+ shelljs.rm(this.deviceListFile);
274
+ }
275
+
276
+ shelljs.cp(this.builtinDeviceListFile, this.deviceListFile);
277
+ this.deviceList = JSON.parse(fs.readFileSync(this.deviceListFile));
278
+ } catch (err) {
279
+ log.verbose("cli-appdata#CliAppData#resetDeviceList()", "err:", err);
280
+ return finish(err, clone(this.deviceList), next);
281
+ }
282
+ return finish(null, clone(this.deviceList), next);
283
+ }
284
+ };
285
+ return CliAppData;
286
+ }());
287
+
288
+ if (typeof module !== 'undefined' && module.exports) {
289
+ module.exports = Cli;
290
+ }