-
Notifications
You must be signed in to change notification settings - Fork 676
Expand file tree
/
Copy pathplatform.ts
More file actions
179 lines (167 loc) · 5.69 KB
/
platform.ts
File metadata and controls
179 lines (167 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type { ProjectCommandArguments, WorkspaceGrantArgument } from '../../types/commands/common_arguments';
import type { ShellProcess } from '../../types/shell';
import { SlackTracerId } from '../../utils/constants';
import logger from '../../utils/logger';
import { type SlackCLICommandOptions, SlackCLIProcess } from '../cli-process';
import { shell } from '../shell';
export interface StringWaitArgument {
/** @description String to wait for in the command output before this function returns. */
stringToWaitFor: string;
}
export interface TimeoutArgument {
/** @description Number of milliseconds to wait for process execution. Defaults to 10 seconds. */
timeout?: number;
}
export interface ProcessArgument {
/** @description CLI process previous created via a `*Start` command. */
proc: ShellProcess;
}
export interface RunDeployArguments extends WorkspaceGrantArgument {
/** @description Hides output and prompts related to triggers. Defaults to `true`. */
hideTriggers?: boolean;
/** @description Delete the app after `run` process finishes. Defaults to `true`. */
cleanup?: boolean;
}
/**
* `slack platform activity`
* @returns command output
*/
export const activity = async function activity(
args: ProjectCommandArguments & {
/** @description Source of logs to filter; can be `slack` or `developer`. */
source?: 'slack' | 'developer';
},
): Promise<string> {
const cmdOpts: SlackCLICommandOptions = {};
if ('source' in args) {
cmdOpts['--source'] = args.source;
}
const cmd = new SlackCLIProcess(['activity'], args, cmdOpts);
const proc = await cmd.execAsync({
cwd: args.appPath,
});
return proc.output;
};
/**
* `slack platform activity` but waits for a specified sequence then returns the shell
* At the specific point where the sequence is found to continue with test
* @returns command output
*/
export const activityTailStart = async function activityTailStart(
args: ProjectCommandArguments & StringWaitArgument & TimeoutArgument,
): Promise<ShellProcess> {
const cmd = new SlackCLIProcess(['activity'], args, { '--tail': true });
const proc = await cmd.execAsyncUntilOutputPresent(args.stringToWaitFor, {
cwd: args.appPath,
timeout: args.timeout,
});
return proc;
};
/**
* Waits for a specified string in the provided `activityTailStart` process output,
* kills the process then returns the output
* @returns command output
*/
export const activityTailStop = async function activityTailStop(
args: StringWaitArgument & ProcessArgument & TimeoutArgument,
): Promise<string> {
return new Promise((resolve, reject) => {
// Wait for output
shell.waitForOutput(args.stringToWaitFor, args.proc, { timeout: args.timeout }).then(() => {
// kill the shell process
shell.kill(args.proc).then(
() => {
resolve(args.proc.output);
},
(err) => {
const msg = `activityTailStop command failed to kill process: ${err}`;
logger.warn(msg);
reject(new Error(msg));
},
);
}, reject);
});
};
/**
* `slack deploy`
* @returns command output
*/
export const deploy = async function deploy(
args: ProjectCommandArguments & Omit<RunDeployArguments, 'cleanup'>,
): Promise<string> {
const cmd = new SlackCLIProcess(['deploy'], args, {
'--hide-triggers': typeof args.hideTriggers !== 'undefined' ? args.hideTriggers : true,
'--org-workspace-grant': args.orgWorkspaceGrantFlag,
});
const proc = await cmd.execAsync({
cwd: args.appPath,
});
return proc.output;
};
/**
* start `slack run`. `runStop` must be used to stop the `run` process returned by this method.
* @returns shell object to kill it explicitly in the test case via `runStop`
*/
export const runStart = async function runStart(
args: ProjectCommandArguments & RunDeployArguments & TimeoutArgument,
): Promise<ShellProcess> {
const cmd = new SlackCLIProcess(['run'], args, {
'--app': 'local',
'--cleanup': typeof args.cleanup !== 'undefined' ? args.cleanup : true,
'--hide-triggers': typeof args.hideTriggers !== 'undefined' ? args.hideTriggers : true,
'--org-workspace-grant': args.orgWorkspaceGrantFlag,
});
const proc = await cmd.execAsyncUntilOutputPresent(SlackTracerId.SLACK_TRACE_PLATFORM_RUN_START, {
cwd: args.appPath,
timeout: args.timeout,
});
return proc;
};
/**
* stop `slack run`
* @param teamName to check that app was deleted from that team
*/
export const runStop = async function runStop(
args: ProcessArgument &
TimeoutArgument & {
/**
* @description Should wait for the `run` process to spin down before exiting this function.
* On Windows, this property is always set to `true`. Defaults to `false`.
*/
waitForShutdown?: boolean;
},
): Promise<void> {
return new Promise((resolve, reject) => {
// kill the shell process
shell.kill(args.proc).then(
() => {
// Due to the complexity of gracefully shutting down processes on Windows / lack of interrupt signal support,
// we don't wait for the SLACK_TRACE_PLATFORM_RUN_STOP trace on Windows
if (process.platform === 'win32') {
resolve();
}
if (args.waitForShutdown) {
// Wait for the output to verify process stopped
shell
.waitForOutput(SlackTracerId.SLACK_TRACE_PLATFORM_RUN_STOP, args.proc, { timeout: args.timeout })
.then(resolve, reject);
} else {
resolve();
}
},
(err) => {
const msg = `runStop command failed to kill process: ${err}`;
logger.warn(msg);
reject(new Error(msg));
},
);
});
};
export default {
activity,
activityTailStart,
activityTailStop,
deploy,
runStart,
runStop,
};