Skip to content

Commit

Permalink
Cli solidity framework option (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-st authored Jun 20, 2024
1 parent 7f6f740 commit bd54b20
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-fishes-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-eth": patch
---

cli: solidity framework options
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
28 changes: 24 additions & 4 deletions src/utils/parse-arguments-into-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Args, RawOptions } from "../types";
import type { Args, RawOptions, SolidityFramework } from "../types";
import arg from "arg";
import * as https from "https";
import { getDataFromExternalExtensionArgument } from "./external-extensions";
Expand Down Expand Up @@ -35,10 +35,12 @@ export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptio

"--skip-install": Boolean,
"--skip": "--skip-install",
"-s": "--skip-install",

"--dev": Boolean,

"--solidity-framework": solidityFrameworkHandler,
"-s": "--solidity-framework",

"--extension": String,
"-e": "--extension",
},
Expand All @@ -49,14 +51,20 @@ 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
const extension = args["--extension"] ? await validateTemplate(args["--extension"]) : null;

if (extension && !CURATED_EXTENSIONS[args["--extension"] as string]) {
Expand All @@ -73,7 +81,19 @@ export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptio
project,
install: hasInstallRelatedFlag ? install || !skipInstall : null,
dev,
extensions: null, // TODO add extensions flags
externalExtension: extension,
solidityFramework,
};
}

const SOLIDITY_FRAMEWORK_OPTIONS = ["hardhat", "foundry", "none"];

function solidityFrameworkHandler(value: string) {
const lowercasedValue = value.toLowerCase();
if (SOLIDITY_FRAMEWORK_OPTIONS.includes(lowercasedValue)) {
return lowercasedValue as SolidityFramework | "none";
}

// 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);
});

return mergedOptions;
}

0 comments on commit bd54b20

Please sign in to comment.