Skip to content

Commit

Permalink
Validate shopify_function version on build
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhassan committed Jan 3, 2025
1 parent f649fa5 commit e121a59
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions packages/app/src/cli/services/function/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {outputContent, outputDebug, outputToken} from '@shopify/cli-kit/node/out
import {exec} from '@shopify/cli-kit/node/system'
import {dirname, joinPath} from '@shopify/cli-kit/node/path'
import {build as esBuild, BuildResult} from 'esbuild'
import {findPathUp, inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
import {findPathUp, inTemporaryDirectory, readFile, writeFile} from '@shopify/cli-kit/node/fs'
import {AbortSignal} from '@shopify/cli-kit/node/abort'
import {renderTasks} from '@shopify/cli-kit/node/ui'
import {pickBy} from '@shopify/cli-kit/common/object'
import {runWithTimer} from '@shopify/cli-kit/node/metadata'
import {AbortError} from '@shopify/cli-kit/node/error'
import {Writable} from 'stream'

const SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION = '1'

interface JSFunctionBuildOptions {
stdout: Writable
stderr: Writable
Expand Down Expand Up @@ -105,6 +107,18 @@ export async function buildGraphqlTypes(
})
}

const MissingShopifyFunctionPackageError = new AbortError(
'Could not find the Shopify Functions JavaScript library.',
outputContent`Make sure you have the ${outputToken.yellow('@shopify/shopify_function')} library installed.`,
[
outputContent`Add ${outputToken.green(
`"@shopify/shopify_function": "~${SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0"`,
)} to the dependencies section of the package.json file in your function's directory, if not already present.`
.value,
`Run your package manager's install command to update dependencies.`,
],
)

async function checkForShopifyFunctionRuntimeEntrypoint(fun: ExtensionInstance<FunctionConfigType>) {
const entryPoint = await findPathUp('node_modules/@shopify/shopify_function/index.ts', {
type: 'file',
Expand All @@ -117,33 +131,52 @@ async function checkForShopifyFunctionRuntimeEntrypoint(fun: ExtensionInstance<F
})

if (!entryPoint || !runModule) {
throw MissingShopifyFunctionPackageError
}

if (!fun.entrySourceFilePath) {
throw new AbortError('Could not find your function entry point. It must be in src/index.js or src/index.ts')
}

return entryPoint
}

async function validateShopifyFunctionPackageVersion(fun: ExtensionInstance<FunctionConfigType>) {
const packageJsonPath = await findPathUp('node_modules/@shopify/shopify_function/package.json', {
type: 'file',
cwd: fun.directory,
})

if (!packageJsonPath) {
throw MissingShopifyFunctionPackageError
}

const packageJson = JSON.parse(await readFile(packageJsonPath))
const majorVersion = packageJson.version.split('.')[0]

if (majorVersion !== SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION) {
throw new AbortError(
'Could not find the Shopify Functions JavaScript library.',
outputContent`Make sure you have the latest ${outputToken.yellow(
'The installed version of the Shopify Functions JavaScript library is not compatible with this version of the CLI.',
outputContent`Make sure you have a compatible version of the ${outputToken.yellow(
'@shopify/shopify_function',
)} library installed.`,
[
outputContent`Add ${outputToken.green(
'"@shopify/shopify_function": "1.0.0"',
`"@shopify/shopify_function": "~${SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0"`,
)} to the dependencies section of the package.json file in your function's directory, if not already present.`
.value,
`Run your package manager's install command to update dependencies.`,
],
)
}

if (!fun.entrySourceFilePath) {
throw new AbortError('Could not find your function entry point. It must be in src/index.js or src/index.ts')
}

return entryPoint
}

export async function bundleExtension(
fun: ExtensionInstance<FunctionConfigType>,
options: JSFunctionBuildOptions,
processEnv = process.env,
) {
await validateShopifyFunctionPackageVersion(fun)
const entryPoint = await checkForShopifyFunctionRuntimeEntrypoint(fun)

const esbuildOptions = {
Expand Down Expand Up @@ -276,6 +309,7 @@ export class ExportJavyBuilder implements JavyBuilder {
}

async bundle(fun: ExtensionInstance<FunctionConfigType>, options: JSFunctionBuildOptions, processEnv = process.env) {
await validateShopifyFunctionPackageVersion(fun)
await checkForShopifyFunctionRuntimeEntrypoint(fun)

const contents = this.entrypointContents
Expand Down

0 comments on commit e121a59

Please sign in to comment.