-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[static-build] Don't rely on
hugo
binary existing in build-container (
#11455) * Makes `hugo` framework preset work when Node.js v20.x is selected in Project Settings. * Stores the downloaded `hugo` binary in the `.vercel/cache` directory, so that it does not need to be re-downloaded upon every deployment. * Makes `vc build` work when run locally for Hugo projects - tested on macOS arm64.
- Loading branch information
1 parent
fc7a8bc
commit f4c181a
Showing
7 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vercel/static-build': minor | ||
--- | ||
|
||
Don't rely on `hugo` binary existing in build-container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import fetch from 'node-fetch'; | ||
import { NowBuildError } from '@vercel/build-utils'; | ||
|
||
export async function getHugoUrl( | ||
version: string, | ||
platform = process.platform, | ||
arch = process.arch | ||
): Promise<string> { | ||
const oses = ( | ||
{ | ||
linux: ['linux'], | ||
darwin: ['darwin', 'macos'], | ||
win32: ['windows'], | ||
} as Record<string, string[]> | ||
)[platform]; | ||
if (!oses) { | ||
throw new Error(`Unsupported platform: ${platform}`); | ||
} | ||
const arches = ( | ||
{ | ||
arm64: ['arm64'], | ||
x64: ['amd64', '64bit'], | ||
} as Record<string, string[]> | ||
)[arch]; | ||
if (!arches) { | ||
throw new Error(`Unsupported arch: ${arch}`); | ||
} | ||
if (platform === 'darwin') { | ||
arches.push('universal'); | ||
if (arch === 'arm64') { | ||
// On Mac ARM64, assume Rosetta is available to execute 64-bit binaries | ||
arches.push('64bit'); | ||
} | ||
} | ||
|
||
const checksumsUrl = `https://github.com/gohugoio/hugo/releases/download/v${version}/hugo_${version}_checksums.txt`; | ||
const checksumsRes = await fetch(checksumsUrl); | ||
if (checksumsRes.status === 404) { | ||
throw new NowBuildError({ | ||
code: 'STATIC_BUILD_BINARY_NOT_FOUND', | ||
message: `Version ${version} of Hugo does not exist. Please specify a different one.`, | ||
link: 'https://vercel.link/framework-versioning', | ||
}); | ||
} | ||
const checksumsBody = await checksumsRes.text(); | ||
const checksums = new Map<string, string>(); | ||
for (const line of checksumsBody.trim().split('\n')) { | ||
const [sha, name] = line.split(/\s+/); | ||
checksums.set(name, sha); | ||
} | ||
|
||
const file = | ||
findFile(checksums.keys(), oses, arches, true) || | ||
findFile(checksums.keys(), oses, arches, false); | ||
if (!file) { | ||
throw new Error( | ||
`Could not determine filename for Hugo v${version} for ${platform} / ${arch}` | ||
); | ||
} | ||
|
||
return `https://github.com/gohugoio/hugo/releases/download/v${version}/${file}`; | ||
} | ||
|
||
function findFile( | ||
names: Iterable<string>, | ||
oses: string[], | ||
arches: string[], | ||
extended: boolean | ||
): string | null { | ||
const prefix = `hugo_${extended ? 'extended_' : ''}`; | ||
for (const name of names) { | ||
if (!name.startsWith(prefix) || !name.endsWith('.tar.gz')) continue; | ||
const suffix = name.split('_').pop(); | ||
if (!suffix) continue; | ||
const [os, arch] = suffix | ||
.replace(/\.(.*)$/, '') | ||
.toLowerCase() | ||
.split('-'); | ||
if (oses.includes(os) && arches.includes(arch)) { | ||
return name; | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { getHugoUrl } from '../src/utils/hugo'; | ||
|
||
describe('getHugoUrl()', () => { | ||
it('should return URL for v0.42.2 on macOS arm64', async () => { | ||
const url = await getHugoUrl('0.42.2', 'darwin', 'arm64'); | ||
expect(url).toEqual( | ||
'https://github.com/gohugoio/hugo/releases/download/v0.42.2/hugo_0.42.2_macOS-64bit.tar.gz' | ||
); | ||
}); | ||
|
||
it('should return URL for v0.42.2 on Linux x64', async () => { | ||
const url = await getHugoUrl('0.42.2', 'linux', 'x64'); | ||
expect(url).toEqual( | ||
'https://github.com/gohugoio/hugo/releases/download/v0.42.2/hugo_0.42.2_Linux-64bit.tar.gz' | ||
); | ||
}); | ||
|
||
it('should return URL for v0.58.2 on macOS arm64', async () => { | ||
const url = await getHugoUrl('0.58.2', 'darwin', 'arm64'); | ||
expect(url).toEqual( | ||
'https://github.com/gohugoio/hugo/releases/download/v0.58.2/hugo_extended_0.58.2_macOS-64bit.tar.gz' | ||
); | ||
}); | ||
|
||
it('should return URL for v0.58.2 on Linux x64', async () => { | ||
const url = await getHugoUrl('0.58.2', 'linux', 'x64'); | ||
expect(url).toEqual( | ||
'https://github.com/gohugoio/hugo/releases/download/v0.58.2/hugo_extended_0.58.2_Linux-64bit.tar.gz' | ||
); | ||
}); | ||
|
||
it('should return URL for v0.125.0 on macOS arm64', async () => { | ||
const url = await getHugoUrl('0.125.0', 'darwin', 'arm64'); | ||
expect(url).toEqual( | ||
'https://github.com/gohugoio/hugo/releases/download/v0.125.0/hugo_extended_0.125.0_darwin-universal.tar.gz' | ||
); | ||
}); | ||
|
||
it('should return URL for v0.125.0 on Linux x64', async () => { | ||
const url = await getHugoUrl('0.125.0', 'linux', 'x64'); | ||
expect(url).toEqual( | ||
'https://github.com/gohugoio/hugo/releases/download/v0.125.0/hugo_extended_0.125.0_Linux-64bit.tar.gz' | ||
); | ||
}); | ||
}); |