|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; |
| 5 | +import { dirname, join } from 'path'; |
| 6 | +import { fileURLToPath } from 'url'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const rootDir = join(__dirname, '..'); |
| 10 | + |
| 11 | +console.log('Building with tsdown...'); |
| 12 | +try { |
| 13 | + execSync('pnpm tsdown', { stdio: 'inherit', cwd: rootDir }); |
| 14 | +} catch (error) { |
| 15 | + console.error('Build failed:', error); |
| 16 | + process.exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +// Create directories after tsdown build. |
| 20 | +const distDir = join(rootDir, 'dist'); |
| 21 | +const distGeneratedDir = join(distDir, 'generated'); |
| 22 | +const parserRawTransferDir = join(distDir, 'parser', 'raw-transfer'); |
| 23 | +const parserGeneratedDir = join(distDir, 'parser', 'generated', 'lazy'); |
| 24 | + |
| 25 | +// Create all necessary directories |
| 26 | +if (!existsSync(distGeneratedDir)) { |
| 27 | + mkdirSync(distGeneratedDir, { recursive: true }); |
| 28 | +} |
| 29 | +if (!existsSync(parserRawTransferDir)) { |
| 30 | + mkdirSync(parserRawTransferDir, { recursive: true }); |
| 31 | +} |
| 32 | +if (!existsSync(parserGeneratedDir)) { |
| 33 | + mkdirSync(parserGeneratedDir, { recursive: true }); |
| 34 | +} |
| 35 | + |
| 36 | +console.log('Copying generated files...'); |
| 37 | +// Copy generated constants file |
| 38 | +const constantsPath = join(rootDir, 'src-js/generated/constants.cjs'); |
| 39 | +if (existsSync(constantsPath)) { |
| 40 | + copyFileSync(constantsPath, join(distGeneratedDir, 'constants.cjs')); |
| 41 | + console.log('Copied constants.cjs'); |
| 42 | +} else { |
| 43 | + console.error( |
| 44 | + 'Warning: generated/constants.cjs not found. Make sure to run napi build first.', |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +// Copy parser files |
| 49 | +const parserFiles = [ |
| 50 | + { |
| 51 | + src: join(rootDir, '../parser/raw-transfer/lazy-common.js'), |
| 52 | + dest: join(parserRawTransferDir, 'lazy-common.cjs'), |
| 53 | + }, |
| 54 | + { |
| 55 | + src: join(rootDir, '../parser/raw-transfer/node-array.js'), |
| 56 | + dest: join(parserRawTransferDir, 'node-array.cjs'), |
| 57 | + }, |
| 58 | + { |
| 59 | + src: join(rootDir, '../parser/generated/lazy/walk.js'), |
| 60 | + dest: join(parserGeneratedDir, 'walk.cjs'), |
| 61 | + }, |
| 62 | + { |
| 63 | + src: join(rootDir, '../parser/generated/lazy/types.js'), |
| 64 | + dest: join(parserGeneratedDir, 'types.cjs'), |
| 65 | + }, |
| 66 | + { |
| 67 | + src: join(rootDir, '../parser/generated/lazy/constructors.js'), |
| 68 | + dest: join(parserGeneratedDir, 'constructors.cjs'), |
| 69 | + }, |
| 70 | +]; |
| 71 | + |
| 72 | +for (const { src, dest } of parserFiles) { |
| 73 | + if (existsSync(src)) { |
| 74 | + // replace a any `require(`*.js`)` with `require(`*`.cjs`)` |
| 75 | + const content = String(readFileSync(src)); |
| 76 | + const updatedContent = content.replace( |
| 77 | + /require\((['"])(.*?)(\.js)(['"])\)/g, |
| 78 | + (match, p1, p2, p3, p4) => { |
| 79 | + return `require(${p1}${p2}.cjs${p4})`; |
| 80 | + }, |
| 81 | + ); |
| 82 | + writeFileSync(dest, updatedContent); |
| 83 | + |
| 84 | + // |
| 85 | + // copyFileSync(src, dest); |
| 86 | + console.log(`Copied ${src.split('/').pop()}`); |
| 87 | + } else { |
| 88 | + console.error(`Warning: parser file not found: ${src}`); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Copy native .node files that might exist in src-js |
| 93 | +const nodeFiles = [ |
| 94 | + 'oxlint.darwin-arm64.node', |
| 95 | + 'oxlint.darwin-x64.node', |
| 96 | + 'oxlint.linux-x64-gnu.node', |
| 97 | + 'oxlint.linux-arm64-gnu.node', |
| 98 | + 'oxlint.linux-x64-musl.node', |
| 99 | + 'oxlint.linux-arm64-musl.node', |
| 100 | + 'oxlint.win32-x64-msvc.node', |
| 101 | + 'oxlint.win32-arm64-msvc.node', |
| 102 | +]; |
| 103 | + |
| 104 | +for (const nodeFile of nodeFiles) { |
| 105 | + const srcPath = join(rootDir, 'src-js', nodeFile); |
| 106 | + if (existsSync(srcPath)) { |
| 107 | + copyFileSync(srcPath, join(distDir, nodeFile)); |
| 108 | + console.log(`Copied ${nodeFile}`); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +console.log('Build complete!'); |
0 commit comments