-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommands.ts
More file actions
219 lines (202 loc) · 6.17 KB
/
Copy pathcommands.ts
File metadata and controls
219 lines (202 loc) · 6.17 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { checkForUpdates } from '@inquirerer/utils';
import { cliExitWithError, CLIOptions, extractFirst, getPackageJson,Inquirerer, ParsedArgs } from 'inquirerer';
import { teardownPgPools } from 'pg-cache';
import add from './commands/add';
import adminUsers from './commands/admin-users';
import analyze from './commands/analyze';
import cache from './commands/cache';
import clear from './commands/clear';
import deploy from './commands/deploy';
import docker from './commands/docker';
import doctor from './commands/doctor';
import dump from './commands/dump';
import env from './commands/env';
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 materialize from './commands/materialize';
import migrate from './commands/migrate';
import _package from './commands/package';
import plan from './commands/plan';
import regen from './commands/regen';
import remove from './commands/remove';
import renameCmd from './commands/rename';
import revert from './commands/revert';
import slice from './commands/slice';
import syncVersions from './commands/sync-versions';
import tag from './commands/tag';
import testPackages from './commands/test-packages';
import transform from './commands/transform';
import tune from './commands/tune';
import updateCmd from './commands/update';
import upgrade from './commands/upgrade';
import verify from './commands/verify';
import {
activateEngine,
deactivateEngine,
EngineArgv,
engineCommandBlocker,
getActiveEngine,
usageText
} from './utils';
/**
* Commands that never open a connection, so they must not activate a driver —
* a workspace-wide `engine` must not make plan/scaffolding commands depend on
* the driver plugin being installed. `init` additionally owns its own `--pglite`
* meaning (scaffold from the PGlite boilerplates), and runs before the plugin
* it scaffolds exists.
*/
const ENGINE_EXEMPT_COMMANDS = new Set([
'add',
'analyze',
'cache',
'doctor',
'env',
'extension',
'init',
'install',
'package',
'materialize',
'plan',
'regen',
'remove',
'rename',
'slice',
'sync-versions',
'tag',
'up',
'update',
'upgrade'
]);
const withPgTeardown = (fn: Function, skipTeardown: boolean = false) => async (...args: any[]) => {
try {
await fn(...args);
} finally {
if (!skipTeardown) {
await teardownPgPools();
}
}
};
export const createPgpmCommandMap = (skipPgTeardown: boolean = false): Record<string, Function> => {
const pgt = (fn: Function) => withPgTeardown(fn, skipPgTeardown);
return {
add,
'admin-users': pgt(adminUsers),
clear: pgt(clear),
deploy: pgt(deploy),
docker,
doctor,
dump: pgt(dump),
env,
verify: pgt(verify),
revert: pgt(revert),
remove: pgt(remove),
init: pgt(init),
extension: pgt(extension),
plan: pgt(plan),
regen,
export: pgt(_export),
package: pgt(_package),
tag: pgt(tag),
kill: pgt(kill),
install: pgt(install),
migrate: pgt(migrate),
materialize,
analyze: pgt(analyze),
rename: pgt(renameCmd),
slice,
'sync-versions': syncVersions,
'test-packages': pgt(testPackages),
transform: pgt(transform),
tune: pgt(tune),
upgrade: pgt(upgrade),
up: pgt(upgrade),
cache,
update: updateCmd
};
};
export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer, options: CLIOptions & { skipPgTeardown?: boolean }) => {
if (argv.version || argv.v) {
const pkg = getPackageJson(__dirname);
console.log(pkg.version);
process.exit(0);
}
let { first: command, newArgv } = extractFirst(argv);
if ((argv.help || argv.h || command === 'help') && !command) {
console.log(usageText);
process.exit(0);
}
if (command === 'help') {
console.log(usageText);
process.exit(0);
}
const commandMap = createPgpmCommandMap(options?.skipPgTeardown);
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;
}
// Run update check (skip on 'update' command to avoid redundant check)
// (checkForUpdates auto-skips in CI or when INQUIRERER_SKIP_UPDATE_CHECK / PGPM_SKIP_UPDATE_CHECK is set)
if (command !== 'update') {
try {
const pkg = getPackageJson(__dirname);
const updateResult = await checkForUpdates({
pkgName: pkg.name,
pkgVersion: pkg.version,
toolName: 'pgpm'
});
if (updateResult.hasUpdate && updateResult.message) {
console.warn(updateResult.message);
console.warn('Run pgpm update to upgrade.');
}
} catch {
// ignore update check failures
}
}
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.log(usageText);
await cliExitWithError(`Unknown command: ${command}`, { beforeExit: teardownPgPools });
}
// Activate the selected migration backend (`--engine`/`--driver`/`--pglite` or
// pgpm.json) before the command runs: the driver plugin registers its
// pool/client factories, so the unmodified engine targets it. The built-in
// `pg` engine activates nothing and behaves exactly as before.
const engineArgv = newArgv as unknown as EngineArgv & { cwd?: string };
const { engine, capabilities } = ENGINE_EXEMPT_COMMANDS.has(command)
? getActiveEngine()
: await activateEngine(engineArgv, engineArgv.cwd).catch(async (error: Error) => {
await cliExitWithError(error.message, { beforeExit: teardownPgPools });
throw error;
});
try {
const blocked = engineCommandBlocker(command, engine, capabilities);
if (blocked) {
await cliExitWithError(blocked, { beforeExit: teardownPgPools });
}
await commandFn(newArgv, prompter, options);
} finally {
await deactivateEngine();
}
prompter.close();
return argv;
};