-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
131 lines (109 loc) · 3.51 KB
/
upload.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { spawn } = require("child_process");
const fs = require('fs/promises')
const { extname } = require('path')
const cheerio = require('cheerio')
const { hashElement } = require('folder-hash');
const FastlyAPI = require('node-fastly-api')
const { trim } = require('lodash')
require('dotenv').config()
const debug = false
const bucketPath = process.env.GCS_BUCKET_PATH || false
const fastlyKey = process.env.FASTLY_API_KEY || false
const fastlyServiceId = process.env.FASTLY_SERVICE_ID || false
const fastlyDictId = process.env.FASTLY_DICT_ID || false
if ( !bucketPath ) {
console.error('missing GCS_BUCKET_PATH environment variable, have you created a .env file?')
process.exit(1)
}
if ( !fastlyKey ) {
console.error('missing FASTLY_API_KEY environment variable, have you created a .env file?')
process.exit(1)
}
if ( !fastlyServiceId ) {
console.error('missing FASTLY_SERVICE_ID')
process.exit(1)
}
if ( !fastlyDictId ) {
console.error('missing FASTLY_DICT_ID')
process.exit(1)
}
function runCmd(cmd, args) {
return new Promise(async (resolve, reject) => {
const spawned = spawn(cmd, args);
if ( debug ) {
spawned.stdout.pipe(process.stdout, { end: false });
spawned.stderr.pipe(process.stderr, { end: false });
}
spawned.on("exit", (code) => {
if (code <= 0) {
resolve();
} else {
console.error(`${cmd} exited with code ${code}`);
reject(code);
}
});
});
}
async function gatherLinks(file) {
const links = []
const html = await (await fs.readFile(`dist/${file}`)).toString()
const dom = cheerio.load(html)
dom('img', 'body').each( (i, img) => {
links.push(img.attribs.src)
})
return links
}
function metaFromLinks(links) {
let meta = ``
links.forEach( (link) => {
meta += `<${link}>; rel=preload; as=image, `
})
return trim(meta, ',')
}
(async () => {
try {
const folderHash = await hashElement(`dist`, {encoding: "hex"})
const uploadFolder = folderHash.hash;
console.log(`Uploading all files to ${bucketPath}/${uploadFolder}`)
await runCmd(
'gsutil',
[
'-m',
'rsync',
'-d',
'-r',
'dist',
`${bucketPath}/${uploadFolder}`
]
)
console.log(`Files sync'd, parsing links and setting headers`)
const files = await fs.readdir(`dist`)
const all = await Promise.all(files.map( async (file) => {
if ( extname(file) === `.html` ) {
const links = await gatherLinks(file);
if ( links ) {
const meta = metaFromLinks(links)
return runCmd(`gsutil`, [
'setmeta',
'-h',
`x-goog-meta-link: ${meta}`,
`${bucketPath}/${uploadFolder}/${file}`
])
}
}
return Promise.resolve()
}))
console.log(`Updating Fastly config`)
const api = FastlyAPI(fastlyKey, fastlyServiceId)
const path = `/nullvariable/${uploadFolder}`
await api.updateDictionaryItem(fastlyServiceId, fastlyDictId, `nullvariable.com_hash`, {
item_value: path,
})
console.info(`upload complete`)
return
} catch (error) {
console.error(error)
debugger
process.exit(1)
}
})();