docgen-utils.ts 1.5 KB

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