Skip to content

Commit

Permalink
refactor(core): improve typings of server/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanmaisse committed Nov 3, 2020
1 parent 0d08492 commit 1394788
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
34 changes: 29 additions & 5 deletions lib/core/src/server/cli/dev.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import program from 'commander';
import program, { CommanderStatic } from 'commander';
import chalk from 'chalk';
import { logger } from '@storybook/node-logger';
import { parseList, getEnvConfig, checkDeprecatedFlags } from './utils';

async function getCLI(packageJson: { version: string; name: string }) {
export interface DevCliOptions {
port?: number;
host?: number;

This comment has been minimized.

Copy link
@bekos

bekos Dec 21, 2020

@gaetanmaisse Is it correct that host is number?

This comment has been minimized.

Copy link
@gaetanmaisse

gaetanmaisse Dec 22, 2020

Author Member

Nop, it surely is a string, well spotted! Fixed in #13501

staticDir?: string[];
configDir?: string;
https?: boolean;
sslCa?: string[];
sslCert?: string;
sslKey?: string;
smokeTest?: boolean;
ci?: boolean;
loglevel?: string;
quiet?: boolean;
versionUpdates?: boolean;
releaseNotes?: boolean;
dll?: boolean;
docs?: boolean;
docsDll?: boolean;
uiDll?: boolean;
debugWebpack?: boolean;
previewUrl?: string;
}

export async function getDevCli(packageJson: {
version: string;
name: string;
}): Promise<CommanderStatic & DevCliOptions> {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

program
Expand Down Expand Up @@ -72,8 +98,6 @@ async function getCLI(packageJson: { version: string; name: string }) {
program.port = parseInt(program.port, 10);
}

checkDeprecatedFlags(program);
checkDeprecatedFlags(program as DevCliOptions);
return { ...program };
}

export default getCLI;
6 changes: 2 additions & 4 deletions lib/core/src/server/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import getDevCli from './dev';
import getProdCli from './prod';

export { getDevCli, getProdCli };
export * from './dev';
export * from './prod';
26 changes: 21 additions & 5 deletions lib/core/src/server/cli/prod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import program from 'commander';
import program, { CommanderStatic } from 'commander';
import chalk from 'chalk';
import { logger } from '@storybook/node-logger';
import { parseList, getEnvConfig, checkDeprecatedFlags } from './utils';

function getCLI(packageJson: { version: string; name: string }) {
export interface ProdCliOptions {
staticDir?: string[];
outputDir?: string;
configDir?: string;
watch?: boolean;
quiet?: boolean;
loglevel?: string;
dll?: boolean;
docsDll?: boolean;
uiDll?: boolean;
debugWebpack?: boolean;
previewUrl?: string;
docs?: boolean;
}

export function getProdCli(packageJson: {
version: string;
name: string;
}): CommanderStatic & ProdCliOptions {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';

program
Expand Down Expand Up @@ -36,8 +54,6 @@ function getCLI(packageJson: { version: string; name: string }) {
configDir: 'SBCONFIG_CONFIG_DIR',
});

checkDeprecatedFlags(program);
checkDeprecatedFlags(program as ProdCliOptions);
return { ...program };
}

export default getCLI;
4 changes: 2 additions & 2 deletions lib/core/src/server/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import deprecate from 'util-deprecate';
import dedent from 'ts-dedent';

export function parseList(str: string) {
export function parseList(str: string): string[] {
return str.split(',');
}

export function getEnvConfig(program: Record<string, any>, configEnv: Record<string, any>) {
export function getEnvConfig(program: Record<string, any>, configEnv: Record<string, any>): void {
Object.keys(configEnv).forEach((fieldName) => {
const envVarName = configEnv[fieldName];
const envVarValue = process.env[envVarName];
Expand Down

0 comments on commit 1394788

Please sign in to comment.