Skip to content

Commit

Permalink
Merge branch 'main' of github.com:scaffold-eth/create-eth into onchai…
Browse files Browse the repository at this point in the history
…nkit-extension
  • Loading branch information
rin-st committed Jul 18, 2024
2 parents 8e028ec + ef79854 commit 841bc48
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 406 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# create-eth

## 0.0.51

### Patch Changes

- fix: BigInt parsing losing precision in IntegerInput (https://github.com/scaffold-eth/scaffold-eth-2/pull/893)
- feat: bundler module resolution (https://github.com/scaffold-eth/scaffold-eth-2/pull/885)
- fix: ignore strings starting with 0 (https://github.com/scaffold-eth/scaffold-eth-2/pull/894)
- cli: don't prompt for install + remove prettier plugins (#80)

## 0.0.50

### Patch Changes

- 5901f51: cli: prettier formatting error

## 0.0.49

### Patch Changes
Expand Down
12 changes: 11 additions & 1 deletion contributors/TEMPLATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,19 @@ The special files and folders are:

## Merging package.json files

The package we use to merge package.json files [merge-packages](3) will use the last version of a dependency given a conflict. For example:
The package we use to merge `package.json` files [merge-packages](3) will attempt to find intersections of dependencies. If there is a conflict, the version from the last `package.json` will be taken.

For example:

```
version on file one: ~1.2.3
version on file two: ^1.0.0
resulting version: ~1.2.3
version on file one: ^1.0.0
version on file two: >=1.2.0 <1.3.0
resulting version: ~1.2.0
version on file one: 1.0.0
version on file two: 0.1.0
resulting version: 0.1.0
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-eth",
"version": "0.0.49",
"version": "0.0.51",
"description": "Create a Scaffold-ETH-2 app",
"repository": {
"type": "git",
Expand Down Expand Up @@ -39,6 +39,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"lefthook": "^1.6.16",
"prettier": "3.3.2",
"rollup": "3.21.0",
"rollup-plugin-auto-external": "2.0.0",
"tslib": "2.5.0",
Expand All @@ -47,17 +48,14 @@
},
"dependencies": {
"@changesets/cli": "^2.26.2",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"arg": "5.0.2",
"chalk": "5.2.0",
"execa": "7.1.1",
"inquirer": "9.2.0",
"listr2": "^8.2.1",
"merge-packages": "^0.1.6",
"ncp": "2.0.0",
"pkg-install": "1.0.0",
"prettier": "3.3.2",
"prettier-plugin-solidity": "^1.3.1"
"pkg-install": "1.0.0"
},
"packageManager": "[email protected]"
}
16 changes: 11 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ export async function createProject(options: Options) {
)}${options.externalExtension ? ` with the ${chalk.green.bold(options.dev ? options.externalExtension : getArgumentFromExternalExtensionOption(options.externalExtension))} extension` : ""}`,
task: () => copyTemplateFiles(options, templateDirectory, targetDirectory),
},
{
title: "🪄 Formatting files",
task: () => prettierFormat(targetDirectory, options),
},
{
title: `📦 Installing dependencies with yarn, this could take a while`,
task: () => installPackages(targetDirectory),
skip: () => {
if (!options.install) {
return "Manually skipped";
return "Manually skipped, since `--skip-install` flag was passed";
}
return false;
},
},
{
title: "🪄 Formatting files",
task: () => prettierFormat(targetDirectory),
skip: () => {
if (!options.install) {
return "Can't use source prettier, since `yarn install` was skipped";
}
return false;
},
Expand Down
26 changes: 15 additions & 11 deletions src/tasks/copy-template-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,22 @@ const processTemplatedFiles = async (
: [];

const externalExtensionFolder = isDev
? path.join(basePath, "../../externalExtensions", externalExtension as string, "extension")
? typeof externalExtension === "string"
? path.join(basePath, "../../externalExtensions", externalExtension, "extension")
: undefined
: path.join(targetDir, EXTERNAL_EXTENSION_TMP_DIR, "extension");
const externalExtensionTemplatedFileDescriptors: TemplateDescriptor[] = externalExtension
? findFilesRecursiveSync(externalExtensionFolder, filePath => isTemplateRegex.test(filePath)).map(
extensionTemplatePath => ({
path: extensionTemplatePath,
fileUrl: pathToFileURL(extensionTemplatePath).href,
relativePath: extensionTemplatePath.split(externalExtensionFolder)[1],
source: `external extension ${isDev ? (externalExtension as string) : getArgumentFromExternalExtensionOption(externalExtension)}`,
}),
)
: [];

const externalExtensionTemplatedFileDescriptors: TemplateDescriptor[] =
externalExtension && externalExtensionFolder
? findFilesRecursiveSync(externalExtensionFolder, filePath => isTemplateRegex.test(filePath)).map(
extensionTemplatePath => ({
path: extensionTemplatePath,
fileUrl: pathToFileURL(extensionTemplatePath).href,
relativePath: extensionTemplatePath.split(externalExtensionFolder)[1],
source: `external extension ${isDev ? (externalExtension as string) : getArgumentFromExternalExtensionOption(externalExtension)}`,
}),
)
: [];

await Promise.all(
[
Expand Down
50 changes: 6 additions & 44 deletions src/tasks/prettier-format.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,15 @@
import { execa } from "execa";
import path from "path";
import { Options } from "../types";
import { SOLIDITY_FRAMEWORKS } from "../utils/consts";

async function runPrettier(targetPath: string[], prettierConfigPath: string, prettierPlugins: string[]) {
const result = await execa("yarn", [
"prettier",
"--write",
...targetPath,
"--config",
prettierConfigPath,
...prettierPlugins,
"--no-editorconfig",
]);
if (result.failed) {
throw new Error(`There was a problem running prettier in ${targetPath.join(" ")}`);
}
}

export async function prettierFormat(targetDir: string, options: Options) {
// TODO: Instead of using execa, use prettier package from cli to format targetDir
export async function prettierFormat(targetDir: string) {
try {
const nextJsPath = path.join(targetDir, "packages", "nextjs");
const nextPrettierConfig = path.join(nextJsPath, ".prettierrc.json");

await runPrettier([nextJsPath], nextPrettierConfig, ["--plugin=@trivago/prettier-plugin-sort-imports"]);

if (options.solidityFramework === SOLIDITY_FRAMEWORKS.HARDHAT) {
const hardhatPackagePath = path.join(targetDir, "packages", SOLIDITY_FRAMEWORKS.HARDHAT);
const hardhatPrettierConfig = path.join(hardhatPackagePath, ".prettierrc.json");
const hardhatPaths = [
`${hardhatPackagePath}/*.ts`,
`${hardhatPackagePath}/deploy/**/*.ts`,
`${hardhatPackagePath}/scripts/**/*.ts`,
`${hardhatPackagePath}/test/**/*.ts`,
`${hardhatPackagePath}/contracts/**/*.sol`,
];

await runPrettier(hardhatPaths, hardhatPrettierConfig, ["--plugin=prettier-plugin-solidity"]);
}
const result = await execa("yarn", ["format"], { cwd: targetDir });

if (options.solidityFramework === SOLIDITY_FRAMEWORKS.FOUNDRY) {
const foundryPackagePath = path.resolve(targetDir, "packages", SOLIDITY_FRAMEWORKS.FOUNDRY);
const foundryResult = await execa("forge", ["fmt"], { cwd: foundryPackagePath });
if (foundryResult.failed) {
throw new Error("There was a problem running forge fmt in the foundry package");
}
if (result.failed) {
throw new Error("There was a problem running the format command");
}
} catch (error) {
throw new Error("Failed to run prettier", { cause: error });
throw new Error("Failed to create directory", { cause: error });
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ExternalExtension = {

type BaseOptions = {
project: string | null;
install: boolean | null;
install: boolean;
dev: boolean;
externalExtension: ExternalExtension | ExternalExtensionNameDev | null;
solidityFramework: SolidityFramework | "none" | null;
Expand Down
12 changes: 1 addition & 11 deletions src/utils/parse-arguments-into-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ const validateExternalExtension = async (
export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptions> {
const args = arg(
{
"--install": Boolean,
"-i": "--install",

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

Expand All @@ -76,15 +73,8 @@ 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 help = args["--help"] ?? false;
Expand Down Expand Up @@ -112,7 +102,7 @@ export async function parseArgumentsIntoOptions(rawArgs: Args): Promise<RawOptio

return {
project,
install: hasInstallRelatedFlag ? install || !skipInstall : null,
install: !skipInstall,
dev,
externalExtension: extension,
help,
Expand Down
8 changes: 1 addition & 7 deletions src/utils/prompt-for-missing-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,13 @@ export async function promptForMissingOptions(options: RawOptions): Promise<Opti
choices: [SOLIDITY_FRAMEWORKS.HARDHAT, SOLIDITY_FRAMEWORKS.FOUNDRY, nullExtensionChoice],
default: SOLIDITY_FRAMEWORKS.HARDHAT,
},
{
type: "confirm",
name: "install",
message: "Install packages?",
default: defaultOptions.install,
},
];

const answers = await inquirer.prompt(questions, cliAnswers);

const mergedOptions: Options = {
project: options.project ?? answers.project,
install: options.install ?? answers.install,
install: options.install,
dev: options.dev ?? defaultOptions.dev,
solidityFramework: options.solidityFramework ?? answers.solidityFramework,
externalExtension: options.externalExtension,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/render-outro-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export function renderOutroMessage(options: Options) {
${chalk.dim("cd")} ${options.project}
`;

if (!options.install) {
message += `
\t${chalk.bold("Install dependencies & format files")}
\t${chalk.dim("yarn")} install && ${chalk.dim("yarn")} format
`;
}

if (
options.solidityFramework === SOLIDITY_FRAMEWORKS.HARDHAT ||
options.solidityFramework === SOLIDITY_FRAMEWORKS.FOUNDRY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,31 @@ const isJsonString = (str: string) => {
}
};

const isBigInt = (str: string) => {
if (str.trim().length === 0 || str.startsWith("0")) return false;
try {
BigInt(str);
return true;
} catch (e) {
return false;
}
};

// Recursive function to deeply parse JSON strings, correctly handling nested arrays and encoded JSON strings
const deepParseValues = (value: any): any => {
if (typeof value === "string") {
// first try with bigInt because we losse precision with JSON.parse
if (isBigInt(value)) {
return BigInt(value);
}

if (isJsonString(value)) {
const parsed = JSON.parse(value);
return deepParseValues(parsed);
} else {
// It's a string but not a JSON string, return as is
return value;
}

// It's a string but not a JSON string, return as is
return value;
} else if (Array.isArray(value)) {
// If it's an array, recursively parse each element
return value.map(element => deepParseValues(element));
Expand Down
8 changes: 4 additions & 4 deletions templates/base/packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"react-hot-toast": "~2.4.0",
"use-debounce": "~8.0.4",
"usehooks-ts": "2.13.0",
"viem": "2.13.6",
"wagmi": "2.9.8",
"viem": "2.17.4",
"wagmi": "2.10.10",
"zustand": "~4.1.2"
},
"devDependencies": {
Expand All @@ -43,7 +43,7 @@
"@types/react": "^18.0.21",
"@types/react-copy-to-clipboard": "^5.0.4",
"@typescript-eslint/eslint-plugin": "~5.40.0",
"abitype": "1.0.2",
"abitype": "1.0.5",
"autoprefixer": "~10.4.12",
"eslint": "~8.24.0",
"eslint-config-next": "~14.0.4",
Expand All @@ -53,7 +53,7 @@
"prettier": "~3.3.2",
"tailwindcss": "~3.4.3",
"type-fest": "~4.6.0",
"typescript": "5.1.6",
"typescript": "5.5.3",
"vercel": "~32.4.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { withDefaults } from "../../../utils.js";

const contents = ({ moduleResolution }) =>
`{
{
"compilerOptions": {
"target": "es2020",
"lib": ["dom", "dom.iterable", "esnext"],
Expand All @@ -12,7 +9,7 @@ const contents = ({ moduleResolution }) =>
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "${moduleResolution}",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
Expand All @@ -28,9 +25,4 @@ const contents = ({ moduleResolution }) =>
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
`;

export default withDefaults(contents, {
moduleResolution: "node",
});
}
12 changes: 0 additions & 12 deletions templates/base/packages/nextjs/types/abitype/abi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,3 @@ declare module "abitype" {
AddressType: AddressType;
}
}

declare module "viem/node_modules/abitype" {
export interface Register {
AddressType: AddressType;
}
}

declare module "wagmi/node_moudles/abitype" {
export interface Register {
AddressType: AddressType;
}
}
Loading

0 comments on commit 841bc48

Please sign in to comment.