-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusis.ts
258 lines (231 loc) Β· 7.09 KB
/
musis.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import fs, { Stats, lstat } from 'fs'
import readline from 'readline'
import path from 'path'
const TEMPLATE_FILENAME = 'musis-template.html'
const PLACEHOLDER = '<div class="musis"></div>'
const DEFAULT_TEMPLATE = `<!DOCTYPE HTML>
<html>
<head>
<title>MusIs</title>
<meta charset="UTF-8" />
<style>
.musis_playlist, .musis_audio {
background: #666;
width: 400px;
padding: 20px;
}
.musis_playlist {
display: none;
}
.musis_playlist a {
color: #eeeedd;
background: #333;
padding: 5px;
display: block;
}
.musis_playlist a:hover {
text-decoration: none;
}
.musis_playlist .musis_active {
color: #5DB0E6;
text-decoration: none;
}
</style>
<script>
function musisRegister(audio, playlist) {
audio.volume = 1.0
audio.play()
var current = 0;
const tracks = playlist.querySelectorAll('li a')
const len = tracks.length
for (var i = 0; i < tracks.length; i++) {
tracks[i].addEventListener('click', (function (i, event) {
event.preventDefault()
run(i)
}).bind(this, i))
}
audio.addEventListener('ended', function (event) {
current++;
if (current == len) {
current = 0;
}
run(current);
});
function run (index, stop) {
current = index
audio.src = tracks[index].dataset.file;
const currentActive = playlist.querySelector('.musis_active')
if (currentActive) currentActive.classList.remove('musis_active')
tracks[index].classList.add('musis_active')
audio.load();
if (!stop) audio.play();
}
run(0, true)
}
</script>
</head>
<body>
<div class="musis"></div>
</body>
</html>
`
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const fsp = fs.promises
function q1 (_recursive?: string[]): Promise<string[]> {
return new Promise((resolve, reject) => {
try {
rl.question('νμ₯μ μ
λ ₯.(μμ: mp3) κ·Έλ§ μ
λ ₯νλ €λ©΄ μν°>', async (answer) => {
if (answer) {
if (_recursive) {
_recursive.push(answer)
return resolve(await q1(_recursive))
} else {
return resolve(await q1([ answer ]))
}
} else {
resolve(_recursive || [])
}
})
} catch (err) {
reject(err)
}
})
}
function q2 (cwd: string, _recursive?: string[]): Promise<string[]> {
return new Promise((resolve, reject) => {
let subdir = ''
try {
rl.question('μμ
μ€μΊν νμ ν΄λ μ΄λ¦. κ·Έλ§ μ
λ ₯νλ €λ©΄ μν°>', async (answer) => {
if (answer) {
const stat = await fsp.lstat(subdir = path.join(cwd, answer))
if (!stat.isDirectory()) throw new Error('ν΄λκ° μλ: ' + subdir)
if (_recursive) {
_recursive.push(answer)
return resolve(await q2(cwd, _recursive))
} else {
return resolve(await q2(cwd, [ answer ]))
}
} else {
resolve(_recursive || [])
}
})
} catch (err) {
if (err.code === 'ENOENT') {
reject(new Error('μ‘΄μ¬νμ§ μλ ν΄λ: ' + subdir))
} else {
reject(err)
}
}
})
}
function checkExtension(fileName: string, extensionArray: string[]): boolean {
const split = fileName.split('.')
const last = split[split.length - 1].toLowerCase()
for (let ext of extensionArray) {
if (last === ext.toLowerCase()) {
return true
}
}
return false
}
async function dirToHtml (cwd: string, subdir: string, exts: string[]): Promise<string|number> {
const subcwd = path.join(cwd, subdir)
// νμΌ μ½κΈ°
console.log('νμΌ μ½μ΄λ€μ΄λ μ€...')
const files = await fsp.readdir(subcwd)
console.log('ν΄λμμ μ΄ νμΌ κ°―μ: ' + files.length)
const filesNameFilter = files.filter(v => checkExtension(v, exts))
console.log('νμ₯μμ λ§λ νμΌ κ°―μ: ' + filesNameFilter.length)
const filesTypeFilter = []
let _path: string
let _stat: Stats
for (let file of filesNameFilter) {
_path = path.join(subcwd, file)
_stat = await fsp.lstat(_path)
if (_stat.isFile()) filesTypeFilter.push(file)
}
console.log('ν΄λ μ μΈ ν κ°―μ: ' + filesTypeFilter.length)
if (filesTypeFilter.length === 0) {
console.error('μ°Ύμ νμΌ κ°―μ 0. 무μλ¨')
return 3
}
console.log('HTML λΉλ μ€...')
let hash = parseInt('' + (Math.random() * 0xFFFFFF)).toString(16).padStart(6, '0')
let source = `\n <p class="musis_title musis_title_${ hash }">${ subdir }</p>
<audio class="musis_audio musis_audio_${ hash }" controls></audio>
<ul class="musis_playlist musis_playlist_${ hash }">`
for (let music of filesTypeFilter) {
source += `\n <li>
<a data-file="${ path.join(subdir, music) }" href="#">${ music }</a>
</li>`
}
source += `\n </ul>
<script>
musisRegister(document.querySelector('.musis_audio_${ hash }'), document.querySelector('.musis_playlist_${ hash }'))
</script>`
return source
}
async function main (): Promise<number> {
const cwd = path.resolve('.')
console.log('νμ¬ ν΄λ: ' + cwd)
// ν
νλ¦Ώ μ½κ±°λ μμΌλ©΄ μλ‘ λ§λ€κΈ°
let template: string
const templateFile = path.join(cwd, TEMPLATE_FILENAME)
try {
template = await fsp.readFile(templateFile, 'utf-8')
} catch (err) {
if (err.code === 'ENOENT') {
fsp.writeFile(templateFile, DEFAULT_TEMPLATE, 'utf-8')
console.log(`ν
νλ¦Ώ μλ‘ μμ±(${ TEMPLATE_FILENAME }) μνλ λλ‘ μ»€μ€ν°λ§μ΄μ§`)
console.log('μ΄ν νλ‘κ·Έλ¨ μ¬μμ')
console.log(`\n(μ£Όμ: "${ PLACEHOLDER }" νκ·Έ νΈμ§ κΈμ§)`)
return 10
} else {
throw err
}
}
// ν
νλ¦Ώ μ ν¨μ± νμΈ
if (template.indexOf(PLACEHOLDER) === -1) {
console.error(`ν
νλ¦Ώμμ "${ PLACEHOLDER }" νκ·Έλ₯Ό μ°Ύμ μ μμ`)
return 10
}
// νμ ν΄λ μ ν
const subdirs = await q2(cwd)
// νμ₯μ
const exts = await q1()
if (exts.length === 0) {
console.error('μ무 νμ₯μλ μ
λ ₯νμ§ μμμΌλ―λ‘ λ¬΄μ')
return 3
} else {
console.log('μ
λ ₯λ νμ₯μ:', exts)
}
let sources = ''
let result: string|number = 0
for (const subdir of subdirs) {
result = await dirToHtml(cwd, subdir, exts)
if (typeof result === 'number') return result
sources += result
}
console.log('HTML νμΌ μμ± μ€...')
const html = template.replace(PLACEHOLDER, sources)
await fsp.writeFile(`MusIs.html`, html, 'utf-8')
console.log('μ±κ³΅')
return 3
}
main().then(timeout => {
console.log('=μ’
λ£=')
console.log(`μ½μ ${ timeout }μ΄ ν λ«ν...`)
setTimeout(() => {
process.exit(0)
}, timeout * 1000)
}).catch(err => {
console.error(err)
console.error('=μΉλͺ
μ μλ¬=')
console.error('μ½μ 10μ΄ ν λ«ν...')
setTimeout(() => {
process.exit(0)
}, 10000)
})