From b64ee5ffce7798868b371a6c6715383285860301 Mon Sep 17 00:00:00 2001 From: suroh Date: Mon, 9 Oct 2023 19:43:50 +0200 Subject: [PATCH] added scripts to create json data files --- scripts/files2json.js | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 scripts/files2json.js diff --git a/scripts/files2json.js b/scripts/files2json.js new file mode 100644 index 0000000..4d1c55f --- /dev/null +++ b/scripts/files2json.js @@ -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()