index.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Runs `hugo mod get <module>@<vers>` for Docsy module dependencies.
  2. // It gets dependency versions from `package.json`.
  3. import fs from 'fs';
  4. import { execSync } from 'child_process';
  5. const packageJson = readPackageJson();
  6. let exitStatus = 0;
  7. const exit = () => process.exit(exitStatus);
  8. function getHugoModule(npmPkgNm, hugoModuleRefAtV) {
  9. try {
  10. // Extract module version
  11. const pkgVers = packageJson.dependencies[npmPkgNm];
  12. if (!pkgVers) {
  13. throw new Error(`${npmPkgNm} not found in dependencies`);
  14. }
  15. if (!/^\d/.test(pkgVers)) {
  16. const msg = `${npmPkgNm} version must be exact (start with a number), not: ${pkgVers}`;
  17. throw new Error(msg);
  18. }
  19. const command = `npx hugo mod get ${hugoModuleRefAtV}${pkgVers}`;
  20. console.log(`> ${command}`);
  21. const output = execSync(command);
  22. console.log(output.toString());
  23. } catch (error) {
  24. console.error(`ERROR: ${error.message}\n`);
  25. exitStatus = 1;
  26. }
  27. }
  28. function readPackageJson() {
  29. try {
  30. const packageJsonData = fs.readFileSync('package.json', 'utf8');
  31. return JSON.parse(packageJsonData);
  32. } catch (error) {
  33. console.error('FAILED to read package.json:', error.message);
  34. exit();
  35. }
  36. }
  37. const packagesToUpdate = [
  38. // NPM package name, `Hugo module name@` optionally follow by `v` if needed
  39. ['@fortawesome/fontawesome-free', 'github.com/FortAwesome/Font-Awesome@'],
  40. ['bootstrap', 'github.com/twbs/bootstrap@v']
  41. ];
  42. packagesToUpdate.forEach(([npmPkgNm, hugoModuleRefAtV]) => {
  43. getHugoModule(npmPkgNm, hugoModuleRefAtV, packageJson);
  44. });
  45. exit();
  46. // cSpell:ignore hugo twbs