-
-
Notifications
You must be signed in to change notification settings - Fork 692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: format build scripts using eslint #3401
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
const { resolve } = require('path'); | ||
const writeJSON = require('../utils/readAndWriteJson.js') | ||
const writeJSON = require('../utils/readAndWriteJson.js'); | ||
|
||
module.exports = async function buildAdoptersList() { | ||
writeJSON('config/adopters.yml',resolve(__dirname, '../../config', 'adopters.json')); | ||
writeJSON( | ||
'config/adopters.yml', | ||
resolve(__dirname, '../../config', 'adopters.json'), | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,26 +1,40 @@ | ||||||||||||||||
const sortBy = require('lodash/sortBy') | ||||||||||||||||
const sortBy = require('lodash/sortBy'); | ||||||||||||||||
function buildNavTree(navItems) { | ||||||||||||||||
try { | ||||||||||||||||
const tree = { | ||||||||||||||||
'welcome': { | ||||||||||||||||
item: { title: 'Welcome', weight: 0, isRootSection: true, isSection: true, rootSectionId: 'welcome', sectionWeight: 0, slug: '/docs' }, | ||||||||||||||||
children: {} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
welcome: { | ||||||||||||||||
item: { | ||||||||||||||||
title: 'Welcome', | ||||||||||||||||
weight: 0, | ||||||||||||||||
isRootSection: true, | ||||||||||||||||
isSection: true, | ||||||||||||||||
rootSectionId: 'welcome', | ||||||||||||||||
sectionWeight: 0, | ||||||||||||||||
slug: '/docs', | ||||||||||||||||
}, | ||||||||||||||||
children: {}, | ||||||||||||||||
}, | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
//first we make sure that list of items lists main section items and then sub sections, documents last | ||||||||||||||||
const sortedItems = sortBy(navItems, ['isRootSection', 'weight', 'isSection']); | ||||||||||||||||
const sortedItems = sortBy(navItems, [ | ||||||||||||||||
'isRootSection', | ||||||||||||||||
'weight', | ||||||||||||||||
'isSection', | ||||||||||||||||
]); | ||||||||||||||||
|
||||||||||||||||
sortedItems.forEach(item => { | ||||||||||||||||
sortedItems.forEach((item) => { | ||||||||||||||||
//identify main sections | ||||||||||||||||
if (item.isRootSection) { | ||||||||||||||||
tree[item.rootSectionId] = { item, children: {} } | ||||||||||||||||
tree[item.rootSectionId] = { item, children: {} }; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
//identify subsections | ||||||||||||||||
if (item.parent) { | ||||||||||||||||
if (!tree[item.parent]) { | ||||||||||||||||
throw new Error(`Parent section ${item.parent} not found for item ${item.title}`); | ||||||||||||||||
throw new Error( | ||||||||||||||||
`Parent section ${item.parent} not found for item ${item.title}`, | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
tree[item.parent].children[item.sectionId] = { item, children: [] }; | ||||||||||||||||
} | ||||||||||||||||
|
@@ -29,7 +43,10 @@ function buildNavTree(navItems) { | |||||||||||||||
if (item.sectionId) { | ||||||||||||||||
let section = tree[item.rootSectionId]?.children[item.sectionId]; | ||||||||||||||||
if (!section) { | ||||||||||||||||
tree[item.rootSectionId].children[item.sectionId] = { item, children: [] }; | ||||||||||||||||
tree[item.rootSectionId].children[item.sectionId] = { | ||||||||||||||||
item, | ||||||||||||||||
children: [], | ||||||||||||||||
}; | ||||||||||||||||
} | ||||||||||||||||
tree[item.rootSectionId].children[item.sectionId].children.push(item); | ||||||||||||||||
} else { | ||||||||||||||||
|
@@ -62,40 +79,39 @@ function buildNavTree(navItems) { | |||||||||||||||
|
||||||||||||||||
// point in slug for specification subgroup to the latest specification version | ||||||||||||||||
if (rootKey === 'reference' && key === 'specification') { | ||||||||||||||||
allChildren[key].item.href = allChildren[key].children.find(c => c.isPrerelease === undefined).slug; | ||||||||||||||||
allChildren[key].item.href = allChildren[key].children.find( | ||||||||||||||||
(c) => c.isPrerelease === undefined, | ||||||||||||||||
).slug; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return tree; | ||||||||||||||||
|
||||||||||||||||
} catch (err) { | ||||||||||||||||
throw new Error(`Failed to build navigation tree: ${err.message}`); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// A recursion function, works on the logic of Depth First Search to traverse all the root and child posts of the | ||||||||||||||||
// A recursion function, works on the logic of Depth First Search to traverse all the root and child posts of the | ||||||||||||||||
// DocTree to get sequential order of the Doc Posts | ||||||||||||||||
const convertDocPosts = (docObject) => { | ||||||||||||||||
try { | ||||||||||||||||
let docsArray = [] | ||||||||||||||||
let docsArray = []; | ||||||||||||||||
// certain entries in the DocPosts are either a parent to many posts or itself a post. | ||||||||||||||||
docsArray.push(docObject?.item || docObject) | ||||||||||||||||
docsArray.push(docObject?.item || docObject); | ||||||||||||||||
if (docObject.children) { | ||||||||||||||||
let children = docObject.children | ||||||||||||||||
let children = docObject.children; | ||||||||||||||||
Object.keys(children).forEach((child) => { | ||||||||||||||||
let docChildArray = convertDocPosts(children[child]) | ||||||||||||||||
docsArray = [...docsArray, ...docChildArray] | ||||||||||||||||
}) | ||||||||||||||||
let docChildArray = convertDocPosts(children[child]); | ||||||||||||||||
docsArray = [...docsArray, ...docChildArray]; | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
return docsArray | ||||||||||||||||
} | ||||||||||||||||
catch (err) { | ||||||||||||||||
return docsArray; | ||||||||||||||||
} catch (err) { | ||||||||||||||||
throw new Error('Error in convertDocPosts:', err); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
function addDocButtons(docPosts, treePosts) { | ||||||||||||||||
let structuredPosts = []; | ||||||||||||||||
|
@@ -115,68 +131,73 @@ function addDocButtons(docPosts, treePosts) { | |||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
// Appending the content of welcome page of Docs from the posts.json | ||||||||||||||||
structuredPosts[0] = docPosts.filter(p => p.slug === '/docs')[0]; | ||||||||||||||||
structuredPosts[0] = docPosts.filter((p) => p.slug === '/docs')[0]; | ||||||||||||||||
|
||||||||||||||||
// Traversing the structuredPosts in order to add `nextPage` and `prevPage` details for each page | ||||||||||||||||
let countDocPages = structuredPosts.length; | ||||||||||||||||
structuredPosts = structuredPosts.map((post, index) => { | ||||||||||||||||
// post item specifying the root Section or sub-section in the docs are excluded as | ||||||||||||||||
// they doesn't comprise any Doc Page or content to be shown in website. | ||||||||||||||||
// post item specifying the root Section or sub-section in the docs are excluded as | ||||||||||||||||
// they doesn't comprise any Doc Page or content to be shown in website. | ||||||||||||||||
if (post?.isRootSection || post?.isSection || index == 0) { | ||||||||||||||||
if (post?.isRootSection || index == 0) | ||||||||||||||||
rootSections.push(post.title) | ||||||||||||||||
return post | ||||||||||||||||
if (post?.isRootSection || index == 0) rootSections.push(post.title); | ||||||||||||||||
return post; | ||||||||||||||||
Comment on lines
134
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use strict equality comparison Replace loose equality (==) with strict equality (===) for more reliable comparisons. - if (post?.isRootSection || post?.isSection || index == 0) {
- if (post?.isRootSection || index == 0) rootSections.push(post.title);
+ if (post?.isRootSection || post?.isSection || index === 0) {
+ if (post?.isRootSection || index === 0) rootSections.push(post.title); 📝 Committable suggestion
Suggested change
🧰 Tools🪛 eslint[error] 134-134: Expected '===' and instead saw '=='. (eqeqeq) [error] 135-135: Expected '===' and instead saw '=='. (eqeqeq) |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
let nextPage = {}, prevPage = {} | ||||||||||||||||
let nextPage = {}, | ||||||||||||||||
prevPage = {}; | ||||||||||||||||
let docPost = post; | ||||||||||||||||
|
||||||||||||||||
// checks whether the next page for the current docPost item exists or not | ||||||||||||||||
if (index + 1 < countDocPages) { | ||||||||||||||||
// checks whether the next item inside structuredPosts is a rootElement or a sectionElement | ||||||||||||||||
// if yes, it goes again to a next to next item in structuredPosts to link the nextPage | ||||||||||||||||
if (!structuredPosts[index + 1].isRootElement && !structuredPosts[index + 1].isSection) { | ||||||||||||||||
if ( | ||||||||||||||||
!structuredPosts[index + 1].isRootElement && | ||||||||||||||||
!structuredPosts[index + 1].isSection | ||||||||||||||||
) { | ||||||||||||||||
nextPage = { | ||||||||||||||||
title: structuredPosts[index + 1].title, | ||||||||||||||||
href: structuredPosts[index + 1].slug | ||||||||||||||||
} | ||||||||||||||||
href: structuredPosts[index + 1].slug, | ||||||||||||||||
}; | ||||||||||||||||
} else { | ||||||||||||||||
nextPage = { | ||||||||||||||||
title: `${structuredPosts[index + 1].title} - ${structuredPosts[index + 2].title}`, | ||||||||||||||||
href: structuredPosts[index + 2].slug | ||||||||||||||||
} | ||||||||||||||||
href: structuredPosts[index + 2].slug, | ||||||||||||||||
}; | ||||||||||||||||
} | ||||||||||||||||
docPost = { ...docPost, nextPage } | ||||||||||||||||
docPost = { ...docPost, nextPage }; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// checks whether the previous page for the current docPost item exists or not | ||||||||||||||||
if (index > 0) { | ||||||||||||||||
// checks whether the previous item inside structuredPosts is a rootElement or a sectionElement | ||||||||||||||||
// if yes, it goes again to a next previous item in structuredPosts to link the prevPage | ||||||||||||||||
if (!structuredPosts[index - 1]?.isRootElement && !structuredPosts[index - 1]?.isSection) { | ||||||||||||||||
if ( | ||||||||||||||||
!structuredPosts[index - 1]?.isRootElement && | ||||||||||||||||
!structuredPosts[index - 1]?.isSection | ||||||||||||||||
) { | ||||||||||||||||
prevPage = { | ||||||||||||||||
title: structuredPosts[index - 1].title, | ||||||||||||||||
href: structuredPosts[index - 1].slug | ||||||||||||||||
} | ||||||||||||||||
docPost = { ...docPost, prevPage } | ||||||||||||||||
href: structuredPosts[index - 1].slug, | ||||||||||||||||
}; | ||||||||||||||||
docPost = { ...docPost, prevPage }; | ||||||||||||||||
} else { | ||||||||||||||||
// additonal check for the first page of Docs so that it doesn't give any Segementation fault | ||||||||||||||||
if (index - 2 >= 0) { | ||||||||||||||||
prevPage = { | ||||||||||||||||
title: `${structuredPosts[index - 1]?.isRootSection ? rootSections[rootSections.length - 2] : rootSections[rootSections.length - 1]} - ${structuredPosts[index - 2].title}`, | ||||||||||||||||
href: structuredPosts[index - 2].slug | ||||||||||||||||
href: structuredPosts[index - 2].slug, | ||||||||||||||||
}; | ||||||||||||||||
docPost = { ...docPost, prevPage }; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve code quality in addDocButtons function Several code quality improvements are needed:
- structuredPosts[0] = docPosts.filter((p) => p.slug === '/docs')[0];
+ [structuredPosts[0]] = docPosts.filter((p) => p.slug === '/docs');
let countDocPages = structuredPosts.length;
structuredPosts = structuredPosts.map((post, index) => {
- if (post?.isRootSection || post?.isSection || index == 0) {
- if (post?.isRootSection || index == 0) rootSections.push(post.title);
+ if (post?.isRootSection || post?.isSection || index === 0) {
+ if (post?.isRootSection || index === 0) rootSections.push(post.title);
return post;
}
- let nextPage = {},
- prevPage = {};
+ let nextPage = {};
+ let prevPage = {};
// Simplify the if-else structure in the previous page logic
- } else {
- if (index - 2 >= 0) {
+ } else if (index - 2 >= 0) {
🧰 Tools🪛 eslint[error] 134-134: Use array destructuring. (prefer-destructuring) [error] 137-137: 'countDocPages' is never reassigned. Use 'const' instead. (prefer-const) [error] 141-141: Expected '===' and instead saw '=='. (eqeqeq) [error] 142-142: Expected '===' and instead saw '=='. (eqeqeq) [error] 146-147: Split 'let' declarations into multiple statements. (one-var) [error] 154-157: Replace (prettier/prettier) [error] 160-160: Delete (prettier/prettier) [error] 165-165: Delete (prettier/prettier) [error] 175-178: Replace (prettier/prettier) [error] 181-181: Delete (prettier/prettier) [error] 186-192: Unexpected if as the only statement in an else block. (no-lonely-if) [error] 189-189: Delete (prettier/prettier) |
||||||||||||||||
} | ||||||||||||||||
return docPost; | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
} catch (err) { | ||||||||||||||||
throw new Error("An error occurred while adding doc buttons:", err); | ||||||||||||||||
throw new Error('An error occurred while adding doc buttons:', err); | ||||||||||||||||
} | ||||||||||||||||
return structuredPosts; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
module.exports = { buildNavTree, addDocButtons, convertDocPosts } | ||||||||||||||||
module.exports = { buildNavTree, addDocButtons, convertDocPosts }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve variable declarations and error handling in convertDocPosts
While the logic is correct, there are several improvements to be made:
🧰 Tools
🪛 eslint
[error] 104-104: Use object destructuring.
(prefer-destructuring)
[error] 104-104: 'children' is never reassigned. Use 'const' instead.
(prefer-const)
[error] 106-106: 'docChildArray' is never reassigned. Use 'const' instead.
(prefer-const)