This repository was archived by the owner on Nov 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetlifyAPI.js
More file actions
112 lines (92 loc) · 2.34 KB
/
Copy pathnetlifyAPI.js
File metadata and controls
112 lines (92 loc) · 2.34 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
const crypto = require ('crypto-hash')
const stream = require('stream')
require('dotenv').config()
const NetlifyAPI = require('netlify')
const client = new NetlifyAPI(process.env.NETLIFY_API_TOKEN)
const site_id = 'b8d481fc-f70c-45f8-9242-3936c27fe7e6'
const fetch = require('node-fetch')
const uploadFile = async ({ deployId, path, buffer }) => {
const bufferStream = new stream.PassThrough()
bufferStream.end(buffer)
const result = await client.uploadDeployFile({
deployId,
path,
body: bufferStream,
})
return result
}
const directUploadFile = async ({ deployId, path, buffer }) => {
const uri = `https://api.netlify.com/api/v1/deploys/${deployId}/files/${path}`
const options = {
method: 'PUT',
headers: {
'Authorization': `Bearer ${process.env.NETLIFY_API_TOKEN}`,
'Content-Type': 'application/octet-stream'
},
body: buffer
}
const result = await fetch(uri, options)
return await result.json()
}
const upload = async () => {
let files = []
try {
files = await client.listSiteFiles({ site_id })
} catch (e) {
console.log(e.json)
}
let existingFiles = {}
files.forEach(({ path, sha }) => {
existingFiles[`${path}`] = sha
})
const now = new Date()
const newFileBuf = Buffer.from(now.toISOString())
const sha = await crypto.sha1(newFileBuf)
const key = `/static/${sha}/now.txt`
const newFile = {}
newFile[key] = sha
let deploy = []
try {
deploy = await client.createSiteDeploy({
site_id,
body: {
files: {
...existingFiles,
...newFile
}
}
})
} catch (e) {
console.log(e.json)
}
const { id: deployId, required } = deploy
if( required.length === 0 ) {
console.log('nothing to upload (1)')
return
}
if (!required.includes(sha)) {
console.log('nothing to upload (2)')
return
}
const uploadList = [{
deployId,
path: key,
buffer: newFileBuf
}]
//console.log(uploadList)
result = await Promise.all(
//uploadList.map(item => uploadFile(item))
uploadList.map(item => directUploadFile(item))
)
console.log('Why are all fields capitalized, even without the js-client?')
console.log(result)
//console.log('.. but other endpoints are *NOT* capitalized?')
//let site
//try {
// site = await client.getSite({ site_id })
//} catch (e) {
// console.log(e.json)
//}
//console.log(site)
}
upload()