Skip to content

Commit 4cdb649

Browse files
committed
feat: add sync-to-monorepo script and update environment configuration
1 parent 6787ef6 commit 4cdb649

5 files changed

Lines changed: 229 additions & 10 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Path to app-monorepo for local sync
2+
APP_MONOREPO_LOCAL_PATH=/path/to/app-monorepo

.vscode/settings.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"type-check": "yarn tsc -p tsconfig.types.json",
3030
"test": "yarn run-s 'test:*'",
3131
"test:unit": "jest -c ./jest.config.js",
32-
"test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg"
32+
"test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg",
33+
"debug:watcher": "node scripts/sync-to-monorepo.js"
3334
},
3435
"devDependencies": {
3536
"@emurgo/cardano-serialization-lib-nodejs": "13.2.0",
@@ -42,10 +43,13 @@
4243
"@typescript-eslint/eslint-plugin": "^8.8.0",
4344
"@typescript-eslint/parser": "8.8.0",
4445
"bignumber.js": "^9.0.1",
46+
"chokidar": "^5.0.0",
47+
"dotenv": "^17.2.3",
4548
"eslint": "^9.11.1",
4649
"eslint-config-prettier": "^9.1.0",
4750
"eslint-plugin-import": "^2.30.0",
4851
"eslint-plugin-prettier": "^5.2.1",
52+
"fs-extra": "^11.3.2",
4953
"jest": "^29.7.0",
5054
"make-coverage-badge": "^1.2.0",
5155
"npm-run-all": "^4.1.5",

scripts/sync-to-monorepo.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
require('dotenv').config();
3+
const chokidar = require('chokidar');
4+
const fs = require('fs-extra');
5+
const path = require('path');
6+
const { exec } = require('child_process');
7+
8+
// Configuration
9+
const config = {
10+
// Path to app-monorepo, read from environment variable
11+
targetDir: process.env.APP_MONOREPO_LOCAL_PATH,
12+
// Current package name
13+
packageName: '@onekeyfe/cardano-coin-selection-asmjs',
14+
// Source directory to watch
15+
watchDir: 'src',
16+
// Build output directory
17+
buildDir: 'lib',
18+
// Debounce time in milliseconds
19+
debounceTime: 300,
20+
// Apps whose caches need to be cleared
21+
appCacheDirs: ['desktop', 'ext', 'web', 'mobile'],
22+
};
23+
24+
if (!config.targetDir) {
25+
console.error(
26+
'❌ APP_MONOREPO_LOCAL_PATH is not set. Please specify it in the .env file.\n' +
27+
'Example: APP_MONOREPO_LOCAL_PATH=/path/to/app-monorepo'
28+
);
29+
process.exit(1);
30+
}
31+
32+
const projectRoot = __dirname;
33+
const destNodeModulesPath = path.join(
34+
config.targetDir,
35+
'node_modules',
36+
config.packageName
37+
);
38+
39+
console.log('📦 Package:', config.packageName);
40+
console.log('📂 Source:', path.join(projectRoot, config.watchDir));
41+
console.log('📁 Target:', destNodeModulesPath);
42+
console.log('');
43+
44+
// Debounced build state
45+
let buildTimeout = null;
46+
let isBuilding = false;
47+
48+
function triggerBuild() {
49+
if (buildTimeout) {
50+
clearTimeout(buildTimeout);
51+
}
52+
53+
buildTimeout = setTimeout(() => {
54+
if (isBuilding) {
55+
console.log('⏳ Build already in progress, queuing...');
56+
triggerBuild();
57+
return;
58+
}
59+
60+
isBuilding = true;
61+
console.log('\n🔨 Building...');
62+
63+
exec('yarn build', { cwd: projectRoot }, (error, stdout, stderr) => {
64+
isBuilding = false;
65+
66+
if (error) {
67+
console.error('❌ Build failed:', error.message);
68+
if (stderr) console.error(stderr);
69+
return;
70+
}
71+
72+
console.log('✅ Build complete');
73+
syncBuildOutput();
74+
});
75+
}, config.debounceTime);
76+
}
77+
78+
async function clearAppCaches() {
79+
const clearedApps = [];
80+
81+
for (const app of config.appCacheDirs) {
82+
const cachePath = path.join(config.targetDir, 'apps', app, 'node_modules', '.cache');
83+
if (await fs.pathExists(cachePath)) {
84+
await fs.remove(cachePath);
85+
clearedApps.push(app);
86+
}
87+
}
88+
89+
if (clearedApps.length > 0) {
90+
console.log(`🧹 Cleared cache for: ${clearedApps.join(', ')}`);
91+
}
92+
}
93+
94+
async function syncBuildOutput() {
95+
const srcLibPath = path.join(projectRoot, config.buildDir);
96+
const destLibPath = path.join(destNodeModulesPath, config.buildDir);
97+
98+
try {
99+
// Sync the entire lib directory
100+
await fs.copy(srcLibPath, destLibPath, { overwrite: true });
101+
console.log(`📤 Synced ${config.buildDir}/ -> ${destLibPath}`);
102+
103+
// Sync package.json (in case version or other fields changed)
104+
const srcPackageJson = path.join(projectRoot, 'package.json');
105+
const destPackageJson = path.join(destNodeModulesPath, 'package.json');
106+
await fs.copy(srcPackageJson, destPackageJson, { overwrite: true });
107+
console.log('📤 Synced package.json');
108+
109+
// Clear webpack caches for all apps
110+
await clearAppCaches();
111+
112+
console.log('✨ Sync complete!\n');
113+
} catch (err) {
114+
console.error('❌ Sync error:', err);
115+
}
116+
}
117+
118+
// Watch for source file changes
119+
const watcher = chokidar.watch(path.join(projectRoot, config.watchDir), {
120+
ignored: [
121+
/(^|[\/\\])\./, // Ignore dotfiles
122+
/node_modules/,
123+
/\.test\./, // Ignore test files
124+
/\.spec\./,
125+
],
126+
persistent: true,
127+
ignoreInitial: true,
128+
});
129+
130+
watcher
131+
.on('add', filePath => {
132+
console.log(`➕ Added: ${path.relative(projectRoot, filePath)}`);
133+
triggerBuild();
134+
})
135+
.on('change', filePath => {
136+
console.log(`✏️ Changed: ${path.relative(projectRoot, filePath)}`);
137+
triggerBuild();
138+
})
139+
.on('unlink', filePath => {
140+
console.log(`➖ Removed: ${path.relative(projectRoot, filePath)}`);
141+
triggerBuild();
142+
})
143+
.on('error', error => console.error(`Watcher error: ${error}`))
144+
.on('ready', () => {
145+
console.log('👀 Watching for changes in src/...');
146+
console.log('💡 Tip: Press Ctrl+C to stop\n');
147+
148+
// Run initial build and sync on startup
149+
console.log('🚀 Initial build and sync...');
150+
triggerBuild();
151+
});
152+
153+
process.on('SIGINT', () => {
154+
console.log('\n👋 Closing watcher...');
155+
watcher.close();
156+
console.log('Bye!');
157+
process.exit(0);
158+
});

yarn.lock

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,10 +1624,13 @@ __metadata:
16241624
"@typescript-eslint/eslint-plugin": "npm:^8.8.0"
16251625
"@typescript-eslint/parser": "npm:8.8.0"
16261626
bignumber.js: "npm:^9.0.1"
1627+
chokidar: "npm:^5.0.0"
1628+
dotenv: "npm:^17.2.3"
16271629
eslint: "npm:^9.11.1"
16281630
eslint-config-prettier: "npm:^9.1.0"
16291631
eslint-plugin-import: "npm:^2.30.0"
16301632
eslint-plugin-prettier: "npm:^5.2.1"
1633+
fs-extra: "npm:^11.3.2"
16311634
jest: "npm:^29.7.0"
16321635
make-coverage-badge: "npm:^1.2.0"
16331636
npm-run-all: "npm:^4.1.5"
@@ -2788,6 +2791,15 @@ __metadata:
27882791
languageName: node
27892792
linkType: hard
27902793

2794+
"chokidar@npm:^5.0.0":
2795+
version: 5.0.0
2796+
resolution: "chokidar@npm:5.0.0"
2797+
dependencies:
2798+
readdirp: "npm:^5.0.0"
2799+
checksum: 10/a1c2a4ee6ee81ba6409712c295a47be055fb9de1186dfbab33c1e82f28619de962ba02fc5f9d433daaedc96c35747460d8b2079ac2907de2c95e3f7cce913113
2800+
languageName: node
2801+
linkType: hard
2802+
27912803
"chownr@npm:^2.0.0":
27922804
version: 2.0.0
27932805
resolution: "chownr@npm:2.0.0"
@@ -3149,6 +3161,13 @@ __metadata:
31493161
languageName: node
31503162
linkType: hard
31513163

3164+
"dotenv@npm:^17.2.3":
3165+
version: 17.2.3
3166+
resolution: "dotenv@npm:17.2.3"
3167+
checksum: 10/f8b78626ebfff6e44420f634773375c9651808b3e1a33df6d4cc19120968eea53e100f59f04ec35f2a20b2beb334b6aba4f24040b2f8ad61773f158ac042a636
3168+
languageName: node
3169+
linkType: hard
3170+
31523171
"eastasianwidth@npm:^0.2.0":
31533172
version: 0.2.0
31543173
resolution: "eastasianwidth@npm:0.2.0"
@@ -3791,6 +3810,17 @@ __metadata:
37913810
languageName: node
37923811
linkType: hard
37933812

3813+
"fs-extra@npm:^11.3.2":
3814+
version: 11.3.2
3815+
resolution: "fs-extra@npm:11.3.2"
3816+
dependencies:
3817+
graceful-fs: "npm:^4.2.0"
3818+
jsonfile: "npm:^6.0.1"
3819+
universalify: "npm:^2.0.0"
3820+
checksum: 10/d559545c73fda69c75aa786f345c2f738b623b42aea850200b1582e006a35278f63787179e3194ba19413c26a280441758952b0c7e88dd96762d497e365a6c3e
3821+
languageName: node
3822+
linkType: hard
3823+
37943824
"fs-minipass@npm:^2.0.0":
37953825
version: 2.1.0
37963826
resolution: "fs-minipass@npm:2.1.0"
@@ -4036,6 +4066,13 @@ __metadata:
40364066
languageName: node
40374067
linkType: hard
40384068

4069+
"graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.2.0":
4070+
version: 4.2.11
4071+
resolution: "graceful-fs@npm:4.2.11"
4072+
checksum: 10/bf152d0ed1dc159239db1ba1f74fdbc40cb02f626770dcd5815c427ce0688c2635a06ed69af364396da4636d0408fcf7d4afdf7881724c3307e46aff30ca49e2
4073+
languageName: node
4074+
linkType: hard
4075+
40394076
"graceful-fs@npm:^4.2.9":
40404077
version: 4.2.10
40414078
resolution: "graceful-fs@npm:4.2.10"
@@ -5239,6 +5276,19 @@ __metadata:
52395276
languageName: node
52405277
linkType: hard
52415278

5279+
"jsonfile@npm:^6.0.1":
5280+
version: 6.2.0
5281+
resolution: "jsonfile@npm:6.2.0"
5282+
dependencies:
5283+
graceful-fs: "npm:^4.1.6"
5284+
universalify: "npm:^2.0.0"
5285+
dependenciesMeta:
5286+
graceful-fs:
5287+
optional: true
5288+
checksum: 10/513aac94a6eff070767cafc8eb4424b35d523eec0fcd8019fe5b975f4de5b10a54640c8d5961491ddd8e6f562588cf62435c5ddaf83aaf0986cd2ee789e0d7b9
5289+
languageName: node
5290+
linkType: hard
5291+
52425292
"keyv@npm:^4.5.4":
52435293
version: 4.5.4
52445294
resolution: "keyv@npm:4.5.4"
@@ -6224,6 +6274,13 @@ __metadata:
62246274
languageName: node
62256275
linkType: hard
62266276

6277+
"readdirp@npm:^5.0.0":
6278+
version: 5.0.0
6279+
resolution: "readdirp@npm:5.0.0"
6280+
checksum: 10/a17a591b51d8b912083660df159e8bd17305dc1a9ef27c869c818bd95ff59e3a6496f97e91e724ef433e789d559d24e39496ea1698822eb5719606dc9c1a923d
6281+
languageName: node
6282+
linkType: hard
6283+
62276284
"regexp.prototype.flags@npm:^1.5.2":
62286285
version: 1.5.2
62296286
resolution: "regexp.prototype.flags@npm:1.5.2"
@@ -7184,6 +7241,13 @@ __metadata:
71847241
languageName: node
71857242
linkType: hard
71867243

7244+
"universalify@npm:^2.0.0":
7245+
version: 2.0.1
7246+
resolution: "universalify@npm:2.0.1"
7247+
checksum: 10/ecd8469fe0db28e7de9e5289d32bd1b6ba8f7183db34f3bfc4ca53c49891c2d6aa05f3fb3936a81285a905cc509fb641a0c3fc131ec786167eff41236ae32e60
7248+
languageName: node
7249+
linkType: hard
7250+
71877251
"update-browserslist-db@npm:^1.0.5":
71887252
version: 1.0.5
71897253
resolution: "update-browserslist-db@npm:1.0.5"

0 commit comments

Comments
 (0)