build-public-api.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_PATTERN = /(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. if (!EXCLUDED_PATTERN.test(filename)) {
  27. const relativeFilename =
  28. '.' + filename.replace(modulePath, '').replace(/\\/g, '/').replace(/\.ts$/, '');
  29. files.push(relativeFilename);
  30. }
  31. });
  32. const header = `// This file was generated by the build-public-api.ts script\n`;
  33. const fileContents = header + files.map((f) => `export * from '${f}';`).join('\n') + '\n';
  34. const publicApiFile = path.join(modulePath, 'public_api.ts');
  35. fs.writeFileSync(publicApiFile, fileContents, 'utf8');
  36. console.log(`Created ${publicApiFile}`);
  37. }
  38. /**
  39. *
  40. * @param startPath {string}
  41. * @param filter {RegExp}
  42. * @param callback {(filename: string) => void}
  43. */
  44. function forMatchingFiles(startPath, filter, callback) {
  45. if (!fs.existsSync(startPath)) {
  46. console.log('Starting path does not exist ', startPath);
  47. return;
  48. }
  49. const files = fs.readdirSync(startPath);
  50. for (let i = 0; i < files.length; i++) {
  51. const filename = path.join(startPath, files[i]);
  52. const stat = fs.lstatSync(filename);
  53. if (stat.isDirectory()) {
  54. forMatchingFiles(filename, filter, callback); // recurse
  55. } else if (filter.test(filename)) {
  56. callback(filename);
  57. }
  58. }
  59. }