UNPKG

@gotake/gotake-sdk

Version:

SDK for interacting with GoTake blockchain contracts

138 lines (111 loc) 4.5 kB
const fs = require('fs'); const path = require('path'); function fixESModuleImports() { const esmDir = path.join(__dirname, '../dist/esm'); function processFile(filePath) { if (!fs.existsSync(filePath)) return; let content = fs.readFileSync(filePath, 'utf8'); // Fix relative imports to include .js extension content = content.replace( /from ['"](\.[^'"]*?)['"];/g, (match, importPath) => { if (importPath.endsWith('.js')) return match; // Check if this is a directory import (no file extension and common directory names) const directoryNames = ['utils', 'api', 'wrappers']; const isDirectoryImport = directoryNames.some(dir => importPath === `./${dir}` || importPath.endsWith(`/${dir}`) ); if (isDirectoryImport) { // Directory import, add /index.js return match.replace(importPath, importPath + '/index.js'); } else { // File import, add .js return match.replace(importPath, importPath + '.js'); } } ); // Fix external package imports that end with directory paths content = content.replace( /from ['"]([^'"]*?)['"];/g, (match, importPath) => { // Skip if already has .js extension or is a relative import if (importPath.endsWith('.js') || importPath.startsWith('.')) return match; // Skip common npm packages that should be left as-is const skipPackages = ['ethers', '@ethersproject']; const shouldSkip = skipPackages.some(pkg => importPath.startsWith(pkg)); if (shouldSkip) return match; // Handle gotake-contracts specifically if (importPath.startsWith('gotake-contracts/') && importPath.includes('/')) { const lastPart = importPath.split('/').pop(); if (lastPart) { // If it's a __factory file, add .js if (lastPart.includes('__factory')) { return match.replace(importPath, importPath + '.js'); } // If it's a .sol file (directory), add /index.js else if (lastPart.endsWith('.sol')) { return match.replace(importPath, importPath + '/index.js'); } // If it's a directory (no extension), add /index.js else if (!lastPart.includes('.')) { return match.replace(importPath, importPath + '/index.js'); } } } return match; } ); fs.writeFileSync(filePath, content); } function processDirectory(dir) { const files = fs.readdirSync(dir); files.forEach(file => { const fullPath = path.join(dir, file); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { processDirectory(fullPath); } else if (file.endsWith('.js')) { processFile(fullPath); } }); } if (fs.existsSync(esmDir)) { processDirectory(esmDir); console.log('ES module imports fixed'); } } function copyTypeDefinitions() { const srcTypes = path.join(__dirname, '../dist/types'); if (fs.existsSync(srcTypes)) { console.log('Type definitions generated successfully'); } console.log('Post-build processing completed'); } function createPackageJsons() { // Create package.json for CJS const cjsPackage = { type: 'commonjs' }; // Create package.json for ESM const esmPackage = { type: 'module' }; fs.writeFileSync( path.join(__dirname, '../dist/cjs/package.json'), JSON.stringify(cjsPackage, null, 2) ); fs.writeFileSync( path.join(__dirname, '../dist/esm/package.json'), JSON.stringify(esmPackage, null, 2) ); console.log('Package.json files created for CJS and ESM'); } function main() { copyTypeDefinitions(); createPackageJsons(); fixESModuleImports(); } if (require.main === module) { main(); } module.exports = { main };