mirror of
https://github.com/lingble/twenty.git
synced 2025-11-02 13:47:55 +00:00
chore: add script to generate twenty-ui barrels before build (#4707)
Split from https://github.com/twentyhq/twenty/pull/4518 Part of #4766 Adds a script to auto-generate twenty-ui exports in `index.ts` files. --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
90
packages/twenty-ui/scripts/generateBarrels.ts
Normal file
90
packages/twenty-ui/scripts/generateBarrels.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import * as fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
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');
|
||||
|
||||
const getSubDirectoryPaths = (directoryPath: string) =>
|
||||
fs
|
||||
.readdirSync(directoryPath)
|
||||
.filter(
|
||||
(fileOrDirectoryName) =>
|
||||
!excludedDirectories.includes(fileOrDirectoryName) &&
|
||||
fs
|
||||
.statSync(path.join(directoryPath, fileOrDirectoryName))
|
||||
.isDirectory(),
|
||||
)
|
||||
.map((subDirectoryName) => path.join(directoryPath, subDirectoryName));
|
||||
|
||||
const getDirectoryPathsRecursive = (directoryPath: string): string[] => [
|
||||
directoryPath,
|
||||
...getSubDirectoryPaths(directoryPath).flatMap(getDirectoryPathsRecursive),
|
||||
];
|
||||
|
||||
const getFilesPaths = (directoryPath: string) =>
|
||||
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 './${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 './${path.relative(srcPath, moduleDirectoryPath)}';`,
|
||||
)
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
.join('\n');
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(srcPath, 'index.ts'),
|
||||
`${mainBarrelExports}\n`,
|
||||
'utf-8',
|
||||
);
|
||||
Reference in New Issue
Block a user