build-public-api.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // @ts-check
  2. const fs = require('fs');
  3. const path = require('path');
  4. // This script finds all app sources and then generates a "public-api.ts" file exporting their
  5. // contents. This is then used as the public API entrypoint for the Angular CLI's library
  6. // builder process.
  7. console.log('Generating public apis...');
  8. const SOURCES_DIR = path.join(__dirname, '/../src/lib');
  9. const APP_SOURCE_FILE_PATTERN = /\.ts$/;
  10. const EXCLUDED_PATTERNS = [/(public_api|spec|mock)\.ts$/];
  11. const MODULES = [
  12. 'catalog',
  13. 'core',
  14. 'customer',
  15. 'dashboard',
  16. 'login',
  17. 'marketing',
  18. 'order',
  19. 'settings',
  20. 'system',
  21. ];
  22. for (const moduleDir of MODULES) {
  23. const modulePath = path.join(SOURCES_DIR, moduleDir, 'src');
  24. const files = [];
  25. forMatchingFiles(modulePath, APP_SOURCE_FILE_PATTERN, filename => {
  26. const excluded = EXCLUDED_PATTERNS.reduce((result, re) => result || re.test(filename), false);
  27. if (!excluded) {
  28. const relativeFilename =
  29. '.' + filename.replace(modulePath, '').replace(/\\/g, '/').replace(/\.ts$/, '');
  30. files.push(relativeFilename);
  31. }
  32. });
  33. const header = `// This file was generated by the build-public-api.ts script\n`;
  34. const fileContents = header + files.map(f => `export * from '${f}';`).join('\n') + '\n';
  35. const publicApiFile = path.join(modulePath, 'public_api.ts');
  36. fs.writeFileSync(publicApiFile, fileContents, 'utf8');
  37. console.log(`Created ${publicApiFile}`);
  38. }
  39. /**
  40. *
  41. * @param startPath {string}
  42. * @param filter {RegExp}
  43. * @param callback {(filename: string) => void}
  44. */
  45. function forMatchingFiles(startPath, filter, callback) {
  46. if (!fs.existsSync(startPath)) {
  47. console.log('Starting path does not exist ', startPath);
  48. return;
  49. }
  50. const files = fs.readdirSync(startPath);
  51. for (let i = 0; i < files.length; i++) {
  52. const filename = path.join(startPath, files[i]);
  53. const stat = fs.lstatSync(filename);
  54. if (stat.isDirectory()) {
  55. forMatchingFiles(filename, filter, callback); // recurse
  56. } else if (filter.test(filename)) {
  57. callback(filename);
  58. }
  59. }
  60. }