mirror of
https://github.com/alexgo-io/clarity-codegen.git
synced 2026-01-12 14:34:34 +08:00
feat: adjust generated filenames to allow to codegen multiple projects
This commit is contained in:
32
README.md
32
README.md
@@ -21,19 +21,33 @@ Alternatively, you can run codegen in JavaScript:
|
||||
```typescript
|
||||
import { generateContracts } from "clarity-codegen/lib/generate";
|
||||
import * as path from "path";
|
||||
import {
|
||||
contracts,
|
||||
DEPLOYER_ACCOUNT_ADDRESS,
|
||||
STACKS_API_URL,
|
||||
} from "../constants";
|
||||
|
||||
const STACKS_API_URL = "https://api.hiro.so";
|
||||
const DEPLOYER_ACCOUNT_ADDRESS = "SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9";
|
||||
|
||||
const alexContracts = [
|
||||
"age000-governance-token",
|
||||
]
|
||||
|
||||
const xlinkContracts = [
|
||||
"token-abtc",
|
||||
"btc-bridge-endpoint-v...",
|
||||
]
|
||||
|
||||
(async function main() {
|
||||
await generateContracts(
|
||||
STACKS_API_URL(),
|
||||
DEPLOYER_ACCOUNT_ADDRESS(),
|
||||
contracts,
|
||||
STACKS_API_URL,
|
||||
DEPLOYER_ACCOUNT_ADDRESS,
|
||||
alexContracts,
|
||||
path.resolve(__dirname, "./generated/"),
|
||||
"Alex"
|
||||
"alex"
|
||||
);
|
||||
await generateContracts(
|
||||
STACKS_API_URL,
|
||||
DEPLOYER_ACCOUNT_ADDRESS,
|
||||
xlinkContracts,
|
||||
path.resolve(__dirname, "./generated/"),
|
||||
"xlink"
|
||||
);
|
||||
})().catch(console.error);
|
||||
```
|
||||
|
||||
@@ -262,19 +262,21 @@ const toFunctionDescriptorDef = (
|
||||
};
|
||||
|
||||
export const generateContractFromAbi = async ({
|
||||
projectName,
|
||||
contractName,
|
||||
principal,
|
||||
apiHost,
|
||||
output,
|
||||
packageName,
|
||||
outputDir: output,
|
||||
runtimePackagePath,
|
||||
contractOverwrites,
|
||||
}: {
|
||||
projectName: string;
|
||||
contractName: string;
|
||||
aliasContractName?: string;
|
||||
principal: string;
|
||||
apiHost: string;
|
||||
output: string;
|
||||
packageName: string;
|
||||
outputDir: string;
|
||||
runtimePackagePath: string;
|
||||
contractOverwrites: { [from: string]: string };
|
||||
}): Promise<void> => {
|
||||
const url = `${apiHost}/v2/contracts/interface/${principal}/${
|
||||
@@ -314,7 +316,7 @@ export const generateContractFromAbi = async ({
|
||||
import {
|
||||
defineContract,
|
||||
${transcoderNames.join(",\n")}
|
||||
} from "${packageName}"
|
||||
} from "${runtimePackagePath}"
|
||||
|
||||
export const ${camelCase(contractName)} = defineContract({
|
||||
"${contractName}": ${inspect(
|
||||
@@ -348,34 +350,47 @@ export const ${camelCase(contractName)} = defineContract({
|
||||
`;
|
||||
|
||||
fs.writeFileSync(
|
||||
path.resolve(output, `./contract_${contractName}.ts`),
|
||||
path.resolve(
|
||||
output,
|
||||
`./${getContractFileName(projectName, contractName)}.ts`
|
||||
),
|
||||
source
|
||||
);
|
||||
};
|
||||
|
||||
export const contractGenerator = async ({
|
||||
contracts,
|
||||
name,
|
||||
output,
|
||||
packageName,
|
||||
projectName,
|
||||
outputDir,
|
||||
runtimePackagePath,
|
||||
}: {
|
||||
contracts: string[];
|
||||
name: string;
|
||||
output: string;
|
||||
packageName: string;
|
||||
projectName: string;
|
||||
outputDir: string;
|
||||
runtimePackagePath: string;
|
||||
}): Promise<void> => {
|
||||
const importsObjects = contracts.map((n) => `...${camelCase(n)}`);
|
||||
const importsHeaders = contracts.map(
|
||||
(n) => `import { ${camelCase(n)} } from "./contract_${n}"`
|
||||
(n) =>
|
||||
`import { ${camelCase(n)} } from "./${getContractFileName(
|
||||
projectName,
|
||||
n
|
||||
)}"`
|
||||
);
|
||||
const code = `import { defineContract } from "${packageName}";
|
||||
const code = `import { defineContract } from "${runtimePackagePath}";
|
||||
${importsHeaders.join("\n")}
|
||||
|
||||
export const ${name}Contracts = defineContract({
|
||||
export const ${projectName}Contracts = defineContract({
|
||||
${importsObjects.join(",\n")}
|
||||
});
|
||||
|
||||
`; /*? */
|
||||
|
||||
fs.writeFileSync(path.resolve(output, `./contracts_${name}.ts`), code);
|
||||
fs.writeFileSync(
|
||||
path.resolve(outputDir, `./contracts_${projectName}.ts`),
|
||||
code
|
||||
);
|
||||
};
|
||||
|
||||
const getContractFileName = (projectName: string, contractName: string) =>
|
||||
`contract_${projectName}_${contractName}`;
|
||||
|
||||
@@ -8,34 +8,43 @@ export async function generateContracts(
|
||||
apiHost: string | ((contract: string) => string),
|
||||
principal: string | ((contract: string) => string),
|
||||
contracts: string[],
|
||||
output: string,
|
||||
name: string,
|
||||
packageName: string = "clarity-codegen",
|
||||
outputDir: string,
|
||||
projectName: string,
|
||||
runtimePackagePath: string = "clarity-codegen",
|
||||
contractOverwrites: { [from: string]: string } = {},
|
||||
options: { concurrency?: number } = {}
|
||||
) {
|
||||
const concurrency = options.concurrency?? 16
|
||||
const concurrency = options.concurrency ?? 16;
|
||||
const batch = new YBatch({ concurrency });
|
||||
for (const cname of contracts) {
|
||||
await batch.add(async () => {
|
||||
console.log(`Generating contract ${typeof principal === 'string' ? principal : principal(cname)}.${cname}`);
|
||||
console.log(
|
||||
`Generating contract ${
|
||||
typeof principal === "string" ? principal : principal(cname)
|
||||
}.${cname}`
|
||||
);
|
||||
await generateContractFromAbi({
|
||||
apiHost: typeof apiHost === 'string' ? apiHost : apiHost(cname) ,
|
||||
principal: typeof principal === 'string' ? principal : principal(cname),
|
||||
apiHost: typeof apiHost === "string" ? apiHost : apiHost(cname),
|
||||
principal: typeof principal === "string" ? principal : principal(cname),
|
||||
projectName,
|
||||
contractName: cname,
|
||||
output,
|
||||
packageName,
|
||||
contractOverwrites
|
||||
outputDir,
|
||||
runtimePackagePath,
|
||||
contractOverwrites,
|
||||
});
|
||||
console.log(`Generated contract ${typeof principal === 'string' ? principal : principal(cname)}.${cname}`);
|
||||
console.log(
|
||||
`Generated contract ${
|
||||
typeof principal === "string" ? principal : principal(cname)
|
||||
}.${cname}`
|
||||
);
|
||||
});
|
||||
}
|
||||
await batch.failFast();
|
||||
|
||||
await contractGenerator({
|
||||
contracts: contracts,
|
||||
output: output,
|
||||
name,
|
||||
packageName,
|
||||
contracts,
|
||||
outputDir,
|
||||
projectName,
|
||||
runtimePackagePath,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user