From f60894f2ce6a52f91c27e17ea9663b4d39596b0b Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 26 Jun 2025 19:57:23 -0400 Subject: [PATCH] working --- package.json | 1 + website/src/repl/create_strudel_json.mjs | 52 ++++++++++++++++++++++++ website/src/repl/files.mjs | 4 +- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 website/src/repl/create_strudel_json.mjs diff --git a/package.json b/package.json index d18291eb2..1c401ea12 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "description": "Port of tidalcycles to javascript", "scripts": { + "strudeljson": "node ./website/src/repl/create_strudel_json.mjs", "setup": "pnpm i", "pretest": "npm run jsdoc-json", "prebuild": "npm run jsdoc-json", diff --git a/website/src/repl/create_strudel_json.mjs b/website/src/repl/create_strudel_json.mjs new file mode 100644 index 000000000..d1a1c54e3 --- /dev/null +++ b/website/src/repl/create_strudel_json.mjs @@ -0,0 +1,52 @@ + +import { createInterface } from 'node:readline'; +import * as fs from 'node:fs' + +const validFileTypes = new Set(['.wav', '.mp3', '.aac', '.m4a']) +/** For automatically creating the strudel JSON that for samples hosted in a remote repo */ +async function writeStrudelJson(subpath, hosturl) { + const children = await fs.promises.readdir(subpath, { recursive: true }); + const name = subpath.split('/').slice(-1)[0]; + const tree = { name, children }; + let samples = { + _base: hosturl + }; + + const files = tree.children.filter((path) => { + const extension = path.match(/\.[0-9a-z]+$/i)?.[0] + return validFileTypes.has(extension) + }).sort((a, b) => a.localeCompare(b)) + + files.forEach(path => { + const groupName = path.match(/([^/]+)\/[^/]+$/)[1] + samples[groupName] = samples[groupName] ?? [] + samples[groupName].push(path) + }) + const json = JSON.stringify(samples, null, 2); + const filepath = subpath + '/strudel.json'; + await fs.promises.writeFile(filepath, json); + console.log(`wrote strudel.json with ${files.length} samples to ${subpath}`); +} + +const rl = createInterface({ + input: process.stdin, + output: process.stdout, +}); + +rl.question(`enter sample folder path: `, async path => { + rl.question(`enter hosted URL: `, async hosturl => { + const githubregex = /github\.com\/([^/]+)\/([^/]+)/; + const githubparams = hosturl.match(githubregex); + if (githubparams) { + const username = githubparams[1]; + const repo = githubparams[2]; + + hosturl = `https://raw.githubusercontent.com/${username}/${repo}/main/` + console.log('reformatting github URL to: ', hosturl ) + } + + await writeStrudelJson(path, hosturl) + rl.close(); + }) + +}); \ No newline at end of file diff --git a/website/src/repl/files.mjs b/website/src/repl/files.mjs index bd83f071d..8e74b9957 100644 --- a/website/src/repl/files.mjs +++ b/website/src/repl/files.mjs @@ -33,7 +33,7 @@ async function loadStrudelJson(subpath) { }); } -async function writeStrudelJson(subpath) { +export async function writeStrudelJson(subpath) { const children = await readDir(subpath, { dir, recursive: true }); const name = subpath.split('/').slice(-1)[0]; const tree = { name, children }; @@ -41,7 +41,7 @@ async function writeStrudelJson(subpath) { let samples = {}; let count = 0; walkFileTree(tree, (entry, parent) => { - if (['wav', 'mp3'].includes(entry.name.split('.').slice(-1)[0])) { + if (['wav', 'mp3', 'aac', 'm4a'].includes(entry.name.split('.').slice(-1)[0])) { samples[parent.name] = samples[parent.name] || []; count += 1; samples[parent.name].push(entry.subpath.slice(1).concat([entry.name]).join('/'));