|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { Project, CallExpression } = require('ts-morph'); |
| 4 | + |
| 5 | +const SRC_DIR = path.join(__dirname, '..', 'src'); |
| 6 | +const project = new Project({ |
| 7 | + tsConfigFilePath: path.join(__dirname, '..', 'tsconfig.json'), |
| 8 | +}); |
| 9 | + |
| 10 | +let detected = false; |
| 11 | + |
| 12 | +const scanDirectory = (dir) => { |
| 13 | + const files = fs.readdirSync(dir); |
| 14 | + files.forEach((file) => { |
| 15 | + const fullPath = path.join(dir, file); |
| 16 | + const stat = fs.statSync(fullPath); |
| 17 | + if (stat.isDirectory()) { |
| 18 | + scanDirectory(fullPath); |
| 19 | + } else if (fullPath.endsWith('.ts')) { |
| 20 | + analyzeFile(fullPath); |
| 21 | + } |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +// This function will detect all the usages of fs.read* and send warnings with the location of the usage |
| 26 | +const analyzeFile = (filePath) => { |
| 27 | + const srcFile = project.addSourceFileAtPath(filePath); |
| 28 | + const funcCalls = srcFile.getDescendantsOfKind(CallExpression); |
| 29 | + |
| 30 | + funcCalls.forEach((callExpression) => { |
| 31 | + const exp = callExpression.getExpression(); |
| 32 | + if (exp.getText().startsWith('fs.read')) { |
| 33 | + detected = true; |
| 34 | + console.warn( |
| 35 | + `Warning: Usage of "${exp.getText()}" in file "${filePath}" at line ${callExpression.getStartLineNumber()}.\n` |
| 36 | + ); |
| 37 | + } |
| 38 | + }); |
| 39 | +}; |
| 40 | + |
| 41 | +scanDirectory(SRC_DIR); |
| 42 | + |
| 43 | +if (detected) { |
| 44 | + console.log('The warnings above do not mean the usages are wrong.'); |
| 45 | + console.log( |
| 46 | + `Avoid reading local artifacts with "fs.read*" since esbuild cannot bundle the artifacts together.` |
| 47 | + ); |
| 48 | + console.log( |
| 49 | + 'Consider using import instead or reach out to IDEx Foundations team' |
| 50 | + ); |
| 51 | +} else { |
| 52 | + console.log('No fs.read* usages detected.'); |
| 53 | +} |
| 54 | + |
| 55 | +console.log('Scan complete'); |
0 commit comments