Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli solidity framework option #801

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config: Config = {
questions: [
typedQuestion({
type: "single-select",
name: "solidity-framework",
name: "solidityFramework",
message: "What solidity framework do you want to use?",
extensions: ["hardhat", "foundry", null],
default: "hardhat",
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export async function createProject(options: Options) {
},
},
{
title: `📡 Initializing Git repository ${
options.extensions.includes("foundry") ? "and submodules" : ""
title: `📡 Initializing Git repository${
options.extensions.includes("foundry") ? " and submodules" : ""
}`,
task: () => createFirstGitCommit(targetDirectory, options),
},
Expand Down
24 changes: 16 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ import type { Question } from "inquirer";

export type Args = string[];

export type RawOptions = {
type BaseOptions = {
project: string | null;
install: boolean | null;
dev: boolean;
extensions: Extension[] | null;
};

type NonNullableRawOptions = {
[Prop in keyof RawOptions]: NonNullable<RawOptions[Prop]>;
export type RawOptions = BaseOptions & {
solidityFramework: SolidityFramework | "none" | null;
};

export type Options = NonNullableRawOptions;
type MergedOptions = BaseOptions & {
extensions: Extension[];
};

type NonNullableMergedOptions = {
[Prop in keyof MergedOptions]: NonNullable<MergedOptions[Prop]>;
};

export type Options = NonNullableMergedOptions;

export type SolidityFramework = "hardhat" | "foundry";

export type Extension = SolidityFramework;

export type Extension =
| "hardhat"
| "foundry"
type NullExtension = null;
export type ExtensionOrNull = Extension | NullExtension;
// corresponds to inquirer question types:
Expand Down
28 changes: 27 additions & 1 deletion src/utils/parse-arguments-into-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export function parseArgumentsIntoOptions(rawArgs: Args): RawOptions {
"--skip": "--skip-install",
"-s": "--skip-install",

"--solidity-framework": solidityFrameworkHandler,
"-f": "--solidity-framework",

"--dev": Boolean,
},
{
Expand All @@ -21,16 +24,39 @@ export function parseArgumentsIntoOptions(rawArgs: Args): RawOptions {

const install = args["--install"] ?? null;
const skipInstall = args["--skip-install"] ?? null;

if (install && skipInstall) {
throw new Error(
'Please select only one of the options: "--install" or "--skip-install".'
);
}

const hasInstallRelatedFlag = install || skipInstall;

const dev = args["--dev"] ?? false; // info: use false avoid asking user

const project = args._[0] ?? null;

const solidityFramework = args["--solidity-framework"] ?? null;

return {
project,
install: hasInstallRelatedFlag ? install || !skipInstall : null,
dev,
extensions: null, // TODO add extensions flags
solidityFramework,
};
}

function solidityFrameworkHandler(value: string) {
const lowercasedValue = value.toLowerCase();
if (
lowercasedValue === "hardhat" ||
lowercasedValue === "foundry" ||
lowercasedValue === "none"
) {
return lowercasedValue;
}

// choose from cli prompts
return null;
}
32 changes: 19 additions & 13 deletions src/utils/prompt-for-missing-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
RawOptions,
extensionWithSubextensions,
isDefined,
isExtension
isExtension,
} from "../types";
import inquirer, { Answers } from "inquirer";
import { extensionDict } from "./extensions-tree";
Expand All @@ -16,14 +16,14 @@ const defaultOptions: RawOptions = {
project: "my-dapp-example",
install: true,
dev: false,
extensions: [],
solidityFramework: "none",
};

const invalidQuestionNames = ["project", "install"];
const nullExtensionChoice = {
name: 'None',
value: null
}
name: "none",
value: null,
};

export async function promptForMissingOptions(
options: RawOptions
Expand Down Expand Up @@ -78,18 +78,21 @@ export async function promptForMissingOptions(
.join(", ")}`
);
}

const extensions = question.extensions
.filter(isExtension)
.map((ext) => extensionDict[ext])
.filter(isDefined);

const hasNoneOption = question.extensions.includes(null)
const hasNoneOption = question.extensions.includes(null);

questions.push({
type: question.type === "multi-select" ? "checkbox" : "list",
name: question.name,
message: question.message,
choices: hasNoneOption ? [...extensions, nullExtensionChoice] : extensions,
choices: hasNoneOption
? [...extensions, nullExtensionChoice]
: extensions,
});

recurringAddFollowUps(extensions, question.name);
Expand All @@ -108,14 +111,17 @@ export async function promptForMissingOptions(
project: options.project ?? answers.project,
install: options.install ?? answers.install,
dev: options.dev ?? defaultOptions.dev,
extensions: [],
extensions: [options.solidityFramework ?? answers.solidityFramework].filter(
(ext) => Boolean(ext) && ext !== "none"
),
};

config.questions.forEach((question) => {
const { name } = question;
const choice: Extension[] = [answers[name]].flat().filter(isDefined);
mergedOptions.extensions.push(...choice);
});
// TODO: check if it needed for nested extensions
// config.questions.forEach((question) => {
// const { name } = question;
// const choice: Extension[] = [answers[name]].flat().filter(isDefined);
// mergedOptions.extensions.push(...choice);
// });

const recurringAddNestedExtensions = (baseExtensions: Extension[]) => {
baseExtensions.forEach((extValue) => {
Expand Down