Skip to content

Commit 66d02f4

Browse files
authored
Possibility to pass global variables (#350)
1 parent 47ea054 commit 66d02f4

File tree

13 files changed

+80
-31
lines changed

13 files changed

+80
-31
lines changed

contributors/TEMPLATING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ export default { one: 1, two: 2}
144144

145145
To avoid issues when named arguments have typos, the `withDefaults` utility will also throw an error when an argument is passed with a name that wasn't expected by the template.
146146

147+
### Using global variables in args file content
148+
149+
You can use global variables in your argument values by defining your argument values as functions.
150+
151+
Instead of static values, export functions that receive global variables as parameters and return the desired content.
152+
153+
**Important:** The function's return type must match the default argument value type defined in the template.
154+
155+
Example:
156+
157+
```js
158+
// Static value (basic approach)
159+
export const description = "Hello world";
160+
161+
// Dynamic value using global variables (advanced approach)
162+
export const description = ({ solidityFramework }) => `Hello ${solidityFramework}`;
163+
```
164+
165+
**Available global variables:**
166+
167+
- `solidityFramework` - The selected Solidity framework (e.g., "hardhat", "foundry")
168+
147169
# Args files injection in Template files
148170

149171
For each Template file, we search on the extensions the user selected for the existence of Args files in the exact same relative path. If Args files are found, we combine them into an array.

src/tasks/copy-template-files.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import path from "path";
99
import { promisify } from "util";
1010
import link from "../utils/link";
1111
import { getArgumentFromExternalExtensionOption } from "../utils/external-extensions";
12-
import { BASE_DIR, SOLIDITY_FRAMEWORKS, SOLIDITY_FRAMEWORKS_DIR, EXAMPLE_CONTRACTS_DIR } from "../utils/consts";
12+
import {
13+
BASE_DIR,
14+
SOLIDITY_FRAMEWORKS,
15+
SOLIDITY_FRAMEWORKS_DIR,
16+
EXAMPLE_CONTRACTS_DIR,
17+
GLOBAL_ARGS_DEFAULTS,
18+
} from "../utils/consts";
1319

1420
const EXTERNAL_EXTENSION_TMP_DIR = "tmp-external-extension";
1521

@@ -282,7 +288,11 @@ const processTemplatedFiles = async (
282288
return accumulated;
283289
}, freshArgs);
284290

285-
const output = fileTemplate(combinedArgs);
291+
const globalArgs = {
292+
solidityFramework: [solidityFramework || GLOBAL_ARGS_DEFAULTS.solidityFramework],
293+
};
294+
295+
const output = fileTemplate({ ...combinedArgs, ...globalArgs });
286296

287297
const targetPath = path.join(
288298
targetDir,

src/utils/consts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export const SOLIDITY_FRAMEWORKS = {
66
HARDHAT: "hardhat",
77
FOUNDRY: "foundry",
88
} as const;
9+
10+
export const GLOBAL_ARGS_DEFAULTS = {
11+
solidityFramework: "",
12+
};

templates/base/.cursor/rules/scaffold-eth.mdc.template.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { withDefaults } from "../../../utils.js"
1+
import { withDefaults, upperCaseFirstLetter } from "../../../utils.js"
22

33
const contents = ({ solidityFramework, deployScriptDir }) => {
44
return `---
@@ -11,7 +11,7 @@ This codebase contains Scaffold-ETH 2 (SE-2), everything you need to build dApps
1111
1212
It's a yarn monorepo that contains following packages:
1313
14-
${Boolean(solidityFramework[0]) ? `- ${solidityFramework[0].toUpperCase()} (\`packages/${solidityFramework[0]}\`): The solidity framework to write, test and deploy EVM Smart Contracts.` : ""}
14+
${Boolean(solidityFramework[0]) ? `- ${upperCaseFirstLetter(solidityFramework[0])} (\`packages/${solidityFramework[0]}\`): The solidity framework to write, test and deploy EVM Smart Contracts.` : ""}
1515
- NextJS (\`packages/nextjs\`): The UI framework extended with utilities to make interacting with Smart Contracts easy (using Next.js App Router, not Pages Router).
1616
1717
@@ -117,4 +117,4 @@ For fully customizable components, you can use the hooks from the \`@scaffold-ui
117117
Find the relevant information from the documentation and the codebase. Think step by step before answering the question.`
118118
}
119119

120-
export default withDefaults(contents, { solidityFramework: "", deployScriptDir: "" });
120+
export default withDefaults(contents, { deployScriptDir: "" });

templates/base/README.md.template.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { withDefaults } from "../utils.js";
1+
import { upperCaseFirstLetter, withDefaults } from "../utils.js";
22

33
const getQuickStart = ({
44
solidityFramework,
@@ -26,7 +26,7 @@ ${
2626
yarn chain
2727
\`\`\`
2828
29-
This command starts a local Ethereum network using ${solidityFramework[0]}. The network runs on your local machine and can be used for testing and development. You can customize the network configuration in ${networkConfigPath[0]}.
29+
This command starts a local Ethereum network using ${upperCaseFirstLetter(solidityFramework[0])}. The network runs on your local machine and can be used for testing and development. You can customize the network configuration in ${networkConfigPath[0]}.
3030
3131
3. On a second terminal, deploy the test contract:
3232
@@ -84,7 +84,7 @@ const contents = ({
8484
🧪 An open-source, up-to-date toolkit for building decentralized applications (dapps) on the Ethereum blockchain. It's designed to make it easier for developers to create and deploy smart contracts and build user interfaces that interact with those contracts.
8585
8686
⚙️ Built using NextJS, RainbowKit, ${
87-
Boolean(solidityFramework[0]) ? solidityFramework[0] + ", " : ""
87+
Boolean(solidityFramework[0]) ? upperCaseFirstLetter(solidityFramework[0]) + ", " : ""
8888
}Wagmi, Viem, and Typescript.
8989
${
9090
Boolean(solidityFramework[0])
@@ -128,7 +128,6 @@ Please see [CONTRIBUTING.MD](https://github.com/scaffold-eth/scaffold-eth-2/blob
128128

129129
export default withDefaults(contents, {
130130
skipQuickStart: false,
131-
solidityFramework: "",
132131
networkConfigPath: "",
133132
contractsPath: "",
134133
scriptsPath: "",

templates/base/packages/nextjs/app/blockexplorer/address/[address]/page.tsx.template.mjs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { withDefaults } from "../../../../../../../utils.js";
22

3-
const contents = ({ chainName, artifactsDirName }) => `
3+
const contents = ({ solidityFramework, artifactsDirName }) => {
4+
if (!solidityFramework[0]) {
5+
solidityFramework[0] = "hardhat";
6+
}
7+
return `
48
import fs from "fs";
59
import path from "path";
610
import { Address } from "viem";
7-
import { ${chainName[0]} } from "viem/chains";
11+
import { ${solidityFramework[0]} } from "viem/chains";
812
import { AddressComponent } from "~~/app/blockexplorer/_components/AddressComponent";
913
import deployedContracts from "~~/contracts/deployedContracts";
1014
import { isZeroAddress } from "~~/utils/scaffold-eth/common";
@@ -42,7 +46,7 @@ async function fetchByteCodeAndAssembly(buildInfoDirectory: string, contractPath
4246
4347
const getContractData = async (address: Address) => {
4448
const contracts = deployedContracts as GenericContractsDeclaration | null;
45-
const chainId = ${chainName[0]}.id;
49+
const chainId = ${solidityFramework[0]}.id;
4650
4751
if (!contracts || !contracts[chainId] || Object.keys(contracts[chainId]).length === 0) {
4852
return null;
@@ -59,7 +63,7 @@ const getContractData = async (address: Address) => {
5963
"..",
6064
"..",
6165
"..",
62-
"${chainName[0]}",
66+
"${solidityFramework[0]}",
6367
"${artifactsDirName[0]}",
6468
"build-info",
6569
);
@@ -101,9 +105,8 @@ const AddressPage = async (props: PageProps) => {
101105
return <AddressComponent address={address} contractData={contractData} />;
102106
};
103107
104-
export default AddressPage;`;
108+
export default AddressPage;`};
105109

106110
export default withDefaults(contents, {
107-
chainName: "hardhat",
108111
artifactsDirName: "artifacts",
109112
});
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export const solidityFramework = "foundry";
21
export const deployScriptDir = "script";
32

templates/solidity-frameworks/foundry/README.md.args.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export const solidityFramework = "Foundry";
21
export const networkConfigPath = `\`packages/foundry/foundry.toml\``;
32
export const contractsPath = `\`packages/foundry/contracts\``;
43
export const scriptsPath = `\`packages/foundry/script\``;
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export const chainName = "foundry";
21
export const artifactsDirName = "out";
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export const solidityFramework = "hardhat";
21
export const deployScriptDir = "deploy";
32

0 commit comments

Comments
 (0)