Skip to content

Commit c13a729

Browse files
committed
feat(cubejs-cli): Improve external commands support
1 parent a4d72fe commit c13a729

File tree

7 files changed

+66
-56
lines changed

7 files changed

+66
-56
lines changed

packages/cubejs-cli/src/cli.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,13 @@ program
283283
console.log(' $ cubejs deploy');
284284
});
285285

286-
configureDevServerCommand(program);
287-
configureServerCommand(program);
286+
(async () => {
287+
await configureDevServerCommand(program);
288+
await configureServerCommand(program);
288289

289-
if (!process.argv.slice(2).length) {
290-
program.help();
291-
}
290+
if (!process.argv.slice(2).length) {
291+
program.help();
292+
}
292293

293-
program.parse(process.argv);
294+
program.parse(process.argv);
295+
})();
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
import type { CommanderStatic } from 'commander';
2-
import { displayError, requireFromPackage, requiredPackageExists } from '../utils';
3-
4-
async function devServerCommand() {
5-
await requiredPackageExists('@cubejs-backend/server');
6-
7-
const OriginalCommandPackage = await requireFromPackage('@cubejs-backend/server/dist/command/dev-server');
8-
// eslint-disable-next-line new-cap
9-
const Command = new OriginalCommandPackage.default([]);
10-
return Command.run();
11-
}
2+
import { proxyCommand } from './proxy-command';
123

134
export function configureDevServerCommand(program: CommanderStatic) {
14-
program
15-
.command('dev-server')
16-
.description('Run server in Development mode')
17-
.action(
18-
() => devServerCommand()
19-
.catch((e) => displayError(e.stack || e))
20-
)
21-
.on('--help', () => {
22-
console.log('');
23-
console.log('Examples:');
24-
console.log('');
25-
console.log(' $ cubejs dev-server');
26-
});
5+
return proxyCommand(program, 'dev-server');
276
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { CommanderStatic } from 'commander';
2+
import { displayError, packageExists, requireFromPackage } from '../utils';
3+
import chalk from 'chalk';
4+
5+
export async function proxyCommand(program: CommanderStatic, command: string) {
6+
const serverPackageExists = packageExists('@cubejs-backend/server');
7+
8+
const commandInfo = program
9+
.command(command);
10+
11+
if (serverPackageExists) {
12+
const OriginalCommandPackage = await requireFromPackage(`@cubejs-backend/server/dist/command/${command}`);
13+
// eslint-disable-next-line new-cap
14+
const Command = new OriginalCommandPackage.default([]);
15+
16+
commandInfo
17+
.description(OriginalCommandPackage.default.description)
18+
.action(
19+
() => Command.run().catch(
20+
(e: any) => displayError(e.stack || e.message)
21+
)
22+
);
23+
} else {
24+
commandInfo
25+
.description(
26+
chalk.red('Unavailable.') + ' Please run this command from project directory.'
27+
)
28+
.action(
29+
() => displayError('Unavailable. Please run this command from project directory.')
30+
);
31+
}
32+
33+
commandInfo
34+
.on('--help', () => {
35+
console.log('');
36+
console.log('Examples:');
37+
console.log('');
38+
console.log(` $ cubejs ${command}`);
39+
});
40+
}
Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
import type { CommanderStatic } from 'commander';
2-
import { displayError, requiredPackageExists, requireFromPackage } from '../utils';
2+
import { proxyCommand } from './proxy-command';
33

4-
async function serverCommand() {
5-
await requiredPackageExists('@cubejs-backend/server');
6-
7-
const OriginalCommandPackage = await requireFromPackage('@cubejs-backend/server/dist/command/server');
8-
// eslint-disable-next-line new-cap
9-
const Command = new OriginalCommandPackage.default([]);
10-
return Command.run();
11-
}
12-
13-
export function configureServerCommand(program: CommanderStatic) {
14-
program
15-
.command('server')
16-
.description('Run server in Production mode')
17-
.action(
18-
() => serverCommand()
19-
.catch((e) => displayError(e.stack || e))
20-
)
21-
.on('--help', () => {
22-
console.log('');
23-
console.log('Examples:');
24-
console.log('');
25-
console.log(' $ cubejs server');
26-
});
4+
export async function configureServerCommand(program: CommanderStatic) {
5+
return proxyCommand(program, 'server');
276
}

packages/cubejs-cli/src/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,35 @@ export const event = async (name: string, props: any) => {
4545
}
4646
};
4747

48-
export const displayError = async (text: string, options = {}) => {
48+
export const displayError = async (text: string|string[], options = {}) => {
4949
console.error('');
5050
console.error(chalk.cyan('Cube.js Error ---------------------------------------'));
5151
console.error('');
52+
5253
if (Array.isArray(text)) {
5354
text.forEach((str) => console.error(str));
5455
} else {
5556
console.error(text);
5657
}
58+
5759
console.error('');
5860
console.error(chalk.yellow('Need some help? -------------------------------------'));
61+
5962
await event('Error', { error: Array.isArray(text) ? text.join('\n') : text.toString(), ...options });
63+
6064
console.error('');
6165
console.error(`${chalk.yellow(' Ask this question in Cube.js Slack:')} https://slack.cube.dev`);
6266
console.error(`${chalk.yellow(' Post an issue:')} https://github.com/cube-js/cube.js/issues`);
6367
console.error('');
68+
6469
process.exit(1);
6570
};
6671

72+
export const packageExists = (moduleName: string) => {
73+
const modulePath = path.join(process.cwd(), 'node_modules', moduleName);
74+
return fs.pathExistsSync(modulePath);
75+
}
76+
6777
export const requiredPackageExists = async (moduleName: string) => {
6878
const modulePath = path.join(process.cwd(), 'node_modules', moduleName);
6979

packages/cubejs-server/src/command/dev-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Command, flags} from '@oclif/command';
1+
import { Command, flags } from '@oclif/command';
22

33
export default class DevServer extends Command {
44
static description = 'Run server in Development mode';

packages/cubejs-server/src/command/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Command, flags} from '@oclif/command';
1+
import { Command, flags } from '@oclif/command';
22

33
export default class Server extends Command {
44
static description = 'Run server in Production mode';

0 commit comments

Comments
 (0)