/
migrate-list.ts
94 lines (77 loc) · 2.57 KB
/
migrate-list.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
import { Command, flags } from '@oclif/command';
import { bold, grey, red, cyan, yellow } from 'chalk';
import { migrateList } from '../api';
import { printLine, printError } from '../util/io';
import { loadConfig, resolveConnections } from '..';
import OperationResult from '../domain/operation/OperationResult';
class MigrateList extends Command {
static description = 'List all the migrations.';
static flags = {
only: flags.string({
helpValue: 'CONNECTION_ID',
description: 'Filter only a single connection.'
}),
'connection-resolver': flags.string({
helpValue: 'PATH',
description: 'Path to the connection resolver.'
}),
config: flags.string({
char: 'c',
description: 'Custom configuration file.'
})
};
/**
* Success handler.
*/
onSuccess = async (result: OperationResult) => {
await printLine(bold(` ▸ ${result.connectionId}`));
const [completedList, remainingList] = result.data;
const ranCount = completedList.length;
const remainingCount = remainingList.length;
// Completed migrations.
for (const item of completedList) {
await printLine(cyan(` • ${item}`));
}
// Remaining Migrations
for (const item of remainingList) {
await printLine(grey(` - ${item}`));
}
if (ranCount === 0 && remainingCount === 0) {
await printLine(yellow(' No migrations.'));
} else if (remainingCount > 0) {
await printLine(yellow(`\n ${remainingList.length} migrations yet to be run.`));
} else if (remainingCount === 0) {
await printLine('\n All up to date.');
}
await printLine();
};
/**
* Failure handler.
*/
onFailed = async (result: OperationResult) => {
printLine(bold(red(` ▸ ${result.connectionId} - Failed`)));
await printError(` ${result.error}\n`);
};
/**
* CLI command execution handler.
*
* @returns {Promise<void>}
*/
async run(): Promise<void> {
const { flags: parsedFlags } = this.parse(MigrateList);
const config = await loadConfig(parsedFlags.config);
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
const results = await migrateList(config, connections, {
...parsedFlags,
onSuccess: this.onSuccess,
onFailed: this.onFailed
});
const failedCount = results.filter(({ success }) => !success).length;
if (failedCount === 0) {
return process.exit(0);
}
printError(`Error: Failed retrieving list for ${failedCount} connection(s).`);
process.exit(-1);
}
}
export default MigrateList;