Fix update host script
[oweals/peertube.git] / scripts / update-host.ts
1 import { database as db } from '../server/initializers/database'
2 import { getServerAccount } from '../server/helpers/utils'
3
4 db.init(true)
5   .then(() => {
6     return getServerAccount()
7   })
8   .then(serverAccount => {
9     return db.AccountFollow.listAcceptedFollowingUrlsForApi([ serverAccount.id ])
10   })
11   .then(res => {
12     return res.total > 0
13   })
14   .then(hasFollowing => {
15     if (hasFollowing === true) {
16       console.log('Cannot update host because you follow other servers!')
17       process.exit(-1)
18     }
19
20     console.log('Updating torrent files.')
21     return db.Video.list()
22   })
23   .then(videos => {
24     const tasks: Promise<any>[] = []
25
26     videos.forEach(video => {
27       console.log('Updating video ' + video.uuid)
28
29       video.VideoFiles.forEach(file => {
30         tasks.push(video.createTorrentAndSetInfoHash(file))
31       })
32     })
33
34     return Promise.all(tasks)
35   })
36   .then(() => {
37     process.exit(0)
38   })