Skip to content

Commit

Permalink
feat(cubejs-cli): Run dev-server/server commands from @cubejs-backend…
Browse files Browse the repository at this point in the history
…/core
  • Loading branch information
ovr committed Oct 20, 2020
1 parent fed9285 commit a4d72fe
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/cubejs-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build": "tsc",
"test": "npm run unit",
"unit": "jest dist",
"lint": "eslint -c ../../.eslintrc.js *.js"
"lint": "eslint -c ../../.eslintrc.js src/"
},
"files": [
"dist",
Expand Down
20 changes: 13 additions & 7 deletions packages/cubejs-cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ eslint import/no-dynamic-require: 0
eslint global-require: 0
*/

const program = require('commander');
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const crypto = require('crypto');
const inquirer = require('inquirer');
import program from 'commander';
import fs from 'fs-extra';
import path from 'path';
import chalk from 'chalk';
import crypto from 'crypto';
import inquirer from 'inquirer';

import { configureDevServerCommand } from './command/dev-server';
import { configureServerCommand } from './command/server';
import { executeCommand, npmInstall, writePackageJson, requireFromPackage, event, displayError } from './utils';

const Config = require('./config');
const templates = require('./templates');
const { deploy } = require('./deploy');
const { token, defaultExpiry, collect } = require('./token');
import { executeCommand, npmInstall, writePackageJson, requireFromPackage, event, displayError } from './utils';

const packageJson = require('../package.json');

Expand Down Expand Up @@ -280,6 +283,9 @@ program
console.log(' $ cubejs deploy');
});

configureDevServerCommand(program);
configureServerCommand(program);

if (!process.argv.slice(2).length) {
program.help();
}
Expand Down
27 changes: 27 additions & 0 deletions packages/cubejs-cli/src/command/dev-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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();
}

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');
});
}
27 changes: 27 additions & 0 deletions packages/cubejs-cli/src/command/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { CommanderStatic } from 'commander';
import { displayError, requiredPackageExists, requireFromPackage } from '../utils';

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');
});
}
13 changes: 8 additions & 5 deletions packages/cubejs-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ export const displayError = async (text: string, options = {}) => {
process.exit(1);
};

export const requireFromPackage = async (moduleName: string) => {
if (
!(await fs.pathExists(path.join(process.cwd(), 'node_modules', moduleName))) &&
!(await fs.pathExists(path.join(process.cwd(), 'node_modules', `${module}.js`)))
) {
export const requiredPackageExists = async (moduleName: string) => {
const modulePath = path.join(process.cwd(), 'node_modules', moduleName);

if (!fs.pathExistsSync(modulePath) && !fs.pathExistsSync(`${modulePath}.js`)) {
await displayError(
`${moduleName} dependency not found. Please run this command from project directory.`
);
}
};

export const requireFromPackage = async (moduleName: string) => {
await requiredPackageExists(moduleName);

// eslint-disable-next-line global-require,import/no-dynamic-require
return require(path.join(process.cwd(), 'node_modules', moduleName));
Expand Down

0 comments on commit a4d72fe

Please sign in to comment.