docgen-utils.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import fs from 'fs';
  2. import klawSync from 'klaw-sync';
  3. // tslint:disable:no-console
  4. /**
  5. * Generates the Hugo front matter with the title of the document
  6. */
  7. export function generateFrontMatter(title: string, weight: number, showToc: boolean = true): string {
  8. return `---
  9. title: "${title.replace('-', ' ')}"
  10. weight: ${weight}
  11. date: ${new Date().toISOString()}
  12. showtoc: ${showToc}
  13. generated: true
  14. ---
  15. <!-- This file was generated from the Vendure source. Do not modify. Instead, re-run the "docs:build" script -->
  16. `;
  17. }
  18. /**
  19. * Delete all generated docs found in the outputPath.
  20. */
  21. export function deleteGeneratedDocs(outputPath: string) {
  22. let deleteCount = 0;
  23. const files = klawSync(outputPath, {nodir: true});
  24. for (const file of files) {
  25. const content = fs.readFileSync(file.path, 'utf-8');
  26. if (isGenerated(content)) {
  27. fs.unlinkSync(file.path);
  28. deleteCount++;
  29. }
  30. }
  31. console.log(`Deleted ${deleteCount} generated docs`);
  32. }
  33. /**
  34. * Returns true if the content matches that of a generated document.
  35. */
  36. function isGenerated(content: string) {
  37. return /generated\: true\n---\n/.test(content);
  38. }