Skip to content

Commit

Permalink
chore: formatting and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
delanni committed Dec 15, 2023
1 parent 2b5159e commit bc9af79
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions scripts/compile.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const fs = require("fs");
const path = require("path");

/*
This script compiles individual sketch files from the sketches folder to a single sketches.json file.
*/

async function main() {
const sketches = [];
const sketchesFolder = path.join(__dirname, '../sketches');
const sketchesFolder = path.join(__dirname, "../sketches");
const sketchesJsonPath = path.join(__dirname, "../sketches.json");
const sketchesFiles = fs.readdirSync(sketchesFolder);

for (let sketchFile of sketchesFiles) {
const sketchFilePath = path.join(sketchesFolder, sketchFile);
const sketchFileContent = fs.readFileSync(sketchFilePath, 'utf8');
const sketchFileLines = sketchFileContent.split('\n');
const sketchName = sketchFileLines[0].replace('/*', '').replace('*/', '').trim();
const sketchCode = sketchFileLines.slice(1, -1).join('\n');
const sketchFileContent = fs.readFileSync(sketchFilePath, "utf8");
const sketchFileLines = sketchFileContent.split("\n");
const sketchName = sketchFileLines[0]
.replace("/*", "")
.replace("*/", "")
.trim();
const sketchCode = sketchFileLines.slice(1, -1).join("\n");

let sketchMetaData = {};
try {
sketchMetadata = JSON.parse(sketchFileLines[sketchFileLines.length - 1].replace('metadata = ', ''));
sketchMetadata = JSON.parse(
sketchFileLines[sketchFileLines.length - 1].replace("metadata = ", "")
);
} catch (err) {
sketchMetaData = {
bpm: sketchCode.match(/bpm\s*=(\d+)/)?.[1] || 120,
Expand All @@ -29,14 +36,14 @@ async function main() {
}
sketches.push({
name: sketchName,
type: 'code',
type: "code",
code: sketchFileContent,
index: sketchMetadata.index ?? (10000 + sketches.length),
index: sketchMetadata.index ?? 10000 + sketches.length,
bpm: sketchMetadata.bpm,
midi: sketchMetadata.midi,
local: false,
heat: sketchMetadata.heat || 0,
author: sketchMetadata.author || 'Anony Mouse',
author: sketchMetadata.author || "Anony Mouse",
});
sketches.sort((a, b) => a.index - b.index);

Expand All @@ -45,10 +52,11 @@ async function main() {
sketches[i].index = i;
}
}
fs.writeFileSync(path.join(__dirname, '../sketches.json'), JSON.stringify(sketches, null, 2));

fs.writeFileSync(sketchesJsonPath, JSON.stringify(sketches, null, 2));
}

main().catch(err => {
main().catch((err) => {
console.error(err);
process.exit(1);
});
});

0 comments on commit bc9af79

Please sign in to comment.