-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplesToJSON.cjs
More file actions
32 lines (27 loc) · 922 Bytes
/
samplesToJSON.cjs
File metadata and controls
32 lines (27 loc) · 922 Bytes
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
const dirTree = require("directory-tree");
const fs = require('fs');
const tree = dirTree("./static/samples");
function formatItem(name, url) {
const [, ext] = name.split(".");
const group = url.split("/")[url.split("/").length - 2];
return ['wav', 'aif', 'mp3'].includes(ext)
? { group, url: '/' + url.replace('static/', '') }
: false;
}
function compile(array) {
return array
.map(({path, name, children}) => children
? compile(children)
: formatItem(name, path)
)
.filter(path => path)
.flat(128)
}
const result = compile(tree.children)
.reduce((arr, item) => ([...arr, item.url]), [])
const json = JSON.stringify(result);
fs.writeFile('./static/samples.json', json, 'utf8', (err) => {
err
? console.log(`Error writing samples file: ${err}`)
: console.log(`Samples file is written successfully!`);
});