Skip to content

Commit

Permalink
feat(cubejs-cli): Improve external commands support
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Oct 20, 2020
1 parent a4d72fe commit c13a729
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 56 deletions.
14 changes: 8 additions & 6 deletions packages/cubejs-cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,13 @@ program
console.log(' $ cubejs deploy');
});

configureDevServerCommand(program);
configureServerCommand(program);
(async () => {
await configureDevServerCommand(program);
await configureServerCommand(program);

if (!process.argv.slice(2).length) {
program.help();
}
if (!process.argv.slice(2).length) {
program.help();
}

program.parse(process.argv);
program.parse(process.argv);
})();
25 changes: 2 additions & 23 deletions packages/cubejs-cli/src/command/dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import type { CommanderStatic } from 'commander';
import { displayError, requireFromPackage, requiredPackageExists } from '../utils';

async function devServerCommand() {
await requiredPackageExists('@cubejs-backend/server');

const OriginalCommandPackage = await requireFromPackage('@cubejs-backend/server/dist/command/dev-server');
// eslint-disable-next-line new-cap
const Command = new OriginalCommandPackage.default([]);
return Command.run();
}
import { proxyCommand } from './proxy-command';

export function configureDevServerCommand(program: CommanderStatic) {
program
.command('dev-server')
.description('Run server in Development mode')
.action(
() => devServerCommand()
.catch((e) => displayError(e.stack || e))
)
.on('--help', () => {
console.log('');
console.log('Examples:');
console.log('');
console.log(' $ cubejs dev-server');
});
return proxyCommand(program, 'dev-server');
}
40 changes: 40 additions & 0 deletions packages/cubejs-cli/src/command/proxy-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CommanderStatic } from 'commander';
import { displayError, packageExists, requireFromPackage } from '../utils';
import chalk from 'chalk';

export async function proxyCommand(program: CommanderStatic, command: string) {
const serverPackageExists = packageExists('@cubejs-backend/server');

const commandInfo = program
.command(command);

if (serverPackageExists) {
const OriginalCommandPackage = await requireFromPackage(`@cubejs-backend/server/dist/command/${command}`);
// eslint-disable-next-line new-cap
const Command = new OriginalCommandPackage.default([]);

commandInfo
.description(OriginalCommandPackage.default.description)
.action(
() => Command.run().catch(
(e: any) => displayError(e.stack || e.message)
)
);
} else {
commandInfo
.description(
chalk.red('Unavailable.') + ' Please run this command from project directory.'
)
.action(
() => displayError('Unavailable. Please run this command from project directory.')
);
}

commandInfo
.on('--help', () => {
console.log('');
console.log('Examples:');
console.log('');
console.log(` $ cubejs ${command}`);
});
}
27 changes: 3 additions & 24 deletions packages/cubejs-cli/src/command/server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import type { CommanderStatic } from 'commander';
import { displayError, requiredPackageExists, requireFromPackage } from '../utils';
import { proxyCommand } from './proxy-command';

async function serverCommand() {
await requiredPackageExists('@cubejs-backend/server');

const OriginalCommandPackage = await requireFromPackage('@cubejs-backend/server/dist/command/server');
// eslint-disable-next-line new-cap
const Command = new OriginalCommandPackage.default([]);
return Command.run();
}

export function configureServerCommand(program: CommanderStatic) {
program
.command('server')
.description('Run server in Production mode')
.action(
() => serverCommand()
.catch((e) => displayError(e.stack || e))
)
.on('--help', () => {
console.log('');
console.log('Examples:');
console.log('');
console.log(' $ cubejs server');
});
export async function configureServerCommand(program: CommanderStatic) {
return proxyCommand(program, 'server');
}
12 changes: 11 additions & 1 deletion packages/cubejs-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,35 @@ export const event = async (name: string, props: any) => {
}
};

export const displayError = async (text: string, options = {}) => {
export const displayError = async (text: string|string[], options = {}) => {
console.error('');
console.error(chalk.cyan('Cube.js Error ---------------------------------------'));
console.error('');

if (Array.isArray(text)) {
text.forEach((str) => console.error(str));
} else {
console.error(text);
}

console.error('');
console.error(chalk.yellow('Need some help? -------------------------------------'));

await event('Error', { error: Array.isArray(text) ? text.join('\n') : text.toString(), ...options });

console.error('');
console.error(`${chalk.yellow(' Ask this question in Cube.js Slack:')} https://slack.cube.dev`);
console.error(`${chalk.yellow(' Post an issue:')} https://github.com/cube-js/cube.js/issues`);
console.error('');

process.exit(1);
};

export const packageExists = (moduleName: string) => {
const modulePath = path.join(process.cwd(), 'node_modules', moduleName);
return fs.pathExistsSync(modulePath);
}

export const requiredPackageExists = async (moduleName: string) => {
const modulePath = path.join(process.cwd(), 'node_modules', moduleName);

Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-server/src/command/dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, flags} from '@oclif/command';
import { Command, flags } from '@oclif/command';

export default class DevServer extends Command {
static description = 'Run server in Development mode';
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-server/src/command/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, flags} from '@oclif/command';
import { Command, flags } from '@oclif/command';

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

0 comments on commit c13a729

Please sign in to comment.