@autifyhq/autify-cli 0.10.1 → 0.12.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 (54) hide show
  1. package/README.md +104 -34
  2. package/dist/autify/connect/client-manager/AccessPoint.d.ts +16 -0
  3. package/dist/autify/connect/client-manager/AccessPoint.js +26 -0
  4. package/dist/autify/connect/client-manager/ClientManager.d.ts +57 -0
  5. package/dist/autify/connect/client-manager/ClientManager.js +242 -0
  6. package/dist/autify/connect/client-manager/DebugServerClient.d.ts +16 -0
  7. package/dist/autify/connect/client-manager/DebugServerClient.js +55 -0
  8. package/dist/autify/connect/client-manager/Logger.d.ts +11 -0
  9. package/dist/autify/connect/client-manager/Logger.js +43 -0
  10. package/dist/autify/connect/client-manager/StateMachine.d.ts +54 -0
  11. package/dist/autify/connect/client-manager/StateMachine.js +149 -0
  12. package/dist/autify/connect/installClient.d.ts +1 -1
  13. package/dist/autify/connect/installClient.js +4 -3
  14. package/dist/autify/mobile/getMobileClient.d.ts +2 -0
  15. package/dist/autify/mobile/getMobileClient.js +17 -0
  16. package/dist/autify/web/getWebClient.d.ts +2 -0
  17. package/dist/autify/web/getWebClient.js +17 -0
  18. package/dist/autify/web/runTest.js +8 -3
  19. package/dist/commands/connect/access-point/create.js +4 -7
  20. package/dist/commands/connect/access-point/set.js +1 -1
  21. package/dist/commands/connect/client/install.js +1 -1
  22. package/dist/commands/connect/client/start.js +26 -48
  23. package/dist/commands/mobile/api/describe-test-result.js +2 -5
  24. package/dist/commands/mobile/api/list-test-results.js +2 -5
  25. package/dist/commands/mobile/api/run-test-plan.js +2 -5
  26. package/dist/commands/mobile/api/upload-build.js +2 -5
  27. package/dist/commands/mobile/build/upload.js +2 -5
  28. package/dist/commands/mobile/test/run.js +2 -5
  29. package/dist/commands/mobile/test/wait.js +2 -5
  30. package/dist/commands/web/api/create-access-point.d.ts +10 -0
  31. package/dist/commands/web/api/create-access-point.js +26 -0
  32. package/dist/commands/web/api/create-url-replacement.js +2 -5
  33. package/dist/commands/web/api/delete-access-point.d.ts +10 -0
  34. package/dist/commands/web/api/delete-access-point.js +26 -0
  35. package/dist/commands/web/api/delete-url-replacement.js +2 -5
  36. package/dist/commands/web/api/describe-result.js +2 -5
  37. package/dist/commands/web/api/describe-scenario.js +2 -5
  38. package/dist/commands/web/api/execute-scenarios.js +2 -5
  39. package/dist/commands/web/api/execute-schedule.d.ts +1 -0
  40. package/dist/commands/web/api/execute-schedule.js +9 -6
  41. package/dist/commands/web/api/list-access-points.d.ts +10 -0
  42. package/dist/commands/web/api/list-access-points.js +26 -0
  43. package/dist/commands/web/api/list-capabilities.js +2 -5
  44. package/dist/commands/web/api/list-results.js +2 -5
  45. package/dist/commands/web/api/list-scenarios.js +2 -5
  46. package/dist/commands/web/api/list-url-replacements.js +2 -5
  47. package/dist/commands/web/api/update-url-replacement.js +2 -5
  48. package/dist/commands/web/test/run.js +41 -42
  49. package/dist/commands/web/test/wait.js +2 -5
  50. package/dist/config.d.ts +1 -1
  51. package/oclif.manifest.json +1 -1
  52. package/package.json +13 -9
  53. package/dist/autify/connect/spawnClient.d.ts +0 -28
  54. package/dist/autify/connect/spawnClient.js +0 -192
package/README.md CHANGED
@@ -33,12 +33,15 @@ See our official document: https://help.autify.com/docs/autify-command-line-inte
33
33
  * [`autify mobile test run TEST-PLAN-URL`](#autify-mobile-test-run-test-plan-url)
34
34
  * [`autify mobile test wait TEST-RESULT-URL`](#autify-mobile-test-wait-test-result-url)
35
35
  * [`autify update [CHANNEL]`](#autify-update-channel)
36
+ * [`autify web api create-access-point`](#autify-web-api-create-access-point)
36
37
  * [`autify web api create-url-replacement`](#autify-web-api-create-url-replacement)
38
+ * [`autify web api delete-access-point`](#autify-web-api-delete-access-point)
37
39
  * [`autify web api delete-url-replacement`](#autify-web-api-delete-url-replacement)
38
40
  * [`autify web api describe-result`](#autify-web-api-describe-result)
39
41
  * [`autify web api describe-scenario`](#autify-web-api-describe-scenario)
40
42
  * [`autify web api execute-scenarios`](#autify-web-api-execute-scenarios)
41
43
  * [`autify web api execute-schedule`](#autify-web-api-execute-schedule)
44
+ * [`autify web api list-access-points`](#autify-web-api-list-access-points)
42
45
  * [`autify web api list-capabilities`](#autify-web-api-list-capabilities)
43
46
  * [`autify web api list-results`](#autify-web-api-list-results)
44
47
  * [`autify web api list-scenarios`](#autify-web-api-list-scenarios)
@@ -50,7 +53,7 @@ See our official document: https://help.autify.com/docs/autify-command-line-inte
50
53
 
51
54
  ## `autify connect access-point create`
52
55
 
53
- [Experimental] Create an Autify Connect Access Point
56
+ Create an Autify Connect Access Point
54
57
 
55
58
  ```
56
59
  USAGE
@@ -58,10 +61,10 @@ USAGE
58
61
 
59
62
  FLAGS
60
63
  --name=<value> (required) Name of Autify Connect Access Point to be created
61
- --web-workspace-id=<value> Workspace ID of Autify for Web to which the Access Point belong
64
+ --web-workspace-id=<value> Workspace ID of Autify for Web to which the Access Point will belong
62
65
 
63
66
  DESCRIPTION
64
- [Experimental] Create an Autify Connect Access Point
67
+ Create an Autify Connect Access Point
65
68
 
66
69
  EXAMPLES
67
70
  $ autify connect access-point create --name NAME --web-workspace-id ID
@@ -69,7 +72,7 @@ EXAMPLES
69
72
 
70
73
  ## `autify connect access-point set`
71
74
 
72
- [Experimental] Set Autify Connect Access Point
75
+ Set Autify Connect Access Point
73
76
 
74
77
  ```
75
78
  USAGE
@@ -79,7 +82,7 @@ FLAGS
79
82
  --name=<value> (required) Name of the Autify Connect Access Point already created
80
83
 
81
84
  DESCRIPTION
82
- [Experimental] Set Autify Connect Access Point
85
+ Set Autify Connect Access Point
83
86
 
84
87
  EXAMPLES
85
88
  Start interactive setup:
@@ -93,17 +96,17 @@ EXAMPLES
93
96
 
94
97
  ## `autify connect client install [VERSION]`
95
98
 
96
- [Experimental] Install Autify Connect Client
99
+ Install Autify Connect Client
97
100
 
98
101
  ```
99
102
  USAGE
100
103
  $ autify connect client install [VERSION]
101
104
 
102
105
  ARGUMENTS
103
- VERSION [default: v0.6.2] Specify the target version of Autify Connect Client.
106
+ VERSION [default: v0.6.3] Specify the target version of Autify Connect Client.
104
107
 
105
108
  DESCRIPTION
106
- [Experimental] Install Autify Connect Client
109
+ Install Autify Connect Client
107
110
 
108
111
  EXAMPLES
109
112
  (Recommended) Install the supported version:
@@ -121,25 +124,31 @@ EXAMPLES
121
124
 
122
125
  ## `autify connect client start`
123
126
 
124
- [Experimental] Start Autify Connect Client
127
+ Start Autify Connect Client
125
128
 
126
129
  ```
127
130
  USAGE
128
131
  $ autify connect client start [--verbose] [--file-logging] [--debug-server-port <value>] [--web-workspace-id <value>]
129
132
 
130
133
  FLAGS
131
- --debug-server-port=<value> [default: 3000] The server for debugging and monitoring launches on your local machine on
132
- the given port.
134
+ --debug-server-port=<value> The server for debugging and monitoring launches on your local machine on the given port.
135
+ It will use a radom port if not specified.
133
136
  --file-logging Logging Autify Connect Client log to a file instead of console.
134
137
  --verbose Make the operation more talkative.
135
138
  --web-workspace-id=<value> Workspace ID of Autify for Web to create an ephemeral Access Point. If not specified, it
136
- will use the one configured by `autify connect access-point set`, instead.
139
+ will use the one configured by `autify connect access-point create/set`, instead.
137
140
 
138
141
  DESCRIPTION
139
- [Experimental] Start Autify Connect Client
142
+ Start Autify Connect Client
140
143
 
141
144
  EXAMPLES
142
- $ autify connect client start
145
+ With pre-created Access Point:
146
+
147
+ $ autify connect client start
148
+
149
+ With ephemeral Access Point of Autify for Web:
150
+
151
+ $ autify connect client start --web-workspace-id 000
143
152
  ```
144
153
 
145
154
  ## `autify help [COMMAND]`
@@ -391,6 +400,26 @@ EXAMPLES
391
400
 
392
401
  _See code: [@oclif/plugin-update](https://github.com/oclif/plugin-update/blob/v3.0.0/src/commands/update.ts)_
393
402
 
403
+ ## `autify web api create-access-point`
404
+
405
+ You can generate a new access point by passing in its name.
406
+
407
+ ```
408
+ USAGE
409
+ $ autify web api create-access-point --project-id <value> --create-access-point-request <value>
410
+
411
+ FLAGS
412
+ --create-access-point-request=<value> (required) The name of the access point to be created
413
+ --project-id=<value> (required) For example, 1 for the following URL:
414
+ https://app.autify.com/projects/1/scenarios
415
+
416
+ DESCRIPTION
417
+ You can generate a new access point by passing in its name.
418
+
419
+ EXAMPLES
420
+ $ autify web api create-access-point
421
+ ```
422
+
394
423
  ## `autify web api create-url-replacement`
395
424
 
396
425
  Create a new url replacement for the test plan
@@ -411,6 +440,26 @@ EXAMPLES
411
440
  $ autify web api create-url-replacement
412
441
  ```
413
442
 
443
+ ## `autify web api delete-access-point`
444
+
445
+ You can delete an access point by passing in its name.
446
+
447
+ ```
448
+ USAGE
449
+ $ autify web api delete-access-point --project-id <value> --delete-access-point-request <value>
450
+
451
+ FLAGS
452
+ --delete-access-point-request=<value> (required) The name of the access point to be deleted
453
+ --project-id=<value> (required) For example, 1 for the following URL:
454
+ https://app.autify.com/projects/1/scenarios
455
+
456
+ DESCRIPTION
457
+ You can delete an access point by passing in its name.
458
+
459
+ EXAMPLES
460
+ $ autify web api delete-access-point
461
+ ```
462
+
414
463
  ## `autify web api delete-url-replacement`
415
464
 
416
465
  Delete a url replacement for the test plan
@@ -496,10 +545,12 @@ Run a test plan. (Note: "Schedule" is called as "TestPlan" now.)
496
545
 
497
546
  ```
498
547
  USAGE
499
- $ autify web api execute-schedule --schedule-id <value>
548
+ $ autify web api execute-schedule --schedule-id <value> [--execute-schedule-request <value>]
500
549
 
501
550
  FLAGS
502
- --schedule-id=<value> (required) For example, 3 for the following URL: https://app.autify.com/projects/1/test_plans/3
551
+ --execute-schedule-request=<value> The options to execute a test plan.
552
+ --schedule-id=<value> (required) For example, 3 for the following URL:
553
+ https://app.autify.com/projects/1/test_plans/3
503
554
 
504
555
  DESCRIPTION
505
556
  Run a test plan. (Note: "Schedule" is called as "TestPlan" now.)
@@ -508,6 +559,25 @@ EXAMPLES
508
559
  $ autify web api execute-schedule
509
560
  ```
510
561
 
562
+ ## `autify web api list-access-points`
563
+
564
+ List access points for the project.
565
+
566
+ ```
567
+ USAGE
568
+ $ autify web api list-access-points --project-id <value> [--page <value>]
569
+
570
+ FLAGS
571
+ --page=<value> The number of page returns.
572
+ --project-id=<value> (required) For example, 1 for the following URL: https://app.autify.com/projects/1/scenarios
573
+
574
+ DESCRIPTION
575
+ List access points for the project.
576
+
577
+ EXAMPLES
578
+ $ autify web api list-access-points
579
+ ```
580
+
511
581
  ## `autify web api list-capabilities`
512
582
 
513
583
  List available Capabilities.
@@ -648,29 +718,25 @@ ARGUMENTS
648
718
  https://app.autify.com/projects/<ID>/(scenarios|test_plans)/<ID>
649
719
 
650
720
  FLAGS
651
- -n, --name=<value> Name of the test execution. (Only for test scenario execution.)
721
+ -n, --name=<value> [Only for test scenario] Name of the test execution.
652
722
  -r, --url-replacements=<value>... URL replacements. Example: http://example.com=http://example.net
653
723
  -t, --timeout=<value> [default: 300] Timeout seconds when waiting for the finish of the
654
724
  test execution.
655
725
  -v, --verbose Verbose output
656
726
  -w, --wait Wait until the test finishes.
657
- --autify-connect=<value> Name of the Autify Connect Access Point (Only for test scenario
658
- execution.)
659
- --autify-connect-client [Experimental] Start Autify Connect Client (Only for test scenario
660
- execution.)
661
- --autify-connect-client-debug-server-port=<value> [Experimental] [default: 3000] Port for Autify Connect Client debug
662
- server.
663
- --autify-connect-client-file-logging [Experimental] Logging Autify Connect Client log to a file instead
664
- of console.
665
- --autify-connect-client-verbose [Experimental] Verbose output for Autify Connect Client (Only for
666
- test scenario execution.)
667
- --browser=<value> Browser to run the test
668
- --device=<value> Device to run the test
669
- --device-type=<value> Device type to run the test
670
- --max-retry-count=<value> Maximum retry count. The command can take up to timeout *
671
- (max-retry-count + 1).
672
- --os=<value> OS to run the test
673
- --os-version=<value> OS version to run the test
727
+ --autify-connect=<value> Name of the Autify Connect Access Point.
728
+ --autify-connect-client Start Autify Connect Client
729
+ --autify-connect-client-debug-server-port=<value> Port for Autify Connect Client debug server. A random port will be
730
+ used if not specified.
731
+ --autify-connect-client-file-logging Logging Autify Connect Client log to a file instead of console.
732
+ --autify-connect-client-verbose Verbose output for Autify Connect Client.
733
+ --browser=<value> [Only for test scenario] Browser to run the test
734
+ --device=<value> [Only for test scenario] Device to run the test
735
+ --device-type=<value> [Only for test scenario] Device type to run the test
736
+ --max-retry-count=<value> Maximum retry count while waiting. The command can take up to
737
+ `timeout * (max-retry-count + 1)`. Only effective with `--wait`.
738
+ --os=<value> [Only for test scenario] OS to run the test
739
+ --os-version=<value> [Only for test scenario] OS version to run the test
674
740
 
675
741
  DESCRIPTION
676
742
  Run a scenario or test plan.
@@ -704,6 +770,10 @@ EXAMPLES
704
770
  Run a test scenario with Autify Connect:
705
771
 
706
772
  $ autify web test run https://app.autify.com/projects/0000/scenarios/0000 --autify-connect NAME
773
+
774
+ Run a test scenario with Autify Connect Client:
775
+
776
+ $ autify web test run https://app.autify.com/projects/0000/scenarios/0000 --wait --autify-connect-client
707
777
  ```
708
778
 
709
779
  ## `autify web test wait TEST-RESULT-URL`
@@ -0,0 +1,16 @@
1
+ import { WebClient } from "@autifyhq/autify-sdk";
2
+ interface StaticAccessPoint {
3
+ readonly type: "static";
4
+ readonly name: string;
5
+ readonly key: string;
6
+ }
7
+ interface EphemeralAccessPoint {
8
+ readonly type: "ephemeral";
9
+ readonly name: string;
10
+ readonly key: string;
11
+ readonly delete: () => Promise<void>;
12
+ }
13
+ export declare type AccessPoint = StaticAccessPoint | EphemeralAccessPoint;
14
+ export declare const createStaticAccessPoint: (name: string, key: string) => StaticAccessPoint;
15
+ export declare const createEphemeralAccessPointForWeb: (client: WebClient, workspaceId: number) => Promise<EphemeralAccessPoint>;
16
+ export {};
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEphemeralAccessPointForWeb = exports.createStaticAccessPoint = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const ACCESS_POINT_NAME_PREFIX = "autify-cli-";
6
+ const createStaticAccessPoint = (name, key) => {
7
+ return { type: "static", name, key };
8
+ };
9
+ exports.createStaticAccessPoint = createStaticAccessPoint;
10
+ const createEphemeralAccessPointForWeb = async (client, workspaceId) => {
11
+ const randomName = `${ACCESS_POINT_NAME_PREFIX}${(0, uuid_1.v4)()}`;
12
+ const res = await client.createAccessPoint(workspaceId, { name: randomName });
13
+ const name = res.data.name;
14
+ const key = res.data.key;
15
+ return {
16
+ type: "ephemeral",
17
+ name,
18
+ key,
19
+ delete: async () => {
20
+ await client.deleteAccessPoint(workspaceId, {
21
+ name,
22
+ });
23
+ },
24
+ };
25
+ };
26
+ exports.createEphemeralAccessPointForWeb = createEphemeralAccessPointForWeb;
@@ -0,0 +1,57 @@
1
+ /// <reference types="node" />
2
+ import { CLIError } from "@oclif/errors";
3
+ import TypedEmitter from "typed-emitter";
4
+ export declare type ClientEvents = TypedEmitter<{
5
+ log: (msg: string) => void;
6
+ starting: (msg: string) => void;
7
+ ready: (msg: string) => void;
8
+ reconnecting: (msg: string) => void;
9
+ error: (error: Error) => void;
10
+ }>;
11
+ export declare type ProcessExit = Readonly<{
12
+ code: number | null;
13
+ signal: NodeJS.Signals | null;
14
+ }>;
15
+ export declare class StateMachineTimeoutError extends CLIError {
16
+ constructor(state: string);
17
+ }
18
+ declare type ClientManagerOptions = Readonly<{
19
+ configDir: string;
20
+ cacheDir: string;
21
+ verbose?: boolean;
22
+ fileLogging?: boolean;
23
+ debugServerPort?: number;
24
+ }>;
25
+ export declare class ClientManager {
26
+ private readonly accessPoint;
27
+ private readonly options;
28
+ static create(options: ClientManagerOptions & {
29
+ userAgent: string;
30
+ webWorkspaceId?: number;
31
+ }): Promise<ClientManager>;
32
+ private static createWithStaticAccessPoint;
33
+ private static createWithEphemeralAccessPointForWeb;
34
+ start(): Promise<void>;
35
+ onceReady(): Promise<void>;
36
+ onceTerminating(): Promise<void>;
37
+ onceDone(): Promise<number | null>;
38
+ exit(options?: {
39
+ ignoreError: boolean;
40
+ }): Promise<number | null>;
41
+ get accessPointName(): string;
42
+ private readonly clientEvents;
43
+ private readonly logger;
44
+ private readonly clientLogger;
45
+ private readonly service;
46
+ private childProcess?;
47
+ private debugServerClient?;
48
+ private constructor();
49
+ private spawn;
50
+ private terminate;
51
+ private kill;
52
+ private cleanup;
53
+ private once;
54
+ private get clientPath();
55
+ private getClientVersion;
56
+ }
57
+ export {};
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientManager = exports.StateMachineTimeoutError = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const errors_1 = require("@oclif/errors");
6
+ const node_child_process_1 = require("node:child_process");
7
+ const node_process_1 = require("node:process");
8
+ const node_stream_1 = require("node:stream");
9
+ const config_1 = require("../../../config");
10
+ const AccessPoint_1 = require("./AccessPoint");
11
+ const installClient_1 = require("../installClient");
12
+ const waitFor_1 = require("xstate/lib/waitFor");
13
+ const DebugServerClient_1 = require("./DebugServerClient");
14
+ const StateMachine_1 = require("./StateMachine");
15
+ const Logger_1 = require("./Logger");
16
+ const node_path_1 = require("node:path");
17
+ const get_port_1 = tslib_1.__importDefault(require("get-port"));
18
+ const getWebClient_1 = require("../../web/getWebClient");
19
+ class StateMachineTimeoutError extends errors_1.CLIError {
20
+ constructor(state) {
21
+ super(`Autify Connect Manager faced timeout at ${state} state.`);
22
+ }
23
+ }
24
+ exports.StateMachineTimeoutError = StateMachineTimeoutError;
25
+ class ClientManager {
26
+ constructor(accessPoint, options) {
27
+ this.accessPoint = accessPoint;
28
+ this.options = options;
29
+ this.clientEvents = new node_stream_1.EventEmitter();
30
+ const level = options.verbose ? "debug" : "info";
31
+ this.logger = (0, Logger_1.createManagerLogger)({ level });
32
+ if (accessPoint.type === "ephemeral")
33
+ this.logger.info(`Ephemeral Access Point was created: ${this.accessPointName}`);
34
+ const filename = options.fileLogging
35
+ ? (0, node_path_1.join)(options.cacheDir, `autifyconnect-${Date.now()}-${process.pid}.log`)
36
+ : undefined;
37
+ if (filename)
38
+ this.logger.info(`Client log will be written on ${filename}`);
39
+ this.clientLogger = (0, Logger_1.createClientLogger)({ level }, filename);
40
+ this.service = (0, StateMachine_1.createService)({
41
+ spawn: (debugServerPort) => this.spawn(debugServerPort),
42
+ terminate: () => this.terminate(),
43
+ kill: () => this.kill(),
44
+ cleanup: () => this.cleanup(),
45
+ errors: [],
46
+ }).onTransition((state, event) => {
47
+ if (!state.changed)
48
+ return;
49
+ this.logger.debug(`Transition to: ${state.value}, event: ${JSON.stringify(event)}`);
50
+ });
51
+ process.on("SIGINT", () => this.service.send("TERMINATE"));
52
+ process.on("SIGTERM", () => this.service.send("TERMINATE"));
53
+ this.clientEvents.on("ready", () => {
54
+ this.service.send("READY");
55
+ });
56
+ this.clientEvents.on("log", (msg) => {
57
+ // Guardrail in case debug server isn't working well.
58
+ if (msg.includes("Successfully connected")) {
59
+ this.clientEvents.emit("ready", msg);
60
+ }
61
+ });
62
+ this.clientEvents.on("error", (error) => {
63
+ this.logger.warn(`Ignoring client error: ${error}`);
64
+ });
65
+ }
66
+ static async create(options) {
67
+ if (options.webWorkspaceId) {
68
+ const { configDir, userAgent, webWorkspaceId } = options;
69
+ const client = (0, getWebClient_1.getWebClient)(configDir, userAgent);
70
+ return this.createWithEphemeralAccessPointForWeb(client, webWorkspaceId, options);
71
+ }
72
+ return this.createWithStaticAccessPoint(options);
73
+ }
74
+ static async createWithStaticAccessPoint(options) {
75
+ const name = (0, config_1.get)(options.configDir, "AUTIFY_CONNECT_ACCESS_POINT_NAME");
76
+ const key = (0, config_1.get)(options.configDir, "AUTIFY_CONNECT_ACCESS_POINT_KEY");
77
+ if (!name || !key)
78
+ throw new errors_1.CLIError("Access Point is not saved. Run `autify connect access-point create/set` first.");
79
+ const accessPoint = (0, AccessPoint_1.createStaticAccessPoint)(name, key);
80
+ return new ClientManager(accessPoint, options);
81
+ }
82
+ static async createWithEphemeralAccessPointForWeb(client, workspaceId, options) {
83
+ const accessPoint = await (0, AccessPoint_1.createEphemeralAccessPointForWeb)(client, workspaceId);
84
+ return new ClientManager(accessPoint, options);
85
+ }
86
+ async start() {
87
+ var _a;
88
+ try {
89
+ this.logger.debug("start");
90
+ const version = await this.getClientVersion();
91
+ const debugServerPort = (_a = this.options.debugServerPort) !== null && _a !== void 0 ? _a : (await (0, get_port_1.default)());
92
+ this.logger.info(`Starting Autify Connect Client (accessPoint: ${this.accessPointName}, debugServerPort: ${debugServerPort}, path: ${this.clientPath}, version: ${version})`);
93
+ this.service.send("SPAWN", { debugServerPort });
94
+ }
95
+ catch (error) {
96
+ this.service.send("FAIL", { error });
97
+ throw error;
98
+ }
99
+ }
100
+ async onceReady() {
101
+ this.logger.debug("onceReady");
102
+ await this.once("ready");
103
+ }
104
+ async onceTerminating() {
105
+ this.logger.debug("onceTerminating");
106
+ await this.once("terminating");
107
+ }
108
+ async onceDone() {
109
+ this.logger.debug("onceDone");
110
+ const { context: { processExit }, } = await this.once("done");
111
+ if (!processExit) {
112
+ this.logger.warn("Autify Connect Client exited but unable to capture the exit status");
113
+ return null;
114
+ }
115
+ this.logger.info(`Autify Connect Client exited (code: ${processExit.code}, signal: ${processExit.signal})`);
116
+ return processExit.code;
117
+ }
118
+ async exit(options) {
119
+ var _a;
120
+ try {
121
+ this.logger.debug("exit");
122
+ const snapshot = this.service.getSnapshot();
123
+ if (snapshot.done) {
124
+ this.logger.debug("Already exited.");
125
+ return ((_a = snapshot.context.processExit) === null || _a === void 0 ? void 0 : _a.code) || null;
126
+ }
127
+ this.service.send("TERMINATE");
128
+ return await this.onceDone();
129
+ }
130
+ catch (error) {
131
+ if (options === null || options === void 0 ? void 0 : options.ignoreError) {
132
+ this.logger.warn(`Ignoring exit error: ${error}`);
133
+ return null;
134
+ }
135
+ throw error;
136
+ }
137
+ }
138
+ get accessPointName() {
139
+ return this.accessPoint.name;
140
+ }
141
+ spawn(debugServerPort) {
142
+ try {
143
+ const args = [
144
+ "--log-format",
145
+ "json",
146
+ "--experimental-debug-server-port",
147
+ debugServerPort.toString(),
148
+ ];
149
+ if (this.options.verbose)
150
+ args.push("--verbose");
151
+ this.childProcess = (0, node_child_process_1.spawn)(this.clientPath, args, {
152
+ env: {
153
+ ...node_process_1.env,
154
+ AUTIFY_CONNECT_KEY: this.accessPoint.key,
155
+ },
156
+ });
157
+ this.childProcess.on("exit", (code, signal) => {
158
+ this.service.send("EXIT", { processExit: { code, signal } });
159
+ });
160
+ this.debugServerClient = new DebugServerClient_1.DebugServerClient(debugServerPort, this.clientEvents);
161
+ (0, Logger_1.setupClientOutputLogger)(this.childProcess, (log) => {
162
+ this.clientLogger.log(log.level, log.msg);
163
+ this.clientEvents.emit("log", log.msg);
164
+ });
165
+ }
166
+ catch (error) {
167
+ this.logger.warn(`Ignoring spawn error: ${error}`);
168
+ }
169
+ }
170
+ async terminate() {
171
+ var _a;
172
+ try {
173
+ this.logger.debug("terminate");
174
+ await ((_a = this.debugServerClient) === null || _a === void 0 ? void 0 : _a.requestTerminate());
175
+ }
176
+ catch (error) {
177
+ this.logger.warn(`Ignoring terminate error: ${error}`);
178
+ }
179
+ }
180
+ kill() {
181
+ var _a;
182
+ try {
183
+ this.logger.debug("kill");
184
+ (_a = this.childProcess) === null || _a === void 0 ? void 0 : _a.kill();
185
+ }
186
+ catch (error) {
187
+ this.logger.warn(`Ignoring kill error: ${error}`);
188
+ }
189
+ }
190
+ async cleanup() {
191
+ var _a;
192
+ try {
193
+ this.logger.debug("cleanup start");
194
+ (_a = this.debugServerClient) === null || _a === void 0 ? void 0 : _a.stopStatusTimer();
195
+ if (this.accessPoint.type === "ephemeral") {
196
+ await this.accessPoint.delete();
197
+ this.logger.info(`Ephemeral Access Point was deleted: ${this.accessPointName}`);
198
+ }
199
+ this.logger.debug("cleanup done");
200
+ }
201
+ catch (error) {
202
+ this.logger.warn(`Ignoring cleanup error: ${error}`);
203
+ }
204
+ }
205
+ async once(waitState) {
206
+ let state;
207
+ try {
208
+ state = await (0, waitFor_1.waitFor)(this.service, (state) => state.matches(waitState) ||
209
+ state.matches("done") ||
210
+ state.matches("failed"), {
211
+ timeout: Number.POSITIVE_INFINITY,
212
+ });
213
+ }
214
+ catch {
215
+ throw new errors_1.CLIError(`Unknown state transition: ${this.service.getSnapshot().value}`);
216
+ }
217
+ if (state.value === "failed")
218
+ this.logger.warn(`Failed (${state.context.errors})`);
219
+ const timeoutError = state.context.errors.find((error) => error instanceof StateMachineTimeoutError);
220
+ if (timeoutError)
221
+ throw timeoutError;
222
+ return state;
223
+ }
224
+ get clientPath() {
225
+ return (0, installClient_1.getInstallPath)(this.options.configDir, this.options.cacheDir);
226
+ }
227
+ async getClientVersion() {
228
+ const clientVersion = await (0, installClient_1.getInstallVersion)(this.clientPath);
229
+ try {
230
+ await (0, installClient_1.validateVersion)(clientVersion, installClient_1.AUTIFY_CONNECT_CLIENT_SUPPORTED_VERSION);
231
+ }
232
+ catch (error) {
233
+ if (error instanceof installClient_1.ConnectClientVersionMismatchError) {
234
+ this.logger.warn("Installed Autify Connect Client version doesn't match our supported version. " +
235
+ "Consider to run `autify connect client install` to install the supported version. " +
236
+ `(version: ${clientVersion}, supported: ${installClient_1.AUTIFY_CONNECT_CLIENT_SUPPORTED_VERSION})`);
237
+ }
238
+ }
239
+ return clientVersion;
240
+ }
241
+ }
242
+ exports.ClientManager = ClientManager;
@@ -0,0 +1,16 @@
1
+ import { ClientEvents } from "./ClientManager";
2
+ declare type DebugServerStatusResponse = Readonly<{
3
+ status: "starting" | "ready" | "reconnecting";
4
+ message: string;
5
+ }>;
6
+ export declare class DebugServerClient {
7
+ private readonly port;
8
+ private readonly clientEvents;
9
+ private statusTimer;
10
+ constructor(port: number, clientEvents: ClientEvents);
11
+ stopStatusTimer(): void;
12
+ requestStatus(): Promise<DebugServerStatusResponse>;
13
+ requestTerminate(): Promise<void>;
14
+ private request;
15
+ }
16
+ export {};
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DebugServerClient = void 0;
4
+ const tslib_1 = require("tslib");
5
+ /* eslint-disable unicorn/filename-case */
6
+ const errors_1 = require("@oclif/errors");
7
+ const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
8
+ const abort_controller_1 = tslib_1.__importDefault(require("abort-controller"));
9
+ class DebugServerClient {
10
+ constructor(port, clientEvents) {
11
+ this.port = port;
12
+ this.clientEvents = clientEvents;
13
+ this.statusTimer = setInterval(async () => {
14
+ try {
15
+ const response = await this.requestStatus();
16
+ this.clientEvents.emit(response.status, response.message);
17
+ }
18
+ catch (error) {
19
+ this.clientEvents.emit("error", error);
20
+ }
21
+ }, 1000);
22
+ }
23
+ stopStatusTimer() {
24
+ clearInterval(this.statusTimer);
25
+ }
26
+ async requestStatus() {
27
+ const req = { method: "GET", path: "/status" };
28
+ const res = (await this.request(req));
29
+ if (res.status && res.message)
30
+ return res;
31
+ throw new errors_1.CLIError(`Invalid response from ${JSON.stringify(req)}: ${JSON.stringify(res)}`);
32
+ }
33
+ async requestTerminate() {
34
+ const req = { method: "POST", path: "/terminate" };
35
+ await this.request(req);
36
+ }
37
+ async request({ method, path }) {
38
+ const controller = new abort_controller_1.default();
39
+ const timeout = setTimeout(() => controller.abort(), 1000);
40
+ let response;
41
+ try {
42
+ response = await (0, node_fetch_1.default)(`http://localhost:${this.port}${path}`, {
43
+ method,
44
+ signal: controller.signal,
45
+ });
46
+ }
47
+ finally {
48
+ clearTimeout(timeout);
49
+ }
50
+ if (response.ok)
51
+ return response.json();
52
+ throw new errors_1.CLIError(`Request to debug server failed: ${method} ${path} => ${response.status} ${response.statusText}`);
53
+ }
54
+ }
55
+ exports.DebugServerClient = DebugServerClient;