Skip to content

Commit

Permalink
Move JS out of data/
Browse files Browse the repository at this point in the history
  • Loading branch information
tyom committed Jul 29, 2024
1 parent 9c45f7a commit 32c07be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
23 changes: 5 additions & 18 deletions data/index.js → src/lib/data.js
Original file line number Diff line number Diff line change
@@ -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,
});

Expand Down Expand Up @@ -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));
13 changes: 11 additions & 2 deletions src/routes/+page.server.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 32c07be

Please sign in to comment.