From c581dfe8956822ce13942c6d426805eb955f56d0 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Wed, 17 Apr 2024 09:03:13 +0300 Subject: [PATCH] Refactor --- lib/index.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1804fd4..4319875 100644 --- a/lib/index.js +++ b/lib/index.js @@ -74,10 +74,9 @@ function normalBin(baseUrl, hugoVersion) { } /** - * @param {string} cwd + * @param {import('pkg-conf').Config} config */ -async function main(cwd) { - const config = await packageConfig('hugo-bin', { cwd }); +function getOptions(config) { const hugoVersion = [ process.env.HUGO_BIN_HUGO_VERSION, process.env.npm_config_hugo_bin_hugo_version, @@ -94,18 +93,31 @@ async function main(cwd) { config.buildTags ].includes('extended'); - debug('[main] hugoVersion:', hugoVersion); - debug('[main] downloadRepo:', downloadRepo); - debug('[main] isExtended:', isExtended); - // Strip any leading `v` from hugoVersion because otherwise we'll end up with duplicate `v`s const version = hugoVersion[0] === 'v' ? hugoVersion.slice(1) : hugoVersion; - const baseUrl = new URL(`gohugoio/hugo/releases/download/v${version}/`, downloadRepo).href; - debug('[main] version:', version); + debug('[getOptions] hugoVersion:', version); + debug('[getOptions] downloadRepo:', downloadRepo); + debug('[getOptions] isExtended:', isExtended); + + return { + hugoVersion: version, + downloadRepo, + isExtended + }; +} + +/** + * @param {string} cwd + */ +async function main(cwd) { + const config = await packageConfig('hugo-bin', { cwd }); + const { hugoVersion, downloadRepo, isExtended } = getOptions(config); + const baseUrl = new URL(`gohugoio/hugo/releases/download/v${hugoVersion}/`, downloadRepo).href; + debug('[main] baseUrl:', baseUrl); - return isExtended ? extendedBin(baseUrl, version) : normalBin(baseUrl, version); + return isExtended ? extendedBin(baseUrl, hugoVersion) : normalBin(baseUrl, hugoVersion); } export default main;