1- import { cpSync , existsSync , mkdirSync , rmSync } from 'fs' ;
1+ import { cpSync , existsSync , mkdirSync , rmSync , statSync } from 'fs' ;
22import path from 'path' ;
33import { 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
942async 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
9363main ( ) . catch ( ( error ) => {
0 commit comments