Skip to content

Commit 55315e0

Browse files
committed
PR cleanup
1 parent 760a6f2 commit 55315e0

File tree

6 files changed

+90
-97
lines changed

6 files changed

+90
-97
lines changed

src/components/ApiDocs/ReferencePage.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { Fragment } from 'react';
22

33
import { FunctionReference } from './FunctionReference';
44
import { Divider, View, Flex } from '@aws-amplify/ui-react';
5-
import { packageCategories } from '@/data/api-categories.mjs';
5+
import { API_CATEGORIES, API_SUB_CATEGORIES } from '@/data/api-categories.mjs';
66
import references from '@/directory/apiReferences/amplify-js.json';
77
import { MDXHeading } from '../MDXComponents';
88

9-
const { API_CATEGORIES, API_SUB_CATEGORIES } = packageCategories['amplify-js'];
10-
119
export const ReferencePage = ({ category }) => {
1210
category = API_CATEGORIES[category] || API_SUB_CATEGORIES[category];
1311
const cat = references['categories'].find(

src/data/api-categories.mjs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// mapping of api categories coming in from libraries to the associated categories in the docs
2-
export const packageCategories = {
3-
'amplify-js': {
4-
ROOT_PACKAGE: 'aws-amplify',
5-
API_CATEGORIES: {
6-
auth: 'auth',
7-
storage: 'storage'
8-
},
9-
API_SUB_CATEGORIES: {
10-
analytics: 'analytics',
11-
'rest-api': 'api',
12-
'in-app-messaging': 'in-app-messaging'
13-
}
14-
}
2+
export const API_CATEGORIES = {
3+
auth: 'auth',
4+
storage: 'storage'
155
};
6+
7+
export const API_SUB_CATEGORIES = {
8+
analytics: 'analytics',
9+
'rest-api': 'api',
10+
'in-app-messaging': 'in-app-messaging'
11+
};
12+
13+
export const ROOT_PACKAGE = 'amplify-js';

src/data/process-typedoc.mjs

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/directory/generateDirectory.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import JSON5 from 'json5';
66
import { directory } from './directory.mjs';
77
import { writeFile } from 'fs/promises';
88
import { getLastModifiedDate } from 'git-jiggy';
9-
import { packageCategories } from '../data/api-categories.mjs';
10-
11-
const { API_CATEGORIES, API_SUB_CATEGORIES } = packageCategories['amplify-js'];
9+
import { API_CATEGORIES, API_SUB_CATEGORIES } from '../data/api-categories.mjs';
1210

1311
// Set up the root path so that we can get the correct path from the current working directory
1412
const rootPath = path.resolve(cwd(), 'src/pages');

src/utils/getApiStaticPath.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { packageCategories } from '@/data/api-categories';
1+
import { API_CATEGORIES, API_SUB_CATEGORIES } from '@/data/api-categories.mjs';
22
import { JS_PLATFORMS } from '@/data/platforms';
33

4-
const { API_CATEGORIES, API_SUB_CATEGORIES } = packageCategories['amplify-js'];
5-
64
export const getApiStaticPath = (sub) => {
75
const paths: any = [];
86

tasks/clean-references.mjs

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { writeFileSync, readFileSync } from 'fs';
2-
import { packageCategories } from '../src/data/api-categories.mjs';
3-
import { processReferences } from '../src/data/process-typedoc.mjs';
2+
import {
3+
API_CATEGORIES,
4+
API_SUB_CATEGORIES,
5+
ROOT_PACKAGE
6+
} from '../src/data/api-categories.mjs';
47

58
/**
69
* The purpose of this script is to create generate an object that only contains
@@ -28,9 +31,6 @@ const packageName = process.argv[packageIndex + 1];
2831

2932
const referencesFile = readFileSync(`../${packageName}/docs/reference.json`);
3033

31-
const { API_CATEGORIES, API_SUB_CATEGORIES, ROOT_PACKAGE } =
32-
packageCategories[packageName];
33-
3434
const references = processReferences(JSON.parse(referencesFile), ROOT_PACKAGE);
3535
const cleanReferences = {};
3636
const categoryNodes = [];
@@ -155,6 +155,77 @@ function recursivelyPopulateNodes(node) {
155155
}
156156
}
157157

158+
function processReferences(references, rootPackage) {
159+
// build flat object for easier faster lookups
160+
const flatReferences = {};
161+
162+
const recursivelyPopulateFlatObject = (referenceObject) => {
163+
if (!referenceObject) return;
164+
if (referenceObject['id']) {
165+
const copy = recursivelyStripObject(structuredClone(referenceObject));
166+
flatReferences[referenceObject['id']] = copy;
167+
}
168+
169+
for (let key in referenceObject) {
170+
if (referenceObject.hasOwnProperty(key)) {
171+
if (Array.isArray(referenceObject[key])) {
172+
referenceObject[key].forEach((child) => {
173+
recursivelyPopulateFlatObject(child);
174+
});
175+
} else if (
176+
typeof referenceObject[key] === 'object' &&
177+
referenceObject[key] !== null
178+
) {
179+
recursivelyPopulateFlatObject(referenceObject[key]);
180+
}
181+
}
182+
}
183+
};
184+
185+
const recursivelyStripObject = (referenceObject) => {
186+
for (let key in referenceObject) {
187+
if (referenceObject.hasOwnProperty(key)) {
188+
if (Array.isArray(referenceObject[key])) {
189+
referenceObject[key] = referenceObject[key].map((child) => {
190+
return child.id || child;
191+
});
192+
} else if (
193+
typeof referenceObject[key] === 'object' &&
194+
referenceObject[key] !== null
195+
) {
196+
recursivelyStripObject(referenceObject[key]);
197+
}
198+
}
199+
}
200+
return referenceObject;
201+
};
202+
203+
const isFunctionObject = (obj) => {
204+
return obj.kind === 64 && obj.variant === 'declaration';
205+
};
206+
recursivelyPopulateFlatObject(references);
207+
208+
const rootId = Object.keys(flatReferences).find(
209+
(id) => flatReferences[id].name == rootPackage
210+
);
211+
212+
flatReferences['categories'] = flatReferences[rootId]?.children?.map(
213+
(catId) => {
214+
const cat = structuredClone(flatReferences[catId]);
215+
if (cat.children && Array.isArray(cat.children)) {
216+
cat.children = cat.children
217+
.map((childId) => flatReferences[childId])
218+
.filter((child) => {
219+
return isFunctionObject(child);
220+
});
221+
}
222+
return cat;
223+
}
224+
);
225+
226+
return flatReferences;
227+
}
228+
158229
const categories = Object.values(API_CATEGORIES).concat(
159230
Object.values(API_SUB_CATEGORIES)
160231
);

0 commit comments

Comments
 (0)