build.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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');
  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[] {
  14. const files: string[] = [];
  15. function traverseDirectory(dir: string) {
  16. const dirContents = fs.readdirSync(dir);
  17. dirContents.forEach(item => {
  18. const itemPath = path.join(dir, item);
  19. const stats = fs.statSync(itemPath);
  20. if (stats.isDirectory()) {
  21. traverseDirectory(itemPath);
  22. } else {
  23. if (item.endsWith(suffix)) {
  24. files.push(itemPath);
  25. }
  26. }
  27. });
  28. }
  29. traverseDirectory(directory);
  30. return files;
  31. }