Skip to content

Commit

Permalink
patch: prettier formatting error (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Jul 9, 2024
1 parent 0a54e5e commit 5901f51
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-pandas-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-eth": patch
---

cli: prettier formatting error
14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ 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),
Expand All @@ -49,6 +45,16 @@ export async function createProject(options: Options) {
return false;
},
},
{
title: "🪄 Formatting files",
task: () => prettierFormat(targetDirectory),
skip: () => {
if (!options.install) {
return "`yarn install` was skipped";
}
return false;
},
},
{
title: `📡 Initializing Git repository${options.solidityFramework === SOLIDITY_FRAMEWORKS.FOUNDRY ? " and submodules" : ""}`,
task: () => createFirstGitCommit(targetDirectory, options),
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
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

0 comments on commit 5901f51

Please sign in to comment.