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