Skip to content

Commit

Permalink
add prettier task (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Mar 6, 2024
1 parent 898f92f commit b03c011
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-coins-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-eth": patch
---

Add format with prettier task
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createProjectDirectory,
installPackages,
createFirstGitCommit,
prettierFormat,
} from "./tasks";
import type { Options } from "./types";
import { renderOutroMessage } from "./utils/render-outro-message";
Expand Down Expand Up @@ -44,6 +45,10 @@ export async function createProject(options: Options) {
}
},
},
{
title: "🪄 Formatting files with prettier",
task: () => prettierFormat(targetDirectory),
},
{
title: `📡 Initializing Git repository ${
options.extensions.includes("foundry") ? "and submodules" : ""
Expand Down
1 change: 1 addition & 0 deletions src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./copy-template-files";
export * from "./create-project-directory";
export * from "./install-packages";
export * from "./create-first-git-commit";
export * from "./prettier-format";
15 changes: 15 additions & 0 deletions src/tasks/prettier-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { execa } from "execa";

export async function prettierFormat(targetDir: string) {
try {
const result = await execa("yarn", ["format"], { cwd: targetDir });

if (result.failed) {
throw new Error("There was a problem running the format command");
}
} catch (error) {
throw new Error("Failed to create directory", { cause: error });
}

return true;
}
1 change: 1 addition & 0 deletions templates/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"next:lint": "yarn workspace @se-2/nextjs lint",
"next:format": "yarn workspace @se-2/nextjs format",
"next:check-types": "yarn workspace @se-2/nextjs check-types",
"format": "yarn next:format",
"postinstall": "husky install",
"precommit": "lint-staged",
"vercel": "yarn workspace @se-2/nextjs vercel",
Expand Down
4 changes: 3 additions & 1 deletion templates/extensions/foundry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"deploy:verify": "yarn workspace @se-2/foundry deploy:verify",
"foundry:lint": "yarn workspace @se-2/foundry lint",
"foundry:test": "yarn workspace @se-2/foundry test",
"foundry:format": "yarn workspace @se-2/foundry format",
"test": "yarn foundry:test",
"verify": "yarn workspace @se-2/foundry verify",
"generate": "yarn workspace @se-2/foundry generate"
"generate": "yarn workspace @se-2/foundry generate",
"format": "yarn next:format && yarn foundry:format"
}
}
6 changes: 6 additions & 0 deletions templates/extensions/foundry/packages/foundry/.prettier.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arrowParens": "avoid",
"printWidth": 120,
"tabWidth": 2,
"trailingComma": "all"
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ contract YourContract {
* The function can only be called by the owner of the contract as defined by the isOwner modifier
*/
function withdraw() public isOwner {
(bool success, ) = owner.call{value: address(this).balance}("");
(bool success,) = owner.call{value: address(this).balance}("");
require(success, "Failed to send Ether");
}

Expand Down
5 changes: 5 additions & 0 deletions templates/extensions/foundry/packages/foundry/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ scroll = "https://rpc.scroll.io"
polygonMumbai = { key = "${ETHERSCAN_API_KEY}" }
goerli = { key = "${ETHERSCAN_API_KEY}" }


[fmt]
line_length = 80
multiline_func_header = "params_first"

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
3 changes: 2 additions & 1 deletion templates/extensions/foundry/packages/foundry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"deploy": "forge build --build-info --build-info-path out/build-info/ && forge script script/Deploy.s.sol --rpc-url ${1:-default_network} --broadcast --legacy && node script/generateTsAbis.js",
"deploy:verify": "forge build --build-info --build-info-path out/build-info/ && forge script script/Deploy.s.sol --rpc-url ${1:-default_network} --broadcast --legacy --verify ; node script/generateTsAbis.js",
"verify": "forge build --build-info --build-info-path out/build-info/ && forge script script/VerifyAll.s.sol --ffi --rpc-url ${1:-default_network}",
"lint": "forge fmt",
"lint": "forge fmt --check && prettier --check ./script/**/*.js",
"format": "forge fmt && prettier --write ./script/**/*.js",
"test": "forge test"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ contract DeployScript is ScaffoldETHDeploy {
);
}
vm.startBroadcast(deployerPrivateKey);
YourContract yourContract = new YourContract(
vm.addr(deployerPrivateKey)
);
YourContract yourContract =
new YourContract(vm.addr(deployerPrivateKey));
console.logString(
string.concat(
"YourContract deployed at: ",
vm.toString(address(yourContract))
"YourContract deployed at: ", vm.toString(address(yourContract))
)
);
vm.stopBroadcast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ contract ScaffoldETHDeploy is Script {

for (uint256 i = 0; i < len; i++) {
vm.serializeString(
jsonWrite,
vm.toString(deployments[i].addr),
deployments[i].name
jsonWrite, vm.toString(deployments[i].addr), deployments[i].name
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import "solidity-bytes-utils/BytesLib.sol";
* @notice calls the tryffi function on the Vm contract
* @notice will be deleted once the forge/std is updated
*/

struct FfiResult {
int32 exit_code;
bytes stdout;
Expand Down Expand Up @@ -43,8 +42,7 @@ contract VerifyAll is Script {
function _verifyIfContractDeployment(string memory content) internal {
string memory txType = abi.decode(
vm.parseJson(
content,
searchStr(currTransactionIdx, "transactionType")
content, searchStr(currTransactionIdx, "transactionType")
),
(string)
);
Expand All @@ -55,31 +53,23 @@ contract VerifyAll is Script {

function _verifyContract(string memory content) internal {
string memory contractName = abi.decode(
vm.parseJson(
content,
searchStr(currTransactionIdx, "contractName")
),
vm.parseJson(content, searchStr(currTransactionIdx, "contractName")),
(string)
);
address contractAddr = abi.decode(
vm.parseJson(
content,
searchStr(currTransactionIdx, "contractAddress")
content, searchStr(currTransactionIdx, "contractAddress")
),
(address)
);
bytes memory deployedBytecode = abi.decode(
vm.parseJson(
content,
searchStr(currTransactionIdx, "transaction.data")
content, searchStr(currTransactionIdx, "transaction.data")
),
(bytes)
);
bytes memory compiledBytecode = abi.decode(
vm.parseJson(
_getCompiledBytecode(contractName),
".bytecode.object"
),
vm.parseJson(_getCompiledBytecode(contractName), ".bytecode.object"),
(bytes)
);
bytes memory constructorArgs = BytesLib.slice(
Expand Down Expand Up @@ -115,27 +105,26 @@ contract VerifyAll is Script {
return;
}

function nextTransaction(
string memory content
) external view returns (bool) {
function nextTransaction(string memory content)
external
view
returns (bool)
{
try this.getTransactionFromRaw(content, currTransactionIdx) {
return true;
} catch {
return false;
}
}

function _getCompiledBytecode(
string memory contractName
) internal view returns (string memory compiledBytecode) {
function _getCompiledBytecode(string memory contractName)
internal
view
returns (string memory compiledBytecode)
{
string memory root = vm.projectRoot();
string memory path = string.concat(
root,
"/out/",
contractName,
".sol/",
contractName,
".json"
root, "/out/", contractName, ".sol/", contractName, ".json"
);
compiledBytecode = vm.readFile(path);
}
Expand All @@ -154,4 +143,4 @@ contract VerifyAll is Script {
return
string.concat(".transactions[", vm.toString(idx), "].", searchKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function getInheritedFromContracts(artifact) {
for (const astNode of artifact.ast.nodes) {
if (astNode.nodeType == "ContractDefinition") {
if (astNode.baseContracts.length > 0) {
inheritedFromContracts = astNode.baseContracts.map(({baseName}) => baseName.name);
inheritedFromContracts = astNode.baseContracts.map(
({ baseName }) => baseName.name
);
}
}
}
Expand Down Expand Up @@ -103,9 +105,7 @@ function main() {
] = {
address: transaction.contractAddress,
abi: artifact.abi,
inheritedFunctions: getInheritedFunctions(
artifact,
),
inheritedFunctions: getInheritedFunctions(artifact),
};
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ contract YourContractTest is Test {

function testMessageOnDeployment() public view {
require(
keccak256(bytes(yourContract.greeting())) ==
keccak256("Building Unstoppable Apps!!!")
keccak256(bytes(yourContract.greeting()))
== keccak256("Building Unstoppable Apps!!!")
);
}

function testSetNewMessage() public {
yourContract.setGreeting("Learn Scaffold-ETH 2! :)");
require(
keccak256(bytes(yourContract.greeting())) ==
keccak256("Learn Scaffold-ETH 2! :)")
keccak256(bytes(yourContract.greeting()))
== keccak256("Learn Scaffold-ETH 2! :)")
);
}
}
4 changes: 3 additions & 1 deletion templates/extensions/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"generate": "yarn workspace @se-2/hardhat generate",
"hardhat:lint": "yarn workspace @se-2/hardhat lint",
"hardhat:lint-staged": "yarn workspace @se-2/hardhat lint-staged",
"hardhat:format": "yarn workspace @se-2/hardhat format",
"hardhat:test": "yarn workspace @se-2/hardhat test",
"test": "yarn hardhat:test"
"test": "yarn hardhat:test",
"format": "yarn next:format && yarn hardhat:format"
}
}
1 change: 1 addition & 0 deletions templates/extensions/hardhat/packages/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"generate": "hardhat run scripts/generateAccount.ts",
"lint": "eslint --config ./.eslintrc.json --ignore-path ./.eslintignore ./*.ts ./deploy/**/*.ts ./scripts/**/*.ts ./test/**/*.ts",
"lint-staged": "eslint --config ./.eslintrc.json --ignore-path ./.eslintignore",
"format": "prettier --write ./*.ts ./deploy/**/*.ts ./scripts/**/*.ts ./test/**/*.ts",
"test": "REPORT_GAS=true hardhat test --network hardhat",
"verify": "hardhat etherscan-verify",
"hardhat-verify": "hardhat verify"
Expand Down

0 comments on commit b03c011

Please sign in to comment.