-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathutil.ts
More file actions
114 lines (100 loc) · 3.15 KB
/
util.ts
File metadata and controls
114 lines (100 loc) · 3.15 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { sep } from 'path'
import type { NetlifyAPI } from '@netlify/api'
import pWaitFor from 'p-wait-for'
import { DEPLOY_POLL } from './constants.js'
import type { FileObject } from './upload-files.js'
type Deploy = Awaited<ReturnType<NetlifyAPI['getSiteDeploy']>>
// normalize windows paths to unix paths
export const normalizePath = (relname: string): string => {
if (relname.includes('#') || relname.includes('?')) {
throw new Error(`Invalid filename ${relname}. Deployed filenames cannot contain # or ? characters`)
}
return relname.split(sep).join('/')
}
// poll an async deployId until its done diffing
export const waitForDiff = async (
api: NetlifyAPI,
deployId: string,
siteId: string,
timeout: number,
): Promise<Deploy> => {
// capture ready deploy during poll
let deploy: Deploy
const loadDeploy = async () => {
const siteDeploy = await api.getSiteDeploy({ siteId, deployId })
switch (siteDeploy.state) {
// https://github.com/netlify/bitballoon/blob/master/app/models/deploy.rb#L21-L33
case 'error': {
const deployError = new Error(siteDeploy.error_message || `Deploy ${deployId} had an error`)
Object.assign(deployError, { deploy: siteDeploy })
throw deployError
}
case 'prepared':
case 'uploading':
case 'uploaded':
case 'ready': {
deploy = siteDeploy
return true
}
case 'preparing':
default: {
return false
}
}
}
await pWaitFor(loadDeploy, {
interval: DEPLOY_POLL,
timeout: {
milliseconds: timeout,
message: 'Timeout while waiting for deploy',
},
})
// @ts-expect-error TS(2454) FIXME: Variable 'deploy' is used before being assigned.
return deploy
}
// Poll a deployId until its ready
export const waitForDeploy = async (
api: NetlifyAPI,
deployId: string,
siteId: string,
timeout: number,
): Promise<Deploy> => {
// capture ready deploy during poll
let deploy: Deploy
const loadDeploy = async () => {
const siteDeploy = await api.getSiteDeploy({ siteId, deployId })
switch (siteDeploy.state) {
// https://github.com/netlify/bitballoon/blob/master/app/models/deploy.rb#L21-L33
case 'error': {
const deployError = new Error(siteDeploy.error_message || `Deploy ${deployId} had an error`)
Object.assign(deployError, { deploy: siteDeploy })
throw deployError
}
case 'ready': {
deploy = siteDeploy
return true
}
case 'preparing':
case 'prepared':
case 'uploaded':
case 'uploading':
default: {
return false
}
}
}
await pWaitFor(loadDeploy, {
interval: DEPLOY_POLL,
timeout: {
milliseconds: timeout,
message: 'Timeout while waiting for deploy',
},
})
// @ts-expect-error TS(2454) FIXME: Variable 'deploy' is used before being assigned.
return deploy
}
// Transform the fileShaMap and fnShaMap into a generic shaMap that file-uploader.js can use
export const getUploadList = (required?: string[] | null, shaMap?: Record<string, FileObject[]> | null) => {
if (!required || !shaMap) return []
return required.flatMap((sha) => shaMap[sha])
}