-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-exports.mjs
More file actions
115 lines (94 loc) · 3.13 KB
/
Copy pathgenerate-exports.mjs
File metadata and controls
115 lines (94 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
/**
* Auto-generate package.json exports for sub-modules
*
* This script automatically scans the src directory for index.ts files
* and generates the corresponding exports in package.json
*
* Usage: node scripts/generate-exports.mjs
*/
import { readdirSync, statSync, readFileSync, writeFileSync } from 'fs';
import { join, resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const projectRoot = resolve(__dirname, '..');
const packageJsonPath = join(projectRoot, 'package.json');
// 扫描 src 目录下的所有 index.ts 文件
function getSubModules(srcDir) {
const modules = [];
const basePath = resolve(srcDir);
function scanDir(dir, relativePath = '') {
const items = readdirSync(dir);
for (const item of items) {
const fullPath = join(dir, item);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
// 检查是否有 index.ts 文件
const indexPath = join(fullPath, 'index.ts');
try {
if (statSync(indexPath).isFile()) {
const modulePath = relativePath ? `${relativePath}/${item}` : item;
modules.push(modulePath);
}
} catch {
// index.ts 不存在,继续递归扫描子目录
}
// 递归扫描子目录
const newRelativePath = relativePath ? `${relativePath}/${item}` : item;
scanDir(fullPath, newRelativePath);
}
}
}
// 扫描 src 目录
scanDir(basePath);
// 过滤出用户可能想要导入的模块
// 排除一些内部目录,如 api, builtin, adapter, core, protocol, utils
const excludedDirs = ['api', 'builtin', 'adapter', 'core', 'protocol', 'utils'];
const mainModules = modules.filter(module => {
const parts = module.split('/');
const lastPart = parts[parts.length - 1];
return !excludedDirs.includes(lastPart);
});
return mainModules;
}
// 生成 exports 配置
function generateExports(modules) {
const exports = {
'.': {
types: './dist/index.d.ts',
import: './dist/index.js',
require: './dist/index.cjs'
}
};
for (const module of modules) {
exports[`./${module}`] = {
types: `./dist/${module}/index.d.ts`,
import: `./dist/${module}/index.js`,
require: `./dist/${module}/index.cjs`
};
}
return exports;
}
// 更新 package.json
function updatePackageJson() {
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const subModules = getSubModules('src');
const newExports = generateExports(subModules);
console.log('Found sub-modules:');
subModules.forEach(mod => console.log(` - ${mod}`));
packageJson.exports = newExports;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
console.log(`\nUpdated package.json exports with ${subModules.length} sub-modules`);
}
// 主函数
function main() {
try {
updatePackageJson();
console.log('✅ Exports generation completed successfully');
} catch (error) {
console.error('❌ Error generating exports:', error);
process.exit(1);
}
}
main();