Skip to content

Commit

Permalink
fix(cli): improve error handling and reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
joneff committed Jan 8, 2023
1 parent ecefacf commit de93f1a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
6 changes: 0 additions & 6 deletions src/cli/commands/build/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export function validateParams( params ) {
if (typeof file !== 'string' && !Array.isArray(file)) {
throw new Error( '--file must be either string or array.' );
}
if (file === '' || (Array.isArray( file ) && file.length === 0 ) ) {
throw new Error( '--file must not be empty.' );
}
if (Array.isArray(file) && outFile) {
throw new Error( 'Supply --outDir instead of --outFile.' );
}
Expand All @@ -35,9 +32,6 @@ export function validateParams( params ) {
if (typeof glob !== 'string' && !Array.isArray(glob)) {
throw new Error( '--glob must be either string or array.' );
}
if (glob === '' || (Array.isArray( glob ) && glob.length === 0 ) ) {
throw new Error( '--glob must not be empty.' );
}
if (outFile) {
throw new Error( 'Supply --outDir instead of --outFile.' );
}
Expand Down
41 changes: 29 additions & 12 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ export function cli() {

const argv = argsParser( process.argv.slice(2), ARGS_PARSER_OPTIONS );

const { command, params, config } = parseArgs( argv );
const { error, version, commandName, params, config } = parseArgs( argv );

if ( typeof command === 'function' ) {
command( params );
if ( error ) {
// process.stderr.write( `${error.message}\n` );
exit( 1, 'error', 'cli', error.message );
}

if ( version ) {
process.stdout.write( `${VERSION}\n` );
process.exit(0);
}

if ( commandName ) {
commands.get( commandName )( params );
exitSuccess();
}

Expand All @@ -48,37 +58,44 @@ export function cli() {
process.exit(0);
}

function parseArgs( argv : argsParser.Arguments ) : { command?: Function, params?: any, config?: string } {
export function parseArgs( argv : argsParser.Arguments ) : {
error?: Error,
version?: boolean,
commandName?: string,
params?: any,
config?: string
} {

try {
validateParams( argv, ARGS_PARSER_OPTIONS );
} catch ( err ) {
exit( 1, 'error', 'cli', err.message );
} catch ( error ) {
return { error };
}

const {
_: commandList,
_: _commandList,
file, source, glob, outFile, outDir, transformer,
config, debug, version
} = argv;

const commandList = <string[]> _commandList || [];
const commandName = <string> commandList[0];
const command = commands.get( commandName );
const params = { file, source, glob, outFile, outDir, transformer };

if ( version ) {
process.stdout.write( `${VERSION}\n` );
process.exit(0);
return { version };
}

if ( debug ) {
logger.level = 'debug';
logger.silly( 'cli', 'Starting in debug mode.' );
}

if ( commandName ) {
if ( commandName && command ) {
logger.silly( 'cli', 'Command is: %s', commandName );

return { command, params };
return { commandName, params };
}
logger.silly( 'cli', 'No command passed.' );

Expand All @@ -95,7 +112,7 @@ function parseArgs( argv : argsParser.Arguments ) : { command?: Function, params
if ( hasParams ) {
logger.silly( 'cli', 'Attempting build command with cli parameters' );

return { command: commands.get( 'build' ), params };
return { commandName: 'build', params };
}

if ( hasConfig ) {
Expand Down
33 changes: 32 additions & 1 deletion src/cli/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import * as commands from './commands';
export function validateParams( argv: argsParser.Arguments, argsParserOpts: argsParser.Options ) {

const {
_: commandList,
_: _commandList,
file, source, glob, outFile, outDir, transformer,
config, debug, version,
...rest
} = argv;

const commandList = <string[]> _commandList || [];
const commandName = <string> commandList[0];
const command = commands.get(commandName);
const params = [ file, source, glob, outFile, outDir, transformer ].filter( item => item !== undefined );
Expand Down Expand Up @@ -69,18 +71,47 @@ export function validateParams( argv: argsParser.Arguments, argsParserOpts: args
throw new Error( '--config parameter cannot be used with other parameters.' );
}

// Throw error for empty file
if (file === '' || (Array.isArray( file ) && file.length === 0 ) ) {
throw new Error( '--file must not be empty.' );
}

// Throw error for empty source
if (source === '' ) {
throw new Error( '--source must not be empty.' );
}

// Throw error for empty glob
if (glob === '' || (Array.isArray( glob ) && glob.length === 0 ) ) {
throw new Error( '--glob must not be empty.' );
}

// Throw error for empty outFile
if (outFile === '' ) {
throw new Error( '--outFile must not be empty.' );
}

// Throw error for empty outDir
if (outDir === '' ) {
throw new Error( '--outDir must not be empty.' );
}

// Throw error for file and source
if ( file !== undefined && source !== undefined ) {
throw new Error( '--file and --source parameters cannot be used together.' );
}

// Throw error for file and glob
if ( file !== undefined && glob !== undefined ) {
throw new Error( '--file and --glob parameters cannot be used together.' );
}

// Throw error for source and glob
if ( source !== undefined && glob !== undefined ) {
throw new Error( '--source and --glob parameters cannot be used together.' );
}

// THrow error for outFile and outDir
if ( outFile !== undefined && outDir !== undefined ) {
throw new Error( '--outFile and --outDir parameters cannot be used together.' );
}
Expand Down

0 comments on commit de93f1a

Please sign in to comment.