From e37364087ae8f1fd4e7a0a13e4dd88e4d33ba60b Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Mon, 9 May 2022 16:13:34 -0500 Subject: [PATCH] Break out stork build into helpers --- build-stork.js | 155 +++----------------------------------- helpers/stork/config.js | 10 +++ helpers/stork/toml.js | 163 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 145 deletions(-) create mode 100644 helpers/stork/config.js create mode 100644 helpers/stork/toml.js diff --git a/build-stork.js b/build-stork.js index 5767940..cbfa70b 100644 --- a/build-stork.js +++ b/build-stork.js @@ -1,163 +1,28 @@ -import execa from 'execa' import fs from 'fs-extra' -import has from 'just-has' -import TOML from '@iarna/toml' -import { config } from './package.json' -import { isDarwin } from './helpers/environment.js' import { - isNonEmptyString, - isNonEmptyArray -} from './helpers/check-types.js' + downloadStorkToml +} from './helpers/api/static.js' - -const storkOptions = config.stork -const storkVersion = '1.4.2' - -const storkExecutableName = storkOptions.executable -const storkExecutablePath = `./${ storkExecutableName }` -const storkTomlPath = storkOptions.toml -const storkIndexPath = storkOptions.binary - -// 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-amazon-linux` -} - -function makeIndexContentsFromListing ( listing ) { - - const propertiesToCheck = { - text: isNonEmptyString, - content: isNonEmptyString, - description: isNonEmptyString, - // status: isNonEmptyString, - aliases: isNonEmptyArray, - tags: isNonEmptyArray, - } - - const contents = [] - - for ( const [ property, isValid ] of Object.entries( propertiesToCheck ) ) { - if ( !has( listing, property ) ) continue - - if ( !isValid( listing[ property ] ) ) continue - - let value = listing[ property ] - - // Convert arrays to string - if ( Array.isArray( value ) ) { - value = value.join(', ') - } - - // Property can be added to content - - contents.push( value ) - } - - let contentString = contents.join('\n') - - if ( contentString.trim().length === 0 ) return 'No content' - - return contentString -} +import { + downloadStorkExecutable, + writeStorkToml +} from './helpers/stork/toml.js' ;(async () => { - 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.' ) + await downloadStorkToml() + await downloadStorkExecutable() console.log( 'Building Stork index TOML...' ) // Get Sitemap Endpoints JSON const sitemap = await fs.readJson( './static/sitemap-endpoints.json' ) - // console.log( 'sitemap', sitemap[0].payload ) - // process.exit() - - // Build Stork Index TOML - // https://stork-search.net/docs/config-ref - const indexString = TOML.stringify({ - input: { - // https://stork-search.net/docs/config-ref#base_directory - base_directory: '.', - url_prefix: 'https://doesitarm.com', - - // https://stork-search.net/docs/config-ref#files - files: sitemap.map( sitemapEntry => { - const { - payload, - route - } = sitemapEntry - - // console.log( 'payload', route, payload ) - - const listing = payload.app || payload.listing || {} - - const contents = makeIndexContentsFromListing( listing ) - - const title = listing.name || route - - // console.log( 'listing', listing ) - // console.log( 'contents', contents ) - // console.log( 'name', listing.name ) - - if ( contents.trim().length === 0 ) { - console.log( 'listing', listing ) - throw new Error('Empty Content') - } - - return { - // https://stork-search.net/docs/config-ref#title - title, - url: route, - contents - } - }) - } - }) - - // Save to file - await fs.outputFile( storkTomlPath, indexString ) + await writeStorkToml( sitemap ) + // From here we hand off to the Stork executable process.exit() })() diff --git a/helpers/stork/config.js b/helpers/stork/config.js new file mode 100644 index 0000000..8d805c3 --- /dev/null +++ b/helpers/stork/config.js @@ -0,0 +1,10 @@ +import { config } from '~/package.json' + + +export const storkOptions = config.stork +export const storkVersion = '1.4.2' + +export const storkExecutableName = storkOptions.executable +export const storkExecutablePath = `./${ storkExecutableName }` +export const storkTomlPath = storkOptions.toml +export const storkIndexPath = storkOptions.binary diff --git a/helpers/stork/toml.js b/helpers/stork/toml.js new file mode 100644 index 0000000..e800e9e --- /dev/null +++ b/helpers/stork/toml.js @@ -0,0 +1,163 @@ +import execa from 'execa' +import fs from 'fs-extra' +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' + + +// 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-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 ) { + + const propertiesToCheck = { + text: isNonEmptyString, + content: isNonEmptyString, + description: isNonEmptyString, + // status: isNonEmptyString, + aliases: isNonEmptyArray, + tags: isNonEmptyArray, + } + + const contents = [] + + for ( const [ property, isValid ] of Object.entries( propertiesToCheck ) ) { + if ( !has( listing, property ) ) continue + + if ( !isValid( listing[ property ] ) ) continue + + let value = listing[ property ] + + // Convert arrays to string + if ( Array.isArray( value ) ) { + value = value.join(', ') + } + + // Property can be added to content + + contents.push( value ) + } + + let contentString = contents.join('\n') + + if ( contentString.trim().length === 0 ) return 'No content' + + return contentString +} + + +function mapSitemapEndpointsToToml ( sitemap ) { + return { + input: { + // https://stork-search.net/docs/config-ref#base_directory + base_directory: '.', + url_prefix: 'https://doesitarm.com', + + // https://stork-search.net/docs/config-ref#files + files: sitemap.map( sitemapEntry => { + const { + payload, + route + } = sitemapEntry + + // console.log( 'payload', route, payload ) + + const listing = payload.app || payload.listing || {} + + const contents = makeIndexContentsFromListing( listing ) + + const title = listing.name || route + + // console.log( 'listing', listing ) + // console.log( 'contents', contents ) + // console.log( 'name', listing.name ) + + if ( contents.trim().length === 0 ) { + console.log( 'listing', listing ) + throw new Error('Empty Content') + } + + return { + // https://stork-search.net/docs/config-ref#title + title, + url: route, + contents + } + }) + } + } +} + + +export async function writeStorkToml ( sitemap ) { + const indexToml = mapSitemapEndpointsToToml( sitemap ) + + // Build Stork Index TOML + // https://stork-search.net/docs/config-ref + const indexString = TOML.stringify( indexToml ) + + // Save to file + await fs.outputFile( storkTomlPath, indexString ) + + return +}