Skip to content

Commit 3ac0751

Browse files
committed
fix(contracts): sync prebuilt artifacts into workspace dist
1 parent 5a5e58d commit 3ac0751

File tree

4 files changed

+56
-78
lines changed

4 files changed

+56
-78
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"gen:docs": "node ./tools/scripts/gen-doc.mjs",
1818
"gen:readme": "node ./tools/scripts/gen-readme.mjs",
1919
"gen:local-network-context": "bun run packages/networks/src/networks/vNaga/envs/naga-local/scripts/00-generate-abi-signatures.ts",
20-
"sync:contracts": "nx run contracts:start",
20+
"sync:contracts": "nx run contracts:sync",
2121
"prettier": "npx nx format:write --all",
2222
"lint": "npx nx run-many --target=lint --all",
2323
"lint:fix": "npx nx run-many --target=lint --all -- --fix",

packages/contracts/project.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
"sourceRoot": "packages/contracts/src",
55
"projectType": "library",
66
"targets": {
7-
"start": {
7+
"build": {
88
"executor": "nx:run-commands",
9+
"outputs": ["{workspaceRoot}/dist/packages/contracts"],
910
"options": {
10-
"command": "tsx src/index.ts",
11+
"command": "tsx src/build.ts",
12+
"cwd": "packages/contracts"
13+
}
14+
},
15+
"sync": {
16+
"executor": "nx:run-commands",
17+
"options": {
18+
"command": "tsx src/sync.ts",
1119
"cwd": "packages/contracts"
1220
}
1321
},

packages/contracts/src/build.ts

Lines changed: 45 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
1-
import { cpSync, existsSync, mkdirSync, rmSync } from 'fs';
1+
import { cpSync, existsSync, mkdirSync, rmSync, statSync } from 'fs';
22
import path from 'path';
33
import { fileURLToPath } from 'url';
4-
import { build as runEsbuild } from 'esbuild';
5-
import { createRequire } from 'module';
6-
import { execFileSync } from 'child_process';
7-
import { runContractsSync } from './index';
4+
5+
function ensureDir(dir: string) {
6+
if (!existsSync(dir)) {
7+
mkdirSync(dir, { recursive: true });
8+
}
9+
}
10+
11+
function cleanDir(dir: string) {
12+
if (existsSync(dir)) {
13+
rmSync(dir, { recursive: true, force: true });
14+
}
15+
}
16+
17+
function copyArtifacts({
18+
packageRoot,
19+
localDistPath,
20+
workspaceDistPath,
21+
}: {
22+
packageRoot: string;
23+
localDistPath: string;
24+
workspaceDistPath: string;
25+
}) {
26+
ensureDir(path.dirname(workspaceDistPath));
27+
cleanDir(workspaceDistPath);
28+
ensureDir(workspaceDistPath);
29+
cpSync(localDistPath, path.join(workspaceDistPath, 'dist'), {
30+
recursive: true,
31+
});
32+
cpSync(
33+
path.join(packageRoot, 'package.json'),
34+
path.join(workspaceDistPath, 'package.json')
35+
);
36+
const readmePath = path.join(packageRoot, 'README.md');
37+
if (existsSync(readmePath)) {
38+
cpSync(readmePath, path.join(workspaceDistPath, 'README.md'));
39+
}
40+
}
841

942
async function main() {
1043
const currentFile = fileURLToPath(import.meta.url);
@@ -15,79 +48,16 @@ async function main() {
1548
packageRoot,
1649
'../../dist/packages/contracts'
1750
);
18-
const customSignaturesEntry = path.join(
19-
packageRoot,
20-
'src/custom-network-signatures.ts'
21-
);
22-
23-
process.chdir(packageRoot);
24-
25-
const ensureDir = (dir: string) => {
26-
if (!existsSync(dir)) {
27-
mkdirSync(dir, { recursive: true });
28-
}
29-
};
30-
31-
const cleanDir = (dir: string) => {
32-
if (existsSync(dir)) {
33-
rmSync(dir, { recursive: true, force: true });
34-
}
35-
};
36-
37-
const buildCustomSignatures = async () => {
38-
ensureDir(localDistPath);
39-
await runEsbuild({
40-
entryPoints: [customSignaturesEntry],
41-
outfile: path.join(localDistPath, 'custom-network-signatures.js'),
42-
platform: 'node',
43-
target: 'node20',
44-
format: 'esm',
45-
bundle: true,
46-
sourcemap: false,
47-
});
48-
49-
await runEsbuild({
50-
entryPoints: [customSignaturesEntry],
51-
outfile: path.join(localDistPath, 'custom-network-signatures.cjs'),
52-
platform: 'node',
53-
target: 'node20',
54-
format: 'cjs',
55-
bundle: true,
56-
sourcemap: false,
57-
});
58-
};
59-
60-
const emitDeclarations = () => {
61-
const require = createRequire(import.meta.url);
62-
const tscPath = require.resolve('typescript/bin/tsc');
63-
execFileSync(
64-
process.execPath,
65-
[tscPath, '-p', path.join(packageRoot, 'tsconfig.lib.json')],
66-
{
67-
stdio: 'inherit',
68-
}
69-
);
70-
};
7151

72-
const copyToWorkspaceDist = () => {
73-
ensureDir(path.dirname(workspaceDistPath));
74-
cleanDir(workspaceDistPath);
75-
cpSync(localDistPath, workspaceDistPath, { recursive: true });
76-
cpSync(
77-
path.join(packageRoot, 'package.json'),
78-
path.join(workspaceDistPath, 'package.json')
52+
if (!existsSync(localDistPath) || !statSync(localDistPath).isDirectory()) {
53+
throw new Error(
54+
'Local dist artifacts not found. Generate contracts manually before running the build.'
7955
);
80-
const readmePath = path.join(packageRoot, 'README.md');
81-
if (existsSync(readmePath)) {
82-
cpSync(readmePath, path.join(workspaceDistPath, 'README.md'));
83-
}
84-
};
56+
}
8557

86-
cleanDir(localDistPath);
87-
await buildCustomSignatures();
88-
await runContractsSync();
89-
emitDeclarations();
90-
copyToWorkspaceDist();
58+
console.log('📦 Syncing prebuilt contracts artifacts...');
59+
copyArtifacts({ packageRoot, localDistPath, workspaceDistPath });
60+
console.log('✅ Contracts artifacts synced to workspace dist.');
9161
}
9262

9363
main().catch((error) => {
File renamed without changes.

0 commit comments

Comments
 (0)