Skip to content

Commit

Permalink
CLI: Refactor jetpack changelog tool (#19613)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdixon194 authored Apr 22, 2021
1 parent 4a769ad commit 80910bb
Showing 1 changed file with 36 additions and 96 deletions.
132 changes: 36 additions & 96 deletions tools/cli/commands/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function changelogDefine( yargs ) {
} );
},
async argv => {
await changelogAdd( argv );
await changelogArgs( argv );
}
)
// Changelog validate subscommand
Expand Down Expand Up @@ -98,7 +98,7 @@ export function changelogDefine( yargs ) {
} );
},
async argv => {
await changelogValidate( argv );
await changelogArgs( argv );
}
)
.command(
Expand Down Expand Up @@ -166,42 +166,38 @@ export function changelogDefine( yargs ) {
} );
},
async argv => {
await changelogWrite( argv );
await changelogArgs( argv );
}
);
},
async argv => {
await changelogRouter( argv );
await changelogCommand( argv );
}
);
return yargs;
}

/**
* Routes Changelog Command to correct place.
* Get a command if we're not passed one as an argument.
*
* @param {argv} argv - the arguments passed.
*/
async function changelogRouter( argv ) {
async function changelogCommand( argv ) {
if ( ! argv.cmd ) {
argv = await promptCommand( argv );
}
switch ( argv.cmd ) {
case 'add':
changelogAdd( argv );
break;
case 'validate':
changelogValidate( argv );
break;
case 'write':
changelogWrite( argv );
break;
case 'version':
console.error( chalk.red( 'Command not yet implemented!' ) );
process.exit( 1 );
default:
throw new Error( 'Unknown command' ); // Yargs should provide a helpful response before this, but to be safe.

const commands = [ 'add', 'validate', 'version', 'write' ];
if ( ! commands.includes( argv.cmd ) ) {
throw new Error( 'Unknown command' ); // Yargs should provide a helpful response before this, but to be safe.
}

// @todo - add support for version, which will require a bit of tweaking since it has required arguments.
if ( argv.cmd === 'version' ) {
throw new Error( 'Version not supported yet!' );
}

changelogArgs( argv );
}

/**
Expand All @@ -222,92 +218,36 @@ async function promptCommand( argv ) {
}

/**
* Changelog add script.
*
* @param {object} argv - arguments passed as cli.
*/
export async function changelogAdd( argv ) {
argv = await validateProject( argv );
const parsedArgKey = Object.keys( argv );
const acceptedArgs = [ 's', 't', 'e', 'f' ]; //significance, type, entry, file
argv.success = `Changelog for ${ argv.project } added successfully!`;
argv.error = `Changelogger couldn't be executed correctly. See error.`;
argv.args = [ 'add' ];

// Check passed arguments against accepted args and add them to our command.
for ( const arg of parsedArgKey ) {
if ( acceptedArgs.includes( arg ) ) {
argv.args.push( `-${ arg }${ argv[ arg ] }` );
}
}
if ( argv.v ) {
argv.args.push( '-v' );
}

// Check if we have all required args for a passthrough, otherwise default to interactive mode.
if ( argv.s && argv.t && argv.e ) {
argv.args.push( '--no-interaction' );
changeloggerCli( argv );
return;
}
if ( argv.args.length > 1 ) {
console.error(
chalk.bgRed(
'Need to pass all arguments for non-interactive mode. Defaulting to interactive mode.'
)
);
changeloggerCli( argv );
return;
}
changeloggerCli( argv );
}

/**
* Changelog validate script.
*
* @param {object} argv - arguments passed as cli.
*/
export async function changelogValidate( argv ) {
argv = await validateProject( argv );
const parsedArgKey = Object.keys( argv );
const acceptedArgs = [ 'gh-action', 'basedir', 'no-strict' ];
argv.success = `Validation for ${ argv.project } completed succesfully!`;
argv.error = 'Changelog validation failed. See above.';
argv.args = [ 'validate' ];

// Add any options we're passing onto the command.
for ( const arg of parsedArgKey ) {
if ( acceptedArgs.includes( arg ) ) {
argv.args.push( `--${ arg }` );
}
}

if ( argv.args.includes( '--basedir' ) ) {
argv.args.push( argv.basedir );
}

if ( argv.v ) {
argv.args.push( '-v' );
}
changeloggerCli( argv );
}

/**
* Changelog write script.
* Adds any passthrough arguments to args before running command.
*
* @param {object} argv - arguments passed to the CLI.
*/
async function changelogWrite( argv ) {
async function changelogArgs( argv ) {
argv = await validateProject( argv );
argv.success = `${ argv.project } CHANGELOG.md written to succesfully!`;
argv.error = 'Writing to the changelog file failed. See error.';
argv.success = `Command '${ argv.cmd || argv._[ 1 ] }' for ${
argv.project
} completed succesfully!`;
argv.error = `Command '${ argv.cmd || argv._[ 1 ] }' for ${ argv.project } has failed! See error`;
argv.args = [ argv.cmd || argv._[ 1 ], ...process.argv.slice( 4 ) ];

// Remove project from command list we pass to changelogger.
if ( argv.args.includes( argv.project ) ) {
argv.args.splice( argv.args.indexOf( argv.project ), 1 );
}

// Passthrough arguments for "add"
if ( argv.args[ 0 ] === 'add' ) {
if ( argv.s && argv.t && argv.e ) {
argv.args.push( '--no-interaction' );
} else if ( argv.s || argv.t || argv.e ) {
console.error(
chalk.bgRed(
'Need to pass all arguments for non-interactive mode. Defaulting to interactive mode.'
)
);
}
}

changeloggerCli( argv );
}

Expand Down

0 comments on commit 80910bb

Please sign in to comment.