@@ -9,14 +9,34 @@ import {outputContent, outputDebug, outputToken} from '@shopify/cli-kit/node/out
99import { exec } from '@shopify/cli-kit/node/system'
1010import { dirname , joinPath } from '@shopify/cli-kit/node/path'
1111import { build as esBuild , BuildResult } from 'esbuild'
12- import { findPathUp , inTemporaryDirectory , writeFile } from '@shopify/cli-kit/node/fs'
12+ import { findPathUp , inTemporaryDirectory , readFile , writeFile } from '@shopify/cli-kit/node/fs'
1313import { AbortSignal } from '@shopify/cli-kit/node/abort'
1414import { renderTasks } from '@shopify/cli-kit/node/ui'
1515import { pickBy } from '@shopify/cli-kit/common/object'
1616import { runWithTimer } from '@shopify/cli-kit/node/metadata'
1717import { AbortError } from '@shopify/cli-kit/node/error'
1818import { Writable } from 'stream'
1919
20+ export const SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION = '1'
21+
22+ class InvalidShopifyFunctionPackageError extends AbortError {
23+ constructor ( message : string ) {
24+ super (
25+ message ,
26+ outputContent `Make sure you have a compatible version of the ${ outputToken . yellow (
27+ '@shopify/shopify_function' ,
28+ ) } library installed.`,
29+ [
30+ outputContent `Add ${ outputToken . green (
31+ `"@shopify/shopify_function": "~${ SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION } .0.0"` ,
32+ ) } to the dependencies section of the package.json file in your function's directory, if not already present.`
33+ . value ,
34+ `Run your package manager's install command to update dependencies.` ,
35+ ] ,
36+ )
37+ }
38+ }
39+
2040interface JSFunctionBuildOptions {
2141 stdout : Writable
2242 stderr : Writable
@@ -117,19 +137,7 @@ async function checkForShopifyFunctionRuntimeEntrypoint(fun: ExtensionInstance<F
117137 } )
118138
119139 if ( ! entryPoint || ! runModule ) {
120- throw new AbortError (
121- 'Could not find the Shopify Functions JavaScript library.' ,
122- outputContent `Make sure you have the latest ${ outputToken . yellow (
123- '@shopify/shopify_function' ,
124- ) } library installed.`,
125- [
126- outputContent `Add ${ outputToken . green (
127- '"@shopify/shopify_function": "1.0.0"' ,
128- ) } to the dependencies section of the package.json file in your function's directory, if not already present.`
129- . value ,
130- `Run your package manager's install command to update dependencies.` ,
131- ] ,
132- )
140+ throw new InvalidShopifyFunctionPackageError ( 'Could not find the Shopify Functions JavaScript library.' )
133141 }
134142
135143 if ( ! fun . entrySourceFilePath ) {
@@ -139,11 +147,32 @@ async function checkForShopifyFunctionRuntimeEntrypoint(fun: ExtensionInstance<F
139147 return entryPoint
140148}
141149
150+ async function validateShopifyFunctionPackageVersion ( fun : ExtensionInstance < FunctionConfigType > ) {
151+ const packageJsonPath = await findPathUp ( 'node_modules/@shopify/shopify_function/package.json' , {
152+ type : 'file' ,
153+ cwd : fun . directory ,
154+ } )
155+
156+ if ( ! packageJsonPath ) {
157+ throw new InvalidShopifyFunctionPackageError ( 'Could not find the Shopify Functions JavaScript library.' )
158+ }
159+
160+ const packageJson = JSON . parse ( await readFile ( packageJsonPath ) )
161+ const majorVersion = packageJson . version . split ( '.' ) [ 0 ]
162+
163+ if ( majorVersion !== SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION ) {
164+ throw new InvalidShopifyFunctionPackageError (
165+ 'The installed version of the Shopify Functions JavaScript library is not compatible with this version of Shopify CLI.' ,
166+ )
167+ }
168+ }
169+
142170export async function bundleExtension (
143171 fun : ExtensionInstance < FunctionConfigType > ,
144172 options : JSFunctionBuildOptions ,
145173 processEnv = process . env ,
146174) {
175+ await validateShopifyFunctionPackageVersion ( fun )
147176 const entryPoint = await checkForShopifyFunctionRuntimeEntrypoint ( fun )
148177
149178 const esbuildOptions = {
@@ -276,6 +305,7 @@ export class ExportJavyBuilder implements JavyBuilder {
276305 }
277306
278307 async bundle ( fun : ExtensionInstance < FunctionConfigType > , options : JSFunctionBuildOptions , processEnv = process . env ) {
308+ await validateShopifyFunctionPackageVersion ( fun )
279309 await checkForShopifyFunctionRuntimeEntrypoint ( fun )
280310
281311 const contents = this . entrypointContents
0 commit comments