Skip to content

Commit

Permalink
chore: reject dynamic imports for esbuild & warning for fs.read* usag…
Browse files Browse the repository at this point in the history
…es in husky commit check
  • Loading branch information
mingxuanzhangsfdx committed Oct 1, 2024
1 parent 56e39c6 commit ada0ec0
Show file tree
Hide file tree
Showing 9 changed files with 666 additions and 23 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/bundle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ jobs:
registry-url: 'https://registry.npmjs.org'
cache: yarn
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Install esbuild Dependencies
run: |
yarn add -D esbuild@^0.19.5 esbuild-plugin-pino@^2.1.0 npm-dts@^1.3.12 esbuild-plugin-tsc@^0.4.0
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Update for Bundling
run: |
node scripts/updateForBundling.js
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/esbuild-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ jobs:
registry-url: 'https://registry.npmjs.org'
cache: yarn
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Install esbuild Dependencies
run: |
yarn add -D esbuild@^0.19.5 esbuild-plugin-pino@^2.1.0 npm-dts@^1.3.12 esbuild-plugin-tsc@^0.4.0
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Update for Bundling
run: |
node scripts/updateForBundling.js
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/testExternalProjects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ jobs:
- name: Add dependencies to bundle node_modules
run: |
yarn install --network-timeout 600000
yarn add -D esbuild@^0.19.5 esbuild-plugin-pino@^2.1.0 npm-dts@^1.3.12 esbuild-plugin-tsc@^0.4.0
working-directory: node_modules/@salesforce/core
- name: Update for bundling
run: node scripts/updateForBundling.js
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

yarn lint && yarn pretty-quick --staged
node ../scripts/scanTs.js
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
"@types/proper-lockfile": "^4.1.4",
"@types/semver": "^7.5.8",
"benchmark": "^2.1.4",
"esbuild": "^0.23.1",
"esbuild-plugin-pino": "^2.2.0",
"esbuild-plugin-tsc": "^0.4.0",
"npm-dts": "^1.3.13",
"ts-morph": "^23.0.0",
"ts-node": "^10.9.2",
"ts-patch": "^3.2.1",
"typescript": "^5.5.4"
Expand Down
6 changes: 6 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const sharedConfig = {
...sharedConfig,
// external: ['src/logger/transformStream.ts'],
platform: 'node', // for CJS
supported: {
'dynamic-import': false,
},
logOverride: {
'unsupported-dynamic-import': 'error',
},
outdir: tmpOutputFolder,
});
const filePath = `${tmpOutputFolder}/index.js`;
Expand Down
49 changes: 49 additions & 0 deletions scripts/scanTs.js
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');
13 changes: 9 additions & 4 deletions scripts/updateForBundling.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ function updatePackageJson() {
// Function to update logger.ts
function updateLoggerTs() {
const loggerPath = './src/logger/logger.ts';
const targetString = "target: path.join('..', '..', 'lib', 'logger', 'transformStream')";
const replacementString = "target: './transformStream'";

fs.readFile(loggerPath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading logger.ts: ${err}`);
return;
}

let updatedData = data.replace(
"target: path.join('..', '..', 'lib', 'logger', 'transformStream')",
"target: './transformStream'"
);
// Check if the target string exists in the file
if (!data.includes(targetString)) {
console.error(`Error: The target string "${targetString}" was not found in logger.ts.`);
return;
}

let updatedData = data.replace(targetString, replacementString);

fs.writeFile(loggerPath, updatedData, 'utf8', (writeErr) => {
if (writeErr) {
Expand Down
Loading

0 comments on commit ada0ec0

Please sign in to comment.