Skip to content

Commit

Permalink
Allow setting hugo version via env (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Apr 7, 2024
1 parent d5de121 commit e248a7f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ See the [Hugo Documentation](https://gohugo.io/) for more information.

## Installation options

hugo-bin supports options to change the variation of Hugo binaries and to overwrite the download repository.
hugo-bin supports options to change the variation of Hugo binaries, to overwrite the download repository and the Hugo version.

Each option can be configured in one of the following ways:

Expand All @@ -81,7 +81,8 @@ Each option can be configured in one of the following ways:
"version": "0.0.1",
"hugo-bin": {
"buildTags": "extended",
"downloadRepo": "https://some.example.com/artifactory/github-releases"
"downloadRepo": "https://some.example.com/artifactory/github-releases",
"version": "0.124.1"
}
}
```
Expand All @@ -91,13 +92,25 @@ Each option can be configured in one of the following ways:
```ini
hugo_bin_build_tags = "extended"
hugo_bin_download_repo = "https://some.example.com/artifactory/github-releases"
hugo_bin_hugo_version = "0.124.1"
```

### As environment variables

On Linux/macOS:

```sh
export HUGO_BIN_BUILD_TAGS="extended"
export HUGO_BIN_DOWNLOAD_REPO="https://some.example.com/artifactory/github-releases"
export HUGO_BIN_HUGO_VERSION="0.124.1"
```

On Windows:

```bat
set HUGO_BIN_BUILD_TAGS=extended
set HUGO_BIN_DOWNLOAD_REPO=https://some.example.com/artifactory/github-releases
set HUGO_BIN_HUGO_VERSION=0.124.1
```

**Note that you have to run `npm install hugo-bin` to re-install hugo-bin itself, if you change any of these options.**
Expand All @@ -118,6 +131,12 @@ Default: `"https://github.com"`

Set it to your proxy URL to download the hugo binary from a different download repository.

#### hugoVersion

Default: the version specified in [package.json](package.json)

You can override the Hugo version here, but please note that if any of the URLs have changed upstream, you might not be able to use any version and you will probably need to update to a newer hugo-bin version which takes into consideration the new URLs.

## Supported Hugo versions

See [the package.json commit history](https://github.com/fenneclab/hugo-bin/commits/main/package.json).
Expand Down
31 changes: 25 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import BinWrapper from '@xhmikosr/bin-wrapper';
import { packageConfig } from 'pkg-conf';

const pkg = new URL('../package.json', import.meta.url);
const { hugoVersion } = JSON.parse(await fs.readFile(pkg, 'utf8'));
const { hugoVersion: HUGO_VERSION } = JSON.parse(await fs.readFile(pkg, 'utf8'));

const destDir = path.join(fileURLToPath(new URL('../vendor/', import.meta.url)));
const binName = process.platform === 'win32' ? 'hugo.exe' : 'hugo';

function extendedBin(baseUrl) {
/**
* @param {string} baseUrl
* @param {string} hugoVersion
*/
function extendedBin(baseUrl, hugoVersion) {
return new BinWrapper()
.src(`${baseUrl}hugo_extended_${hugoVersion}_darwin-universal.tar.gz`, 'darwin', 'arm64')
.src(`${baseUrl}hugo_extended_${hugoVersion}_darwin-universal.tar.gz`, 'darwin', 'x64')
Expand All @@ -30,7 +34,11 @@ function extendedBin(baseUrl) {
.use(binName);
}

function normalBin(baseUrl) {
/**
* @param {string} baseUrl
* @param {string} [hugoVersion]
*/
function normalBin(baseUrl, hugoVersion) {
return new BinWrapper()
.src(`${baseUrl}hugo_${hugoVersion}_darwin-universal.tar.gz`, 'darwin', 'arm64')
.src(`${baseUrl}hugo_${hugoVersion}_darwin-universal.tar.gz`, 'darwin', 'x64')
Expand All @@ -48,21 +56,32 @@ function normalBin(baseUrl) {
.use(binName);
}

/**
* @param {string} cwd
*/
async function main(cwd) {
const config = await packageConfig('hugo-bin', { cwd });
const hugoVersion = [
process.env.HUGO_BIN_HUGO_VERSION,
process.env.npm_config_hugo_bin_hugo_version,
config.version
].find(Boolean) ?? HUGO_VERSION;
const downloadRepo = [
process.env.HUGO_BIN_DOWNLOAD_REPO,
process.env.npm_config_hugo_bin_download_repo,
config.downloadRepo
].find(Boolean) || 'https://github.com';
].find(Boolean) ?? 'https://github.com';
const isExtended = [
process.env.HUGO_BIN_BUILD_TAGS,
process.env.npm_config_hugo_bin_build_tags,
config.buildTags
].includes('extended');
const baseUrl = `${downloadRepo}/gohugoio/hugo/releases/download/v${hugoVersion}/`;

return isExtended ? extendedBin(baseUrl) : normalBin(baseUrl);
// 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 = `${downloadRepo}/gohugoio/hugo/releases/download/v${version}/`;

return isExtended ? extendedBin(baseUrl, version) : normalBin(baseUrl, version);
}

export default main;
42 changes: 37 additions & 5 deletions test/custom-repository.test.js → test/options.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import fs from 'node:fs/promises';
import process from 'node:process';
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import hugoBin from '../lib/index.js';

const pkg = new URL('../package.json', import.meta.url);
const { hugoVersion: HUGO_VERSION } = JSON.parse(await fs.readFile(pkg, 'utf8'));

const environmentVariables = [
'HUGO_BIN_BUILD_TAGS',
'npm_config_hugo_bin_build_tags',
'HUGO_BIN_DOWNLOAD_REPO',
'npm_config_hugo_bin_download_repo'
'npm_config_hugo_bin_download_repo',
'HUGO_BIN_HUGO_VERSION',
'npm_config_hugo_bin_hugo_version'
];

/**
* Verify Custom/Enterprise Repository overwrite.
*/
const testSuite = suite('overwrites download repository');
const testSuite = suite('options');

testSuite.before.each(() => {
for (const variable of environmentVariables) {
Expand Down Expand Up @@ -91,4 +94,33 @@ testSuite('should return custom repository url - Repository: custom - Extended:
}
});

testSuite('should return default version', async() => {
const lib = await hugoBin(process.cwd());
const repoSources = lib._src.map(v => v.url);

for (const sourceUrl of repoSources) {
assert.is(sourceUrl.startsWith(`https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/`), true);
}
});

testSuite('should return custom version', async() => {
process.env.npm_config_hugo_bin_hugo_version = '122.0';
const lib = await hugoBin(process.cwd());
const repoSources = lib._src.map(v => v.url);

for (const sourceUrl of repoSources) {
assert.is(sourceUrl.startsWith('https://github.com/gohugoio/hugo/releases/download/v122.0/'), true);
}
});

testSuite('should strip `v` from custom version', async() => {
process.env.npm_config_hugo_bin_hugo_version = 'v122.0';
const lib = await hugoBin(process.cwd());
const repoSources = lib._src.map(v => v.url);

for (const sourceUrl of repoSources) {
assert.is(sourceUrl.startsWith('https://github.com/gohugoio/hugo/releases/download/v122.0/'), true);
}
});

testSuite.run();

0 comments on commit e248a7f

Please sign in to comment.