build-public-api.js 2.2 KB

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