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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
'use strict'
const readline = require('readline')
const path = require('path')
const util = require('util')
const fs = require('fs')
const { getCrawlerByName } = require('./crawlers')
const writeFile = util.promisify(fs.writeFile)
const access = util.promisify(fs.access)
async function exists(file) {
try {
await access(file)
return true
} catch(err) {
return false
}
}
function prompt(rl, promptMessage = '', defaultChoice = null, options = {}) {
return new Promise((resolve, reject) => {
const hasOptions = (Object.keys(options).length > 0)
console.log('')
if (hasOptions) {
for (const [ option, message ] of Object.entries(options)) {
if (option === defaultChoice) {
console.log(` [${option.toUpperCase()} (default)]: ${message}`)
} else {
console.log(` [${option.toLowerCase()}]: ${message}`)
}
}
console.log('')
}
let promptStr = ''
if (promptMessage) {
promptStr += promptMessage + ' '
}
if (hasOptions) {
promptStr += '['
promptStr += Object.keys(options).map(option => {
if (option === defaultChoice) {
return option.toUpperCase()
} else {
return option.toLowerCase()
}
}).join('/')
promptStr += ']'
} else if (defaultChoice) {
promptStr += `[default: ${defaultChoice}]`
}
promptStr += '> '
rl.question(promptStr, choice => {
toRepeat: {
if (choice.length === 0 && defaultChoice) {
resolve(defaultChoice)
} else if (
hasOptions && Object.keys(options).includes(choice.toLowerCase())
) {
resolve(choice.toLowerCase())
} else if (choice.length > 0 && !hasOptions) {
resolve(choice)
} else {
break toRepeat
}
console.log('')
return
}
resolve(prompt(rl, promptMessage, defaultChoice, options))
})
})
}
async function setupTool() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
console.log('Which source would you like to play music from?')
const wd = process.cwd()
const crawlerCommand = {
l: 'crawl-local',
h: 'crawl-http'
}[await prompt(rl, 'Which source?', 'l', {
l: 'Files on this local computer.',
h: 'Downloadable files linked from a page on the web.'
})]
const crawlerOptions = []
if (crawlerCommand === 'crawl-local') {
console.log('What directory would you like to download music from?')
console.log(`(Your current working directory is: ${wd})`)
crawlerOptions.push(await prompt(rl, 'What directory path?', '.'))
}
if (crawlerCommand === 'crawl-http') {
console.log('What URL would you like to download music from?')
console.log('(This only works if the actual song files are linked; you')
console.log("can't, for example, give a Bandcamp album link here.")
crawlerOptions.push(await prompt(rl, 'What URL?'))
}
console.log('Would you like http-music to automatically process your')
console.log('playlist to find out which music to play every time?')
console.log('This is handy if you expect new music to be added to your')
console.log('source often (e.g. a folder you frequently add new music')
console.log('to, or a webpage that often has new links added to it).')
console.log('')
console.log('If you choose this, http-music may take longer to run.')
console.log('(If you are loading music from a webpage, then the amount of')
console.log("time you'll have to wait depends on your internet connection;")
console.log('if you are loading files from your own computer, the delay')
console.log('will depend on your hard drive speed - not a big deal, on')
console.log('most computers.)')
const useSmartPlaylist = {
y: true,
n: false
}[await prompt(rl, 'Process playlist every time?', 'y', {
y: 'Yes, process the playlist for new items every time.',
n: "No, don't automatically process the playlist."
})]
console.log("Do you want to save your playlist to a file? If not, you'll")
console.log('just be given the command you can use to generate the file.')
const smartPlaylistString = JSON.stringify({
source: [crawlerCommand, ...crawlerOptions]
}, null, 2)
const savePlaylist = {
y: true,
n: false
}[await prompt(rl, 'Save playlist?', 'y', {
y: 'Yes, save the playlist to a file.',
n: 'No, just show the command.'
})]
if (savePlaylist) {
console.log('What would you like to name your playlist file?')
console.log('"playlist.json" will be automatically detected by http-music,')
console.log('but you can specify a different file or path if you want.')
let defaultOutput = 'playlist.json'
const playlistExists = await exists('playlist.json')
if (playlistExists) {
console.log('')
console.log(
'\x1b[1mBeware!\x1b[0m There is already a file called playlist.json' +
' in this'
)
console.log(`directory. (Your current working directory is: ${wd})`)
console.log('You may want to write to another file.')
defaultOutput = null
}
let outputFile = await prompt(rl, 'Playlist file name?', defaultOutput)
if (path.extname(outputFile) !== '.json') {
console.log('(http-music playlist files are JSON files, so your file')
console.log('was changed to a .json file.)')
console.log('')
outputFile = path.basename(outputFile, path.extname(outputFile))
if (playlistExists && path.relative(outputFile, 'playlist') === '') {
console.log('(Since that would overwrite the playlist.json that already')
console.log(
"exists in this directory, it'll instead be saved to playlist2.json.)"
)
console.log('')
outputFile += '2'
}
outputFile += '.json'
}
if (useSmartPlaylist) {
await writeFile(outputFile, smartPlaylistString)
} else {
console.log('Generating your playlist file. This could take a little while..')
const { main: crawlerMain } = getCrawlerByName(crawlerCommand)
const out = await crawlerMain(crawlerOptions, true)
await writeFile(outputFile, out)
}
console.log('Done setting up and saving your playlist file.')
console.log(`Try it out with \x1b[1mhttp-music play${
(path.relative(outputFile, 'playlist.json') === '')
? ''
: ` --open ${path.relative('.', outputFile)}`
}\x1b[0m!`)
} else {
if (useSmartPlaylist) {
console.log("You'll want to create a playlist JSON file containing")
console.log('the following:')
console.log('')
console.log(`\x1b[1m${smartPlaylistString}\x1b[0m`)
} else {
console.log(
`You'll want to use the \x1b[1m${crawlerCommand}\x1b[0m crawler command.`
)
if (crawlerOptions.length > 1) {
console.log(
'You should give it these arguments:',
crawlerOptions.map(l => `\x1b[1m${l}\x1b[0m`).join(', ')
)
} else if (crawlerOptions.length === 1) {
const opt = crawlerOptions[0]
console.log(`You should give it this argument: \x1b[1m${opt}\x1b[0m`)
}
}
console.log('')
}
rl.close()
}
module.exports = setupTool
if (require.main === module) {
setupTool()
.catch(err => console.error(err))
}
|