Skip to content

Commit

Permalink
chore(kadena-cli): refactor devnet commands to createCommand pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jessevanmuijden committed Nov 16, 2023
1 parent f4282bb commit bfe0e2b
Show file tree
Hide file tree
Showing 23 changed files with 523 additions and 716 deletions.
3 changes: 3 additions & 0 deletions packages/tools/kadena-cli/src/config/initConfigCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export function initCommand(program: Command, version: string): void {
await import('./../networks/init.js');
console.log(chalk.green('Configured default networks.'));

await import('./../devnet/init.js');
console.log(chalk.green('Configured default devnets.'));

console.log(chalk.green('Configuration complete!'));
});
}
11 changes: 2 additions & 9 deletions packages/tools/kadena-cli/src/constants/devnets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TDevnetsCreateOptions } from '../devnet/devnetsCreateQuestions.js';
import { IDevnetsCreateOptions } from "../devnet/devnetHelpers.js";

export interface IDefaultDevnetOptions {
[key: string]: TDevnetsCreateOptions;
[key: string]: IDevnetsCreateOptions;
}

/**
Expand All @@ -16,13 +16,6 @@ export const devnetDefaults: IDefaultDevnetOptions = {
mountPactFolder: '',
version: 'latest',
},
other: {
name: '',
port: 8080,
useVolume: false,
mountPactFolder: '',
version: '',
},
};

export const defaultDevnetsPath: string = `${process.cwd()}/.kadena/devnets`;
Expand Down
14 changes: 0 additions & 14 deletions packages/tools/kadena-cli/src/constants/options.ts

This file was deleted.

123 changes: 122 additions & 1 deletion packages/tools/kadena-cli/src/constants/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { program } from 'commander';
import path from 'path';
import { ICustomNetworksChoice } from '../networks/networksHelpers.js';
import { ensureFileExists } from '../utils/filesystem.js';
import { getExistingAccounts, getExistingKeypairs, getExistingKeysets, getExistingNetworks } from '../utils/helpers.js';
import { getExistingAccounts, getExistingDevnets, getExistingKeypairs, getExistingKeysets, getExistingNetworks } from '../utils/helpers.js';
import { defaultNetworksPath } from './networks.js';
import { ChainId } from '@kadena/types';
import { defaultKeypairsPath } from './keypairs.js';
import { ICustomKeypairsChoice } from '../keypair/keypairHelpers.js';
import { defaultKeysetsPath } from './keysets.js';
import { ICustomKeysetsChoice } from '../keyset/keysetHelpers.js';
import { ICustomAccountsChoice } from '../account/accountHelpers.js';
import { defaultDevnetsPath } from './devnets.js';
import { ICustomDevnetsChoice } from '../devnet/devnetHelpers.js';

export const gasPayerPrompt = async (): Promise<string> => {
const existingAccounts: ICustomAccountsChoice[] = await getExistingAccounts();
Expand Down Expand Up @@ -353,3 +355,122 @@ export const selectKeypairsPrompt = async () => {
choices: existingKeypairs,
})
}

export const devnetNamePrompt = async (): Promise<string> => {
const containerName = await input({
message: 'Enter a devnet name (e.g. "devnet")',
});

const filePath = path.join(defaultDevnetsPath, `${containerName}.yaml`);
if (ensureFileExists(filePath)) {
const overwrite = await devnetOverwritePrompt();
if (overwrite === 'no') {
return await devnetNamePrompt();
}
}

return containerName;
};

export const devnetOverwritePrompt = async (name?: string) => {
const message = name
? `Are you sure you want to save this configuration for devnet "${name}"?`
: 'A devnet configuration with this name already exists. Do you want to update it?'

return await select({
message,
choices: [
{ value: 'yes', name: 'Yes' },
{ value: 'no', name: 'No' },
],
});
};

export const devnetPortPrompt = async (): Promise<number> => {
const port = await input({
default: '8080',
message: 'Enter a port number to forward to the Chainweb node API',
validate: function (input) {
const port = parseInt(input);
if (isNaN(port)) {
return 'Port must be a number! Please enter a valid port number.';
}
return true;
},
});
return parseInt(port);
};

export const devnetUseVolumePrompt = async (): Promise<boolean> =>
await select({
message: 'Would you like to create a persistent volume?',
choices: [
{ value: false, name: 'No' },
{ value: true, name: 'Yes' },
],
});

export const devnetMountPactFolderPrompt = async (): Promise<string> =>
await input({
default: '',
message:
'Enter the relative path to a folder containing your Pact files to mount (e.g. ./pact) or leave empty to skip.',
});

export const devnetVersionPrompt = async (): Promise<string> =>
await input({
default: 'latest',
message:
'Enter the version of the kadena/devnet image you would like to use.',
});

export const devnetSelectPrompt = async (): Promise<string> => {
const existingDevnets: ICustomDevnetsChoice[] = await getExistingDevnets();

if (existingDevnets.length > 0) {
return await select({
message: 'Select a devnet',
choices: existingDevnets,
});
}

// At this point there is no devnet defined yet.
// Create and select a new devnet.
await program.parseAsync(['', '', 'devnet', 'create']);

return await networkSelectPrompt();
};

export const devnetDeletePrompt = async (name: string) =>
await select({
message: `Are you sure you want to delete the devnet "${name}"?`,
choices: [
{ value: 'yes', name: 'Yes' },
{ value: 'no', name: 'No' },
],
});

export const devnetPrompt = async (): Promise<string> => {
const existingDevnets: ICustomDevnetsChoice[] = await getExistingDevnets();

if (existingDevnets.length > 0) {
const selectedDevnet = await select({
message: 'Select a devnet',
choices: [
...existingDevnets,
{ value: undefined, name: 'Create a new devnet' },
],
});

if (selectedDevnet !== undefined) {
return selectedDevnet;
}
}

// At this point there is either no devnet defined yet,
// or the user chose to create a new devnet.
// Create and select new devnet.
await program.parseAsync(['', '', 'devnet', 'create']);

return await devnetPrompt();
};
40 changes: 0 additions & 40 deletions packages/tools/kadena-cli/src/constants/questions.ts

This file was deleted.

40 changes: 40 additions & 0 deletions packages/tools/kadena-cli/src/devnet/createDevnetCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { defaultDevnetsPath } from '../constants/devnets.js';
import { ensureFileExists } from '../utils/filesystem.js';
import { writeDevnet } from './devnetHelpers.js';

import debug from 'debug';
import path from 'path';

import { createCommand } from '../utils/createCommand.js';
import { globalOptions } from '../utils/globalOptions.js';
import chalk from 'chalk';
import { devnetOverwritePrompt } from '../constants/prompts.js';

export const createDevnetCommand = createCommand(
'create',
'Create devnet',
[
globalOptions.devnetName(),
globalOptions.devnetPort(),
globalOptions.devnetUseVolume(),
globalOptions.devnetMountPactFolder(),
globalOptions.devnetVersion(),
],
async (config) => {
debug('devnet-create:action')({config});

const filePath = path.join(defaultDevnetsPath, `${config.name}.yaml`);

if (ensureFileExists(filePath)) {
const overwrite = await devnetOverwritePrompt(config.name);
if (overwrite === 'no') {
console.log(chalk.yellow(`\nThe existing devnet configuration "${config.name}" will not be updated.\n`));
return;
}
}

writeDevnet(config);

console.log(chalk.green(`\nThe devnet configuration "${config.name}" has been saved.\n`));
},
);
107 changes: 0 additions & 107 deletions packages/tools/kadena-cli/src/devnet/createDevnetsCommand.ts

This file was deleted.

Loading

0 comments on commit bfe0e2b

Please sign in to comment.