stamping-mes/scan_deps.js

32 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs');
const path = require('path');
const reqs = new Set();
const builtinModules = require('module').builtinModules;
function scan(dir) {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
if (file !== 'node_modules' && file !== '.git') scan(fullPath);
} else if (file.endsWith('.js')) {
const content = fs.readFileSync(fullPath, 'utf8');
const matches = content.match(/require\(['"]([^'"]+)['"]\)/g);
if (matches) {
matches.forEach(m => {
const pkg = m.match(/require\(['"]([^'"]+)['"]\)/)[1];
if (!pkg.startsWith('.') && !pkg.startsWith('/') && !builtinModules.includes(pkg)) {
reqs.add(pkg.split('/')[0]);
}
});
}
}
});
}
if (fs.existsSync('./server')) {
scan('./server');
console.log('=== 代码中实际引用的第三方包 ===');
console.log([...reqs].join('\n'));
} else {
console.log('未找到 server 目录,请确保在项目根目录(D:\\StampingMES)下执行此脚本。');
}