-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.js
executable file
·157 lines (142 loc) · 5.2 KB
/
cli.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
'use strict'
// @flow
/*:: import type { Options } from './types' */
const inquirer = require('inquirer')
const rc = require('rc')
const _ = require('lodash')
const updateNotifier = require('update-notifier')
const utils = require('./utils')
const showTime = require('./')
const upgrade = require('./upgrade')
const chalk = require('chalk')
const configure = require('./configure')
const clearCache = require('./clear-cache')
const errors = require('./errors')
const pkg = require('./package.json')
const ui = new inquirer.ui.BottomBar()
const log = ui.log.write.bind(ui.log)
const args = rc('show-time', {
cache: utils.dotPath('cache'),
player: null,
feed: null,
lang: 'eng',
port: '8888',
'peer-port': '6881',
log: log,
offline: false,
browse: false,
movie: false,
'cache-warning-size': 500 * 1024 * 1024,
'cache-warning-on-start': true,
'warning-no-eng': true,
})
const options /*:Options*/ = Object.assign(_.pick(args,
'cache', 'player', 'feed', 'lang', 'port', 'peer-port', 'log',
'offline', 'browse', 'movie'), { title: args._ && args._[0] })
const configFile = utils.dotPath('config')
if (args.download) {
options.port = 0
options['peer-port'] = 0
options.player = null
}
if (args.help || args.h) {
console.log('show-time [options]')
console.log('')
console.log('Options:')
console.log(' <title> Directly select this show (ignoring your feed)')
console.log(' --version, -v Show version and exit')
console.log(' --help, -h Show this help and exit')
console.log(' --clear-cache Clears cache and exit')
console.log(' --configure Configuration wizard')
console.log(' --config <file> Use alternative configuration file')
console.log(' --cache <path> Path to cache (--no-cache to disable)')
console.log(' --player <name> Automatically play to given player')
console.log(' --feed <url> ShowRSS feed URL')
console.log(' --lang <langs> Preferred languages for subtitles (eg. "fre,eng")')
console.log(' --port <port> Stream port (default 8888)')
console.log(' --peer-port <port> Peer listening port (default 6881)')
console.log(' --download Download only mode')
console.log(' --offline Offline mode')
console.log(' --browse Ignore your feed, browse and select individual show')
console.log(' --movie Search for movie instead of TV show')
console.log('')
console.log('Valid players: ' + utils.players.join(', '))
process.exit(0)
}
else if (args['clear-cache']) {
if (!options.cache) {
console.error('No cache directory configured')
process.exit(0)
} else {
clearCache(options.cache, args['dry-run'], true)
}
}
else if (args.version || args.v) {
console.log(pkg.version)
process.exit(0)
}
else {
if (args['update-notifier'] === false) {
process.env.NO_UPDATE_NOTIFIER = '1'
}
updateNotifier({ pkg, updateCheckInterval: 10 }).notify({ defer: false })
upgrade(args, configFile)
.then(main)
.then(() => {
log(chalk.green('Terminated.'))
process.exit(0)
})
.catch(err => {
log('Error: ' + errors.getMessage(err))
if (process.env.NODE_ENV === 'development') {
log('(dev) Error details: ' + err.stack)
}
process.exit(1)
})
}
const start = (shouldCheckCache = args['cache-warning-on-start'], shouldCheckEng = args['warning-no-eng']) => {
if (shouldCheckCache && options.cache) {
const help = () => chalk.dim(
`
You can customize this warning by editing ${configFile}:
- You can disable this warning by setting "cache-warning-on-start" to false
- You can change size threshold by setting "cache-warning-size" (current value ${args['cache-warning-size']})
`
)
return clearCache.checkOldies(options.cache, args['cache-warning-size'], help)
.then(() => start(false))
} else if (shouldCheckEng && args.lang.indexOf('eng') === -1) {
console.error(chalk.dim('Note that languages management has changed since version 5.3.0'))
console.error(chalk.dim('You can not have more than one favorite languages, and English is not automatic fallback'))
console.error(chalk.dim('However, it\'s very likely you want to add this language to your list'))
return configure.checkEng(args, configFile)
.then(conf => configure.save(conf, configFile))
.then(conf => {
options.lang = args.lang = conf.lang
return start(false, false)
})
} else {
return showTime(options)
}
}
function main () {
if (options.feed || options.movie || options.title || options.browse || options.offline) {
// Ignore local feed: start directly
return start()
} else if (args.configure) {
// Run configuration wizard
return configure(configFile, args)
} else {
if (!utils.canRead(configFile) || !options.feed) {
// No feed set for this user, no direct show, default mode = browse…
// …unless he requested offline mode, then don't bother him with our defaults, he knows what he's doing
if (!options.offline) {
console.log(chalk.cyan(chalk.bold('Notice') + ': No feed configured; fallback to browse mode'))
console.log(chalk.cyan(' Run `show-time --configure` to register your own showrss feed'))
options.browse = true
}
}
return start()
}
}