From 32c07bee443e2a83030bd873ac931048f67c1edf Mon Sep 17 00:00:00 2001 From: Tyom Semonov Date: Sat, 27 Jul 2024 23:30:49 +0100 Subject: [PATCH] Move JS out of data/ --- data/index.js => src/lib/data.js | 23 +++++------------------ src/routes/+page.server.js | 13 +++++++++++-- 2 files changed, 16 insertions(+), 20 deletions(-) rename data/index.js => src/lib/data.js (68%) diff --git a/data/index.js b/src/lib/data.js similarity index 68% rename from data/index.js rename to src/lib/data.js index f2eb277..7ba78ba 100644 --- a/data/index.js +++ b/src/lib/data.js @@ -1,13 +1,12 @@ -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; import { globSync } from 'glob'; import { isPlainObject } from 'is-plain-object'; -import { basename } from 'path'; -import { parseYaml } from '$lib/yaml'; -import { createDefinitionFinder } from '$lib/utils'; +import { parseYaml } from '$lib/yaml.js'; +import { createDefinitionFinder } from '$lib/utils.js'; const DEFINITIONS_YAML_FILE = 'data/definitions.yaml'; -const allYamlFiles = globSync('data/*.yaml', { +export const allYamlFiles = globSync('data/*.yaml', { ignore: DEFINITIONS_YAML_FILE, }); @@ -52,16 +51,4 @@ export async function linkWithDefinitions(yamlData) { }); } -const readYaml = (path) => parseYaml(readFileSync(path)); - -export async function getData(filePaths = allYamlFiles) { - const yamlData = await Promise.all(filePaths.map(readYaml)); - const linkedData = await linkWithDefinitions(yamlData); - - const entries = filePaths.map((path, i) => [ - basename(path, '.yaml'), - linkedData[i], - ]); - - return Object.fromEntries(entries); -} +export const readYaml = (path) => parseYaml(readFileSync(path)); diff --git a/src/routes/+page.server.js b/src/routes/+page.server.js index ab97a4e..1100702 100644 --- a/src/routes/+page.server.js +++ b/src/routes/+page.server.js @@ -1,6 +1,15 @@ -import { getData } from '$data'; +import { allYamlFiles, linkWithDefinitions, readYaml } from '$lib/data'; +import { basename } from 'node:path'; /** @type {import('./$types').PageServerLoad} */ export async function load() { - return getData(); + const yamlData = await Promise.all(allYamlFiles.map(readYaml)); + const linkedData = await linkWithDefinitions(yamlData); + + const entries = allYamlFiles.map((path, i) => [ + basename(path, '.yaml'), + linkedData[i], + ]); + + return Object.fromEntries(entries); }