mkdirp-hugo-mod.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Helper script to create empty Hugo-module directories for Docsy dependencies
  2. // listed in `go.mod`. This is necessary for projects not using Hugo modules. For
  3. // details, see
  4. // https://www.docsy.dev/docs/get-started/other-options/#docsy-npm-install-side-effect
  5. const fs = require('fs');
  6. const path = require('path');
  7. if (process.env.DOCSY_MKDIR_HUGO_MOD_SKIP) {
  8. console.log("DOCSY_MKDIR_HUGO_MOD_SKIP is set. Skipping directory creation.");
  9. process.exit(0);
  10. }
  11. const modulePathPrefix = process.argv[2] || '..';
  12. console.log(
  13. `Creating empty directories under MODULE_PATH_PREFIX: ${modulePathPrefix}
  14. which resolves to: ${path.resolve(modulePathPrefix)}\n`
  15. );
  16. // Extract module paths from `go.mod`, assuming the dependencies appear in the form:
  17. //
  18. // require (
  19. // github.com/...
  20. // ...
  21. // )
  22. function extractModulePaths() {
  23. const goModPath = path.join(__dirname, '..', 'go.mod');
  24. let directories = [];
  25. try {
  26. const goModContent = fs.readFileSync(goModPath, 'utf8');
  27. const lines = goModContent.split('\n');
  28. lines.forEach((line) => {
  29. line = line.trim();
  30. if (!line.startsWith('github.com')) return;
  31. const modulePath = line.split(' ')[0];
  32. directories.push(modulePath);
  33. });
  34. } catch (error) {
  35. console.error(`Error reading go.mod file: ${error.message}`);
  36. process.exit(1);
  37. }
  38. return directories;
  39. }
  40. function createDirectory(targetPath) {
  41. if (!fs.existsSync(targetPath)) {
  42. console.log(`+ Creating directory ${targetPath}`);
  43. fs.mkdirSync(targetPath, { recursive: true });
  44. } else {
  45. console.log(`> Directory already exists: ${targetPath}`);
  46. }
  47. }
  48. const directories = extractModulePaths();
  49. directories.forEach((dir) => {
  50. const targetPath = path.join(modulePathPrefix, dir);
  51. createDirectory(targetPath);
  52. });