Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add retry logic to resolve Lisk Core start command from user
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Oct 11, 2023
1 parent d848dfa commit de2cfdf
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const INSTALL_PM2_COMMAND = 'npm i -g pm2';
const PM2_FILE_NAME = 'pm2.migrator.config.json';

const LISK_V3_BACKUP_DATA_DIR = `${homedir()}/.lisk/lisk-core-v3`;
const START_COMMAND_OPTION = '<option>';
const START_COMMAND_VALUE = '<value>';

export const installLiskCore = async (): Promise<string> => execAsync(INSTALL_LISK_CORE_COMMAND);

Expand Down Expand Up @@ -78,16 +80,19 @@ const getFinalConfigPath = async (outputDir: string, network: string) =>
export const validateStartCommandParams = async (
allowedFlags: string[],
userInputs: string,
): Promise<boolean | Error> => {
): Promise<boolean> => {
try {
const userInputsArray = userInputs.split(/[\s=]+/);

for (let i = 0; i < userInputsArray.length; i += 1) {
const userInput = userInputsArray[i];
if (userInput.startsWith('-')) {
const isFlagExists = allowedFlags.find(e => e.split(/[\s=,]+/).includes(userInput));
if (!isFlagExists) throw new Error('Invalid Lisk Core command params');
else if (isFlagExists.includes('value') || isFlagExists.includes('option')) {
if (!isFlagExists) throw new Error('Invalid Lisk Core command params.');
else if (
isFlagExists.includes(START_COMMAND_VALUE) ||
isFlagExists.includes(START_COMMAND_OPTION)
) {
const value = userInputsArray[i + 1];
if (value.startsWith('-')) {
throw new Error(
Expand All @@ -104,35 +109,51 @@ export const validateStartCommandParams = async (
};

const resolveLiskCoreStartCommand = async (_this: Command, network: string, configPath: string) => {
let startCommand = `lisk core start --network ${network} --config ${configPath}/config.json`;

const isUserConfirmed = await cli.confirm(
'Would you like to customize the Lisk Core v4 start command params? [yes/no]',
);

// TODO: Make it retry based
if (isUserConfirmed) {
_this.log('Customizing Lisk Core start parameters');
const userInput = await cli.prompt(
let userInput = await cli.prompt(
'Please provide all parameters you would like to use to start Lisk Core (for e.g. --network mainnet)',
);

const command = "lisk-core start --help | grep -- '^\\s\\+-' | cut -d ' ' -f 3,4";
const allowedFlags = await execAsync(command);
const allowedFlagsArray = allowedFlags.split(/\n+/);

const isValidUserInput = await validateStartCommandParams(allowedFlagsArray, userInput);
let isValidUserInput = await validateStartCommandParams(allowedFlagsArray, userInput);

let numTriesLeft = 3;
while (numTriesLeft) {
numTriesLeft -= 1;

if (isValidUserInput) {
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
startCommand = `lisk core start ${userInput}`;
break;
}

if (numTriesLeft >= 0) {
userInput = await cli.prompt(
'Invalid parameters passed, please provide all valid parameters you would like to use to start Lisk Core (for e.g. --network mainnet)',
);

if (isValidUserInput) {
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
const startCommand = `lisk core start ${userInput}`;
return startCommand;
isValidUserInput = await validateStartCommandParams(allowedFlagsArray, userInput);

if (numTriesLeft === 0 && !isValidUserInput) {
throw new Error(
'Invalid Lisk Core start command params. Cannot proceed with Lisk Core v4 auto-start. Please continue manually. Exiting!!!',
);
}
}
}
_this.error(
'Invalid Lisk Core start command params. Skipping the Lisk Core v4 auto-start process.',
);
}

const defaultStartCommand = `lisk core start --network ${network} --config ${configPath}/config.json`;
return defaultStartCommand;
return startCommand;
};

export const startLiskCore = async (
Expand Down

0 comments on commit de2cfdf

Please sign in to comment.