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 c980c84
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
25 changes: 25 additions & 0 deletions packages/app/src/cli/services/function/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ async function installShopifyLibrary(tmpDir: string) {
const runModule = joinPath(shopifyFunctionDir, 'run.ts')
await writeFile(runModule, '')

const packageJson = joinPath(shopifyFunctionDir, 'package.json')
await writeFile(packageJson, JSON.stringify({version: '1.0.0'}))

return shopifyFunction
}

Expand Down Expand Up @@ -136,6 +139,28 @@ describe('bundleExtension', () => {
})
})

test('errors if shopify library is not on a compatible version', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const incompatibleVersion = '0.0.1'
const ourFunction = await testFunctionExtension({dir: tmpDir})
ourFunction.entrySourceFilePath = joinPath(tmpDir, 'src/index.ts')
await installShopifyLibrary(tmpDir)
writeFile(
joinPath(tmpDir, 'node_modules/@shopify/shopify_function/package.json'),
JSON.stringify({version: incompatibleVersion}),
)

Check failure on line 152 in packages/app/src/cli/services/function/build.test.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/function/build.test.ts#L149-L152

[@typescript-eslint/no-floating-promises] Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator.

// When
const got = bundleExtension(ourFunction, {stdout, stderr, signal, app})

// Then
await expect(got).rejects.toThrow(
/The installed version of the Shopify Functions JavaScript library is not compatible with this version of Shopify CLI./,
)
})
})

test('errors if user function not found', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
Expand Down
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 Shopify 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 c980c84

Please sign in to comment.