-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: reject dynamic imports for esbuild & warning for fs.read* usag…
…es in husky commit check
- Loading branch information
1 parent
56e39c6
commit ada0ec0
Showing
9 changed files
with
666 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn lint && yarn pretty-quick --staged | ||
node ../scripts/scanTs.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { Project, SyntaxKind, CallExpression } = require('ts-morph'); | ||
|
||
const SRC_DIR = path.join(__dirname, '..', 'src'); | ||
const project = new Project({ | ||
tsConfigFilePath: path.join(__dirname, 'tsconfig.json'), | ||
}); | ||
|
||
let detected = false; | ||
|
||
const scanDirectory = (dir) => { | ||
const files = fs.readdirSync(dir); | ||
files.forEach((file) => { | ||
const fullPath = path.join(dir, file); | ||
const stat = fs.statSync(fullPath); | ||
if (stat.isDirectory()) { | ||
scanDirectory(fullPath); | ||
} else if (fullPath.endsWith('.ts')) { | ||
analyzeFile(fullPath); | ||
} | ||
}); | ||
}; | ||
|
||
// This function will detect all the usages of fs.read* and send warnings with the location of the usage | ||
const analyzeFile = (filePath) => { | ||
const srcFile = project.addSourceFileAtPath(filePath); | ||
const funcCalls = srcFile.getDescendantsOfKind(SyntaxKind.CallExpression); | ||
|
||
funcCalls.forEach((callExpression) => { | ||
const exp = callExpression.getExpression(); | ||
if (exp.getText().startsWith('fs.read')) { | ||
detected = true; | ||
console.warn( | ||
`Warning: Usage of "${exp.getText()}" in file "${filePath}" at line ${callExpression.getStartLineNumber()}.\n` | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
scanDirectory(SRC_DIR); | ||
|
||
if (detected) { | ||
console.log('The warnings above do not mean the usages are wrong.'); | ||
console.log(`Avoid reading local artifacts with "fs.read*" since esbuild cannot bundle the artifacts together.`); | ||
console.log('Consider using import instead or reach out to IDEx Foundations team'); | ||
} | ||
|
||
console.log('Scan complete'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.