-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommands.ts
More file actions
127 lines (112 loc) · 3.42 KB
/
commands.ts
File metadata and controls
127 lines (112 loc) · 3.42 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
import { CLIOptions, Inquirerer } from 'inquirerer';
import { ParsedArgs } from 'minimist';
import { teardownPgPools } from 'pg-cache';
// Commands
import adminUsers from './commands/admin-users';
import clear from './commands/clear';
import deploy from './commands/deploy';
import explorer from './commands/explorer';
import _export from './commands/export';
import extension from './commands/extension';
import init from './commands/init';
import install from './commands/install';
import kill from './commands/kill';
import migrate from './commands/migrate';
import _package from './commands/package';
import plan from './commands/plan';
import remove from './commands/remove';
import revert from './commands/revert';
import server from './commands/server';
import tag from './commands/tag';
import verify from './commands/verify';
import version from './commands/version';
import analyze from './commands/analyze';
import renameCmd from './commands/rename';
import { readAndParsePackageJson } from './package';
import { extractFirst, usageText } from './utils';
const withPgTeardown = (fn: Function, skipTeardown: boolean = false) => async (...args: any[]) => {
try {
await fn(...args);
} finally {
if (!skipTeardown) {
await teardownPgPools();
}
}
};
const createCommandMap = (skipPgTeardown: boolean = false): Record<string, Function> => {
const pgt = (fn: Function) => withPgTeardown(fn, skipPgTeardown);
return {
'admin-users': pgt(adminUsers),
clear: pgt(clear),
deploy: pgt(deploy),
verify: pgt(verify),
revert: pgt(revert),
remove: pgt(remove),
init: pgt(init),
extension: pgt(extension),
plan: pgt(plan),
export: pgt(_export),
package: pgt(_package),
tag: pgt(tag),
version: pgt(version),
kill: pgt(kill),
install: pgt(install),
migrate: pgt(migrate),
analyze: pgt(analyze),
rename: pgt(renameCmd),
// These manage their own connection lifecycles
server,
explorer
};
};
export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer, options: CLIOptions & { skipPgTeardown?: boolean }) => {
if (argv.version || argv.v) {
const pkg = readAndParsePackageJson();
console.log(pkg.version);
process.exit(0);
}
let { first: command, newArgv } = extractFirst(argv);
// Show usage if explicitly requested but no command specified
if ((argv.help || argv.h || command === 'help') && !command) {
console.log(usageText);
process.exit(0);
}
// Show usage for help command specifically
if (command === 'help') {
console.log(usageText);
process.exit(0);
}
const commandMap = createCommandMap(options?.skipPgTeardown);
// Prompt if no command provided
if (!command) {
const answer = await prompter.prompt(argv, [
{
type: 'autocomplete',
name: 'command',
message: 'What do you want to do?',
options: Object.keys(commandMap)
}
]);
command = answer.command;
}
// Prompt for working directory
newArgv = await prompter.prompt(newArgv, [
{
type: 'text',
name: 'cwd',
message: 'Working directory',
required: false,
default: process.cwd(),
useDefault: true
}
]);
const commandFn = commandMap[command];
if (!commandFn) {
console.error(`Unknown command: ${command}`);
console.log(usageText);
process.exit(1);
}
await commandFn(newArgv, prompter, options);
prompter.close();
return argv;
};