2022-02-06 22:19:09 +01:00
|
|
|
const { create: createPlaylist } = require('./playlist')
|
|
|
|
const logger = require('./logger')
|
2021-12-12 05:10:18 +01:00
|
|
|
const file = require('./file')
|
2022-02-06 22:04:56 +01:00
|
|
|
const generators = require('../generators')
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:19:09 +01:00
|
|
|
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
|
2022-02-06 22:04:56 +01:00
|
|
|
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/generators'
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
const generator = {}
|
2022-02-05 06:12:17 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
generator.generate = async function (name, items = []) {
|
|
|
|
if (typeof generators[name] === 'function') {
|
|
|
|
try {
|
2022-02-06 22:19:09 +01:00
|
|
|
const output = await generators[name].bind()(items)
|
|
|
|
await file.create(`${LOGS_DIR}/${name}.log`, output.map(toJSON).join('\n'))
|
|
|
|
for (const type of output) {
|
|
|
|
const playlist = createPlaylist(type.items, { public: true })
|
|
|
|
await file.create(`${PUBLIC_DIR}/${name}/${type.id}.m3u`, playlist.toString())
|
|
|
|
}
|
2022-02-06 22:04:56 +01:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(`generators/${name}.js: ${error.message}`)
|
2022-02-05 06:12:17 +01:00
|
|
|
}
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
module.exports = generator
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:19:09 +01:00
|
|
|
function toJSON(type) {
|
|
|
|
return JSON.stringify({ id: type.id, count: type.items.length })
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|