Push new promise-based infra

pull/8873/head
Puru Vijay 3 years ago
parent 5cf4d09445
commit deb35b0768

@ -1,6 +1,5 @@
// @ts-check // @ts-check
import { extractFrontmatter } from '@sveltejs/site-kit/markdown'; import { extractFrontmatter } from '@sveltejs/site-kit/markdown';
import fs from 'node:fs';
import { CONTENT_BASE_PATHS } from '../../../constants.js'; import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { render_content } from '../renderer.js'; import { render_content } from '../renderer.js';
@ -23,16 +22,18 @@ export async function get_processed_blog_post(blog_data, slug) {
const BLOG_NAME_REGEX = /^(\d{4}-\d{2}-\d{2})-(.+)\.md$/; const BLOG_NAME_REGEX = /^(\d{4}-\d{2}-\d{2})-(.+)\.md$/;
/** @returns {import('./types').BlogData} */ /** @returns {Promise<import('./types').BlogData>} */
export function get_blog_data(base = CONTENT_BASE_PATHS.BLOG) { export async function get_blog_data(base = CONTENT_BASE_PATHS.BLOG) {
const { readdir, readFile } = await import('node:fs/promises');
/** @type {import('./types').BlogData} */ /** @type {import('./types').BlogData} */
const blog_posts = []; const blog_posts = [];
for (const file of fs.readdirSync(base).reverse()) { for (const file of (await readdir(base)).reverse()) {
if (!BLOG_NAME_REGEX.test(file)) continue; if (!BLOG_NAME_REGEX.test(file)) continue;
const { date, date_formatted, slug } = get_date_and_slug(file); const { date, date_formatted, slug } = get_date_and_slug(file);
const { metadata, body } = extractFrontmatter(fs.readFileSync(`${base}/${file}`, 'utf-8')); const { metadata, body } = extractFrontmatter(await readFile(`${base}/${file}`, 'utf-8'));
blog_posts.push({ blog_posts.push({
date, date,

@ -6,7 +6,6 @@ import {
normalizeSlugify, normalizeSlugify,
removeMarkdown removeMarkdown
} from '@sveltejs/site-kit/markdown'; } from '@sveltejs/site-kit/markdown';
import fs from 'node:fs';
import { CONTENT_BASE_PATHS } from '../../../constants.js'; import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { render_content } from '../renderer'; import { render_content } from '../renderer';
@ -29,12 +28,14 @@ export async function get_parsed_docs(docs_data, slug) {
return null; return null;
} }
/** @return {import('./types').DocsData} */ /** @return {Promise<import('./types').DocsData>} */
export function get_docs_data(base = CONTENT_BASE_PATHS.DOCS) { export async function get_docs_data(base = CONTENT_BASE_PATHS.DOCS) {
const { readdir, readFile } = await import('node:fs/promises');
/** @type {import('./types').DocsData} */ /** @type {import('./types').DocsData} */
const docs_data = []; const docs_data = [];
for (const category_dir of fs.readdirSync(base)) { for (const category_dir of await readdir(base)) {
const match = /\d{2}-(.+)/.exec(category_dir); const match = /\d{2}-(.+)/.exec(category_dir);
if (!match) continue; if (!match) continue;
@ -42,7 +43,7 @@ export function get_docs_data(base = CONTENT_BASE_PATHS.DOCS) {
// Read the meta.json // Read the meta.json
const { title: category_title, draft = 'false' } = JSON.parse( const { title: category_title, draft = 'false' } = JSON.parse(
fs.readFileSync(`${base}/${category_dir}/meta.json`, 'utf-8') await readFile(`${base}/${category_dir}/meta.json`, 'utf-8')
); );
if (draft === 'true') continue; if (draft === 'true') continue;
@ -54,7 +55,7 @@ export function get_docs_data(base = CONTENT_BASE_PATHS.DOCS) {
pages: [] pages: []
}; };
for (const filename of fs.readdirSync(`${base}/${category_dir}`)) { for (const filename of await readdir(`${base}/${category_dir}`)) {
if (filename === 'meta.json') continue; if (filename === 'meta.json') continue;
const match = /\d{2}-(.+)/.exec(filename); const match = /\d{2}-(.+)/.exec(filename);
if (!match) continue; if (!match) continue;
@ -62,7 +63,7 @@ export function get_docs_data(base = CONTENT_BASE_PATHS.DOCS) {
const page_slug = match[1].replace('.md', ''); const page_slug = match[1].replace('.md', '');
const page_data = extractFrontmatter( const page_data = extractFrontmatter(
fs.readFileSync(`${base}/${category_dir}/${filename}`, 'utf-8') await readFile(`${base}/${category_dir}/${filename}`, 'utf-8')
); );
if (page_data.metadata.draft === 'true') continue; if (page_data.metadata.draft === 'true') continue;

@ -1,5 +1,4 @@
import { extractFrontmatter } from '@sveltejs/site-kit/markdown'; import { extractFrontmatter } from '@sveltejs/site-kit/markdown';
import fs from 'node:fs';
import { CONTENT_BASE_PATHS } from '../../../constants.js'; import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { render_content } from '../renderer.js'; import { render_content } from '../renderer.js';
@ -23,24 +22,27 @@ export async function get_parsed_tutorial(tutorial_data, slug) {
} }
/** /**
* @returns {import('./types').TutorialData} * @returns {Promise<import('./types').TutorialData>}
*/ */
export function get_tutorial_data(base = CONTENT_BASE_PATHS.TUTORIAL) { export async function get_tutorial_data(base = CONTENT_BASE_PATHS.TUTORIAL) {
const { readdir, readFile, stat } = await import('node:fs/promises');
const tutorials = []; const tutorials = [];
for (const subdir of fs.readdirSync(base)) { for (const subdir of await readdir(base)) {
const section = { const section = {
title: '', // Initialise with empty title: '', // Initialise with empty
slug: subdir.split('-').slice(1).join('-'), slug: subdir.split('-').slice(1).join('-'),
tutorials: [] tutorials: []
}; };
if (!(fs.statSync(`${base}/${subdir}`).isDirectory() || subdir.endsWith('meta.json'))) continue; if (!((await stat(`${base}/${subdir}`)).isDirectory() || subdir.endsWith('meta.json')))
continue;
if (!subdir.endsWith('meta.json')) if (!subdir.endsWith('meta.json'))
section.title = JSON.parse(fs.readFileSync(`${base}/${subdir}/meta.json`, 'utf-8')).title; section.title = JSON.parse(await readFile(`${base}/${subdir}/meta.json`, 'utf-8')).title;
for (const section_dir of fs.readdirSync(`${base}/${subdir}`)) { for (const section_dir of await readdir(`${base}/${subdir}`)) {
const match = /\d{2}-(.+)/.exec(section_dir); const match = /\d{2}-(.+)/.exec(section_dir);
if (!match) continue; if (!match) continue;
@ -49,22 +51,22 @@ export function get_tutorial_data(base = CONTENT_BASE_PATHS.TUTORIAL) {
const tutorial_base_dir = `${base}/${subdir}/${section_dir}`; const tutorial_base_dir = `${base}/${subdir}/${section_dir}`;
// Read the file, get frontmatter // Read the file, get frontmatter
const contents = fs.readFileSync(`${tutorial_base_dir}/text.md`, 'utf-8'); const contents = await readFile(`${tutorial_base_dir}/text.md`, 'utf-8');
const { metadata, body } = extractFrontmatter(contents); const { metadata, body } = extractFrontmatter(contents);
// Get the contents of the apps. // Get the contents of the apps.
const completion_states_data = { initial: [], complete: [] }; const completion_states_data = { initial: [], complete: [] };
for (const app_dir of fs.readdirSync(tutorial_base_dir)) { for (const app_dir of await readdir(tutorial_base_dir)) {
if (!app_dir.startsWith('app-')) continue; if (!app_dir.startsWith('app-')) continue;
const app_dir_path = `${tutorial_base_dir}/${app_dir}`; const app_dir_path = `${tutorial_base_dir}/${app_dir}`;
const app_contents = fs.readdirSync(app_dir_path, 'utf-8'); const app_contents = await readdir(app_dir_path, 'utf-8');
for (const file of app_contents) { for (const file of app_contents) {
completion_states_data[app_dir === 'app-a' ? 'initial' : 'complete'].push({ completion_states_data[app_dir === 'app-a' ? 'initial' : 'complete'].push({
name: file, name: file,
type: file.split('.').at(-1), type: file.split('.').at(-1),
content: fs.readFileSync(`${app_dir_path}/${file}`, 'utf-8') content: await readFile(`${app_dir_path}/${file}`, 'utf-8')
}); });
} }
} }

@ -4,6 +4,6 @@ export const prerender = true;
export async function load() { export async function load() {
return { return {
posts: get_blog_list(get_blog_data()) posts: get_blog_list(await get_blog_data())
}; };
} }

@ -4,7 +4,7 @@ import { error } from '@sveltejs/kit';
export const prerender = true; export const prerender = true;
export async function load({ params }) { export async function load({ params }) {
const post = get_processed_blog_post(get_blog_data(), params.slug); const post = get_processed_blog_post(await get_blog_data(), params.slug);
if (!post) throw error(404); if (!post) throw error(404);

@ -11,8 +11,8 @@ const width = 1200;
export const prerender = true; export const prerender = true;
export const GET = async ({ params }) => { export async function GET({ params }) {
const post = await get_processed_blog_post(get_blog_data(), params.slug); const post = await get_processed_blog_post(await get_blog_data(), params.slug);
if (!post) throw error(404); if (!post) throw error(404);
@ -48,4 +48,4 @@ export const GET = async ({ params }) => {
'cache-control': 'public, max-age=600' // cache for 10 minutes 'cache-control': 'public, max-age=600' // cache for 10 minutes
} }
}); });
}; }

@ -58,7 +58,7 @@ const get_rss = (posts) =>
.trim(); .trim();
export async function GET() { export async function GET() {
const posts = get_blog_list(get_blog_data()); const posts = get_blog_list(await get_blog_data());
return new Response(get_rss(posts), { return new Response(get_rss(posts), {
headers: { headers: {

@ -10,6 +10,6 @@ export async function load({ url }) {
const { get_docs_data, get_docs_list } = await import('$lib/server/docs/index.js'); const { get_docs_data, get_docs_list } = await import('$lib/server/docs/index.js');
return { return {
sections: get_docs_list(get_docs_data()) sections: get_docs_list(await get_docs_data())
}; };
} }

@ -4,7 +4,7 @@ import { error } from '@sveltejs/kit';
export const prerender = true; export const prerender = true;
export async function load({ params }) { export async function load({ params }) {
const processed_page = await get_parsed_docs(get_docs_data(), params.slug); const processed_page = await get_parsed_docs(await get_docs_data(), params.slug);
if (!processed_page) throw error(404); if (!processed_page) throw error(404);

@ -14,13 +14,16 @@ export const GET = async () => {
* @returns {Promise<import('@sveltejs/site-kit').NavigationLink[]>} * @returns {Promise<import('@sveltejs/site-kit').NavigationLink[]>}
*/ */
async function get_nav_list() { async function get_nav_list() {
const docs_list = get_docs_list(get_docs_data()); const [docs_list, blog_list] = await Promise.all([
get_docs_list(await get_docs_data()),
get_blog_list(await get_blog_data())
]);
const processed_docs_list = docs_list.map(({ title, pages }) => ({ const processed_docs_list = docs_list.map(({ title, pages }) => ({
title, title,
sections: pages.map(({ title, path }) => ({ title, path })) sections: pages.map(({ title, path }) => ({ title, path }))
})); }));
const blog_list = get_blog_list(get_blog_data());
const processed_blog_list = [ const processed_blog_list = [
{ {
title: 'Blog', title: 'Blog',

@ -10,7 +10,7 @@ export const prerender = true;
export async function load({ params }) { export async function load({ params }) {
if (params.slug === 'local-transitions') throw redirect(307, '/tutorial/global-transitions'); if (params.slug === 'local-transitions') throw redirect(307, '/tutorial/global-transitions');
const tutorial_data = get_tutorial_data(); const tutorial_data = await get_tutorial_data();
const tutorials_list = get_tutorial_list(tutorial_data); const tutorials_list = get_tutorial_list(tutorial_data);
const tutorial = await get_parsed_tutorial(tutorial_data, params.slug); const tutorial = await get_parsed_tutorial(tutorial_data, params.slug);
@ -25,7 +25,7 @@ export async function load({ params }) {
} }
export async function entries() { export async function entries() {
const tutorials_list = get_tutorial_list(get_tutorial_data()); const tutorials_list = get_tutorial_list(await get_tutorial_data());
const slugs = tutorials_list const slugs = tutorials_list
.map(({ tutorials }) => tutorials) .map(({ tutorials }) => tutorials)
.flatMap((val) => val.map(({ slug }) => ({ slug }))); .flatMap((val) => val.map(({ slug }) => ({ slug })));

@ -4,7 +4,10 @@ import adapter from '@sveltejs/adapter-vercel';
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
export default { export default {
kit: { kit: {
adapter: adapter({ runtime: 'edge' }) adapter: adapter({ runtime: 'edge' }),
prerender: {
concurrency: 3
}
}, },
vitePlugin: { vitePlugin: {

Loading…
Cancel
Save