-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
72 lines (62 loc) · 2.17 KB
/
build.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
'use strict'
// for deployment outside of GitLab CI, e.g. Cloudflare Pages and Netlify
import { Extract } from 'unzipper'
import { dirname, join } from 'node:path'
import { mkdir, readdir, rm, stat } from 'node:fs/promises'
import { pipeline } from 'node:stream/promises'
import { fileURLToPath } from 'node:url'
import { Readable } from 'node:stream'
const __dirname = dirname(fileURLToPath(import.meta.url))
const rootPath = join(__dirname, '..')
const publicPath = join(rootPath, 'public')
const artifactsUrl = 'https://gitlab.com/malware-filter/phishing-filter/-/jobs/artifacts/main/download?job=pages'
const pipelineUrl = 'https://gitlab.com/malware-filter/phishing-filter/badges/main/pipeline.svg'
const ghMirror = 'https://nightly.link/curbengh/phishing-filter/workflows/pages/main/public.zip'
const pipelineStatus = async (url) => {
console.log(`Checking pipeline from "${url}"`)
try {
const svg = await (await fetch(url)).text()
if (!svg.includes('passed')) throw new Error('last gitlab pipeline failed')
} catch ({ message }) {
throw new Error(message)
}
}
const f = async () => {
console.log(`Downloading artifacts.zip from "${artifactsUrl}"`)
try {
await pipeline(
Readable.fromWeb((await fetch(artifactsUrl)).body),
Extract({ path: rootPath })
)
await pipelineStatus(pipelineUrl)
} catch ({ message }) {
console.error(JSON.stringify({
error: message,
link: artifactsUrl
}))
console.log(`Downloading artifacts.zip from "${ghMirror}"`)
await mkdir(publicPath, { recursive: true })
try {
await pipeline(
Readable.fromWeb((await fetch(ghMirror)).body),
Extract({ path: publicPath })
)
} catch ({ message }) {
throw new Error(JSON.stringify({
error: message,
link: ghMirror
}))
}
}
// Cloudflare Pages has maximum file size of 25MiB
const files = await readdir(publicPath)
if (process.env.CF_PAGES) {
await Promise.all(files.map(async (filename) => {
const { size } = await stat(join(publicPath, filename))
if (size >= (25 * 1024 * 1024)) {
await rm(join(publicPath, filename), { force: true })
}
}))
}
}
f()