File tree Expand file tree Collapse file tree 7 files changed +66
-56
lines changed
cubejs-server/src/command Expand file tree Collapse file tree 7 files changed +66
-56
lines changed Original file line number Diff line number Diff line change @@ -283,11 +283,13 @@ program
283
283
console . log ( ' $ cubejs deploy' ) ;
284
284
} ) ;
285
285
286
- configureDevServerCommand ( program ) ;
287
- configureServerCommand ( program ) ;
286
+ ( async ( ) => {
287
+ await configureDevServerCommand ( program ) ;
288
+ await configureServerCommand ( program ) ;
288
289
289
- if ( ! process . argv . slice ( 2 ) . length ) {
290
- program . help ( ) ;
291
- }
290
+ if ( ! process . argv . slice ( 2 ) . length ) {
291
+ program . help ( ) ;
292
+ }
292
293
293
- program . parse ( process . argv ) ;
294
+ program . parse ( process . argv ) ;
295
+ } ) ( ) ;
Original file line number Diff line number Diff line change 1
1
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' ;
12
3
13
4
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' ) ;
27
6
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
import type { CommanderStatic } from 'commander' ;
2
- import { displayError , requiredPackageExists , requireFromPackage } from '../utils ' ;
2
+ import { proxyCommand } from './proxy-command ' ;
3
3
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' ) ;
27
6
}
Original file line number Diff line number Diff line change @@ -45,25 +45,35 @@ export const event = async (name: string, props: any) => {
45
45
}
46
46
} ;
47
47
48
- export const displayError = async ( text : string , options = { } ) => {
48
+ export const displayError = async ( text : string | string [ ] , options = { } ) => {
49
49
console . error ( '' ) ;
50
50
console . error ( chalk . cyan ( 'Cube.js Error ---------------------------------------' ) ) ;
51
51
console . error ( '' ) ;
52
+
52
53
if ( Array . isArray ( text ) ) {
53
54
text . forEach ( ( str ) => console . error ( str ) ) ;
54
55
} else {
55
56
console . error ( text ) ;
56
57
}
58
+
57
59
console . error ( '' ) ;
58
60
console . error ( chalk . yellow ( 'Need some help? -------------------------------------' ) ) ;
61
+
59
62
await event ( 'Error' , { error : Array . isArray ( text ) ? text . join ( '\n' ) : text . toString ( ) , ...options } ) ;
63
+
60
64
console . error ( '' ) ;
61
65
console . error ( `${ chalk . yellow ( ' Ask this question in Cube.js Slack:' ) } https://slack.cube.dev` ) ;
62
66
console . error ( `${ chalk . yellow ( ' Post an issue:' ) } https://github.com/cube-js/cube.js/issues` ) ;
63
67
console . error ( '' ) ;
68
+
64
69
process . exit ( 1 ) ;
65
70
} ;
66
71
72
+ export const packageExists = ( moduleName : string ) => {
73
+ const modulePath = path . join ( process . cwd ( ) , 'node_modules' , moduleName ) ;
74
+ return fs . pathExistsSync ( modulePath ) ;
75
+ }
76
+
67
77
export const requiredPackageExists = async ( moduleName : string ) => {
68
78
const modulePath = path . join ( process . cwd ( ) , 'node_modules' , moduleName ) ;
69
79
Original file line number Diff line number Diff line change 1
- import { Command , flags } from '@oclif/command' ;
1
+ import { Command , flags } from '@oclif/command' ;
2
2
3
3
export default class DevServer extends Command {
4
4
static description = 'Run server in Development mode' ;
Original file line number Diff line number Diff line change 1
- import { Command , flags } from '@oclif/command' ;
1
+ import { Command , flags } from '@oclif/command' ;
2
2
3
3
export default class Server extends Command {
4
4
static description = 'Run server in Production mode' ;
You can’t perform that action at this time.
0 commit comments