Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] Handle creation of empty Hugo-modules for deps #2128

Merged
merged 11 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"_cp:bs-rfs": "npx cpy 'node_modules/bootstrap/scss/vendor/*' assets/_vendor/bootstrap/scss/",
"_diff:check": "git diff --name-only --exit-code",
"_gen-chroma-styles": "bash -c tools/gen-chroma-styles.sh && bash -c 'tools/gen-chroma-styles.sh -s onedark -o _dark.scss'",
"_mkdir:hugo-mod": "npx mkdirp ../github.com/FortAwesome/Font-Awesome ../github.com/twbs/bootstrap",
"_mkdir:hugo-mod": "node tools/mkdirp-hugo-mod.js ..",
"_prepare": "npm run _cp:bs-rfs && npm run _gen-chroma-styles && npm run get:hugo-modules",
"build:preview": "npm run cd:docs build:preview",
"build:production": "npm run cd:docs build:production",
Expand Down Expand Up @@ -41,7 +41,6 @@
"devDependencies": {
"cpy-cli": "^5.0.0",
"hugo-extended": "0.138.0",
"mkdirp": "^3.0.1",
"prettier": "^3.3.3",
"netlify-cli": "^17.37.2",
"npm-check-updates": "^17.1.11"
Expand Down
59 changes: 59 additions & 0 deletions tools/mkdirp-hugo-mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Helper script to create empty Hugo-module directories for Docsy dependencies
// listed in `go.mod`. This is necessary for projects not using Hugo modules. For
// details, see
// https://www.docsy.dev/docs/get-started/other-options/#docsy-npm-install-side-effect

const fs = require('fs');
const path = require('path');

if (process.env.DOCSY_MKDIR_HUGO_MOD_SKIP) {
console.log("DOCSY_MKDIR_HUGO_MOD_SKIP is set. Skipping directory creation.");
process.exit(0);
}

const modulePathPrefix = process.argv[2] || '..';
console.log(
`Creating empty directories under MODULE_PATH_PREFIX: ${modulePathPrefix}
which resolves to: ${path.resolve(modulePathPrefix)}\n`
);

// Extract module paths from `go.mod`, assuming the dependencies appear in the form:
//
// require (
// github.com/...
// ...
// )
function extractModulePaths() {
const goModPath = path.join(__dirname, '..', 'go.mod');

let directories = [];
try {
const goModContent = fs.readFileSync(goModPath, 'utf8');
const lines = goModContent.split('\n');
lines.forEach((line) => {
line = line.trim();
if (!line.startsWith('github.com')) return;
const modulePath = line.split(' ')[0];
directories.push(modulePath);
});
} catch (error) {
console.error(`Error reading go.mod file: ${error.message}`);
process.exit(1);
}
return directories;
}

function createDirectory(targetPath) {
if (!fs.existsSync(targetPath)) {
console.log(`+ Creating directory ${targetPath}`);
fs.mkdirSync(targetPath, { recursive: true });
} else {
console.log(`> Directory already exists: ${targetPath}`);
}
}

const directories = extractModulePaths();
directories.forEach((dir) => {
const targetPath = path.join(modulePathPrefix, dir);
createDirectory(targetPath);
});
6 changes: 5 additions & 1 deletion userguide/content/en/docs/get-started/other-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ $ ls themes
docsy github.com
```

This is a workaround necessary to support Docsy's use as a single [Hugo module] ([#1120]).
This is a workaround necessary to support Docsy's use as a single [Hugo module]
([#1120]) in the context of projects _not_ using Hugo modules. The `github.com`
folder is created via Docsy's `postinstall` script. To disable this behavior,
set the environment variable `DOCSY_MKDIR_HUGO_MOD_SKIP=1` before running NPM
install.

[#1120]: https://github.com/google/docsy/issues/1120
[0.8.0]: https://github.com/google/docsy/blob/main/CHANGELOG.md/#080
Expand Down