Skip to content
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

fix: consolidates utility functions by merging into a single utils.ts file #3996

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
902008c
ci: add .deepsource.toml
deepsource-io[bot] Mar 14, 2025
ab32216
removed deepsource.toml
Siddhant-Pragyan-Sinha Mar 14, 2025
d3a0ada
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 15, 2025
2d7076d
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 16, 2025
7bd442c
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 17, 2025
09be095
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 17, 2025
6e14d0d
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 18, 2025
71bfece
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 20, 2025
362a3f4
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 20, 2025
ac8f8db
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 22, 2025
faaa212
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 24, 2025
6660a67
Merge branch 'asyncapi:master' into master
Siddhant-Pragyan-Sinha Mar 26, 2025
3e7bbb4
Merge branch 'asyncapi:master' into utils-2
Siddhant-Pragyan-Sinha Mar 28, 2025
804d33c
fixed import paths in scripts and tests folders
Siddhant-Pragyan-Sinha Mar 29, 2025
b3aee11
modified jest test cases and utils.test.js as well
Siddhant-Pragyan-Sinha Mar 30, 2025
4f970b8
minor changes
Siddhant-Pragyan-Sinha Mar 30, 2025
322b582
Minor refactoring
Siddhant-Pragyan-Sinha Mar 30, 2025
7c1838f
fixed the immediate hanging worker process by properly handling timeo…
Siddhant-Pragyan-Sinha Mar 30, 2025
928d434
removed processBatch error soloution and fixed ts version
Siddhant-Pragyan-Sinha Mar 31, 2025
dfa8e43
Merge branch 'master' into utils-2
asyncapi-bot Mar 31, 2025
d0c47bd
implemented coderabbitai suggestions
Siddhant-Pragyan-Sinha Mar 31, 2025
ef25721
implemented coderabbitai suggestions again
Siddhant-Pragyan-Sinha Mar 31, 2025
6f7e651
removed redundant localTestPaths component
Siddhant-Pragyan-Sinha Mar 31, 2025
73c7700
highly improved utils.test.js, readAndWriteJson.test.js, check-edit-l…
Siddhant-Pragyan-Sinha Mar 31, 2025
41f887a
solved NodeJS PR error in build-tools.test.js
Siddhant-Pragyan-Sinha Mar 31, 2025
9a8db1a
faulty file check-1
Siddhant-Pragyan-Sinha Apr 1, 2025
2536b6d
file error solved hopefully and minor changes done
Siddhant-Pragyan-Sinha Apr 1, 2025
d4d1d7a
improved utils.test.js by ensuring directory and files exist
Siddhant-Pragyan-Sinha Apr 1, 2025
f8dce7b
implemented coderabbit suggestions again again
Siddhant-Pragyan-Sinha Apr 1, 2025
13b77d5
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 1, 2025
fd10b11
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 2, 2025
e24b9a0
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 3, 2025
1709936
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 3, 2025
7be6961
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 4, 2025
ebd744f
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 5, 2025
805bda8
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 5, 2025
5b15519
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 6, 2025
0ed28d9
Merge branch 'asyncapi:master' into utils-2
Siddhant-Pragyan-Sinha Apr 6, 2025
269ce05
ran npm audit fix
Siddhant-Pragyan-Sinha Apr 6, 2025
ab78fa4
Merge branch 'master' into utils-2
Siddhant-Pragyan-Sinha Apr 7, 2025
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
2,352 changes: 669 additions & 1,683 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/adopters/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

import { writeJSON } from '../utils/readAndWriteJson';
import { writeJSON } from '../utils';

const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-meetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { google } from 'googleapis';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

import { logger } from './utils/logger';
import { logger } from './utils';

const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-newsroom-videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dirname, resolve } from 'path';
import process from 'process';
import { fileURLToPath } from 'url';

import { logger } from './utils/logger';
import { logger } from './utils';

const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url';
import { combineTools } from './tools/combine-tools';
import { getData } from './tools/extract-tools-github';
import { convertTools } from './tools/tools-object';
import { logger } from './utils/logger';
import { logger } from './utils';

const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);
Expand Down
2 changes: 1 addition & 1 deletion scripts/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from 'fs';
import inquirer from 'inquirer';
import moment from 'moment';

import { logger } from './utils/logger';
import { logger } from './utils';

/**
* Type definition for the answers from the compose prompt.
Expand Down
2 changes: 1 addition & 1 deletion scripts/dashboard/build-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
} from '@/types/scripts/dashboard';

import { pause } from '../utils';
import { logger } from '../utils/logger';
import { logger } from '../utils';
import { Queries } from './issue-queries';

const currentFilePath = fileURLToPath(import.meta.url);
Expand Down
2 changes: 1 addition & 1 deletion scripts/finance/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mkdir } from 'fs/promises';
import { resolve } from 'path';

import { writeJSON } from '../utils/readAndWriteJson';
import { writeJSON } from '../utils';

/**
* Interface for the properties required to build the finance info list.
Expand Down
2 changes: 1 addition & 1 deletion scripts/markdown/check-edit-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url';

import editUrls from '../../config/edit-page-config.json';
import { pause } from '../utils';
import { logger } from '../utils/logger';
import { logger } from '../utils';

const ignoreFiles = [
'reference/specification/v2.x.md',
Expand Down
2 changes: 1 addition & 1 deletion scripts/markdown/check-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import matter from 'gray-matter';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

import { logger } from '../utils/logger';
import { logger } from '../utils';

const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);
Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/combine-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
ToolsListObject
} from '@/types/scripts/tools';

import { logger } from '../utils/logger';
import { logger } from '../utils';
import { categoryList } from './categorylist';
import { languagesColor, technologiesColor } from './tags-color';
import { createToolObject } from './tools-object';
Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/extract-tools-github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dotenv from 'dotenv';
import type { ToolsData } from '@/types/scripts/tools';

import { pause } from '../utils';
import { logger } from '../utils/logger';
import { logger } from '../utils';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/tools-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Fuse from 'fuse.js';
import type { AsyncAPITool, ToolsData, ToolsListObject } from '@/types/scripts/tools';

import { convertToJson } from '../utils';
import { logger } from '../utils/logger';
import { logger } from '../utils';
import { categoryList } from './categorylist';
import schema from './tools-schema.json';

Expand Down
50 changes: 47 additions & 3 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import yaml from 'yaml';
import winston from 'winston';
import { readFile, writeFile } from 'fs/promises';

const { combine, timestamp, printf, colorize, align } = winston.format;

/**
* Converts a YAML or JSON string to a JSON object.
Expand All @@ -8,7 +12,7 @@ import yaml from 'yaml';
* @returns {any} - The converted JSON object.
* @throws {Error} - Throws an error if the content is neither valid JSON nor valid YAML.
*/
function convertToJson(contentYAMLorJSON: unknown): any {
export function convertToJson(contentYAMLorJSON: unknown): any {
// Axios handles conversion to JSON by default, if data returned from the server allows it
// So if returned content is not a string (not YAML), we just return JSON back
if (typeof contentYAMLorJSON !== 'string') {
Expand Down Expand Up @@ -38,10 +42,50 @@ function convertToJson(contentYAMLorJSON: unknown): any {
* @param {number} ms - The number of milliseconds to pause.
* @returns {Promise<void>}
*/
async function pause(ms: number): Promise<void> {
export async function pause(ms: number): Promise<void> {
return new Promise((res) => {
setTimeout(res, ms);
});
}

export { convertToJson, pause };
/**
* Logger configuration
*/
export const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: combine(
colorize({ level: true }),
timestamp({
format: 'YYYY-MM-DD hh:mm:ss.SSS A'
}),
align(),
printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
),
transports: [new winston.transports.Console()]
});

/**
* Reads a file, converts its content to JSON, and writes the JSON content to another file.
*/
export async function writeJSON(readPath: string, writePath: string) {
try {
const readContent = await readFile(readPath, 'utf-8');
const jsonContent = convertToJson(readContent);
await writeFile(writePath, JSON.stringify(jsonContent));
return jsonContent;
} catch (err: any) {
// Better detection of error types
if (err.code === 'ENOENT' || err.message.includes('read')) {
throw new Error(`Error while reading file\nError: ${err}`);
} else if (err.message.includes('write')) {
throw new Error(`Error while writing file\nError: ${err}`);
} else if (err.message.includes('Invalid content') ||
err.message.includes('JSON') ||
err.message.includes('YAML')) {
throw new Error(`Error while conversion\nError: ${err}`);
} else {
throw new Error(`Error processing file: ${err}`);
}
}
}

18 changes: 0 additions & 18 deletions scripts/utils/logger.ts

This file was deleted.

36 changes: 0 additions & 36 deletions scripts/utils/readAndWriteJson.ts

This file was deleted.

10 changes: 7 additions & 3 deletions tests/adopters/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { writeJSON } from '../../scripts/utils.ts';
import { buildAdoptersList } from '../../scripts/adopters/index.ts';

const { resolve } = require('path');
const { writeJSON } = require('../../scripts/utils/readAndWriteJson.ts');
const { buildAdoptersList } = require('../../scripts/adopters/index.ts');

jest.mock('../../scripts/utils/readAndWriteJson.ts');

jest.mock('../../scripts/utils.ts', () => ({
writeJSON: jest.fn()
}));

describe('buildAdoptersList', () => {
test('should call writeJSON with correct arguments', async () => {
Expand Down
11 changes: 8 additions & 3 deletions tests/dashboard/build-dashboard.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logger } from '../../scripts/utils.ts';

const { graphql } = require('@octokit/graphql');
const { promises: fs, mkdirSync, rmSync } = require('fs-extra');
const { resolve } = require('path');
Expand All @@ -19,10 +21,10 @@ const {
fullDiscussionDetails,
mockRateLimitResponse
} = require('../fixtures/dashboardData');
const { logger } = require('../../scripts/utils/logger.ts');

jest.mock('../../scripts/utils/logger', () => ({
logger: { error: jest.fn(), warn: jest.fn() }
jest.mock('../../scripts/utils', () => ({
logger: { error: jest.fn(), warn: jest.fn() },
pause: jest.fn().mockResolvedValue(undefined)
}));

jest.mock('@octokit/graphql');
Expand Down Expand Up @@ -120,6 +122,9 @@ describe('GitHub Discussions Processing', () => {
graphql.mockResolvedValue(mockRateLimitResponse);

const filePath = resolve(tempDir, 'success-output.json');
// Ensure the file exists by writing a basic object to it
await fs.writeFile(filePath, JSON.stringify({}));

await start(filePath);

const content = JSON.parse(await fs.readFile(filePath, 'utf-8'));
Expand Down
9 changes: 6 additions & 3 deletions tests/markdown/check-edit-links.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logger } from '../../scripts/utils';

const path = require('path');
const fetch = require('node-fetch-2');
const editOptions = require('../../config/edit-page-config.json');
Expand All @@ -9,10 +11,10 @@ const {
main
} = require('../../scripts/markdown/check-edit-links.ts');
const { determineEditLinkData, processBatchData, testPaths } = require('../fixtures/markdown/check-edit-links-data');
const { logger } = require('../../scripts/utils/logger.ts');

jest.mock('../../scripts/utils/logger', () => ({
logger: { info: jest.fn() }
jest.mock('../../scripts/utils', () => ({
logger: { info: jest.fn() },
pause: jest.fn().mockResolvedValue(undefined)
}));
jest.mock('node-fetch-2', () => jest.fn());

Expand Down Expand Up @@ -138,6 +140,7 @@ describe('URL Checker Tests', () => {
});
const results = await checkUrls(testPaths);
expect(results.length).toBe(2);
expect(results[0].urlPath).toContain('migration');
});
});

Expand Down
6 changes: 3 additions & 3 deletions tests/markdown/check-markdown.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logger } from '../../scripts/utils.ts';

const fs = require('fs').promises;
const path = require('path');
const os = require('os');
Expand All @@ -8,9 +10,7 @@ const {
validateDocs,
checkMarkdownFiles
} = require('../../scripts/markdown/check-markdown.ts');
const { logger } = require('../../scripts/utils/logger.ts');

jest.mock('../../scripts/utils/logger', () => ({
jest.mock('../../scripts/utils', () => ({
logger: { error: jest.fn(), warn: jest.fn() }
}));

Expand Down
Loading
Loading