forked from microlinkhq/youtube-dl-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
43 lines (37 loc) · 1.2 KB
/
postinstall.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
'use strict'
const debug = require('debug-logfmt')('youtube-dl-exec:install')
const { mkdir, chmod } = require('node:fs/promises')
const { pipeline } = require('node:stream/promises')
const { createWriteStream } = require('node:fs')
const {
YOUTUBE_DL_PATH,
YOUTUBE_DL_HOST,
YOUTUBE_DL_DIR,
YOUTUBE_DL_FILE,
YOUTUBE_DL_SKIP_DOWNLOAD
} = require('../src/constants')
const getLatest = ({ assets }) => {
const { browser_download_url: url } = assets.find(
({ name }) => name === YOUTUBE_DL_FILE
)
return fetch(url)
}
const getBinary = async url => {
let response = await fetch(url)
if (response.headers.get('content-type') !== 'application/octet-stream') {
response = await getLatest(await response.json())
}
return response.body
}
const installBinary = async () => {
debug('downloading', { url: YOUTUBE_DL_HOST })
const [binary] = await Promise.all([
getBinary(YOUTUBE_DL_HOST),
mkdir(YOUTUBE_DL_DIR, { recursive: true })
])
debug('writing', { path: YOUTUBE_DL_PATH })
await pipeline(binary, createWriteStream(YOUTUBE_DL_PATH))
await chmod(YOUTUBE_DL_PATH, 0o755)
debug({ status: 'success' })
}
YOUTUBE_DL_SKIP_DOWNLOAD ? debug({ status: 'skipped' }) : installBinary()