-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
proxy-command.ts
102 lines (87 loc) · 2.85 KB
/
proxy-command.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
import { CommanderStatic } from 'commander';
import chalk from 'chalk';
import semver from 'semver';
import {
isDockerImage,
packageExists,
requireFromPackage,
requirePackageManifest,
} from '@cubejs-backend/shared';
import type { Command, flags } from '@oclif/command';
import { displayError, loadCliManifest, } from '../utils';
export async function proxyCommand(program: CommanderStatic, command: string) {
const relative = isDockerImage();
const serverPackageExists = packageExists('@cubejs-backend/server', relative);
const commandInfo = program
.command(command);
if (serverPackageExists) {
const packageManifest = await requirePackageManifest<any>('@cubejs-backend/server', {
relative,
});
if (packageManifest.cubejsCliVersion) {
const cliManifest = loadCliManifest();
if (semver.satisfies(cliManifest.version, packageManifest.cubejsCliVersion)) {
const OriginalCommandPackage = requireFromPackage<any>(
`@cubejs-backend/server/dist/command/${command}.js`,
{
relative,
}
);
commandInfo
.description(OriginalCommandPackage.default.description);
if (OriginalCommandPackage.default.flags) {
const commandFlags: Record<string, flags.IFlag<any>> = OriginalCommandPackage.default.flags;
// eslint-disable-next-line no-restricted-syntax
for (const [name, option] of Object.entries(commandFlags)) {
commandInfo
.option(`--${name}`, option.description || '', option.default);
}
}
commandInfo.action(async () => {
try {
// eslint-disable-next-line new-cap
const CommandInstance: Command = new OriginalCommandPackage.default(process.argv.slice(3));
await CommandInstance.run();
} catch (e) {
displayError(e.stack || e.message);
}
});
return;
}
const message = `${chalk.red('Unavailable.')} @cubejs-backend/server inside current directory requires ` +
`cubejs-cli (${packageManifest.cubejsCliVersion}).`;
commandInfo
.description(
message
)
.action(
() => displayError(message)
);
return;
}
const message = `${chalk.red('Unavailable.')} Please upgrade @cubejs-backend/server.`;
commandInfo
.description(
message
)
.action(
() => displayError(message)
);
} else {
const message = `${chalk.red('Unavailable.')} Please run this command from project directory.`;
commandInfo
.description(
message
)
.action(
() => displayError(message)
);
}
commandInfo
.on('--help', () => {
console.log('');
console.log('Examples:');
console.log('');
console.log(` $ cubejs ${command}`);
});
}