1
0

build.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import fs from 'fs-extra';
  2. import path from 'path';
  3. // This build script copies all .template.ts files from the "src" directory to the "dist" directory.
  4. // This is necessary because the .template.ts files are used to generate the actual source files.
  5. const templateFiles = findFilesWithSuffix(path.join(__dirname, 'src'), ['.template.ts', '.template.tsx']);
  6. for (const file of templateFiles) {
  7. // copy to the equivalent path in the "dist" rather than "src" directory
  8. const relativePath = path.relative(path.join(__dirname, 'src'), file);
  9. const distPath = path.join(__dirname, 'dist', relativePath);
  10. fs.ensureDirSync(path.dirname(distPath));
  11. fs.copyFileSync(file, distPath);
  12. }
  13. function findFilesWithSuffix(directory: string, suffix: string | string[]): string[] {
  14. const files: string[] = [];
  15. const suffixes = Array.isArray(suffix) ? suffix : [suffix];
  16. function traverseDirectory(dir: string) {
  17. const dirContents = fs.readdirSync(dir);
  18. dirContents.forEach(item => {
  19. const itemPath = path.join(dir, item);
  20. const stats = fs.statSync(itemPath);
  21. if (stats.isDirectory()) {
  22. traverseDirectory(itemPath);
  23. } else {
  24. if (suffixes.some(s => item.endsWith(s))) {
  25. files.push(itemPath);
  26. }
  27. }
  28. });
  29. }
  30. traverseDirectory(directory);
  31. return files;
  32. }