-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathbundle.ts
More file actions
51 lines (45 loc) · 2.1 KB
/
bundle.ts
File metadata and controls
51 lines (45 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {AppManifest} from '../models/app/app.js'
import {AssetUrlSchema, DeveloperPlatformClient} from '../utilities/developer-platform-client.js'
import {MinimalAppIdentifiers} from '../models/organization.js'
import {joinPath} from '@shopify/cli-kit/node/path'
import {brotliCompress, zip} from '@shopify/cli-kit/node/archiver'
import {formData, fetch} from '@shopify/cli-kit/node/http'
import {readFileSync} from '@shopify/cli-kit/node/fs'
import {AbortError} from '@shopify/cli-kit/node/error'
import {writeFile} from 'fs/promises'
export async function writeManifestToBundle(appManifest: AppManifest, bundlePath: string) {
const manifestPath = joinPath(bundlePath, 'manifest.json')
await writeFile(manifestPath, JSON.stringify(appManifest, null, 2))
}
export async function compressBundle(inputDirectory: string, outputPath: string, customMatchFilePattern?: string[]) {
const matchFilePattern = customMatchFilePattern ?? ['**/*', '!**/*.js.map']
if (outputPath.endsWith('.br')) {
await brotliCompress({inputDirectory, outputPath, matchFilePattern})
} else {
await zip({inputDirectory, outputZipPath: outputPath, matchFilePattern})
}
}
/**
* Upload a file to GCS
* @param signedURL - The signed URL to upload the file to
* @param filePath - The path to the file
*/
export async function uploadToGCS(signedURL: string, filePath: string) {
const form = formData()
const buffer = readFileSync(filePath)
form.append('my_upload', buffer)
console.log(`🐝🐝🐝 Uploading file to GCS: ${signedURL}`)
await fetch(signedURL, {method: 'put', body: buffer, headers: form.getHeaders()}, 'slow-request')
}
/**
* It generates a URL to upload an app bundle.
* @param apiKey - The application API key
*/
export async function getUploadURL(developerPlatformClient: DeveloperPlatformClient, app: MinimalAppIdentifiers) {
const result: AssetUrlSchema = await developerPlatformClient.generateSignedUploadUrl(app)
if (!result.assetUrl || result.userErrors?.length > 0) {
const errors = result.userErrors.map((error) => error.message).join(', ')
throw new AbortError(errors)
}
return result.assetUrl
}