From a8b993d9011e17a03df162dda9df18a972431251 Mon Sep 17 00:00:00 2001 From: Patrice Chalin Date: Fri, 10 Nov 2023 17:09:42 -0500 Subject: [PATCH] Update index.mjs --- tools/getHugoModules/index.mjs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tools/getHugoModules/index.mjs b/tools/getHugoModules/index.mjs index 2e47d43c2f..33312e8fb9 100644 --- a/tools/getHugoModules/index.mjs +++ b/tools/getHugoModules/index.mjs @@ -1,5 +1,5 @@ // Runs `hugo mod get @` for Docsy module dependencies. -// Get dependencies versions from `package.json`. +// It gets dependency versions from `package.json`. import fs from 'fs'; import { execSync } from 'child_process'; @@ -7,20 +7,26 @@ import { execSync } from 'child_process'; const packageJson = readPackageJson(); let exitStatus = 0; +const exit = () => process.exit(exitStatus); + function getHugoModule(npmPkgNm, hugoModuleRefAtV) { try { // Extract module version - const packageVersion = packageJson.dependencies[npmPkgNm]; - if (!packageVersion) { + const pkgVers = packageJson.dependencies[npmPkgNm]; + if (!pkgVers) { throw new Error(`${npmPkgNm} not found in dependencies`); } + if (!/^\d/.test(pkgVers)) { + const msg = `${npmPkgNm} version must be exact (start with a number), not: ${pkgVers}`; + throw new Error(msg); + } - const command = `hugo mod get ${hugoModuleRefAtV}${packageVersion}`; + const command = `hugo mod get ${hugoModuleRefAtV}${pkgVers}`; console.log(`> ${command}`); const output = execSync(command); console.log(output.toString()); } catch (error) { - console.error('An error occurred:', error.message); + console.error(`ERROR: ${error.message}\n`); exitStatus = 1; } } @@ -30,13 +36,11 @@ function readPackageJson() { const packageJsonData = fs.readFileSync('package.json', 'utf8'); return JSON.parse(packageJsonData); } catch (error) { - console.error('Failed to read package.json:', error.message); + console.error('FAILED to read package.json:', error.message); exit(); } } -const exit = () => process.exit(exitStatus); - const packagesToUpdate = [ // NPM package name, `Hugo module name@` optionally follow by `v` if needed ['@fortawesome/fontawesome-free', 'github.com/FortAwesome/Font-Awesome@'],