Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Path to app-monorepo for local sync
APP_MONOREPO_LOCAL_PATH=/path/to/app-monorepo
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,4 @@ dist
!.yarn/sdks
!.yarn/versions

.vscode/
9 changes: 0 additions & 9 deletions .vscode/settings.json

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/cardano-coin-selection-asmjs",
"version": "1.1.5",
"version": "1.1.10-alpha.8",
"description": "",
"keywords": [
"coin selection"
Expand Down Expand Up @@ -29,7 +29,8 @@
"type-check": "yarn tsc -p tsconfig.types.json",
"test": "yarn run-s 'test:*'",
"test:unit": "jest -c ./jest.config.js",
"test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg"
"test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg",
"debug:watcher": "node scripts/sync-to-monorepo.js"
},
"devDependencies": {
"@emurgo/cardano-serialization-lib-nodejs": "13.2.0",
Expand All @@ -42,10 +43,13 @@
"@typescript-eslint/eslint-plugin": "^8.8.0",
"@typescript-eslint/parser": "8.8.0",
"bignumber.js": "^9.0.1",
"chokidar": "^5.0.0",
"dotenv": "^17.2.3",
"eslint": "^9.11.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-prettier": "^5.2.1",
"fs-extra": "^11.3.2",
"jest": "^29.7.0",
"make-coverage-badge": "^1.2.0",
"npm-run-all": "^4.1.5",
Expand Down
158 changes: 158 additions & 0 deletions scripts/sync-to-monorepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* eslint-disable @typescript-eslint/no-var-requires */
require('dotenv').config();
const chokidar = require('chokidar');
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');

// Configuration
const config = {
// Path to app-monorepo, read from environment variable
targetDir: process.env.APP_MONOREPO_LOCAL_PATH,
// Current package name
packageName: '@onekeyfe/cardano-coin-selection-asmjs',
// Source directory to watch
watchDir: 'src',
// Build output directory
buildDir: 'lib',
// Debounce time in milliseconds
debounceTime: 300,
// Apps whose caches need to be cleared
appCacheDirs: ['desktop', 'ext', 'web', 'mobile'],
};

if (!config.targetDir) {
console.error(
'❌ APP_MONOREPO_LOCAL_PATH is not set. Please specify it in the .env file.\n' +
'Example: APP_MONOREPO_LOCAL_PATH=/path/to/app-monorepo'
);
process.exit(1);
}

const projectRoot = path.join(__dirname, '..');
const destNodeModulesPath = path.join(
config.targetDir,
'node_modules',
config.packageName
);

console.log('📦 Package:', config.packageName);
console.log('📂 Source:', path.join(projectRoot, config.watchDir));
console.log('📁 Target:', destNodeModulesPath);
console.log('');

// Debounced build state
let buildTimeout = null;
let isBuilding = false;

function triggerBuild() {
if (buildTimeout) {
clearTimeout(buildTimeout);
}

buildTimeout = setTimeout(() => {
if (isBuilding) {
console.log('⏳ Build already in progress, queuing...');
triggerBuild();
return;
}

isBuilding = true;
console.log('\n🔨 Building...');

exec('yarn build', { cwd: projectRoot }, (error, stdout, stderr) => {
isBuilding = false;

if (error) {
console.error('❌ Build failed:', error.message);
if (stderr) console.error(stderr);
return;
}

console.log('✅ Build complete');
syncBuildOutput();
});
}, config.debounceTime);
}

async function clearAppCaches() {
const clearedApps = [];

for (const app of config.appCacheDirs) {
const cachePath = path.join(config.targetDir, 'apps', app, 'node_modules', '.cache');
if (await fs.pathExists(cachePath)) {
await fs.remove(cachePath);
clearedApps.push(app);
}
}

if (clearedApps.length > 0) {
console.log(`🧹 Cleared cache for: ${clearedApps.join(', ')}`);
}
}

async function syncBuildOutput() {
const srcLibPath = path.join(projectRoot, config.buildDir);
const destLibPath = path.join(destNodeModulesPath, config.buildDir);

try {
// Sync the entire lib directory
await fs.copy(srcLibPath, destLibPath, { overwrite: true });
console.log(`📤 Synced ${config.buildDir}/ -> ${destLibPath}`);

// Sync package.json (in case version or other fields changed)
const srcPackageJson = path.join(projectRoot, 'package.json');
const destPackageJson = path.join(destNodeModulesPath, 'package.json');
await fs.copy(srcPackageJson, destPackageJson, { overwrite: true });
console.log('📤 Synced package.json');

// Clear webpack caches for all apps
await clearAppCaches();

console.log('✨ Sync complete!\n');
} catch (err) {
console.error('❌ Sync error:', err);
}
}

// Watch for source file changes
const watcher = chokidar.watch(path.join(projectRoot, config.watchDir), {
ignored: [
/(^|[\/\\])\./, // Ignore dotfiles
/node_modules/,
/\.test\./, // Ignore test files
/\.spec\./,
],
persistent: true,
ignoreInitial: true,
});

watcher
.on('add', filePath => {
console.log(`➕ Added: ${path.relative(projectRoot, filePath)}`);
triggerBuild();
})
.on('change', filePath => {
console.log(`✏️ Changed: ${path.relative(projectRoot, filePath)}`);
triggerBuild();
})
.on('unlink', filePath => {
console.log(`➖ Removed: ${path.relative(projectRoot, filePath)}`);
triggerBuild();
})
.on('error', error => console.error(`Watcher error: ${error}`))
.on('ready', () => {
console.log('👀 Watching for changes in src/...');
console.log('💡 Tip: Press Ctrl+C to stop\n');

// Run initial build and sync on startup
console.log('🚀 Initial build and sync...');
triggerBuild();
});

process.on('SIGINT', () => {
console.log('\n👋 Closing watcher...');
watcher.close();
console.log('Bye!');
process.exit(0);
});
Loading
Loading