Convert scripts to typescript
[oweals/peertube.git] / scripts / update-host.ts
1 import { readFileSync, writeFileSync } from 'fs'
2 import * as parseTorrent from 'parse-torrent'
3
4 import { CONFIG, STATIC_PATHS } from '../server/initializers/constants'
5 import { database as db } from '../server/initializers/database'
6 import { hasFriends } from '../server/lib/friends'
7
8 db.init(true, function () {
9   hasFriends(function (err, itHasFriends) {
10     if (err) throw err
11
12     if (itHasFriends === true) {
13       console.log('Cannot update host because you have friends!')
14       process.exit(-1)
15     }
16
17     console.log('Updating torrent files.')
18     db.Video.list(function (err, videos) {
19       if (err) throw err
20
21       videos.forEach(function (video) {
22         const torrentName = video.id + '.torrent'
23         const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName
24         const filename = video.id + video.extname
25
26         const parsed = parseTorrent(readFileSync(torrentPath))
27         parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
28         parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ]
29
30         const buf = parseTorrent.toTorrentFile(parsed)
31         writeFileSync(torrentPath, buf)
32       })
33
34       process.exit(0)
35     })
36   })
37 })