diff --git a/my-patterns/README.md b/my-patterns/README.md index 153d198c..63d2b3f4 100644 --- a/my-patterns/README.md +++ b/my-patterns/README.md @@ -6,3 +6,10 @@ This directory can be used to save your own patterns. 1. Save one or more .txt files here. 2. and run `npm run repl` 3. open `http://localhost:3000/my-patterns` ! + +## deploy + +1. in your fork, go to settings -> pages and select "Github Actions" as source +2. edit `website/public/CNAME` to contain `.github.io/strudel` +3. go to Actions -> "Build and Deploy" and click "Run workflow" +4. view your patterns at `.github.io/strudel/my-patterns` diff --git a/website/src/pages/my-patterns/index.astro b/website/src/pages/my-patterns/index.astro index 15ea7da4..c73e2572 100644 --- a/website/src/pages/my-patterns/index.astro +++ b/website/src/pages/my-patterns/index.astro @@ -17,7 +17,10 @@ const myPatterns = await getMyPatterns();
{ Object.entries(myPatterns).map(([name, tune]) => ( - +
{name}
diff --git a/website/src/pages/my-patterns/list.json.js b/website/src/pages/my-patterns/list.json.js index e310eacf..6d86e384 100644 --- a/website/src/pages/my-patterns/list.json.js +++ b/website/src/pages/my-patterns/list.json.js @@ -1,36 +1,13 @@ -import fs from 'fs'; -import path from 'path'; -import { dirname } from 'path'; -import { fileURLToPath } from 'url'; - -const __dirname = dirname(fileURLToPath(import.meta.url)); - -async function readTextFiles(folder) { - const absolutePath = path.resolve(__dirname, folder); - - // Use `fs.promises.readdir()` to get a list of all the files in the folder - const files = await fs.promises.readdir(absolutePath); - - // Filter the list of files to only include those with a `.txt` extension - const textFiles = files.filter((file) => file.endsWith('.txt')); - // Initialize an empty object to store the file contents - const fileContents = {}; - - // Use `fs.promises.readFile()` to read the contents of each text file - for (const file of textFiles) { - const filePath = `${absolutePath}/${file}`; - const data = await fs.promises.readFile(filePath, 'utf8'); - fileContents[file] = data; - } - // Return the object with the filenames as keys and the file contents as values - return fileContents; -} - export function getMyPatterns() { - return readTextFiles('../../../../my-patterns'); + const my = import.meta.glob('../../../../my-patterns/**', { as: 'raw', eager: true }); + return Object.fromEntries( + Object.entries(my) // + .filter(([name]) => name.endsWith('.txt')) // + .map(([name, raw]) => [name.split('/').slice(-1), raw]), // + ); } -export async function get({ params, request }) { +export async function get() { const all = await getMyPatterns(); return { body: JSON.stringify(all),