-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathgenerateBarrels.js
105 lines (94 loc) · 2.57 KB
/
generateBarrels.js
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
import * as fs from 'fs';
import path from 'path';
import slash from 'slash';
const extensions = ['.ts', '.tsx'];
const excludedExtensions = [
'.test.ts',
'.test.tsx',
'.spec.ts',
'.spec.tsx',
'.stories.ts',
'.stories.tsx',
];
const excludedDirectories = [
'__tests__',
'__mocks__',
'__stories__',
'internal',
];
const srcPath = path.resolve('packages/twenty-ui/src');
/**
* @param {string} directoryPath
* @returns {string[]}
*/
const getSubDirectoryPaths = (directoryPath) =>
fs
.readdirSync(directoryPath)
.filter(
(fileOrDirectoryName) =>
!excludedDirectories.includes(fileOrDirectoryName) &&
fs
.statSync(path.join(directoryPath, fileOrDirectoryName))
.isDirectory(),
)
.map((subDirectoryName) => path.join(directoryPath, subDirectoryName));
/**
*
* @param {string} directoryPath
* @returns {string[]}
*/
const getDirectoryPathsRecursive = (directoryPath) => [
directoryPath,
...getSubDirectoryPaths(directoryPath).flatMap(getDirectoryPathsRecursive),
];
/**
*
* @param {string} directoryPath
* @returns {string[]}
*/
const getFilesPaths = (directoryPath) =>
fs
.readdirSync(directoryPath)
.filter(
(filePath) =>
fs.statSync(path.join(directoryPath, filePath)).isFile() &&
!filePath.startsWith('index.') &&
extensions.some((extension) => filePath.endsWith(extension)) &&
excludedExtensions.every(
(excludedExtension) => !filePath.endsWith(excludedExtension),
),
);
const moduleDirectories = getSubDirectoryPaths(srcPath);
moduleDirectories.forEach((moduleDirectoryPath) => {
const directoryPaths = getDirectoryPathsRecursive(moduleDirectoryPath);
const moduleExports = directoryPaths
.flatMap((directoryPath) => {
const directFilesPaths = getFilesPaths(directoryPath);
return directFilesPaths.map((filePath) => {
const fileName = filePath.split('.').slice(0, -1).join('.');
return `export * from './${slash(path.relative(
moduleDirectoryPath,
path.join(directoryPath, fileName),
))}';`;
});
})
.sort((a, b) => a.localeCompare(b))
.join('\n');
fs.writeFileSync(
path.join(moduleDirectoryPath, 'index.ts'),
`${moduleExports}\n`,
'utf-8',
);
});
const mainBarrelExports = moduleDirectories
.map(
(moduleDirectoryPath) =>
`export * from './${slash(path.relative(srcPath, moduleDirectoryPath))}';`,
)
.sort((a, b) => a.localeCompare(b))
.join('\n');
fs.writeFileSync(
path.join(srcPath, 'index.ts'),
`${mainBarrelExports}\n`,
'utf-8',
);