-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprune.ts
86 lines (69 loc) · 2.47 KB
/
prune.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
import { bold, magenta, red } from 'chalk';
import { Command, flags } from '@oclif/command';
import { prune } from '../api';
import { loadConfig, resolveConnections } from '..';
import { printLine, printError, printInfo } from '../util/io';
import OperationResult from '../domain/operation/OperationResult';
class Prune extends Command {
static description = 'Drop all the synchronized db objects except the ones created via migrations.';
static flags = {
'dry-run': flags.boolean({ description: 'Dry run prune.', default: false }),
only: flags.string({
helpValue: 'CONNECTION_ID(s)',
description: 'Filter provided connection(s). Comma separated ids eg: id1,id2'
}),
'connection-resolver': flags.string({
helpValue: 'PATH',
description: 'Path to the connection resolver.'
}),
config: flags.string({
char: 'c',
description: 'Custom configuration file.'
})
};
/**
* Started event handler.
*/
onStarted = async (result: OperationResult) => {
await printLine(bold(` ▸ ${result.connectionId}`));
await printInfo(' [✓] Pruning - started');
};
/**
* Success handler.
*/
onSuccess = (result: OperationResult) => printInfo(` [✓] Pruning - completed (${result.timeElapsed}s)\n`);
/**
* Failure handler.
*/
onFailed = async (result: OperationResult) => {
await printLine(red(` [✖] Pruning - failed (${result.timeElapsed}s)\n`));
await printError(` ${result.error}\n`);
};
/**
* CLI command execution handler.
*
* @returns {Promise<void>}
*/
async run(): Promise<void> {
const { flags: parsedFlags } = this.parse(Prune);
const isDryRun = parsedFlags['dry-run'];
const config = await loadConfig(parsedFlags.config);
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
if (isDryRun) await printLine(magenta('\n• DRY RUN STARTED\n'));
const results = await prune(config, connections, {
...parsedFlags,
onStarted: this.onStarted,
onSuccess: this.onSuccess,
onFailed: this.onFailed
});
const failedCount = results.filter(({ success }) => !success).length;
if (failedCount === 0) {
if (isDryRun) await printLine(magenta('• DRY RUN ENDED\n'));
return process.exit(0);
}
printError(`Error: Prune failed for ${failedCount} connection(s).`);
if (isDryRun) await printLine(magenta('\n• DRY RUN ENDED\n'));
process.exit(-1);
}
}
export default Prune;