added scripts to create json data files

This commit is contained in:
suroh 2023-10-09 19:43:50 +02:00
parent 99976ecc11
commit b64ee5ffce
1 changed files with 70 additions and 0 deletions

70
scripts/files2json.js Normal file
View File

@ -0,0 +1,70 @@
import fs from 'fs/promises'
import path from 'path'
async function main() {
// variable for incoming variable
let mediaDir = ''
let outputDir = ''
let recurse = false
// get command line arguments
process.argv.forEach(function (val, index) {
// console.log(index + ': ' + val)
if (val == '-dir') {
// console.log(process.argv[index + 1])
mediaDir = process.argv[index + 1] || ''
}
if (val == '-r' || val == '-R') {
recurse = true
}
if (val == '-o') {
outputDir = process.argv[index + 1]
}
})
// if no media dir jump out
if (!mediaDir) {
console.log('no directory passed to script')
return 0
}
// read directory
const media = await fs.readdir(mediaDir)
// clean up directory for json
let parent_dir = mediaDir
.match(/(?<=\/)(\w|\d)+\/*$/i)[0]
.replace(/\/$/i, '')
if (!outputDir) {
outputDir = '.'
}
const obj = await Promise.all(media.map(async m => {
const _r = {
title: '',
parent_dir,
media: m,
}
if (recurse) {
const rDir = path.join(mediaDir, m)
const files = await fs.readdir(rDir)
_r.album = true
_r.tracks = files
}
return _r
}))
const json = JSON.stringify(obj, null, 2)
let output = `${parent_dir}.json`
fs.writeFile(path.join(outputDir, output), json)
}
main()