Skip to content

Commit 253b73a

Browse files
JeelRajodiyasambhavgupta0705akshatnema
authored
refactor: scripts code for better code quality (#3851)
Co-authored-by: JeelRajodiya <jeelrajodiyajeel@gmail.com> Co-authored-by: Sambhav Gupta <81870866+sambhavgupta0705@users.noreply.github.com> Co-authored-by: Akshat Nema <76521428+akshatnema@users.noreply.github.com>
1 parent 1e5cdb1 commit 253b73a

File tree

72 files changed

+1892
-1112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1892
-1112
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const config = {
22
transform: {
3-
'^.+\\.[t|j]sx?$': ['babel-jest', { configFile: './tests/babel.test.config.cjs' }]
3+
'^.+\\.[t|j]sx?$': ['babel-jest', { configFile: './tests/babel.test.config.cts' }]
44
},
55
verbose: true,
66
collectCoverage: true,

package-lock.json

Lines changed: 11 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"fuse.js": "^7.0.0",
7676
"googleapis": "^133.0.0",
7777
"gray-matter": "^4.0.3",
78+
"he": "^1.2.0",
7879
"i18next": "^23.12.2",
7980
"jgexml": "^0.4.4",
8081
"js-cookie": "^3.0.5",
@@ -117,7 +118,9 @@
117118
"tailwindcss": "^3.4.3",
118119
"tsx": "^4.19.3",
119120
"winston": "^3.17.0",
120-
"yaml": "^2.3.4"
121+
"yaml": "^2.3.4",
122+
"inquirer": "^9.2.14",
123+
"dedent": "^1.5.1"
121124
},
122125
"devDependencies": {
123126
"@babel/preset-typescript": "^7.26.0",
@@ -133,6 +136,7 @@
133136
"@storybook/react": "^8.2.4",
134137
"@storybook/test": "^8.2.4",
135138
"@types/fs-extra": "^11.0.4",
139+
"@types/he": "^1.2.3",
136140
"@types/inquirer": "^9.0.7",
137141
"@types/jest": "^29.5.14",
138142
"@types/lodash": "^4.17.0",
@@ -145,7 +149,6 @@
145149
"@typescript-eslint/eslint-plugin": "^6.21.0",
146150
"@typescript-eslint/parser": "^6.21.0",
147151
"babel-plugin-transform-import-meta": "^2.2.1",
148-
"dedent": "^1.5.1",
149152
"eslint": "^8",
150153
"eslint-config-airbnb-typescript": "^17.1.0",
151154
"eslint-config-next": "14.1.0",
@@ -159,7 +162,6 @@
159162
"eslint-plugin-tailwindcss": "^3.14.2",
160163
"eslint-plugin-unused-imports": "^3.1.0",
161164
"fast-xml-parser": "^4.5.0",
162-
"inquirer": "^9.2.14",
163165
"jest": "^29.7.0",
164166
"postcss-import": "^16.0.1",
165167
"remark-cli": "^12.0.1",

scripts/build-docs.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function buildNavTree(navItems: Details[]): NavTree {
133133
* @returns {Details[]} - The sequential array of document posts.
134134
* @throws {Error} - Throws an error if there is an issue during the conversion process.
135135
*/
136-
const convertDocPosts = (docObject: NavTree | Details): Details[] => {
136+
function convertDocPosts(docObject: NavTree | Details | NavTreeItem): Details[] {
137137
try {
138138
let docsArray: Details[] = [];
139139

@@ -154,10 +154,10 @@ const convertDocPosts = (docObject: NavTree | Details): Details[] => {
154154
}
155155

156156
return docsArray;
157-
} catch (err) {
158-
throw new Error('Error in convertDocPosts:', err as Error);
157+
} catch (err: unknown) {
158+
throw new Error(`Error in convertDocPosts: ${(err as Error).message}`);
159159
}
160-
};
160+
}
161161

162162
/**
163163
* Enhances document posts by appending next and previous navigation data based on the navigation tree.
@@ -215,18 +215,18 @@ function addDocButtons(docPosts: Details[], treePosts: NavTree): Details[] {
215215
if (index + 1 < countDocPages) {
216216
// checks whether the next item inside structuredPosts is a rootElement or a sectionElement
217217
// if yes, it goes again to a next to next item in structuredPosts to link the nextPage
218+
/* istanbul ignore else */
218219
if (!structuredPosts[index + 1].isRootElement && !structuredPosts[index + 1].isSection) {
219220
nextPage = {
220221
title: structuredPosts[index + 1].title,
221222
href: structuredPosts[index + 1].slug
222223
};
223-
} else {
224+
} else if (index + 2 < countDocPages) {
224225
nextPage = {
225226
title: `${structuredPosts[index + 1].title} - ${structuredPosts[index + 2].title}`,
226227
href: structuredPosts[index + 2].slug
227228
};
228229
}
229-
230230
docPost = { ...docPost, nextPage };
231231
}
232232

@@ -255,7 +255,7 @@ function addDocButtons(docPosts: Details[], treePosts: NavTree): Details[] {
255255
return docPost;
256256
});
257257
} catch (err) {
258-
throw new Error('An error occurred while adding doc buttons:', err as Error);
258+
throw new Error(`An error occurred while adding doc buttons: ${(err as Error).message}`);
259259
}
260260

261261
return structuredPosts;

scripts/build-meetings.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@ async function buildMeetings(writePath: string) {
2121
let auth;
2222
let calendar;
2323

24+
// Check if the CALENDAR_SERVICE_ACCOUNT is present in the environment variables
25+
// Check if required environment variables are present
26+
if (!process.env.CALENDAR_SERVICE_ACCOUNT) {
27+
throw new Error('CALENDAR_SERVICE_ACCOUNT environment variable is not set');
28+
}
29+
30+
if (!process.env.CALENDAR_ID) {
31+
throw new Error('CALENDAR_ID environment variable is not set');
32+
}
33+
2434
try {
2535
auth = new google.auth.GoogleAuth({
2636
scopes: ['https://www.googleapis.com/auth/calendar'],
27-
credentials: process.env.CALENDAR_SERVICE_ACCOUNT ? JSON.parse(process.env.CALENDAR_SERVICE_ACCOUNT) : undefined
37+
credentials: JSON.parse(process.env.CALENDAR_SERVICE_ACCOUNT)
2838
});
2939

3040
calendar = google.calendar({ version: 'v3', auth });

scripts/build-pages.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export function copyAndRenameFiles(srcDir: string, targetDir: string) {
5353
const srcPath = path.join(srcDir, entry.name);
5454
const targetPath = path.join(targetDir, entry.name);
5555

56-
/* istanbul ignore else */
57-
5856
if (entry.isDirectory()) {
5957
// If entry is a directory, create it in target directory and recurse
6058
if (!fs.existsSync(targetPath)) {

scripts/build-post-list.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function capitalize(text: string) {
7171
* @param {Details} details - The details of the item to add.
7272
* @throws {Error} - Throws an error if the details object is invalid.
7373
*/
74-
export const addItem = (details: Details) => {
74+
export const addItem = (details: Details, resultObj: Result) => {
7575
if (!details || typeof details.slug !== 'string') {
7676
throw new Error('Invalid details object provided to addItem');
7777
}
@@ -84,10 +84,8 @@ export const addItem = (details: Details) => {
8484
};
8585
const section = Object.keys(sectionMap).find((key) => details.slug!.startsWith(key));
8686

87-
/* istanbul ignore else */
88-
8987
if (section) {
90-
finalResult[sectionMap[section]].push(details);
88+
resultObj[sectionMap[section]].push(details);
9189
}
9290
};
9391

@@ -181,7 +179,6 @@ async function walkDirectories(
181179
try {
182180
for (const dir of directories) {
183181
const directory = posix.normalize(dir[0]);
184-
/* istanbul ignore next */
185182
const sectionSlug = dir[1] || '';
186183
const files = await readdir(directory);
187184

@@ -196,7 +193,6 @@ async function walkDirectories(
196193
if (await pathExists(fileNameWithSection)) {
197194
// Passing a second argument to frontMatter disables cache. See https://github.com/asyncapi/website/issues/1057
198195
details = frontMatter(await readFile(fileNameWithSection, 'utf-8'), {}).data as Details;
199-
/* istanbul ignore next */
200196
details.title = details.title || capitalize(basename(fileName));
201197
} else {
202198
details = {
@@ -214,7 +210,7 @@ async function walkDirectories(
214210
}
215211
details.sectionWeight = sectionWeight;
216212
details.slug = slug;
217-
addItem(details);
213+
addItem(details, finalResult);
218214
const rootId = details.parent || details.rootSectionId;
219215

220216
await walkDirectories(
@@ -235,7 +231,6 @@ async function walkDirectories(
235231
details.toc = toc(content, { slugify: slugifyToC }).json;
236232
details.readingTime = Math.ceil(readingTime(content).minutes);
237233
details.excerpt = details.excerpt || markdownToTxt(content).substr(0, 200);
238-
/* istanbul ignore next */
239234
details.sectionSlug = sectionSlug || slug.replace(/\.mdx$/, '');
240235
details.sectionWeight = sectionWeight;
241236
details.sectionTitle = sectionTitle;
@@ -266,7 +261,7 @@ async function walkDirectories(
266261
releaseNotes.push(version);
267262
}
268263

269-
addItem(details);
264+
addItem(details, finalResult);
270265
}
271266
}
272267
}

0 commit comments

Comments
 (0)