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 #58

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ 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
22 changes: 16 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ 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;
externalExtension: ExternalExtension | null;
};

Expand All @@ -15,15 +14,26 @@ export type ExternalExtension = {
branch?: string | null;
};

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

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

type NonNullableMergedOptions = {
[Prop in keyof Omit<MergedOptions, "externalExtension">]: NonNullable<MergedOptions[Prop]>;
} & {
externalExtension: RawOptions["externalExtension"];
};

export type Options = NonNullableRawOptions;
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
22 changes: 21 additions & 1 deletion src/utils/parse-arguments-into-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptio

"--dev": Boolean,

"--solidity-framework": solidityFrameworkHandler,
"-f": "--solidity-framework",
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved

"--extension": String,
"-e": "--extension",
},
Expand All @@ -49,12 +52,19 @@ export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptio

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;

// ToDo. Allow multiple
// ToDo. Allow core extensions too
rin-st marked this conversation as resolved.
Show resolved Hide resolved
const extension = args["--extension"] ? await validateTemplate(args["--extension"]) : null;
Expand All @@ -73,7 +83,17 @@ export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptio
project,
install: hasInstallRelatedFlag ? install || !skipInstall : null,
dev,
extensions: null, // TODO add extensions flags
externalExtension: extension,
solidityFramework,
};
}

function solidityFrameworkHandler(value: string) {
const lowercasedValue = value.toLowerCase();
if (lowercasedValue === "hardhat" || lowercasedValue === "foundry" || lowercasedValue === "none") {
return lowercasedValue;
}
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved

// choose from cli prompts
return null;
}
14 changes: 4 additions & 10 deletions src/utils/prompt-for-missing-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from "../config";
import { Extension, Options, RawOptions, isDefined, isExtension } from "../types";
import { Options, RawOptions, isDefined, isExtension } from "../types";
import inquirer from "inquirer";
import { extensionDict } from "./extensions-dictionary";

Expand All @@ -8,13 +8,13 @@ const defaultOptions: RawOptions = {
project: "my-dapp-example",
install: true,
dev: false,
extensions: [],
externalExtension: null,
solidityFramework: null,
};

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

Expand Down Expand Up @@ -67,15 +67,9 @@ export async function promptForMissingOptions(options: RawOptions): Promise<Opti
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"),
externalExtension: options.externalExtension,
};

config.questions.forEach(question => {
const { name } = question;
const choice: Extension[] = [answers[name]].flat().filter(isDefined);
mergedOptions.extensions.push(...choice);
});

technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
return mergedOptions;
}
Loading