Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/directory/generateDirectory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import JSON5 from 'json5';
import { directory } from './directory.mjs';
import { writeFile } from 'fs/promises';
import { getLastModifiedDate } from 'git-jiggy';
import { API_CATEGORIES, API_SUB_CATEGORIES } from '../data/api-categories.mjs';

// Set up the root path so that we can get the correct path from the current working directory
const rootPath = path.resolve(cwd(), 'src/pages');
Expand Down Expand Up @@ -117,11 +118,64 @@ async function traverseDirectoryObject(directoryNode) {
}
}

const findDirectoryNode = (route, dir) => {
if (dir.route === route) {
return dir;
} else if (dir.children && dir.children.length) {
for (let i = 0; i < dir.children.length; i++) {
const child = dir.children[i];
const res = findDirectoryNode(route, child);
if (res) return res;
}
}

return null;
};

async function generateDirectory() {
const directoryCopy = { ...directory };

await traverseDirectoryObject(directoryCopy);

// Add directory entries into the generated directory
// file for any api reference categories found
const JS_PLATFORMS = [
'angular',
'javascript',
'nextjs',
'react',
'react-native',
'vue'
];
const categoryKeys = Object.keys(API_CATEGORIES);
categoryKeys.forEach((cat) => {
const name = API_CATEGORIES[cat];
const route = `/[platform]/build-a-backend/${cat}`;
const catNode = findDirectoryNode(route, directoryCopy);
if (catNode) {
catNode.children.push({
title: `API References`,
description: `API References - ${name}`,
platforms: JS_PLATFORMS,
route: `${route}/reference`
});
}
});

Object.keys(API_SUB_CATEGORIES).forEach((cat) => {
const name = API_SUB_CATEGORIES[cat];
const route = `/[platform]/build-a-backend/add-aws-services/${cat}`;
const catNode = findDirectoryNode(route, directoryCopy);
if (catNode) {
catNode.children.push({
title: `API References`,
description: `API References - ${name}`,
platforms: JS_PLATFORMS,
route: `${route}/reference`
});
}
});

try {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down