Skip to content

Commit

Permalink
clean prettier-format logic
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Jun 26, 2024
1 parent 277e55c commit fd3b4be
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/tasks/prettier-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,46 @@ 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,
]);
if (result.failed) {
throw new Error(`There was a problem running prettier in ${targetPath}`);
}
}

export async function prettierFormat(targetDir: string, options: Options) {
try {
const nextJsPath = path.join(targetDir, "packages", "nextjs");
const nextPrettierConfig = path.join(nextJsPath, ".prettierrc.json");
const result = await execa("yarn", [
"prettier",
"--write",
nextJsPath,
"--config",
nextPrettierConfig,
"--plugin=@trivago/prettier-plugin-sort-imports",
]);
if (result.failed) {
throw new Error("There was a problem running the prettier in nextjs package");
}

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

if (options.extensions.includes(SOLIDITY_FRAMEWORKS.HARDHAT)) {
const hardhatPackagePath = path.join(targetDir, "packages", SOLIDITY_FRAMEWORKS.HARDHAT);
const hardhatPrettierConfig = path.join(hardhatPackagePath, ".prettierrc.json");
const hardhatResult = await execa("yarn", [
"prettier",
"--write",
const hardhatPaths = [
`${hardhatPackagePath}/*.ts`,
`${hardhatPackagePath}/deploy/**/*.ts`,
`${hardhatPackagePath}/scripts/**/*.ts`,
`${hardhatPackagePath}/test/**/*.ts`,
`${hardhatPackagePath}/contracts/**/*.sol`,
"--config",
hardhatPrettierConfig,
"--plugin=prettier-plugin-solidity",
]);
if (hardhatResult.failed) {
throw new Error("There was a problem running prettier in the hardhat package");
}
];

await runPrettier(hardhatPaths.join(" "), hardhatPrettierConfig, ["--plugin=prettier-plugin-solidity"]);
}

if (options.extensions.includes(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 the forge fmt in the foundry package");
throw new Error("There was a problem running forge fmt in the foundry package");
}
}
} catch (error) {
Expand Down

0 comments on commit fd3b4be

Please sign in to comment.