github.com/evanw/esbuild@v0.21.4/scripts/parse-ts-files.js (about) 1 // This script parses all .ts and .tsx files in the current directory using 2 // esbuild. This is useful to check for parser bugs and/or crashes in esbuild. 3 4 const fs = require('fs'); 5 const os = require('os'); 6 const path = require('path'); 7 const ts = require('typescript'); 8 const child_process = require('child_process'); 9 const esbuildPath = path.join(path.dirname(__dirname), 'esbuild'); 10 11 function walkDir(root, cb) { 12 for (const entry of fs.readdirSync(root)) { 13 const absolute = path.join(root, entry); 14 if (fs.statSync(absolute).isDirectory()) { 15 walkDir(absolute, cb); 16 } else if ((entry.endsWith('.ts') && !entry.endsWith('.d.ts')) || entry.endsWith('.tsx')) { 17 cb(absolute) 18 } 19 } 20 } 21 22 child_process.execSync('make', { cwd: path.dirname(__dirname) }); 23 24 // Doing one file at a time is useful for debugging crashes 25 if (process.argv.includes('--individual')) { 26 walkDir(process.cwd(), absolute => { 27 let output = child_process.spawnSync(esbuildPath, [absolute, '--outfile=/dev/null'], { stdio: ['inherit', 'pipe', 'pipe'] }); 28 if (output.status) { 29 let result; 30 try { 31 result = ts.transpileModule(fs.readFileSync(absolute, 'utf8'), { reportDiagnostics: true }); 32 } catch (e) { 33 // Ignore this file if the TypeScript compiler crashes on it 34 return 35 } 36 if (result.diagnostics.length > 0) { 37 // Ignore this file if the TypeScript compiler has parse errors 38 return 39 } 40 console.log('-'.repeat(80)); 41 console.log('Failure:', absolute); 42 console.log('-'.repeat(20) + ' esbuild output:'); 43 console.log(output.stdout + output.stderr); 44 } 45 }); 46 } 47 48 // Otherwise it's much faster to do everything at once 49 else { 50 const tempDir = path.join(os.tmpdir(), 'esbuild-parse-ts-files'); 51 try { 52 fs.mkdirSync(tempDir); 53 } catch (e) { 54 } 55 const all = []; 56 walkDir(process.cwd(), absolute => all.push(absolute)); 57 try { 58 child_process.execFileSync(esbuildPath, ['--outdir=' + tempDir].concat(all), { stdio: 'inherit' }); 59 } catch (e) { 60 } 61 }