diff --git a/helpers/stork/executable.js b/helpers/stork/executable.js new file mode 100644 index 0000000..d236ce5 --- /dev/null +++ b/helpers/stork/executable.js @@ -0,0 +1,94 @@ +import fs from 'fs-extra' +import execa from 'execa' + +import { isDarwin } from '~/helpers/environment.js' +import { + storkVersion, + storkExecutableName, + storkExecutablePath, + storkTomlPath, + storkIndexPath +} from '~/helpers/stork/config.js' + +// https://stork-search.net/docs/install +const execDownloadUrls = { + darwin: `https://files.stork-search.net/releases/v${ storkVersion }/stork-macos-10-15`, + default: `https://files.stork-search.net/releases/v${ storkVersion }/stork-ubuntu-20-04` + // default: `https://files.stork-search.net/releases/v${ storkVersion }/stork-amazon-linux` +} + +// Check if a file is executable +// https://stackoverflow.com/a/69897809/1397641 +async function isExecutable ( path ) { + const stats = await fs.stat( path ) + const isExecutable = !!(stats.mode & fs.constants.S_IXUSR) + + return isExecutable +} + +// 👩‍💻 Bash Download example - https://github.com/jmooring/hugo-stork/blob/main/build.sh +export async function downloadStorkExecutable () { + const envKey = isDarwin() ? 'darwin' : 'default' + + const execDownloadUrl = execDownloadUrls[ envKey ] + + // console.log( 'execDownloadUrl', execDownloadUrl ) + + // Delete any existing executable + // so we don't get write errors + // or false positives from preexisting executable files + await fs.remove( storkExecutablePath ) + + // Download the binary + await execa( `curl`, [ + execDownloadUrl, + + // Set filename + '-o', + storkExecutableName + ]) + + + // Set the downloaded binary as executable + await fs.chmod( storkExecutablePath, '755' ) + // Check that our downloaded binary is executable + + + // console.log( 'isExecutable', isExecutable ) + if ( !isExecutable( storkExecutablePath ) ) throw new Error( `Downloaded binary at ${ storkExecutablePath } is not executable.` ) + + + // Check Stork version + // so we know our binary is working + const { stdout } = await execa( storkExecutablePath, [ + '--version' + ]) + + console.log( 'Stork Version', stdout ) + if ( !stdout.includes( storkVersion ) ) throw new Error( 'Stork --version command failed.' ) + + return stdout +} + + +export async function buildIndex () { + + if ( !isExecutable( storkExecutablePath ) ) throw new Error( `Binary at ${ storkExecutablePath } is not executable.` ) + + // Check Stork version + // so we know our binary is working + const { stdout } = await execa( storkExecutablePath, [ + 'build', + + '--input', + storkTomlPath, + + '--output', + storkIndexPath + ]) + + console.log( 'Stork Build', stdout ) + if ( !stdout.includes( storkVersion ) ) throw new Error( 'Stork --version command failed.' ) + + return stdout +} diff --git a/helpers/stork/toml.js b/helpers/stork/toml.js index 2220f01..2650f8d 100644 --- a/helpers/stork/toml.js +++ b/helpers/stork/toml.js @@ -4,70 +4,17 @@ import has from 'just-has' import TOML from '@iarna/toml' -import { isDarwin } from '~/helpers/environment.js' import { isNonEmptyString, isNonEmptyArray } from '~/helpers/check-types.js' import { - storkVersion, - storkExecutableName, - storkExecutablePath, storkTomlPath, } from '~/helpers/stork/config.js' +import { downloadStorkExecutable } from '~/helpers/stork/executable.js' -// https://stork-search.net/docs/install -const execDownloadUrls = { - darwin: `https://files.stork-search.net/releases/v${ storkVersion }/stork-macos-10-15`, - default: `https://files.stork-search.net/releases/v${ storkVersion }/stork-ubuntu-20-04` - // default: `https://files.stork-search.net/releases/v${ storkVersion }/stork-amazon-linux` -} -export async function downloadStorkExecutable () { - const envKey = isDarwin() ? 'darwin' : 'default' - - const execDownloadUrl = execDownloadUrls[ envKey ] - - // console.log( 'execDownloadUrl', execDownloadUrl ) - - // Delete any existing executable - // so we don't get write errors - // or false positives from preexisting executable files - await fs.remove( storkExecutablePath ) - - // Download the binary - await execa( `curl`, [ - execDownloadUrl, - - // Set filename - '-o', - storkExecutableName - ]) - - - // Set the downloaded binary as executable - await fs.chmod( storkExecutablePath, '755' ) - // Check that our downloaded binary is executable - // https://stackoverflow.com/a/69897809/1397641 - const stats = await fs.stat( storkExecutablePath ) - const isExecutable = !!(stats.mode & fs.constants.S_IXUSR) - - // console.log( 'isExecutable', isExecutable ) - if ( !isExecutable ) throw new Error( 'Downloaded binary is not executable.' ) - - - // Check Stork version - // so we know our binary is working - const { stdout } = await execa( storkExecutablePath, [ - '--version' - ]) - - console.log( 'Stork Version', stdout ) - if ( !stdout.includes( storkVersion ) ) throw new Error( 'Stork --version command failed.' ) - - return stdout -} function makeIndexContentsFromListing ( listing ) {