Skip to content

Commit

Permalink
draftable dev works now and addressing pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
saga-dasgupta committed Sep 23, 2024
1 parent c5a48a8 commit 4c9da9a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
7 changes: 2 additions & 5 deletions packages/app/src/cli/models/extensions/extension-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {constantize, slugify} from '@shopify/cli-kit/common/string'
import {hashString, randomUUID} from '@shopify/cli-kit/node/crypto'
import {partnersFqdn} from '@shopify/cli-kit/node/context/fqdn'
import {joinPath} from '@shopify/cli-kit/node/path'
import {fileExists, touchFile, writeFile, copyFile} from '@shopify/cli-kit/node/fs'
import {fileExists, touchFile, writeFile} from '@shopify/cli-kit/node/fs'
import {getPathValue} from '@shopify/cli-kit/common/object'
import {useThemebundling} from '@shopify/cli-kit/node/context/local'

Expand Down Expand Up @@ -336,13 +336,10 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi

if (this.features.includes('bundling')) {
// Modules that are going to be inclued in the bundle should be built in the bundle directory
this.outputPath = this.isFunctionExtension ? this.outputPath : joinPath(bundleDirectory, extensionId, outputFile)
this.outputPath = joinPath(bundleDirectory, extensionId, outputFile)
}

await this.build(options)
if (this.isFunctionExtension) {
await copyFile(this.outputPath, joinPath(bundleDirectory, extensionId, 'index.wasm'))
}
if (this.isThemeExtension && useThemebundling()) {
await bundleThemeExtension(this, options)
}
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/cli/services/build/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {AbortError, AbortSilentError} from '@shopify/cli-kit/node/error'
import lockfile from 'proper-lockfile'
import {joinPath} from '@shopify/cli-kit/node/path'
import {outputDebug} from '@shopify/cli-kit/node/output'
import {readFile, touchFile, writeFile, fileExistsSync} from '@shopify/cli-kit/node/fs'
import {Writable} from 'stream'

export interface ExtensionBuildOptions {
Expand Down Expand Up @@ -141,11 +142,18 @@ export async function buildFunctionExtension(
}

try {
const bundlePath = joinPath(extension.outputPath.split('/').slice(0, -2).join('/'), 'index.wasm')
extension.outputPath = joinPath(extension.directory, joinPath('dist', 'function.wasm'))
if (extension.isJavaScript) {
await runCommandOrBuildJSFunction(extension, options)
} else {
await buildOtherFunction(extension, options)
}
if (fileExistsSync(extension.outputPath)) {
const base64Contents = await readFile(extension.outputPath, {encoding: 'base64'})
await touchFile(bundlePath)
await writeFile(bundlePath, base64Contents)
}
} finally {
await releaseLock()
}
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/cli/services/deploy/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ describe('bundleAndBuildExtensions', () => {
})
})

test('creates a zip file with wasm file in it for a function extension', async () => {
await file.inTemporaryDirectory(async (tmpDir: string) => {
test('creates a zip file for a function extension', async () => {
await file.inTemporaryDpackages/app/package.jsonirectory(async (tmpDir: string) => {
// Given
const bundlePath = joinPath(tmpDir, 'bundle.zip')

Expand Down
45 changes: 45 additions & 0 deletions packages/app/src/cli/services/dev/update-extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {reloadExtensionConfig, updateExtensionDraft} from './update-extension.js'
import {
placeholderAppConfiguration,
testFunctionExtension,
testDeveloperPlatformClient,
testPaymentExtensions,
testThemeExtensions,
Expand All @@ -9,12 +10,14 @@ import {
import {parseConfigurationFile, parseConfigurationObjectAgainstSpecification} from '../../models/app/loader.js'
import {DeveloperPlatformClient} from '../../utilities/developer-platform-client.js'
import {ExtensionUpdateDraftMutationVariables} from '../../api/graphql/partners/generated/update-draft.js'
import {uploadWasmBlob} from '../deploy/upload.js'
import {inTemporaryDirectory, mkdir, writeFile} from '@shopify/cli-kit/node/fs'
import {outputInfo} from '@shopify/cli-kit/node/output'
import {describe, expect, vi, test} from 'vitest'
import {joinPath} from '@shopify/cli-kit/node/path'
import {platformAndArch} from '@shopify/cli-kit/node/os'

vi.mock('../deploy/upload.js')
vi.mock('@shopify/cli-kit/node/output')
vi.mock('../../models/app/loader.js', async () => {
const actual: any = await vi.importActual('../../models/app/loader.js')
Expand Down Expand Up @@ -186,6 +189,48 @@ describe('updateExtensionDraft()', () => {
})
})

test('updates draft successfully for function app extension', async () => {
const developerPlatformClient: DeveloperPlatformClient = testDeveloperPlatformClient()
await inTemporaryDirectory(async (tmpDir) => {
const mockExtension = await testFunctionExtension({dir: tmpDir})

const filepath = 'index.wasm'
const content = 'test content'
const base64Content = Buffer.from(content).toString('base64')
await mkdir(joinPath(mockExtension.directory, 'dist'))
await writeFile(joinPath(mockExtension.directory, 'dist', filepath), content)
vi.mocked(uploadWasmBlob).mockResolvedValue({url: 'url', moduleId: 'moduleId'})

await updateExtensionDraft({
extension: mockExtension,
developerPlatformClient,
apiKey,
registrationId,
stdout,
stderr,
appConfiguration: placeholderAppConfiguration,
})

expect(developerPlatformClient.updateExtension).toHaveBeenCalledWith({
apiKey,
context: '',
handle: mockExtension.handle,
registrationId,
config: JSON.stringify({
title: 'test function extension',
module_id: 'moduleId',
description: 'description',
app_key: 'mock-api-key',
api_type: 'product_discounts',
api_version: '2022-07',
enable_creation_ui: true,
localization: {},
uploaded_files: {'index.wasm': base64Content},
}),
})
})
})

test('handles user errors with stderr message', async () => {
const errorResponse = {
extensionUpdateDraft: {
Expand Down
16 changes: 12 additions & 4 deletions packages/app/src/cli/services/dev/update-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ export async function updateExtensionDraft({
config = (await extension.deployConfig({apiKey, developerPlatformClient, appConfiguration})) || {}
}

let draftableConfig: {[key: string]: unknown} = {
...config,
serialized_script: encodedFile,
}
if (extension.isFunctionExtension) {
const compiledFiles = await readFile(extension.outputPath, {encoding: 'base64'})
draftableConfig = {
...draftableConfig,
uploaded_files: {'index.wasm': compiledFiles},
}
}
const extensionInput: ExtensionUpdateDraftMutationVariables = {
apiKey,
config: JSON.stringify({
...config,
serialized_script: encodedFile,
}),
config: JSON.stringify(draftableConfig),
handle: extension.handle,
context: extension.contextValue,
registrationId,
Expand Down

0 comments on commit 4c9da9a

Please sign in to comment.