-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.ts
88 lines (75 loc) · 2.57 KB
/
index.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { version as nodeVersion } from 'node:process'
import { getDeployStore } from '@netlify/blobs'
import pMap from 'p-map'
import semver from 'semver'
import { log, logError } from '../../log/logger.js'
import { anyBlobsToUpload, getBlobsDir, getKeysToUpload, uploadBlob } from '../../utils/blobs.js'
import { type CoreStep, type CoreStepCondition, type CoreStepFunction } from '../types.js'
const coreStep: CoreStepFunction = async function ({
debug,
logs,
deployId,
buildDir,
quiet,
packagePath,
constants: { SITE_ID, NETLIFY_API_TOKEN, NETLIFY_API_HOST },
}) {
// This should never happen due to the condition check
if (!deployId || !NETLIFY_API_TOKEN) {
return {}
}
// for cli deploys with `netlify deploy --build` the `NETLIFY_API_HOST` is undefined
const apiHost = NETLIFY_API_HOST || 'api.netlify.com'
const storeOpts: { siteID: string; deployID: string; token: string; apiURL: string; fetch?: typeof fetch } = {
siteID: SITE_ID,
deployID: deployId,
token: NETLIFY_API_TOKEN,
apiURL: `https://${apiHost}`,
}
if (semver.lt(nodeVersion, '18.0.0')) {
const nodeFetch = await import('node-fetch')
storeOpts.fetch = nodeFetch.default as unknown as typeof fetch
}
const blobStore = getDeployStore(storeOpts)
const blobsDir = getBlobsDir(buildDir, packagePath)
const keys = await getKeysToUpload(blobsDir)
// We checked earlier, but let's be extra safe
if (keys.length === 0) {
if (!quiet) {
log(logs, 'No blobs to upload to development store.')
}
return {}
}
if (!quiet) {
log(logs, `Uploading ${keys.length} blobs to development store...`)
}
try {
await pMap(
keys,
async (key) => {
if (debug && !quiet) {
log(logs, `- Uploading blob ${key}`, { indent: true })
}
await uploadBlob(blobStore, blobsDir, key)
},
{ concurrency: 10 },
)
} catch (err) {
logError(logs, `Error uploading blobs to development store: ${err.message}`)
throw new Error(`Failed while uploading blobs to development store`)
}
if (!quiet) {
log(logs, `Done uploading blobs to development store.`)
}
return {}
}
const condition: CoreStepCondition = async ({ deployId, buildDir, packagePath, constants: { NETLIFY_API_TOKEN } }) =>
Boolean(NETLIFY_API_TOKEN && deployId && (await anyBlobsToUpload(buildDir, packagePath)))
export const devUploadBlobs: CoreStep = {
event: 'onDev',
coreStep,
coreStepId: 'dev_blobs_upload',
coreStepName: 'Uploading blobs',
coreStepDescription: () => 'Uploading blobs to development deploy store',
condition,
}