|
| 1 | +import { readdir, rename, rm } from 'node:fs/promises'; |
| 2 | +import { join, dirname } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | + |
| 5 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 6 | +const cjsDir = join(__dirname, '../dist/cjs-temp'); |
| 7 | +const distDir = join(__dirname, '../dist'); |
| 8 | + |
| 9 | +async function renameCjsFiles() { |
| 10 | + // Recursively get all .js files |
| 11 | + async function getJsFiles(dir) { |
| 12 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 13 | + const files = await Promise.all(entries.map((entry) => { |
| 14 | + const res = join(dir, entry.name); |
| 15 | + return entry.isDirectory() ? getJsFiles(res) : res; |
| 16 | + })); |
| 17 | + return files.flat().filter(file => file.endsWith('.js')); |
| 18 | + } |
| 19 | + |
| 20 | + // Rename all .js files to .cjs and move them to dist |
| 21 | + const jsFiles = await getJsFiles(cjsDir); |
| 22 | + await Promise.all(jsFiles.map(async (file) => { |
| 23 | + const relativePath = file.split('cjs-temp/')[1]; |
| 24 | + const destPath = join(distDir, relativePath.replace('.js', '.cjs')); |
| 25 | + await rename(file, destPath); |
| 26 | + })); |
| 27 | + |
| 28 | + // Clean up temporary directory |
| 29 | + await rm(cjsDir, { recursive: true }); |
| 30 | +} |
| 31 | + |
| 32 | +// Run TypeScript builds |
| 33 | +const { execSync } = await import('node:child_process'); |
| 34 | +execSync('tsc -p tsconfig.json', { stdio: 'inherit' }); |
| 35 | +execSync('tsc -p tsconfig.cjs.json', { stdio: 'inherit' }); |
| 36 | + |
| 37 | +// Rename CJS files |
| 38 | +await renameCjsFiles(); |
0 commit comments