@ohos-ports/pm2 0.0.0-stage → 7.1.2-beta.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 (267) hide show
  1. package/CHANGELOG.md +2607 -0
  2. package/CONTRIBUTING.md +124 -0
  3. package/GNU-AGPL-3.0.txt +665 -0
  4. package/LICENSE +1 -0
  5. package/README.md +255 -2
  6. package/SECURITY.md +41 -0
  7. package/bin/pm2 +3 -0
  8. package/bin/pm2-dev +3 -0
  9. package/bin/pm2-docker +3 -0
  10. package/bin/pm2-runtime +3 -0
  11. package/bin/pm2.ps1 +3 -0
  12. package/constants.js +113 -0
  13. package/index.js +12 -0
  14. package/lib/API/Configuration.js +212 -0
  15. package/lib/API/Containerizer.js +335 -0
  16. package/lib/API/Dashboard.js +726 -0
  17. package/lib/API/Deploy.js +117 -0
  18. package/lib/API/Extra.js +798 -0
  19. package/lib/API/ExtraMgmt/Docker.js +30 -0
  20. package/lib/API/Log.js +388 -0
  21. package/lib/API/LogManagement.js +351 -0
  22. package/lib/API/Modules/LOCAL.js +122 -0
  23. package/lib/API/Modules/Modularizer.js +148 -0
  24. package/lib/API/Modules/NPM.js +443 -0
  25. package/lib/API/Modules/TAR.js +368 -0
  26. package/lib/API/Modules/flagExt.js +46 -0
  27. package/lib/API/Modules/index.js +120 -0
  28. package/lib/API/Monit.js +247 -0
  29. package/lib/API/Serve.js +497 -0
  30. package/lib/API/Startup.js +630 -0
  31. package/lib/API/UX/helpers.js +213 -0
  32. package/lib/API/UX/index.js +9 -0
  33. package/lib/API/UX/pm2-describe.js +197 -0
  34. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  35. package/lib/API/UX/pm2-ls.js +581 -0
  36. package/lib/API/Version.js +383 -0
  37. package/lib/API/interpreter.json +12 -0
  38. package/lib/API/pm2-plus/PM2IO.js +381 -0
  39. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
  40. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
  41. package/lib/API/pm2-plus/helpers.js +97 -0
  42. package/lib/API/pm2-plus/link.js +126 -0
  43. package/lib/API/pm2-plus/pres/motd +16 -0
  44. package/lib/API/pm2-plus/pres/motd.update +26 -0
  45. package/lib/API/pm2-plus/pres/welcome +28 -0
  46. package/lib/API/pm2-plus/process-selector.js +52 -0
  47. package/lib/API/schema.json +375 -0
  48. package/lib/API.js +1980 -0
  49. package/lib/Client.js +793 -0
  50. package/lib/Common.js +949 -0
  51. package/lib/Configuration.js +309 -0
  52. package/lib/Daemon.js +468 -0
  53. package/lib/Event.js +37 -0
  54. package/lib/God/ActionMethods.js +955 -0
  55. package/lib/God/ClusterMode.js +192 -0
  56. package/lib/God/ForkMode.js +205 -0
  57. package/lib/God/Methods.js +292 -0
  58. package/lib/God/Reload.js +300 -0
  59. package/lib/God/StdioLogger.js +239 -0
  60. package/lib/God.js +653 -0
  61. package/lib/HttpInterface.js +75 -0
  62. package/lib/OtelManager.js +74 -0
  63. package/lib/ProcessContainer.js +321 -0
  64. package/lib/ProcessContainerBun.js +355 -0
  65. package/lib/ProcessContainerFork.js +42 -0
  66. package/lib/ProcessContainerForkBun.js +31 -0
  67. package/lib/ProcessUtils.js +74 -0
  68. package/lib/TreeKill.js +111 -0
  69. package/lib/Utility.js +304 -0
  70. package/lib/VersionCheck.js +46 -0
  71. package/lib/Watcher.js +117 -0
  72. package/lib/Worker.js +200 -0
  73. package/lib/binaries/CLI.js +1092 -0
  74. package/lib/binaries/DevCLI.js +183 -0
  75. package/lib/binaries/Runtime.js +101 -0
  76. package/lib/binaries/Runtime4Docker.js +192 -0
  77. package/lib/completion.js +229 -0
  78. package/lib/completion.sh +40 -0
  79. package/lib/motd +36 -0
  80. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  81. package/lib/templates/ecosystem-es.tpl +24 -0
  82. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  83. package/lib/templates/ecosystem-simple.tpl +6 -0
  84. package/lib/templates/ecosystem.tpl +22 -0
  85. package/lib/templates/init-scripts/launchd.tpl +35 -0
  86. package/lib/templates/init-scripts/openrc.tpl +52 -0
  87. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  88. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  89. package/lib/templates/init-scripts/rcd.tpl +44 -0
  90. package/lib/templates/init-scripts/smf.tpl +43 -0
  91. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  92. package/lib/templates/init-scripts/systemd.tpl +22 -0
  93. package/lib/templates/init-scripts/upstart.tpl +103 -0
  94. package/lib/templates/logrotate.d/pm2 +10 -0
  95. package/lib/templates/sample-apps/http-server/README.md +14 -0
  96. package/lib/templates/sample-apps/http-server/api.js +9 -0
  97. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  98. package/lib/templates/sample-apps/http-server/package.json +11 -0
  99. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  100. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  101. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  102. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  103. package/lib/templates/sample-apps/python-app/README.md +4 -0
  104. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  105. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  106. package/lib/templates/sample-apps/python-app/package.json +11 -0
  107. package/lib/tools/Config.js +251 -0
  108. package/lib/tools/SysMetrics.js +375 -0
  109. package/lib/tools/copydirSync.js +101 -0
  110. package/lib/tools/deleteFolderRecursive.js +19 -0
  111. package/lib/tools/find-package-json.js +74 -0
  112. package/lib/tools/fmt.js +72 -0
  113. package/lib/tools/interpreter.js +88 -0
  114. package/lib/tools/isbinaryfile.js +94 -0
  115. package/lib/tools/json5.js +752 -0
  116. package/lib/tools/multimeter/bar.js +111 -0
  117. package/lib/tools/multimeter/charm/encode.js +18 -0
  118. package/lib/tools/multimeter/charm/index.js +305 -0
  119. package/lib/tools/multimeter/index.js +76 -0
  120. package/lib/tools/open.js +56 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/pm2-config-keys.js +165 -0
  123. package/lib/tools/prompt.js +76 -0
  124. package/lib/tools/serviceStop.js +149 -0
  125. package/lib/tools/sexec.js +55 -0
  126. package/lib/tools/treeify.js +113 -0
  127. package/lib/tools/typestrip.js +55 -0
  128. package/lib/tools/which.js +120 -0
  129. package/lib/tools/xdg-open +861 -0
  130. package/modules/blessed/LICENSE +20 -0
  131. package/modules/blessed/README.md +34 -0
  132. package/modules/blessed/index.js +1 -0
  133. package/modules/blessed/lib/alias.js +526 -0
  134. package/modules/blessed/lib/blessed.js +32 -0
  135. package/modules/blessed/lib/colors.js +530 -0
  136. package/modules/blessed/lib/events.js +189 -0
  137. package/modules/blessed/lib/gpmclient.js +221 -0
  138. package/modules/blessed/lib/helpers.js +165 -0
  139. package/modules/blessed/lib/keys.js +339 -0
  140. package/modules/blessed/lib/program.js +4297 -0
  141. package/modules/blessed/lib/tput.js +3022 -0
  142. package/modules/blessed/lib/unicode.js +885 -0
  143. package/modules/blessed/lib/widget.js +32 -0
  144. package/modules/blessed/lib/widgets/box.js +34 -0
  145. package/modules/blessed/lib/widgets/element.js +2570 -0
  146. package/modules/blessed/lib/widgets/list.js +599 -0
  147. package/modules/blessed/lib/widgets/log.js +83 -0
  148. package/modules/blessed/lib/widgets/node.js +282 -0
  149. package/modules/blessed/lib/widgets/screen.js +2298 -0
  150. package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
  151. package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
  152. package/modules/blessed/lib/widgets/text.js +35 -0
  153. package/modules/blessed/usr/linux +0 -0
  154. package/modules/blessed/usr/windows-ansi +0 -0
  155. package/modules/blessed/usr/xterm +0 -0
  156. package/modules/blessed/usr/xterm-256color +0 -0
  157. package/modules/blessed/usr/xterm.termcap +243 -0
  158. package/modules/chokidar/LICENSE +21 -0
  159. package/modules/chokidar/VENDORED.md +76 -0
  160. package/modules/chokidar/index.js +979 -0
  161. package/modules/chokidar/lib/brace-expand.js +180 -0
  162. package/modules/chokidar/lib/constants.js +66 -0
  163. package/modules/chokidar/lib/fsevents-handler.js +526 -0
  164. package/modules/chokidar/lib/nodefs-handler.js +654 -0
  165. package/modules/fclone.js +49 -0
  166. package/modules/pm2-axon/index.js +2 -0
  167. package/modules/pm2-axon/lib/configurable/History.md +5 -0
  168. package/modules/pm2-axon/lib/configurable/Makefile +7 -0
  169. package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
  170. package/modules/pm2-axon/lib/configurable/index.js +2 -0
  171. package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
  172. package/modules/pm2-axon/lib/configurable/package.json +68 -0
  173. package/modules/pm2-axon/lib/index.js +44 -0
  174. package/modules/pm2-axon/lib/plugins/queue.js +71 -0
  175. package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
  176. package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
  177. package/modules/pm2-axon/lib/sockets/pub.js +77 -0
  178. package/modules/pm2-axon/lib/sockets/pull.js +37 -0
  179. package/modules/pm2-axon/lib/sockets/push.js +32 -0
  180. package/modules/pm2-axon/lib/sockets/rep.js +73 -0
  181. package/modules/pm2-axon/lib/sockets/req.js +101 -0
  182. package/modules/pm2-axon/lib/sockets/sock.js +430 -0
  183. package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
  184. package/modules/pm2-axon/lib/sockets/sub.js +147 -0
  185. package/modules/pm2-axon/lib/utils.js +19 -0
  186. package/modules/pm2-axon-rpc/index.js +3 -0
  187. package/modules/pm2-axon-rpc/lib/client.js +63 -0
  188. package/modules/pm2-axon-rpc/lib/server.js +139 -0
  189. package/modules/pm2-io-agent/LICENSE +661 -0
  190. package/modules/pm2-io-agent/README.md +62 -0
  191. package/modules/pm2-io-agent/config.js +30 -0
  192. package/modules/pm2-io-agent/constants.js +118 -0
  193. package/modules/pm2-io-agent/index.js +2 -0
  194. package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
  195. package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
  196. package/modules/pm2-io-agent/src/PM2Client.js +112 -0
  197. package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
  198. package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
  199. package/modules/pm2-io-agent/src/Utility.js +392 -0
  200. package/modules/pm2-io-agent/src/WatchDog.js +70 -0
  201. package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
  202. package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
  203. package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
  204. package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
  205. package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
  206. package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
  207. package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
  208. package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
  209. package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
  210. package/modules/pm2-io-agent/src/utils/units.js +10 -0
  211. package/modules/pm2-io-bpm/configuration.js +141 -0
  212. package/modules/pm2-io-bpm/constants.js +10 -0
  213. package/modules/pm2-io-bpm/featureManager.js +110 -0
  214. package/modules/pm2-io-bpm/features/dependencies.js +48 -0
  215. package/modules/pm2-io-bpm/features/events.js +50 -0
  216. package/modules/pm2-io-bpm/features/metrics.js +102 -0
  217. package/modules/pm2-io-bpm/features/notify.js +238 -0
  218. package/modules/pm2-io-bpm/features/profiling.js +70 -0
  219. package/modules/pm2-io-bpm/features/tracing.js +174 -0
  220. package/modules/pm2-io-bpm/index.js +11 -0
  221. package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
  222. package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
  223. package/modules/pm2-io-bpm/metrics/network.js +132 -0
  224. package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
  225. package/modules/pm2-io-bpm/metrics/v8.js +104 -0
  226. package/modules/pm2-io-bpm/otel/constants.js +7 -0
  227. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
  228. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
  229. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
  230. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
  231. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
  232. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
  233. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
  234. package/modules/pm2-io-bpm/pmx.js +380 -0
  235. package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
  236. package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
  237. package/modules/pm2-io-bpm/serviceManager.js +21 -0
  238. package/modules/pm2-io-bpm/services/actions.js +159 -0
  239. package/modules/pm2-io-bpm/services/inspector.js +43 -0
  240. package/modules/pm2-io-bpm/services/metrics.js +186 -0
  241. package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
  242. package/modules/pm2-io-bpm/services/transport.js +11 -0
  243. package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
  244. package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
  245. package/modules/pm2-io-bpm/utils/EDS.js +113 -0
  246. package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
  247. package/modules/pm2-io-bpm/utils/autocast.js +61 -0
  248. package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
  249. package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
  250. package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
  251. package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
  252. package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
  253. package/modules/pm2-io-bpm/utils/module.js +67 -0
  254. package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
  255. package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
  256. package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
  257. package/modules/pm2-io-bpm/utils/units.js +16 -0
  258. package/modules/vizion/LICENSE +13 -0
  259. package/modules/vizion/README.md +133 -0
  260. package/modules/vizion/index.js +1 -0
  261. package/modules/vizion/lib/git/git.js +247 -0
  262. package/modules/vizion/lib/vizion.js +52 -0
  263. package/package.json +170 -4
  264. package/paths.js +121 -0
  265. package/pm2 +11 -0
  266. package/types/index.d.ts +732 -0
  267. package/types/tsconfig.json +14 -0
@@ -0,0 +1,45 @@
1
+
2
+ # pm2 custom metrics boilerplate
3
+
4
+ In this boilerplate you will discover a working example of custom metrics feature.
5
+
6
+ Metrics covered are:
7
+ - io.metric
8
+ - io.counter
9
+ - io.meter
10
+ - io.histogram
11
+
12
+ ## What is Custom Metrics?
13
+
14
+ Custom metrics is a powerfull way to get more visibility from a running application. It will allow you to monitor in realtime the current value of variables, know the number of actions being processed, measure latency and much more.
15
+
16
+ Once you have plugged in some custom metrics you will be able to monitor their value in realtime with
17
+
18
+ `pm2 monit`
19
+
20
+ Or
21
+
22
+ `pm2 describe`
23
+
24
+ Or on the PM2+ Web interface
25
+
26
+ `pm2 open`
27
+
28
+ ## Example
29
+
30
+ ```javascript
31
+ const io = require('@pm2/io')
32
+
33
+ const currentReq = io.counter({
34
+ name: 'CM: Current Processing',
35
+ type: 'counter'
36
+ })
37
+
38
+ setInterval(() => {
39
+ currentReq.inc()
40
+ }, 1000)
41
+ ```
42
+
43
+ ## Documentation
44
+
45
+ https://doc.pm2.io/en/plus/guide/custom-metrics/
@@ -0,0 +1,66 @@
1
+
2
+ const io = require('@pm2/io')
3
+
4
+ // Straight Metric
5
+ var user_count = 10
6
+
7
+ const users = io.metric({
8
+ name: 'CM: Realtime user',
9
+ value: () => {
10
+ return user_count
11
+ }
12
+ })
13
+
14
+ // or users.set(user_count)
15
+
16
+ // Counter (.inc() .dec())
17
+ const currentReq = io.counter({
18
+ name: 'CM: Current Processing',
19
+ type: 'counter'
20
+ })
21
+
22
+ setInterval(() => {
23
+ currentReq.inc()
24
+ }, 1000)
25
+
26
+ // Meter
27
+ const reqsec = io.meter({
28
+ name: 'CM: req/sec'
29
+ })
30
+
31
+ setInterval(() => {
32
+ reqsec.mark()
33
+ }, 100)
34
+
35
+
36
+ // Histogram
37
+ const latency = io.histogram({
38
+ name: 'CM: latency'
39
+ });
40
+
41
+ var latencyValue = 0;
42
+
43
+ setInterval(() => {
44
+ latencyValue = Math.round(Math.random() * 100);
45
+ latency.update(latencyValue);
46
+ }, 100)
47
+
48
+
49
+ ////////////////////
50
+ // Custom Actions //
51
+ ////////////////////
52
+
53
+ io.action('add user', (done) => {
54
+ user_count++
55
+ done({success:true})
56
+ })
57
+
58
+ io.action('remove user', (done) => {
59
+ user_count++
60
+ done({success:true})
61
+ })
62
+
63
+ io.action('with params', (arg, done) => {
64
+ console.log(arg)
65
+ done({success:arg})
66
+ })
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ apps : [{
3
+ name: 'Custom Metrics',
4
+ script: 'custom-metrics.js',
5
+ env: {
6
+ NODE_ENV: 'development'
7
+ },
8
+ env_production: {
9
+ NODE_ENV: 'production'
10
+ }
11
+ }]
12
+ };
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "pm2-plus-custom-metrics",
3
+ "version": "1.0.0",
4
+ "description": "Example that shows how to use pm2+ custom metrics",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC"
11
+ }
@@ -0,0 +1,4 @@
1
+
2
+ # Managing a Python Application
3
+
4
+ On this boilerlate you will see how you can start a Python application with PM2.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/python
2
+ import time
3
+
4
+ while 1:
5
+ print("Start : %s" % time.ctime())
6
+ print("second line")
7
+ time.sleep(1)
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ apps : [{
3
+ name: 'API',
4
+ script: 'echo.py',
5
+ env: {
6
+ NODE_ENV: 'development'
7
+ },
8
+ env_production: {
9
+ NODE_ENV: 'production'
10
+ }
11
+ }]
12
+ };
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "python-app",
3
+ "version": "1.0.0",
4
+ "description": "Example on how to manage a Python application with PM2",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC"
11
+ }
@@ -0,0 +1,251 @@
1
+ /**
2
+ * Copyright 2013-present the PM2 project authors. All rights reserved.
3
+ * Use of this source code is governed by a license that
4
+ * can be found in the LICENSE file.
5
+ */
6
+ var util = require('util');
7
+
8
+ /**
9
+ * Validator of configured file / commander options.
10
+ */
11
+ var Config = module.exports = {
12
+ _errMsgs: {
13
+ 'require': '"%s" is required',
14
+ 'type' : 'Expect "%s" to be a typeof %s, but now is %s',
15
+ 'regex' : 'Verify "%s" with regex failed, %s',
16
+ 'max' : 'The maximum of "%s" is %s, but now is %s',
17
+ 'min' : 'The minimum of "%s" is %s, but now is %s'
18
+ },
19
+ /**
20
+ * Schema definition.
21
+ * @returns {exports|*}
22
+ */
23
+ get schema(){
24
+ // Cache.
25
+ if (this._schema) {
26
+ return this._schema;
27
+ }
28
+ // Render aliases.
29
+ this._schema = require('../API/schema');
30
+ for (var k in this._schema) {
31
+ if (k.indexOf('\\') > 0) {
32
+ continue;
33
+ }
34
+ var aliases = [
35
+ k.split('_').map(function(n, i){
36
+ if (i != 0 && n && n.length > 1) {
37
+ return n[0].toUpperCase() + n.slice(1);
38
+ }
39
+ return n;
40
+ }).join('')
41
+ ];
42
+
43
+ if (this._schema[k].alias && Array.isArray(this._schema[k].alias)) {
44
+ // If multiple aliases, merge
45
+ this._schema[k].alias.forEach(function(alias) {
46
+ aliases.splice(0, 0, alias);
47
+ });
48
+ }
49
+ else if (this._schema[k].alias)
50
+ aliases.splice(0, 0, this._schema[k].alias);
51
+
52
+ this._schema[k].alias = aliases;
53
+ }
54
+ return this._schema;
55
+ }
56
+ };
57
+
58
+ /**
59
+ * Filter / Alias options
60
+ */
61
+ Config.filterOptions = function(cmd) {
62
+ var conf = {};
63
+ var schema = this.schema;
64
+
65
+ for (var key in schema) {
66
+ var aliases = schema[key].alias;
67
+ aliases && aliases.forEach(function(alias){
68
+ if (typeof(cmd[alias]) !== 'undefined') {
69
+ conf[key] || (conf[key] = cmd[alias]);
70
+ }
71
+ });
72
+ }
73
+
74
+ return conf;
75
+ };
76
+
77
+ /**
78
+ * Verify JSON configurations.
79
+ * @param {Object} json
80
+ * @returns {{errors: Array, config: {}}}
81
+ */
82
+ Config.validateJSON = function(json){
83
+ // clone config
84
+ var conf = Object.assign({}, json),
85
+ res = {};
86
+ this._errors = [];
87
+
88
+ var regexKeys = {}, defines = this.schema;
89
+
90
+ for (var sk in defines) {
91
+ // Pick up RegExp keys.
92
+ if (sk.indexOf('\\') >= 0) {
93
+ regexKeys[sk] = false;
94
+ continue;
95
+ }
96
+
97
+ var aliases = defines[sk].alias;
98
+
99
+ aliases && aliases.forEach(function(alias){
100
+ conf[sk] || (conf[sk] = json[alias]);
101
+ })
102
+
103
+ var val = conf[sk];
104
+ delete conf[sk];
105
+
106
+ // Validate key-value pairs.
107
+ if (val === undefined ||
108
+ val === null ||
109
+ ((val = this._valid(sk, val)) === null)) {
110
+
111
+ // If value is not defined
112
+ // Set default value (via schema.json)
113
+ if (typeof(defines[sk].default) !== 'undefined')
114
+ res[sk] = defines[sk].default;
115
+ continue;
116
+ }
117
+ //console.log(sk, val, val === null, val === undefined);
118
+ res[sk] = val;
119
+ }
120
+
121
+ // Validate RegExp values.
122
+ var hasRegexKey = false;
123
+ for (var k in regexKeys) {
124
+ hasRegexKey = true;
125
+ regexKeys[k] = new RegExp(k);
126
+ }
127
+ if (hasRegexKey) {
128
+ for (var k in conf) {
129
+ for (var rk in regexKeys) {
130
+ if (regexKeys[rk].test(k))
131
+ if (this._valid(k, conf[k], defines[rk])) {
132
+ res[k] = conf[k];
133
+ delete conf[k];
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ return {errors: this._errors, config: res};
140
+ };
141
+
142
+ /**
143
+ * Validate key-value pairs by specific schema
144
+ * @param {String} key
145
+ * @param {Mixed} value
146
+ * @param {Object} sch
147
+ * @returns {*}
148
+ * @private
149
+ */
150
+ Config._valid = function(key, value, sch){
151
+ var sch = sch || this.schema[key],
152
+ scht = typeof sch.type == 'string' ? [sch.type] : sch.type;
153
+
154
+ // Required value.
155
+ var undef = typeof value == 'undefined';
156
+ if(this._error(sch.require && undef, 'require', key)){
157
+ return null;
158
+ }
159
+
160
+ // If undefined, make a break.
161
+ if (undef) {
162
+ return null;
163
+ }
164
+
165
+ // Wrap schema types.
166
+ scht = scht.map(function(t){
167
+ return '[object ' + t[0].toUpperCase() + t.slice(1) + ']'
168
+ });
169
+
170
+ // Typeof value.
171
+ var type = Object.prototype.toString.call(value), nt = '[object Number]';
172
+
173
+ // Auto parse Number
174
+ if (type != '[object Boolean]' && scht.indexOf(nt) >= 0 && !isNaN(value)) {
175
+ value = parseFloat(value);
176
+ type = nt;
177
+ }
178
+
179
+ // Verify types.
180
+ if (this._error(!~scht.indexOf(type), 'type', key, scht.join(' / '), type)) {
181
+ return null;
182
+ }
183
+
184
+ // Verify RegExp if exists.
185
+ if (this._error(type == '[object String]' && sch.regex && !(new RegExp(sch.regex)).test(value),
186
+ 'regex', key, sch.desc || ('should match ' + sch.regex))) {
187
+ return null;
188
+ }
189
+
190
+ // Verify maximum / minimum of Number value.
191
+ if (type == '[object Number]') {
192
+ if (this._error(typeof sch.max != 'undefined' && value > sch.max, 'max', key, sch.max, value)) {
193
+ return null;
194
+ }
195
+ if (this._error(typeof sch.min != 'undefined' && value < sch.min, 'min', key, sch.min, value)) {
196
+ return null;
197
+ }
198
+ }
199
+
200
+ // If first type is Array, but current is String, try to split them.
201
+ if(scht.length > 1 && type != scht[0] && type == '[object String]'){
202
+ if(scht[0] == '[object Array]') {
203
+ value = (value.match(/[^\s"'=]+="[^"]*"|[^\s"'=]+='[^']*'|"([^"]*)"|'([^']*)'|\S+/g) || [])
204
+ .map(function(v) {
205
+ if (v && v[0] === '"' && v[v.length - 1] === '"') return v.slice(1, -1);
206
+ if (v && v[0] === "'" && v[v.length - 1] === "'") return v.slice(1, -1);
207
+ return v;
208
+ })
209
+ .filter(function(v) {
210
+ return v && v.trim();
211
+ });
212
+ }
213
+ }
214
+
215
+ // Custom types: sbyte && stime.
216
+ if(sch.ext_type && type == '[object String]' && value.length >= 2) {
217
+ var seed = {
218
+ 'sbyte': {
219
+ 'G': 1024 * 1024 * 1024,
220
+ 'M': 1024 * 1024,
221
+ 'K': 1024
222
+ },
223
+ 'stime': {
224
+ 'h': 60 * 60 * 1000,
225
+ 'm': 60 * 1000,
226
+ 's': 1000
227
+ }
228
+ }[sch.ext_type];
229
+
230
+ if(seed){
231
+ value = parseFloat(value.slice(0, -1)) * (seed[value.slice(-1)]);
232
+ }
233
+ }
234
+ return value;
235
+ };
236
+
237
+ /**
238
+ * Wrap errors.
239
+ * @param {Boolean} possible A value indicates whether it is an error or not.
240
+ * @param {String} type
241
+ * @returns {*}
242
+ * @private
243
+ */
244
+ Config._error = function(possible, type){
245
+ if (possible) {
246
+ var args = Array.prototype.slice.call(arguments);
247
+ args.splice(0, 2, this._errMsgs[type]);
248
+ this._errors && this._errors.push(util.format.apply(null, args));
249
+ }
250
+ return possible;
251
+ }