This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstop.ts
127 lines (109 loc) · 3.6 KB
/
stop.ts
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
import { Command } from '@oclif/command';
import * as inquirer from 'inquirer';
import { ProcessDescription } from 'pm2';
import { commonFlags } from '../constants/command.constants';
import { Messages } from '../constants/messages.constants';
import { cleanDataFiles } from '../libs/data';
import { ProcessListManager, ProcessManager } from '../libs/process-manager';
import { logProcesses } from '../libs/utils';
export default class Stop extends Command {
public static description = 'Stop a mock API';
public static examples = [
'$ mockoon-cli stop',
'$ mockoon-cli stop 0',
'$ mockoon-cli stop "name"',
'$ mockoon-cli stop "all"'
];
public static flags = {
...commonFlags
};
public static args = [
{
name: 'id',
description: 'Running API pid or name',
required: false
}
];
public async run(): Promise<void> {
const { args } = this.parse(Stop);
let relistProcesses = false;
let processesToStop: (string | number)[] = [];
const processes: ProcessDescription[] = await ProcessManager.list();
if (processes.length === 0) {
this.log(Messages.CLI.NO_RUNNING_PROCESS);
ProcessManager.disconnect();
return;
}
// prompt for process name or id
if (args.id === undefined) {
const response: { process: string } = await inquirer.prompt([
{
name: 'process',
message: 'Please select a process',
type: 'list',
choices: processes.map((process) => ({
name: process.name || process.pid
}))
}
]);
processesToStop.push(response.process);
} else if (args.id === 'all') {
// list all mockoon's processes to stop
processesToStop = processes.reduce<(string | number)[]>(
(processes1, process) => {
const nameOrId = process.name || process.pm_id;
if (nameOrId !== undefined) {
processes1.push(nameOrId);
}
return processes1;
},
[]
);
} else {
processesToStop.push(args.id);
}
for (const processToStop of processesToStop) {
try {
// typing is wrong, delete() returns an array
const stoppedProcesses: ProcessDescription[] =
(await ProcessManager.delete(processToStop)) as ProcessDescription[];
// verify that something has been stopped
stoppedProcesses.forEach((stoppedProcess) => {
if (stoppedProcess !== undefined) {
this.log(
Messages.CLI.PROCESS_STOPPED,
stoppedProcess.pm_id,
stoppedProcess.name
);
ProcessListManager.deleteProcess(stoppedProcess.name);
}
});
} catch (error: any) {
if (error.message === 'process name not found' && args.id === 'all') {
// if 'all' was specified and no process was stopped, do not list and immediately exit
this.log(Messages.CLI.NO_RUNNING_PROCESS);
} else {
this.error(error.message, { exit: false });
relistProcesses = true;
}
}
}
try {
const runningProcesses: ProcessDescription[] =
await ProcessManager.list();
if (relistProcesses) {
if (runningProcesses.length) {
this.log(Messages.CLI.RUNNING_PROCESSES);
logProcesses(runningProcesses);
} else {
this.log(Messages.CLI.NO_RUNNING_PROCESS);
}
}
// always clean data files after a stop
await cleanDataFiles(runningProcesses);
} catch (error: any) {
this.error(error.message, { exit: false });
}
ProcessManager.disconnect();
}
}