@codiac.io/codiac-cli 1.3.67 → 1.3.69

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 (272) hide show
  1. package/README.md +1 -1
  2. package/dist/apis/api-plus-local/api-client-plus-local.d.ts +12 -4
  3. package/dist/apis/api-plus-local/api-client-plus-local.js +121 -92
  4. package/dist/apis/api-plus-local/api-client-plus-local.js.map +1 -1
  5. package/dist/apis/api-plus-local/asset-where-entity-merger.d.ts +4 -2
  6. package/dist/apis/api-plus-local/asset-where-entity-merger.js +17 -8
  7. package/dist/apis/api-plus-local/asset-where-entity-merger.js.map +1 -1
  8. package/dist/apis/api-plus-local/enterprise-state-entity-merger.d.ts +3 -2
  9. package/dist/apis/api-plus-local/enterprise-state-entity-merger.js +13 -2
  10. package/dist/apis/api-plus-local/enterprise-state-entity-merger.js.map +1 -1
  11. package/dist/apis/api-plus-local/root-map-entity-merger.d.ts +7 -3
  12. package/dist/apis/api-plus-local/root-map-entity-merger.js +19 -8
  13. package/dist/apis/api-plus-local/root-map-entity-merger.js.map +1 -1
  14. package/dist/apis/codiac-api/gen/contracts.d.ts +38 -4
  15. package/dist/apis/codiac-api/gen/contracts.js +2 -0
  16. package/dist/apis/codiac-api/gen/contracts.js.map +1 -1
  17. package/dist/apis/codiac-relay/common/api-user-creds.d.ts +2 -2
  18. package/dist/apis/codiac-relay/common/api-user-creds.js +2 -1
  19. package/dist/apis/codiac-relay/common/api-user-creds.js.map +1 -1
  20. package/dist/apis/codiac-relay/gen/client.d.ts +5 -1
  21. package/dist/apis/codiac-relay/gen/client.js +16 -0
  22. package/dist/apis/codiac-relay/gen/client.js.map +1 -1
  23. package/dist/apis/codiac-relay/gen/contracts.d.ts +129 -2
  24. package/dist/apis/codiac-relay/gen/contracts.js +12 -1
  25. package/dist/apis/codiac-relay/gen/contracts.js.map +1 -1
  26. package/dist/apis/nats/nats-client.d.ts +1 -0
  27. package/dist/apis/nats/nats-client.js +8 -4
  28. package/dist/apis/nats/nats-client.js.map +1 -1
  29. package/dist/cli-common/argument-manager.d.ts +1 -1
  30. package/dist/cli-common/command-base.d.ts +6 -4
  31. package/dist/cli-common/command-base.js +32 -22
  32. package/dist/cli-common/command-base.js.map +1 -1
  33. package/dist/codiac-constants.d.ts +2 -0
  34. package/dist/codiac-constants.js +2 -0
  35. package/dist/codiac-constants.js.map +1 -1
  36. package/dist/command-base-enterprise.d.ts +2 -1
  37. package/dist/command-base-enterprise.js +10 -7
  38. package/dist/command-base-enterprise.js.map +1 -1
  39. package/dist/command-base-tenantless.d.ts +2 -1
  40. package/dist/command-base-tenantless.js +12 -10
  41. package/dist/command-base-tenantless.js.map +1 -1
  42. package/dist/commands/asset/create.d.ts +2 -8
  43. package/dist/commands/asset/create.js +24 -4
  44. package/dist/commands/asset/create.js.map +1 -1
  45. package/dist/commands/asset/deploy.d.ts +8 -4
  46. package/dist/commands/asset/deploy.js +58 -29
  47. package/dist/commands/asset/deploy.js.map +1 -1
  48. package/dist/commands/asset/entrypoint/delete.js +3 -3
  49. package/dist/commands/asset/entrypoint/delete.js.map +1 -1
  50. package/dist/commands/asset/entrypoint/set.js +3 -3
  51. package/dist/commands/asset/entrypoint/set.js.map +1 -1
  52. package/dist/commands/asset/list.d.ts +2 -0
  53. package/dist/commands/asset/list.js +4 -0
  54. package/dist/commands/asset/list.js.map +1 -1
  55. package/dist/commands/asset/view.d.ts +4 -4
  56. package/dist/commands/asset/view.js +3 -2
  57. package/dist/commands/asset/view.js.map +1 -1
  58. package/dist/commands/cabinet/create.d.ts +2 -1
  59. package/dist/commands/cabinet/create.js +15 -2
  60. package/dist/commands/cabinet/create.js.map +1 -1
  61. package/dist/commands/cabinet/obliterate.d.ts +3 -1
  62. package/dist/commands/cabinet/obliterate.js +6 -2
  63. package/dist/commands/cabinet/obliterate.js.map +1 -1
  64. package/dist/commands/cluster/start.d.ts +37 -0
  65. package/dist/commands/cluster/start.js +191 -0
  66. package/dist/commands/cluster/start.js.map +1 -0
  67. package/dist/commands/cluster/stop.d.ts +36 -0
  68. package/dist/commands/cluster/stop.js +190 -0
  69. package/dist/commands/cluster/stop.js.map +1 -0
  70. package/dist/commands/config/add.d.ts +3 -2
  71. package/dist/commands/config/add.js +8 -25
  72. package/dist/commands/config/add.js.map +1 -1
  73. package/dist/commands/config/delete.js +7 -4
  74. package/dist/commands/config/delete.js.map +1 -1
  75. package/dist/commands/config/settings/get.js +7 -4
  76. package/dist/commands/config/settings/get.js.map +1 -1
  77. package/dist/commands/config/view.d.ts +0 -1
  78. package/dist/commands/config/view.js +5 -39
  79. package/dist/commands/config/view.js.map +1 -1
  80. package/dist/commands/environment/cluster/attach.js +15 -4
  81. package/dist/commands/environment/cluster/attach.js.map +1 -1
  82. package/dist/commands/environment/cluster/detach.js +6 -1
  83. package/dist/commands/environment/cluster/detach.js.map +1 -1
  84. package/dist/commands/environment/create.d.ts +1 -0
  85. package/dist/commands/environment/create.js +22 -3
  86. package/dist/commands/environment/create.js.map +1 -1
  87. package/dist/commands/environment/list.d.ts +3 -0
  88. package/dist/commands/environment/list.js +20 -7
  89. package/dist/commands/environment/list.js.map +1 -1
  90. package/dist/commands/filestore/capture.js.map +1 -1
  91. package/dist/commands/imageRegistry/capture.js +4 -0
  92. package/dist/commands/imageRegistry/capture.js.map +1 -1
  93. package/dist/commands/imageRegistry/list.d.ts +29 -0
  94. package/dist/commands/imageRegistry/list.js +108 -0
  95. package/dist/commands/imageRegistry/list.js.map +1 -0
  96. package/dist/commands/init.js +4 -4
  97. package/dist/commands/init.js.map +1 -1
  98. package/dist/commands/noc/cluster/capture.d.ts +0 -2
  99. package/dist/commands/noc/cluster/capture.js +2 -9
  100. package/dist/commands/noc/cluster/capture.js.map +1 -1
  101. package/dist/commands/noc/cluster/create.d.ts +20 -0
  102. package/dist/commands/noc/cluster/create.js +244 -17
  103. package/dist/commands/noc/cluster/create.js.map +1 -1
  104. package/dist/commands/noc/cluster/destroy.d.ts +4 -0
  105. package/dist/commands/noc/cluster/destroy.js +38 -9
  106. package/dist/commands/noc/cluster/destroy.js.map +1 -1
  107. package/dist/commands/noc/cluster/forget.d.ts +2 -1
  108. package/dist/commands/noc/cluster/forget.js +20 -3
  109. package/dist/commands/noc/cluster/forget.js.map +1 -1
  110. package/dist/commands/noc/cluster/list.js +6 -2
  111. package/dist/commands/noc/cluster/list.js.map +1 -1
  112. package/dist/message-contracts/terminal-command-request.d.ts +66 -0
  113. package/dist/message-contracts/terminal-command-request.js +13 -0
  114. package/dist/message-contracts/terminal-command-request.js.map +1 -0
  115. package/dist/ops/LocalOps.d.ts +1 -1
  116. package/dist/ops/LocalOps.js +4 -4
  117. package/dist/ops/LocalOps.js.map +1 -1
  118. package/dist/ops/entities/cli-config.d.ts +2 -1
  119. package/dist/ops/entities/cli-config.js +3 -2
  120. package/dist/ops/entities/cli-config.js.map +1 -1
  121. package/dist/ops/index.d.ts +0 -3
  122. package/dist/ops/index.js +3 -3
  123. package/dist/ops/index.js.map +1 -1
  124. package/dist/ops/k8s/k8s-utils.js.map +1 -1
  125. package/dist/ops/scaffolding/base-scaffolding.d.ts +1 -1
  126. package/dist/ops/scaffolding/base-scaffolding.js +2 -1
  127. package/dist/ops/scaffolding/base-scaffolding.js.map +1 -1
  128. package/dist/ops/scaffolding/codiac-api-scaffolding.js.map +1 -1
  129. package/dist/ops/scaffolding/typescript-scaffolding.d.ts +2 -1
  130. package/dist/ops/scaffolding/typescript-scaffolding.js +2 -1
  131. package/dist/ops/scaffolding/typescript-scaffolding.js.map +1 -1
  132. package/dist/ops/terminal-command-runner/local-command-runner.d.ts +41 -0
  133. package/dist/ops/terminal-command-runner/local-command-runner.js +146 -0
  134. package/dist/ops/terminal-command-runner/local-command-runner.js.map +1 -0
  135. package/dist/ops/terminal-utils.d.ts +56 -0
  136. package/dist/ops/terminal-utils.js +721 -0
  137. package/dist/ops/terminal-utils.js.map +1 -0
  138. package/dist/relay/relay-container.js +4 -1
  139. package/dist/relay/relay-container.js.map +1 -1
  140. package/dist/relay/wsclient/relay-websocket-client.d.ts +3 -0
  141. package/dist/relay/wsclient/relay-websocket-client.js +9 -0
  142. package/dist/relay/wsclient/relay-websocket-client.js.map +1 -1
  143. package/dist/sdk/codiac-sdk-for-cli.d.ts +29 -0
  144. package/dist/sdk/codiac-sdk-for-cli.js +83 -0
  145. package/dist/sdk/codiac-sdk-for-cli.js.map +1 -0
  146. package/dist/sdk/i-cluster-configurator.js.map +1 -0
  147. package/dist/sdk/i-file-store-configurator.js.map +1 -0
  148. package/dist/{uilogic → sdk}/i-login-configurator.d.ts +10 -3
  149. package/dist/sdk/i-login-configurator.js.map +1 -0
  150. package/dist/sdk/i-registry-configurator.js.map +1 -0
  151. package/dist/sdk/i-secret-store-configurator.js.map +1 -0
  152. package/dist/sdk/index.d.ts +7 -0
  153. package/dist/sdk/index.js +11 -0
  154. package/dist/sdk/index.js.map +1 -0
  155. package/dist/sdk-adapters/artifact-hub/artifact-hub-cli-adapter.d.ts +6 -0
  156. package/dist/sdk-adapters/artifact-hub/artifact-hub-cli-adapter.js +16 -0
  157. package/dist/sdk-adapters/artifact-hub/artifact-hub-cli-adapter.js.map +1 -0
  158. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/artifact-hub}/helm-resource-configurator.d.ts +4 -4
  159. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/artifact-hub}/helm-resource-configurator.js +5 -1
  160. package/dist/sdk-adapters/artifact-hub/helm-resource-configurator.js.map +1 -0
  161. package/dist/sdk-adapters/artifact-hub/index.d.ts +2 -0
  162. package/dist/sdk-adapters/artifact-hub/index.js +6 -0
  163. package/dist/sdk-adapters/artifact-hub/index.js.map +1 -0
  164. package/dist/sdk-adapters/aws/aws-cli-adapter.d.ts +6 -0
  165. package/dist/sdk-adapters/aws/aws-cli-adapter.js +20 -0
  166. package/dist/sdk-adapters/aws/aws-cli-adapter.js.map +1 -0
  167. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/aws}/aws-resource-configurator.d.ts +4 -7
  168. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/aws}/aws-resource-configurator.js +8 -2
  169. package/dist/sdk-adapters/aws/aws-resource-configurator.js.map +1 -0
  170. package/dist/sdk-adapters/aws/index.d.ts +2 -0
  171. package/dist/sdk-adapters/aws/index.js +6 -0
  172. package/dist/sdk-adapters/aws/index.js.map +1 -0
  173. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/azure}/az-interactive-login-configurator.d.ts +6 -7
  174. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/azure}/az-interactive-login-configurator.js +7 -8
  175. package/dist/sdk-adapters/azure/az-interactive-login-configurator.js.map +1 -0
  176. package/dist/sdk-adapters/azure/azure-cli-adapter.d.ts +6 -0
  177. package/dist/sdk-adapters/azure/azure-cli-adapter.js +20 -0
  178. package/dist/sdk-adapters/azure/azure-cli-adapter.js.map +1 -0
  179. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/azure}/azure-resource-configurator.d.ts +2 -11
  180. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/azure}/azure-resource-configurator.js +13 -27
  181. package/dist/sdk-adapters/azure/azure-resource-configurator.js.map +1 -0
  182. package/dist/sdk-adapters/azure/index.d.ts +3 -0
  183. package/dist/sdk-adapters/azure/index.js +7 -0
  184. package/dist/sdk-adapters/azure/index.js.map +1 -0
  185. package/dist/sdk-adapters/docker-hub/docker-hub-cli-adapter.d.ts +6 -0
  186. package/dist/sdk-adapters/docker-hub/docker-hub-cli-adapter.js +16 -0
  187. package/dist/sdk-adapters/docker-hub/docker-hub-cli-adapter.js.map +1 -0
  188. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/docker-hub}/docker-hub-resource-configurator.d.ts +4 -4
  189. package/dist/{uilogic/cloud-resource-configurators → sdk-adapters/docker-hub}/docker-hub-resource-configurator.js +7 -1
  190. package/dist/sdk-adapters/docker-hub/docker-hub-resource-configurator.js.map +1 -0
  191. package/dist/sdk-adapters/docker-hub/index.d.ts +2 -0
  192. package/dist/sdk-adapters/docker-hub/index.js +6 -0
  193. package/dist/sdk-adapters/docker-hub/index.js.map +1 -0
  194. package/dist/sdk-adapters/local-machine/index.js +4 -0
  195. package/dist/sdk-adapters/local-machine/index.js.map +1 -0
  196. package/dist/sdk-adapters/local-machine/local-machine-cli-adapter.js +14 -0
  197. package/dist/sdk-adapters/local-machine/local-machine-cli-adapter.js.map +1 -0
  198. package/dist/sdk-adapters/local-machine/local-machine-login-configurator.d.ts +17 -0
  199. package/dist/sdk-adapters/local-machine/local-machine-login-configurator.js +66 -0
  200. package/dist/sdk-adapters/local-machine/local-machine-login-configurator.js.map +1 -0
  201. package/dist/sdk-adapters/local-machine/local-machine-resource-configurator.d.ts +20 -0
  202. package/dist/sdk-adapters/local-machine/local-machine-resource-configurator.js +121 -0
  203. package/dist/sdk-adapters/local-machine/local-machine-resource-configurator.js.map +1 -0
  204. package/dist/sdk-adapters/shared/resource-configurator-base.d.ts +30 -0
  205. package/dist/sdk-adapters/shared/resource-configurator-base.js +124 -0
  206. package/dist/sdk-adapters/shared/resource-configurator-base.js.map +1 -0
  207. package/dist/uilogic/build-configurator.d.ts +2 -1
  208. package/dist/uilogic/build-configurator.js.map +1 -1
  209. package/dist/uilogic/cloud-provider-contextualizer.d.ts +4 -4
  210. package/dist/uilogic/cloud-provider-contextualizer.js +24 -9
  211. package/dist/uilogic/cloud-provider-contextualizer.js.map +1 -1
  212. package/dist/uilogic/cloud-resource-configurator-factory.d.ts +6 -10
  213. package/dist/uilogic/cloud-resource-configurator-factory.js +31 -19
  214. package/dist/uilogic/cloud-resource-configurator-factory.js.map +1 -1
  215. package/dist/uilogic/cloud-resource-configurators/asset-configurator.d.ts +8 -1
  216. package/dist/uilogic/cloud-resource-configurators/asset-configurator.js.map +1 -1
  217. package/dist/uilogic/cloud-resource-configurators/unsupported-resource-configurator.d.ts +1 -1
  218. package/dist/uilogic/config-ui-utils.d.ts +19 -14
  219. package/dist/uilogic/config-ui-utils.js +66 -21
  220. package/dist/uilogic/config-ui-utils.js.map +1 -1
  221. package/dist/uilogic/enterprise-contextualizer.d.ts +2 -1
  222. package/dist/uilogic/enterprise-contextualizer.js.map +1 -1
  223. package/dist/uilogic/i-messaging-client.d.ts +1 -0
  224. package/dist/uilogic/image-registry-contextualizer.d.ts +3 -3
  225. package/dist/uilogic/image-registry-contextualizer.js +4 -2
  226. package/dist/uilogic/image-registry-contextualizer.js.map +1 -1
  227. package/dist/uilogic/index.d.ts +0 -4
  228. package/dist/uilogic/index.js +0 -4
  229. package/dist/uilogic/index.js.map +1 -1
  230. package/dist/uilogic/lrp-watcher-predef.js +10 -4
  231. package/dist/uilogic/lrp-watcher-predef.js.map +1 -1
  232. package/dist/uilogic/rootmap-ui-utils.d.ts +2 -0
  233. package/dist/uilogic/rootmap-ui-utils.js +35 -11
  234. package/dist/uilogic/rootmap-ui-utils.js.map +1 -1
  235. package/dist/uilogic/tenant-contextualizer.d.ts +1 -1
  236. package/dist/uilogic/tenant-contextualizer.js.map +1 -1
  237. package/dist/uilogic/type-safety-utils.d.ts +11 -0
  238. package/dist/uilogic/type-safety-utils.js +13 -0
  239. package/dist/uilogic/type-safety-utils.js.map +1 -1
  240. package/dist/uilogic/validators/non-empty-validator.d.ts +7 -0
  241. package/dist/uilogic/validators/non-empty-validator.js +20 -0
  242. package/dist/uilogic/validators/non-empty-validator.js.map +1 -0
  243. package/dist/uilogic/validators/semver-range-validator.d.ts +1 -1
  244. package/dist/uilogic/validators/semver-range-validator.js +2 -2
  245. package/dist/uilogic/validators/semver-range-validator.js.map +1 -1
  246. package/oclif.manifest.json +243 -23
  247. package/package.json +4 -4
  248. package/dist/ops/azure-container-registry-client/azure-container-registry-client.js +0 -148
  249. package/dist/ops/azure-container-registry-client/azure-container-registry-client.js.map +0 -1
  250. package/dist/uilogic/cloud-resource-configurators/aws-resource-configurator.js.map +0 -1
  251. package/dist/uilogic/cloud-resource-configurators/az-interactive-login-configurator.js.map +0 -1
  252. package/dist/uilogic/cloud-resource-configurators/azure-resource-configurator.js.map +0 -1
  253. package/dist/uilogic/cloud-resource-configurators/azure-types.js +0 -10
  254. package/dist/uilogic/cloud-resource-configurators/azure-types.js.map +0 -1
  255. package/dist/uilogic/cloud-resource-configurators/docker-hub-resource-configurator.js.map +0 -1
  256. package/dist/uilogic/cloud-resource-configurators/helm-resource-configurator.js.map +0 -1
  257. package/dist/uilogic/i-cluster-configurator.js.map +0 -1
  258. package/dist/uilogic/i-file-store-configurator.js.map +0 -1
  259. package/dist/uilogic/i-login-configurator.js.map +0 -1
  260. package/dist/uilogic/i-registry-configurator.js.map +0 -1
  261. package/dist/uilogic/i-secret-store-configurator.js.map +0 -1
  262. /package/dist/{uilogic → sdk}/i-cluster-configurator.d.ts +0 -0
  263. /package/dist/{uilogic → sdk}/i-cluster-configurator.js +0 -0
  264. /package/dist/{uilogic → sdk}/i-file-store-configurator.d.ts +0 -0
  265. /package/dist/{uilogic → sdk}/i-file-store-configurator.js +0 -0
  266. /package/dist/{uilogic → sdk}/i-login-configurator.js +0 -0
  267. /package/dist/{uilogic → sdk}/i-registry-configurator.d.ts +0 -0
  268. /package/dist/{uilogic → sdk}/i-registry-configurator.js +0 -0
  269. /package/dist/{uilogic → sdk}/i-secret-store-configurator.d.ts +0 -0
  270. /package/dist/{uilogic → sdk}/i-secret-store-configurator.js +0 -0
  271. /package/dist/{ops/azure-container-registry-client/azure-container-registry-client.d.ts → sdk-adapters/local-machine/index.d.ts} +0 -0
  272. /package/dist/{uilogic/cloud-resource-configurators/azure-types.d.ts → sdk-adapters/local-machine/local-machine-cli-adapter.d.ts} +0 -0
@@ -0,0 +1,721 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TerminalUtils = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const inversify_1 = require("inversify");
6
+ const child_process_1 = require("child_process");
7
+ const codiac_common_1 = require("@codiac.io/codiac-common");
8
+ const IBetterLogger_1 = require("@codiac.io/codiac-common/IBetterLogger");
9
+ const types = require("@codiac.io/codiac-ops-common");
10
+ const terminal_command_request_1 = require("../message-contracts/terminal-command-request");
11
+ let TerminalUtils = class TerminalUtils {
12
+ constructor(logger) {
13
+ this.logger = logger;
14
+ this.cliExec = new types.CliExec(this.logger);
15
+ }
16
+ execute(request) {
17
+ var _a, _b, _c;
18
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
19
+ try {
20
+ // TODO: Consider launching a completely separate terminal WINDOW for these.
21
+ let output;
22
+ const ultEnv = (_a = request.env) !== null && _a !== void 0 ? _a : {};
23
+ const ultCwd = (_b = request.cwd) !== null && _b !== void 0 ? _b : process.cwd();
24
+ if (request.shellExe) { // (WITH SHELL)
25
+ let shellArgs = (_c = request.shellArgs) !== null && _c !== void 0 ? _c : [];
26
+ let collectStdOut = Boolean(request.outputMode != terminal_command_request_1.CommandOutputModeEnum.log); // always collect, unless they called out for a void
27
+ if (request.inlineOperator) {
28
+ // INLINE
29
+ shellArgs.push(request.inlineOperator.trim());
30
+ output = yield this.spawnTerminalCommands(request.shellExe, shellArgs, request.command, ultCwd, ultEnv, collectStdOut);
31
+ }
32
+ else {
33
+ // SEPARATE
34
+ output = yield this.executeSingleCommandInTerminal(request.shellExe, shellArgs, request.command, ultCwd, ultEnv);
35
+ }
36
+ }
37
+ else { // (NO SHELL GIVEN)
38
+ if (request.outputMode == terminal_command_request_1.CommandOutputModeEnum.log) {
39
+ // Execute VOID
40
+ output = { code: 1, stdout: "" };
41
+ delete output.stdout;
42
+ output.code = yield this.cliExec.spawnAndLog(request.command, ultCwd, ultEnv, undefined, true);
43
+ }
44
+ else {
45
+ // Execute STDOUT
46
+ output = { code: 1, stdout: "" };
47
+ output.stdout = yield this.cliExec.execAsyncStdOut(request.command, { cwd: ultCwd, env: ultEnv }, undefined, true);
48
+ output.code = 0;
49
+ }
50
+ }
51
+ return output;
52
+ }
53
+ catch (err) {
54
+ throw err;
55
+ }
56
+ });
57
+ }
58
+ /** Fires the given shell command into a child process
59
+ * that inherits the parent's stdio (input, output, and error streams),
60
+ * and resolves the promise on exit of that child process.
61
+ * Returns the exit code from the child process.
62
+ *
63
+ * @param terminalExe The executable to open the terminal. Examples: `powershell`, `wsl`, `bash`, `sh`
64
+ * @param terminalArgs Arguments for the terminal executable itself. Examples: (for powershell: `["-c"]`, for wsl: `["-d", "ubuntu-20.04", "bash", "-l", "-c"]` OR `["-d", "ubuntu-20.04", "bash", "-l", "--"]`, for bash: `["-c"]`). IMPORTANT! Be sure to include the `-c` or `--` arg at the end because these arguments string will be suffixed by the `commandString` parameter of this function.
65
+ * @param commandString the commands to execute from within the terminal process (typically delimited by "&&" or "&"). NOTE: This string will be automatically enclosed in double quotes for final execution, with any existing double-quotes escaped as `\"`, unless you set the `doNotEnquoteCommand` parameter to true on this fucntion
66
+ * @param env If this gets passed in, it replaces the current env. That is, no env vars from the parent get carried into the child process.
67
+ * So if you use this param and you still want the current env vars, you'll have to add process.env to your dictionary (use a spread operator).
68
+ * @param collectStdOut (optional, defaults to false) Prevents piping runtime data directly to stdout and instead renders it after completion. Some processes provide runtime status messages (eg: docker's percent completes, etc), which get obstructed by piping to stdout. If so, set this argument to true.
69
+ * @param enquoteCommand (optional, defaults to auto) Setting to true forces enclosing the command string in double quotes for final execution. By default the given commandString param will get appended as is to the terminal command when the last terminal argument is the double-dash: `--`, otherwise it will be enclosed in double quotes (with escaping).
70
+ * */
71
+ spawnTerminalCommands(terminalExe, terminalArgs, commandString, cwd, env, collectStdOut = false, enquoteCommand) {
72
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
73
+ return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
74
+ var _a, _b;
75
+ try {
76
+ // DEV NOTE: This is a good refresher: https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/
77
+ this.logger.detail(`Executing: ${commandString} from: ${cwd}`);
78
+ let output = { code: 1, stdout: "" };
79
+ let termData;
80
+ let options = {
81
+ cwd: cwd,
82
+ // stdio: "inherit",
83
+ stdio: [process.stdin, process.stdout, process.stderr],
84
+ shell: false
85
+ };
86
+ if (env)
87
+ options.env = env;
88
+ // Dim = "\x1b[2m"
89
+ // process.stdout.write("\x1b[2m")
90
+ let ultimateCmd = commandString; //.join(" ");
91
+ const lastTermArg = (terminalArgs.length > 0) ? terminalArgs[terminalArgs.length - 1].trim() : undefined;
92
+ const DOUBLE_DASH = "--";
93
+ if (enquoteCommand == true || (enquoteCommand == undefined && lastTermArg !== DOUBLE_DASH)) {
94
+ ultimateCmd = "\"".concat(ultimateCmd.replace("\"", "\\\""), "\"");
95
+ }
96
+ // TO PREVENT mismatched encodings (which results in every other character being a space)
97
+ if (options.env == undefined)
98
+ options.env = {};
99
+ options.env["WSL_UTF8"] = "1"; // from the internet: https://stackoverflow.com/questions/64104790/powershell-strange-wsl-output-string-encoding
100
+ let child = (0, child_process_1.spawn)(terminalExe, [...terminalArgs, ultimateCmd], options);
101
+ // Can we set child stdout to a chalk stream that colors the output with our single color?
102
+ // eg:
103
+ // let chalkStrm = new Writable();
104
+ // chalkStrm.addListener('pipe', (src) => {
105
+ // let chunk = src.read();
106
+ // process.stdout.write(chalk.grey(chunk));
107
+ // });
108
+ // NOTE ALSO: child processes tend to lose their own coloring when being piped (see link below)
109
+ // https://stackoverflow.com/questions/31620298/is-it-possible-for-child-processes-in-node-js-to-preserve-colored-output
110
+ // For since the child proc is already writing to our own stdio streams,
111
+ // logging these error feeds is going to show up as duplicate output in the console.
112
+ // child.stderr.on('data', (data) => { this.logger.trace(data)});
113
+ if (child.stdout) {
114
+ child.stdout.on('data', (chunk) => {
115
+ var _a, _b;
116
+ // this.logger.trace(chunk);
117
+ // process.stdout.write(chalk.grey(chunk));
118
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
119
+ if (collectStdOut === true) {
120
+ output.stdout = (_b = (_a = output.stdout) === null || _a === void 0 ? void 0 : _a.concat(stringChunk)) !== null && _b !== void 0 ? _b : stringChunk;
121
+ }
122
+ else {
123
+ // It appears that we lose the runtime status messages
124
+ // when we pipe like this (eg: docker's percent completes, etc).
125
+ // If so, set collectStdOut to true.
126
+ process.stdout.write(stringChunk); // this could be accomplished by setting stdio to: { stdout: "pipe" }
127
+ }
128
+ });
129
+ }
130
+ child.on('data', (chunk) => {
131
+ var _a;
132
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
133
+ this.logger.trace(stringChunk);
134
+ // process.stdout.write(chalk.grey(chunk));
135
+ if (collectStdOut === true) {
136
+ termData = (_a = termData === null || termData === void 0 ? void 0 : termData.concat(stringChunk)) !== null && _a !== void 0 ? _a : stringChunk;
137
+ }
138
+ else {
139
+ // It appears that we lose the runtime status messages
140
+ // when we pipe like this (eg: docker's percent completes, etc).
141
+ // If so, set collectStdOut to true.
142
+ process.stdout.write(stringChunk);
143
+ }
144
+ });
145
+ if (child.stderr) {
146
+ child.stderr.on('data', (chunk) => {
147
+ // this.logger.trace(chunk);
148
+ // process.stdout.write(chalk.grey(chunk));
149
+ var _a, _b;
150
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
151
+ this.logger.error(stringChunk);
152
+ if (collectStdOut === true) {
153
+ output.stderr = (_b = (_a = output.stderr) === null || _a === void 0 ? void 0 : _a.concat(stringChunk)) !== null && _b !== void 0 ? _b : stringChunk;
154
+ }
155
+ else {
156
+ // It appears that we lose the runtime status messages
157
+ // when we pipe like this (eg: docker's percent completes, etc).
158
+ // If so, set collectStdOut to true.
159
+ process.stderr.write(stringChunk);
160
+ }
161
+ });
162
+ }
163
+ // When we do this, something is keeping the process from exiting. unpiping afterwards didnt help
164
+ // child.stdout.pipe(process.stdout);
165
+ // child.stderr.pipe(process.stderr);
166
+ // process.stdin.pipe(child.stdin);
167
+ // child.on('close', (code, signal) => {
168
+ // // this.logger.trace(`Command closed with code: ${code} and signal ${signal}`);
169
+ // if (code == 0) {
170
+ // // this.logger.trace(`Command exited ${(code != undefined) ? 'with code: ' + code : 'without code'} and ${(signal) ? 'with signal ' + signal : 'without signal'}`);
171
+ // resolve(output);
172
+ // } else {
173
+ // // TODO: Add the stderr output to the error object.
174
+ // reject(new Error(`Error (return code: ${code})`))
175
+ // }
176
+ // });
177
+ child.on('error', (err) => {
178
+ this.logger.error(`Command returned error ${err.message || ""}`, err);
179
+ reject(err);
180
+ });
181
+ child.on('exit', (code, signal) => {
182
+ // process.stdin.unpipe(child.stdin);
183
+ // child.stderr.unpipe(process.stderr);
184
+ // child.stdout.unpipe(process.stdout);
185
+ if (code != null)
186
+ output.code = code;
187
+ if (output)
188
+ this.logger.detail("exiting terminal command", output);
189
+ // reset: \x1b[0m
190
+ process.stdout.write("\x1b[0m");
191
+ // If we're handling both close and error, we dont need this handler, right?
192
+ if (code == 0) {
193
+ // this.logger.trace(`Command exited ${(code != undefined) ? 'with code: ' + code : 'without code'} and ${(signal) ? 'with signal ' + signal : 'without signal'}`);
194
+ resolve(output);
195
+ }
196
+ else {
197
+ // TODO: Add the stderr output to the error object.
198
+ reject(new Error(`Error (return code: ${code})`));
199
+ }
200
+ });
201
+ (_a = child.stdin) === null || _a === void 0 ? void 0 : _a.write("exit");
202
+ (_b = child.stdin) === null || _b === void 0 ? void 0 : _b.end();
203
+ }
204
+ catch (err) {
205
+ reject(err);
206
+ }
207
+ }));
208
+ });
209
+ }
210
+ /** Fires the given shell command into a child process
211
+ * that inherits the parent's stdio (input, output, and error streams),
212
+ * and resolves the promise on exit of that child process.
213
+ * Returns the exit code from the child process.
214
+ * @param env If this gets passed in, it replaces the current env. That is, no env vars from the parent get carried into the child process.
215
+ * So if you use this param and you still want the current env vars, you'll have to add process.env to your dictionary (use a spread operator).
216
+ * @param collectStdOut (optional, defaults to false) Prevents piping runtime data directly to stdout and instead renders it after completion. Some processes provide runtime status messages (eg: docker's percent completes, etc), which get obstructed by piping to stdout. If so, set this argument to true.
217
+ * */
218
+ spawnAndLog(cmd, cwd, env, logLevel = IBetterLogger_1.LogLevel.debug, collectStdOut = false, stdoutEncoding = "utf8") {
219
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
220
+ return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
221
+ try {
222
+ // DEV NOTE: This is a good refresher: https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/
223
+ this.logger.write(`Executing: ${cmd} from: ${cwd}`, logLevel);
224
+ let output;
225
+ let options = {
226
+ cwd: cwd,
227
+ // stdio: "inherit",
228
+ // stdio: [process.stdin, process.stdout, process.stderr], // TODO: Capture stderr instead
229
+ shell: true
230
+ };
231
+ if (env)
232
+ options.env = env;
233
+ // Dim = "\x1b[2m"
234
+ process.stdout.write("\x1b[2m");
235
+ // TO PREVENT mismatched encodings (which results in every other character being a space)
236
+ if (options.env == undefined)
237
+ options.env = {};
238
+ options.env["WSL_UTF8"] = "1"; // from the internet: https://stackoverflow.com/questions/64104790/powershell-strange-wsl-output-string-encoding
239
+ let child = (0, child_process_1.spawn)(cmd, options);
240
+ // TO PREVENT mismatched encodings (which results in every other character being a space)
241
+ // if (stdoutEncoding) {
242
+ // child.stdout?.setEncoding(stdoutEncoding);
243
+ // }
244
+ // Can we set child stdout to a chalk stream that colors the output with our single color?
245
+ // eg:
246
+ // let chalkStrm = new Writable();
247
+ // chalkStrm.addListener('pipe', (src) => {
248
+ // let chunk = src.read();
249
+ // process.stdout.write(chalk.grey(chunk));
250
+ // });
251
+ // NOTE ALSO: child processes tend to lose their own coloring when being piped (see link below)
252
+ // https://stackoverflow.com/questions/31620298/is-it-possible-for-child-processes-in-node-js-to-preserve-colored-output
253
+ // For since the child proc is already writing to our own stdio streams,
254
+ // logging these error feeds is going to show up as duplicate output in the console.
255
+ // child.stderr.on('data', (data) => { this.logger.trace(data)});
256
+ if (child.stdout) {
257
+ child.stdout.on('data', (chunk) => {
258
+ // this.logger.trace(chunk);
259
+ // process.stdout.write(chalk.grey(chunk));
260
+ var _a;
261
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
262
+ this.logger.detail(stringChunk);
263
+ if (collectStdOut === true) {
264
+ output = (_a = output === null || output === void 0 ? void 0 : output.concat(stringChunk)) !== null && _a !== void 0 ? _a : stringChunk;
265
+ }
266
+ else {
267
+ // It appears that we lose the runtime status messages
268
+ // when we pipe like this (eg: docker's percent completes, etc).
269
+ // If so, set collectStdOut to true.
270
+ process.stdout.write(stringChunk);
271
+ }
272
+ });
273
+ } // else {
274
+ child.on('data', (chunk) => {
275
+ var _a;
276
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
277
+ this.logger.detail(stringChunk);
278
+ // process.stdout.write(chalk.grey(chunk));
279
+ if (collectStdOut === true) {
280
+ output = (_a = output === null || output === void 0 ? void 0 : output.concat(stringChunk)) !== null && _a !== void 0 ? _a : stringChunk;
281
+ }
282
+ else {
283
+ // It appears that we lose the runtime status messages
284
+ // when we pipe like this (eg: docker's percent completes, etc).
285
+ // If so, set collectStdOut to true.
286
+ process.stdout.write(stringChunk);
287
+ }
288
+ });
289
+ // }
290
+ if (child.stderr) {
291
+ child.stderr.on('data', (chunk) => {
292
+ // this.logger.trace(chunk);
293
+ // process.stdout.write(chalk.grey(chunk));
294
+ var _a;
295
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
296
+ this.logger.error(stringChunk);
297
+ if (collectStdOut === true) {
298
+ output = (_a = output === null || output === void 0 ? void 0 : output.concat(stringChunk)) !== null && _a !== void 0 ? _a : stringChunk;
299
+ }
300
+ else {
301
+ // It appears that we lose the runtime status messages
302
+ // when we pipe like this (eg: docker's percent completes, etc).
303
+ // If so, set collectStdOut to true.
304
+ process.stdout.write(stringChunk);
305
+ }
306
+ });
307
+ }
308
+ // When we do this, something is keeping the process from exiting. unpiping afterwards didnt help
309
+ // child.stdout.pipe(process.stdout);
310
+ // child.stderr.pipe(process.stderr);
311
+ // process.stdin.pipe(child.stdin);
312
+ child.on('close', (code, signal) => {
313
+ // this.logger.trace(`Command closed with code: ${code} and signal ${signal}`);
314
+ if (code == 0) {
315
+ // this.logger.trace(`Command exited ${(code != undefined) ? 'with code: ' + code : 'without code'} and ${(signal) ? 'with signal ' + signal : 'without signal'}`);
316
+ resolve([code, output]);
317
+ }
318
+ else {
319
+ // TODO: Add the stderr output to the error object.
320
+ reject(new Error(`Error (return code: ${code})`));
321
+ }
322
+ });
323
+ child.on('error', (err) => {
324
+ this.logger.error(`Command returned error ${err.message || ""}`, err);
325
+ reject(err);
326
+ });
327
+ child.on('exit', (code, signal) => {
328
+ // process.stdin.unpipe(child.stdin);
329
+ // child.stderr.unpipe(process.stderr);
330
+ // child.stdout.unpipe(process.stdout);
331
+ if (output)
332
+ this.logger.write(output, logLevel);
333
+ // reset: \x1b[0m
334
+ process.stdout.write("\x1b[0m");
335
+ // If we're handling both close and error, we dont need this handler, right?
336
+ if (code == 0) {
337
+ // this.logger.trace(`Command exited ${(code != undefined) ? 'with code: ' + code : 'without code'} and ${(signal) ? 'with signal ' + signal : 'without signal'}`);
338
+ resolve([code, output]);
339
+ }
340
+ else {
341
+ // TODO: Add the stderr output to the error object.
342
+ reject(new Error(`Error (return code: ${code})`));
343
+ }
344
+ });
345
+ }
346
+ catch (err) {
347
+ reject(err);
348
+ }
349
+ }));
350
+ });
351
+ }
352
+ /** Fires the given shell command into a child process
353
+ * that inherits the parent's stdio (input, output, and error streams),
354
+ * and resolves the promise on exit of that child process.
355
+ * Returns the exit code from the child process.
356
+ *
357
+ * @param terminalExe The executable to open the terminal. Examples: `powershell`, `wsl`, `bash`, `sh`
358
+ * @param terminalArgs Arguments for the terminal executable itself. Examples: (for powershell: `["-c"]`, for wsl: `["-d", "ubuntu-20.04", "bash", "-l", "-c"]` OR `["-d", "ubuntu-20.04", "bash", "-l", "--"]`, for bash: `["-c"]`). IMPORTANT! Be sure to include the `-c` or `--` arg at the end because these arguments string will be suffixed by the `commandString` parameter of this function.
359
+ * @param commandString the commands to execute from within the terminal process (typically delimited by "&&" or "&"). NOTE: This string will be automatically enclosed in double quotes for final execution, with any existing double-quotes escaped as `\"`, unless you set the `doNotEnquoteCommand` parameter to true on this fucntion
360
+ * @param env If this gets passed in, it replaces the current env. That is, no env vars from the parent get carried into the child process.
361
+ * So if you use this param and you still want the current env vars, you'll have to add process.env to your dictionary (use a spread operator).
362
+ * @param collectStdOut (optional, defaults to false) Prevents piping runtime data directly to stdout and instead renders it after completion. Some processes provide runtime status messages (eg: docker's percent completes, etc), which get obstructed by piping to stdout. If so, set this argument to true.
363
+ * @param enquoteCommand (optional, defaults to auto) Setting to true forces enclosing the command string in double quotes for final execution. By default the given commandString param will get appended as is to the terminal command when the last terminal argument is the double-dash: `--`, otherwise it will be enclosed in double quotes (with escaping).
364
+ * */
365
+ openTerminal(terminalExe, terminalArgs, cwd, env, collectStdOut = false, enquoteCommand) {
366
+ var _a;
367
+ // let termData: string | undefined;
368
+ // let openResult: CommandResponse = { code: 1, stdout: "" };
369
+ try {
370
+ // DEV NOTE: This is a good refresher: https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/
371
+ this.logger.detail("Opening ".concat(terminalExe, " terminal..."));
372
+ // let output: string | undefined;
373
+ let options = {
374
+ cwd: cwd,
375
+ // stdio: "inherit",
376
+ // stdio: ['pipe', process.stdout, process.stderr], // TODO: Capture stderr instead
377
+ shell: false
378
+ };
379
+ if (env)
380
+ options.env = env;
381
+ // Dim = "\x1b[2m"
382
+ // process.stdout.write("\x1b[2m")
383
+ // TO PREVENT mismatched encodings (which results in every other character being a space)
384
+ if (options.env == undefined)
385
+ options.env = {};
386
+ options.env["WSL_UTF8"] = "1"; // from the internet: https://stackoverflow.com/questions/64104790/powershell-strange-wsl-output-string-encoding
387
+ let child = (0, child_process_1.spawn)(terminalExe, terminalArgs, options);
388
+ // Can we set child stdout to a chalk stream that colors the output with our single color?
389
+ // eg:
390
+ // let chalkStrm = new Writable();
391
+ // chalkStrm.addListener('pipe', (src) => {
392
+ // let chunk = src.read();
393
+ // process.stdout.write(chalk.grey(chunk));
394
+ // });
395
+ // NOTE ALSO: child processes tend to lose their own coloring when being piped (see link below)
396
+ // https://stackoverflow.com/questions/31620298/is-it-possible-for-child-processes-in-node-js-to-preserve-colored-output
397
+ // For since the child proc is already writing to our own stdio streams,
398
+ // logging these error feeds is going to show up as duplicate output in the console.
399
+ // child.stderr.on('data', (data) => { this.logger.trace(data)});
400
+ // ----------------------------
401
+ // Set event handlers
402
+ // ----------------------------
403
+ // child.on("error", (err) => {
404
+ // this.logger.error("Error in terminal command: ".concat(err.name ?? "", " ", err.message ?? ""), err);
405
+ // openResult.error = err;
406
+ // // reject(err);
407
+ // });
408
+ // child.on('data', (chunk) => {
409
+ // // this.logger.trace(" on terminal data");
410
+ // let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
411
+ // termData = termData?.concat(stringChunk) ?? stringChunk;
412
+ // });
413
+ // child.on("exit", (code, signal) => {
414
+ // if (code != null) openResult.code = code;
415
+ // this.logger.debug("Resolving on terminal exit", openResult);
416
+ // // resolve(response);
417
+ // });
418
+ // if (child.stdout) {
419
+ // child.stdout.on('data', (chunk) => {
420
+ // // this.logger.trace(chunk);
421
+ // // process.stdout.write(chalk.grey(chunk));
422
+ // let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
423
+ // if (collectStdOut === true) {
424
+ // output = output?.concat(stringChunk) ?? stringChunk;
425
+ // } else {
426
+ // // It appears that we lose the runtime status messages
427
+ // // when we pipe like this (eg: docker's percent completes, etc).
428
+ // // If so, set collectStdOut to true.
429
+ // process.stdout.write(stringChunk);
430
+ // }
431
+ // })
432
+ // child.stdout.on('readable', () => {
433
+ // this.logger.debug("stdout is now readable");
434
+ // })
435
+ // }
436
+ // if (child.stderr) {
437
+ // child.stderr.on('data', (chunk) => {
438
+ // // this.logger.trace(chunk);
439
+ // // process.stdout.write(chalk.grey(chunk));
440
+ // let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
441
+ // this.logger.error(stringChunk);
442
+ // if (collectStdOut === true) {
443
+ // output = output?.concat(stringChunk) ?? stringChunk;
444
+ // } else {
445
+ // // It appears that we lose the runtime status messages
446
+ // // when we pipe like this (eg: docker's percent completes, etc).
447
+ // // If so, set collectStdOut to true.
448
+ // process.stdout.write(stringChunk);
449
+ // }
450
+ // })
451
+ // child.stderr.on('error', (err) => {
452
+ // // this.logger.trace(chunk);
453
+ // // process.stdout.write(chalk.grey(chunk));
454
+ // this.logger.error(err.message);
455
+ // console.dir(err);
456
+ // if (collectStdOut === true) {
457
+ // output = output?.concat(err.message) ?? err.message;
458
+ // } else {
459
+ // // It appears that we lose the runtime status messages
460
+ // // when we pipe like this (eg: docker's percent completes, etc).
461
+ // // If so, set collectStdOut to true.
462
+ // process.stdout.write(err.message);
463
+ // }
464
+ // })
465
+ // }
466
+ // child.on('data', (chunk) => {
467
+ //
468
+ // let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
469
+ // this.logger.trace(stringChunk);
470
+ // // process.stdout.write(chalk.grey(chunk));
471
+ //
472
+ // if (collectStdOut === true) {
473
+ // output = output?.concat(stringChunk) ?? stringChunk;
474
+ // } else {
475
+ // // It appears that we lose the runtime status messages
476
+ // // when we pipe like this (eg: docker's percent completes, etc).
477
+ // // If so, set collectStdOut to true.
478
+ // process.stdout.write(stringChunk);
479
+ // }
480
+ // })
481
+ // // When we do this, something is keeping the process from exiting. unpiping afterwards didnt help
482
+ // // child.stdout.pipe(process.stdout);
483
+ // // child.stderr.pipe(process.stderr);
484
+ // // process.stdin.pipe(child.stdin);
485
+ // child.on('close', (code, signal) => {
486
+ // // this.logger.trace(`Command closed with code: ${code} and signal ${signal}`);
487
+ // if (code == 0) {
488
+ // // this.logger.trace(`Command exited ${(code != undefined) ? 'with code: ' + code : 'without code'} and ${(signal) ? 'with signal ' + signal : 'without signal'}`);
489
+ // this.logger.debug("Closing ".concat(terminalExe, " child process [pid: ", child.pid.toString(), "] with code ".concat(code.toString())));
490
+ // // resolve([code, output]);
491
+ // } else {
492
+ // // TODO: Add the stderr output to the error object.
493
+ // // reject(new Error(`Error (return code: ${code})`))
494
+ // throw new Error(`Error (return code: ${code})`); // <--- where will this get caught?
495
+ // }
496
+ // });
497
+ child.on('error', (err) => {
498
+ this.logger.error(`Open Terminal command returned error ${err.message || ""}`, err);
499
+ // reject(err);
500
+ throw err; // <--- where will this get caught?
501
+ });
502
+ // child.on('message', (msg, sendHandle) => {
503
+ // let serialized = msg.toString();
504
+ // this.logger.debug("child process message: ".concat(serialized))
505
+ // })
506
+ // child.on('exit', (code, signal) => {
507
+ // // process.stdin.unpipe(child.stdin);
508
+ // // child.stderr.unpipe(process.stderr);
509
+ // // child.stdout.unpipe(process.stdout);
510
+ // if (output) this.logger.detail(output);
511
+ // // reset: \x1b[0m
512
+ // process.stdout.write("\x1b[0m")
513
+ // // If we're handling both close and error, we dont need this handler, right?
514
+ // if (code == 0) {
515
+ // // this.logger.trace(`Command exited ${(code != undefined) ? 'with code: ' + code : 'without code'} and ${(signal) ? 'with signal ' + signal : 'without signal'}`);
516
+ // // resolve([code, output]);
517
+ // this.logger.debug("Closing ".concat(terminalExe, " child process [pid: ", child.pid.toString(), "] with code ".concat(code.toString())));
518
+ // } else {
519
+ // // TODO: Add the stderr output to the error object.
520
+ // // reject(new Error(`Error (return code: ${code})`))
521
+ // throw new Error(`Error (return code: ${code})`); // <--- where will this get caught?
522
+ // }
523
+ // });
524
+ return (child);
525
+ }
526
+ catch (err) {
527
+ this.logger.error("Error spawning ".concat(terminalExe, " terminal process: ", (_a = err.message) !== null && _a !== void 0 ? _a : ""));
528
+ throw err;
529
+ }
530
+ }
531
+ executeInExistingTerminal(terminal, command) {
532
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
533
+ return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
534
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
535
+ let response = { code: 1, stdout: "" };
536
+ try {
537
+ // terminal.removeAllListeners();
538
+ (_a = terminal.stdout) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
539
+ (_b = terminal.stderr) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
540
+ let termData;
541
+ // ----------------------------------------
542
+ // terminal events
543
+ // ----------------------------------------
544
+ terminal.on("error", (err) => {
545
+ var _a, _b;
546
+ this.logger.error("Error in terminal command: ".concat((_a = err.name) !== null && _a !== void 0 ? _a : "", " ", (_b = err.message) !== null && _b !== void 0 ? _b : ""), err);
547
+ response.error = err;
548
+ reject(err);
549
+ });
550
+ terminal.on('data', (chunk) => {
551
+ var _a;
552
+ // this.logger.trace(" on terminal data");
553
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
554
+ termData = (_a = termData === null || termData === void 0 ? void 0 : termData.concat(stringChunk)) !== null && _a !== void 0 ? _a : stringChunk;
555
+ });
556
+ terminal.on("exit", (code, signal) => {
557
+ if (code != null)
558
+ response.code = code;
559
+ this.logger.debug("Resolving on terminal exit", response);
560
+ resolve(response);
561
+ });
562
+ // terminal.on("disconnect", () => {
563
+ // this.logger.trace("on terminal disconnect");
564
+ // // resolve()
565
+ // });
566
+ // terminal.on("close", () => {
567
+ // this.logger.trace("on terminal close");
568
+ // // resolve()
569
+ // });
570
+ // terminal.on("message", (msg, sendhandle) => {
571
+ // this.logger.trace("on terminal message");
572
+ // // resolve()
573
+ // });
574
+ // ----------------------------------------
575
+ // stdout events
576
+ // ----------------------------------------
577
+ (_c = terminal.stdout) === null || _c === void 0 ? void 0 : _c.on('error', (err) => {
578
+ var _a, _b;
579
+ this.logger.trace("Error from stdout during terminal command: ".concat((_a = err.name) !== null && _a !== void 0 ? _a : "", " ", (_b = err.message) !== null && _b !== void 0 ? _b : ""), err);
580
+ response.error = err;
581
+ reject(err);
582
+ });
583
+ (_d = terminal.stdout) === null || _d === void 0 ? void 0 : _d.on('end', () => {
584
+ this.logger.debug("Terminal command end of stdout", response.stdout);
585
+ resolve(response);
586
+ });
587
+ (_e = terminal.stdout) === null || _e === void 0 ? void 0 : _e.on('data', (chunk) => {
588
+ var _a, _b;
589
+ // this.logger.trace(" on terminal stdout data");
590
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
591
+ response.stdout = (_b = (_a = response.stdout) === null || _a === void 0 ? void 0 : _a.concat(stringChunk)) !== null && _b !== void 0 ? _b : stringChunk;
592
+ });
593
+ // terminal.stdout?.on('close', () => {
594
+ // // this.logger.trace("on terminal stdout close");
595
+ // });
596
+ // terminal.stdout?.on('readable', () => {
597
+ // // this.logger.trace("terminal stdout readable");
598
+ // });
599
+ // ----------------------------------------
600
+ // stderr events
601
+ // ----------------------------------------
602
+ (_f = terminal.stderr) === null || _f === void 0 ? void 0 : _f.on('error', (err) => {
603
+ var _a, _b;
604
+ this.logger.trace("Error from stderr during terminal command: ".concat((_a = err.name) !== null && _a !== void 0 ? _a : "", " ", (_b = err.message) !== null && _b !== void 0 ? _b : ""), err);
605
+ response.error = err;
606
+ reject(err);
607
+ });
608
+ (_g = terminal.stderr) === null || _g === void 0 ? void 0 : _g.on('data', (chunk) => {
609
+ var _a, _b;
610
+ // this.logger.trace(" on terminal stderr data");
611
+ let stringChunk = (typeof chunk != 'string') ? chunk.toString("utf8") : chunk;
612
+ response.stderr = (_b = (_a = response.stderr) === null || _a === void 0 ? void 0 : _a.concat(stringChunk)) !== null && _b !== void 0 ? _b : stringChunk;
613
+ });
614
+ (_h = terminal.stderr) === null || _h === void 0 ? void 0 : _h.on('end', () => {
615
+ this.logger.debug("Terminal command end of stderr", response.stderr);
616
+ resolve(response);
617
+ });
618
+ // terminal.stderr?.on('close', () => {
619
+ // // this.logger.trace("terminal stderr close");
620
+ // });
621
+ // ISSUE THE COMMAND
622
+ if (terminal.stdin) {
623
+ this.logger.debug("Executing command in terminal: ".concat(command));
624
+ terminal.stdin.write(command);
625
+ terminal.stdin.end(); // <-- acts as the user hitting the "ENTER" key in a cli console
626
+ }
627
+ else {
628
+ throw new Error("Cannot send command into terminal via stdin; process has a null stdin.");
629
+ }
630
+ this.logger.trace("Command was issued.");
631
+ }
632
+ catch (err) {
633
+ this.logger.error("Error executing terminal commmand: ".concat(err.message, " ", (_j = response.stderr) !== null && _j !== void 0 ? _j : ""), err);
634
+ reject(err);
635
+ }
636
+ }));
637
+ });
638
+ }
639
+ /** Returns the exit code (NOTE: this exit code reflects the exit code of the most recently executed command). */
640
+ closeTerminal(term) {
641
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
642
+ return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
643
+ var _a, _b, _c;
644
+ try {
645
+ const DEFAULT_EXIT_CODE = 1; // this should never happen
646
+ (_a = term.stdout) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
647
+ (_b = term.stderr) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
648
+ term.removeAllListeners();
649
+ term.on("close", () => {
650
+ this.logger.debug("terminal close");
651
+ });
652
+ term.on("exit", (code) => {
653
+ var _a;
654
+ this.logger.debug("Resolving on terminal exit ".concat("(exit code: ", (_a = code === null || code === void 0 ? void 0 : code.toString()) !== null && _a !== void 0 ? _a : "not available", ")"));
655
+ resolve(code !== null && code !== void 0 ? code : DEFAULT_EXIT_CODE);
656
+ });
657
+ term.on("message", (msg, sendHandle) => {
658
+ this.logger.debug("terminal message");
659
+ });
660
+ term.on("error", (err) => {
661
+ this.logger.error("Rejecting on terminal error: ".concat(err.message), err);
662
+ reject(err);
663
+ });
664
+ this.logger.debug("Closing terminal...");
665
+ if (term.stdin && (term.stdin.writable == true)) {
666
+ term.stdin.write("exit");
667
+ term.stdin.end(); // <-- acts as the user hitting the "ENTER" key in a cli console
668
+ }
669
+ else {
670
+ this.logger.debug("Cannot send an exit command to the terminal bc the child process has no stdin; killing the terminal process instead.");
671
+ if (!term.killed) {
672
+ let done = term.kill();
673
+ this.logger.debug("Terminal killed: ".concat(done.toString()));
674
+ }
675
+ else {
676
+ this.logger.debug("Terminal already killed.");
677
+ }
678
+ }
679
+ }
680
+ catch (err) {
681
+ this.logger.error("Error attempting to close terminal: ".concat((_c = err.message) !== null && _c !== void 0 ? _c : "(check logs for details)"), err.stack);
682
+ reject(err);
683
+ }
684
+ }));
685
+ });
686
+ }
687
+ /** Equivalent of: `openTerminal() + executeInTerminal() + closeTerminal()`.
688
+ *
689
+ */
690
+ executeSingleCommandInTerminal(terminalExe, terminalArgs, command, cwd, env) {
691
+ var _a;
692
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
693
+ let termA;
694
+ let code;
695
+ let response;
696
+ try {
697
+ termA = yield this.openTerminal(terminalExe, [...terminalArgs], process.cwd(), process.env);
698
+ response = yield this.executeInExistingTerminal(termA, command);
699
+ }
700
+ catch (err) {
701
+ this.logger.error("Error executing terminal command: ".concat((_a = err.message) !== null && _a !== void 0 ? _a : "(see log for details)"), err);
702
+ throw err;
703
+ }
704
+ finally {
705
+ if (termA) {
706
+ code = yield this.closeTerminal(termA); // exit code isnt available until now
707
+ }
708
+ }
709
+ if (code != undefined)
710
+ response.code = code;
711
+ return response;
712
+ });
713
+ }
714
+ };
715
+ TerminalUtils = tslib_1.__decorate([
716
+ (0, inversify_1.injectable)(),
717
+ tslib_1.__param(0, (0, inversify_1.inject)(codiac_common_1.INTERFACES.IBetterLogger)),
718
+ tslib_1.__metadata("design:paramtypes", [Object])
719
+ ], TerminalUtils);
720
+ exports.TerminalUtils = TerminalUtils;
721
+ //# sourceMappingURL=terminal-utils.js.map