forked from aws-amplify/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19ab4f4
commit ed525ad
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import references from '../src/references/apiReferences.json' assert { type: 'json' }; | ||
import { writeFileSync } from 'fs'; | ||
import { | ||
API_CATEGORIES, | ||
API_SUB_CATEGORIES | ||
} from '../src/data/api-categories.mjs'; | ||
|
||
const cleanReferences = {}; | ||
const categoryNodes = []; | ||
|
||
const categories = Object.values(API_CATEGORIES).concat( | ||
Object.values(API_SUB_CATEGORIES) | ||
); | ||
|
||
function recursivelyPopulateNodes(node) { | ||
if (!node) return; | ||
if (node.id) { | ||
if (cleanReferences.hasOwnProperty(node.id)) return; | ||
cleanReferences[node.id] = node; | ||
} | ||
if (node.target && typeof node.target === 'number') { | ||
recursivelyPopulateNodes(references[node.target]); | ||
} | ||
if (node.type) { | ||
if (typeof node.type === 'number') { | ||
const nextNode = references[node.type]; | ||
recursivelyPopulateNodes(nextNode); | ||
} else if (typeof node.type === 'object') { | ||
recursivelyPopulateNodes(node.type); | ||
} | ||
} | ||
if (node.typeArguments && Array.isArray(node.typeArguments)) { | ||
node.typeArguments.forEach((arg) => { | ||
if (typeof arg === 'number') { | ||
recursivelyPopulateNodes(references[arg]); | ||
} else if (typeof arg === 'object' && arg !== null) { | ||
recursivelyPopulateNodes(arg); | ||
} | ||
}); | ||
} | ||
if (node.types && Array.isArray(node.types)) { | ||
node.types.forEach((arg) => { | ||
if (typeof arg === 'number') { | ||
recursivelyPopulateNodes(references[arg]); | ||
} else if (typeof arg === 'object' && arg !== null) { | ||
recursivelyPopulateNodes(arg); | ||
} | ||
}); | ||
} | ||
if ( | ||
node.declaration && | ||
node.declaration.children && | ||
Array.isArray(node.declaration.children) | ||
) { | ||
node.declaration.children.forEach((arg) => { | ||
if (typeof arg === 'number') { | ||
recursivelyPopulateNodes(references[arg]); | ||
} else if (typeof arg === 'object' && arg !== null) { | ||
recursivelyPopulateNodes(arg); | ||
} | ||
}); | ||
} | ||
if (node.elementType && node.elementType.target) { | ||
const elemType = node.elementType.target; | ||
if (typeof elemType === 'number') { | ||
recursivelyPopulateNodes(references[elemType]); | ||
} else if (typeof elemType === 'object' && elemType !== null) { | ||
recursivelyPopulateNodes(elemType); | ||
} | ||
} | ||
if (node.children && Array.isArray(node.children)) { | ||
node.children.forEach((arg) => { | ||
if (typeof arg === 'number') { | ||
recursivelyPopulateNodes(references[arg]); | ||
} else if (typeof arg === 'object' && arg !== null) { | ||
recursivelyPopulateNodes(arg); | ||
} | ||
}); | ||
} | ||
if (node.signatures && Array.isArray(node.signatures)) { | ||
node.signatures.forEach((arg) => { | ||
if (typeof arg === 'number') { | ||
recursivelyPopulateNodes(references[arg]); | ||
} else if (typeof arg === 'object' && arg !== null) { | ||
recursivelyPopulateNodes(arg); | ||
} | ||
}); | ||
} | ||
if (node.parameters && Array.isArray(node.parameters)) { | ||
node.parameters.forEach((arg) => { | ||
if (typeof arg === 'number') { | ||
recursivelyPopulateNodes(references[arg]); | ||
} else if (typeof arg === 'object' && arg !== null) { | ||
recursivelyPopulateNodes(arg); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Ids to look for and populate are | ||
* target | ||
* typeArguments[] | ||
* types[] | ||
* declaration->children[] | ||
* elementType -> target | ||
* children[] | ||
* signatures[] | ||
* parameters[] | ||
*/ | ||
} | ||
|
||
categories.forEach((catName) => { | ||
const catNode = references.categories.filter((cat) => { | ||
return cat.name === catName; | ||
})[0]; | ||
|
||
if (catNode) { | ||
categoryNodes.push(catNode); | ||
recursivelyPopulateNodes(catNode); | ||
} | ||
}); | ||
|
||
cleanReferences['categories'] = categoryNodes; | ||
|
||
try { | ||
writeFileSync( | ||
'src/references/cleanReferences.json', | ||
JSON.stringify(cleanReferences, null, 2), | ||
'utf8' | ||
); | ||
console.log('Data successfully saved to disk'); | ||
} catch (error) { | ||
console.log('An error has occurred ', error); | ||
} |