Don't stuck on active jobs
[oweals/peertube.git] / server / tools / upload-directory.ts
1 import * as program from 'commander'
2 import * as Promise from 'bluebird'
3 import { isAbsolute, join } from 'path'
4
5 import { readdirPromise } from '../helpers/core-utils'
6 import { execCLI } from '../tests/utils/index'
7
8 program
9   .option('-u, --url <url>', 'Server url')
10   .option('-U, --username <username>', 'Username')
11   .option('-p, --password <token>', 'Password')
12   .option('-i, --input <directory>', 'Videos directory absolute path')
13   .option('-d, --description <description>', 'Video descriptions')
14   .option('-c, --category <category>', 'Video categories')
15   .option('-l, --licence <licence>', 'Video licences')
16   .option('-t, --tags <tags>', 'Video tags', list)
17   .parse(process.argv)
18
19 if (
20   !program['url'] ||
21   !program['username'] ||
22   !program['password'] ||
23   !program['input'] ||
24   !program['description'] ||
25   !program['category'] ||
26   !program['licence'] ||
27   !program['tags']
28 ) {
29   throw new Error('All arguments are required.')
30 }
31
32 if (isAbsolute(program['input']) === false) {
33   throw new Error('Input path should be absolute.')
34 }
35
36 let command = `npm run ts-node -- ${__dirname}/get-access-token.ts`
37 command += ` -u "${program['url']}"`
38 command += ` -n "${program['username']}"`
39 command += ` -p "${program['password']}"`
40
41 execCLI(command)
42   .then(stdout => {
43     const accessToken = stdout.replace('\n', '')
44
45     console.log(accessToken)
46
47     return readdirPromise(program['input']).then(files => ({ accessToken, files }))
48   })
49   .then(({ accessToken, files }) => {
50     return Promise.each(files, file => {
51       const video = {
52         tags: program['tags'],
53         name: file,
54         description: program['description'],
55         category: program['category'],
56         licence: program['licence']
57       }
58
59       let command = `npm run ts-node -- ${__dirname}/upload.ts`
60       command += ` -u "${program['url']}"`
61       command += ` -a "${accessToken}"`
62       command += ` -n "${video.name}"`
63       command += ` -d "${video.description}"`
64       command += ` -c "${video.category}"`
65       command += ` -l "${video.licence}"`
66       command += ` -t "${video.tags.join(',')}"`
67       command += ` -f "${join(program['input'], file)}"`
68
69       return execCLI(command).then(stdout => console.log(stdout))
70     })
71   })
72   .then(() => process.exit(0))
73   .catch(err => {
74     console.error(err)
75     process.exit(-1)
76   })
77
78 // ----------------------------------------------------------------------------
79
80 function list (val) {
81   return val.split(',')
82 }