-
Notifications
You must be signed in to change notification settings - Fork 684
Expand file tree
/
Copy pathcli-process.ts
More file actions
166 lines (154 loc) · 5.72 KB
/
Copy pathcli-process.ts
File metadata and controls
166 lines (154 loc) · 5.72 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
import type { SpawnOptionsWithoutStdio } from 'node:child_process';
import type { ShellProcess } from '../types/shell';
import { shell } from './shell';
export interface SlackCLIGlobalOptions {
/**
* @description The API host the command should interact with, full domain name. (`--apihost qa1234.slack.com`)
* Takes precendence over `qa` and `dev` options.
* @example `qa1234.slack.com` or `dev2345.slack.com`
*/
apihost?: string;
/**
* @description Whether the command should interact with dev.slack (`--slackdev`)
* `qa` and `apihost` will both supersede this option.
*/
dev?: boolean;
/** @description Ignore warnings and continue executing command. Defaults to `true`. */
force?: boolean;
/**
* @description Application instance to target. Can be `local`, `deployed` or an app ID string.
* Defaults to `deployed`.
*/
app?: 'local' | 'deployed' | string;
/**
* @description Whether the command should interact with qa.slack (`--apihost qa.slack.com`)
* Takes precendence over `dev` option but is superseded by `apihost`.
*/
qa?: boolean;
/**
* @description Whether the CLI should skip updating (`--skip-update`). Defaults to `true`.
*/
skipUpdate?: boolean;
/**
* @description The ID of your team. If you are using a Standard Slack plan, this is your workspace ID.
* If you are using an Enterprise Grid plan, this is the organization ID, even if your app is only granted to a
* subset of workspaces within the org.
*/
team?: string;
/** @description Access token to use when making Slack API calls. */
token?: string;
/** @description Print debug logging and additional CLI information. */
verbose?: boolean;
}
export type SlackCLIHostTargetOptions = Pick<SlackCLIGlobalOptions, 'qa' | 'dev' | 'apihost'>;
export type SlackCLICommandOptions = Record<string, string | boolean | number | undefined>;
export class SlackCLIProcess {
/**
* @description The CLI command to invoke
*/
public command: string[];
/**
* @description The global CLI options to pass to the command
*/
public globalOptions: SlackCLIGlobalOptions | undefined;
/**
* @description The CLI command-specific options to pass to the command
*/
public commandOptions: SlackCLICommandOptions | undefined;
public constructor(
command: string[],
globalOptions?: SlackCLIGlobalOptions,
commandOptions?: SlackCLICommandOptions,
) {
if (!process.env.SLACK_CLI_PATH) {
throw new Error('`SLACK_CLI_PATH` environment variable not found! Aborting!');
}
this.command = command;
this.globalOptions = globalOptions;
this.commandOptions = commandOptions;
}
/**
* @description Executes the command asynchronously, returning the process details once the process finishes executing
*/
public async execAsync(shellOpts?: Partial<SpawnOptionsWithoutStdio>): Promise<ShellProcess> {
const cmd = this.assembleShellInvocation();
// biome-ignore lint/style/noNonNullAssertion: the constructor checks for the truthiness of this environment variable
const proc = shell.spawnProcess(process.env.SLACK_CLI_PATH!, cmd, shellOpts);
await shell.checkIfFinished(proc);
return proc;
}
/**
* @description Executes the command asynchronously, returning the process details once the process finishes executing
*/
public async execAsyncUntilOutputPresent(
output: string,
shellOpts?: Partial<SpawnOptionsWithoutStdio>,
): Promise<ShellProcess> {
const cmd = this.assembleShellInvocation();
// biome-ignore lint/style/noNonNullAssertion: the constructor checks for the truthiness of this environment variable
const proc = shell.spawnProcess(process.env.SLACK_CLI_PATH!, cmd, shellOpts);
await shell.waitForOutput(output, proc, {
timeout: shellOpts?.timeout,
});
return proc;
}
/**
* @description Executes the command synchronously, returning the process standard output
*/
public execSync(shellOpts?: Partial<SpawnOptionsWithoutStdio>): string {
const cmd = this.assembleShellInvocation();
// biome-ignore lint/style/noNonNullAssertion: the constructor checks for the truthiness of this environment variable
return shell.runCommandSync(process.env.SLACK_CLI_PATH!, cmd, shellOpts);
}
private assembleShellInvocation(): string[] {
let cmd: string[] = [];
if (this.globalOptions) {
const opts = this.globalOptions;
// Determine API host target
if (opts.apihost) {
cmd = cmd.concat(['--apihost', opts.apihost]);
} else if (opts.qa) {
cmd = cmd.concat(['--apihost', 'qa.slack.com']);
} else if (opts.dev) {
cmd = cmd.concat(['--slackdev']);
}
// Always skip update unless explicitly set to something falsy
if (opts.skipUpdate || opts.skipUpdate === undefined) {
cmd = cmd.concat(['--skip-update']);
}
// Target team
if (opts.team) {
cmd = cmd.concat(['--team', opts.team]);
}
// App instance
if (opts.app) {
cmd = cmd.concat(['--app', opts.app]);
}
// Ignore warnings via --force; defaults to true
if (opts.force || typeof opts.force === 'undefined') {
cmd = cmd.concat(['--force']);
}
// Specifying custom token
if (opts.token) {
cmd = cmd.concat(['--token', opts.token]);
}
if (opts.verbose) {
cmd = cmd.concat(['--verbose']);
}
} else {
cmd = cmd.concat(['--skip-update', '--force']);
}
cmd = cmd.concat(this.command);
if (this.commandOptions) {
for (const [key, value] of Object.entries(this.commandOptions)) {
if (key && value) {
cmd.push(key);
if (value !== true) {
cmd.push(String(value));
}
}
}
}
return cmd;
}
}