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

refactor: consolidate utility functions into utils/utils.ts #3957

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 changes: 1 addition & 1 deletion scripts/casestudies/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readdir, readFile, writeFile } from 'fs/promises';
import { join } from 'path';

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

/**
* Builds a list of case studies from files in a directory and writes it to a specified file.
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 @@ -14,7 +14,7 @@ import type {
PullRequestById
} from '@/types/scripts/dashboard';

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

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 @@ -4,7 +4,7 @@ import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

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

const ignoreFiles = [
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 @@ -4,7 +4,7 @@ import dotenv from 'dotenv';

import type { ToolsData } from '@/types/scripts/tools';

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

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 @@ -5,7 +5,7 @@ import Fuse from 'fuse.js';

import type { AsyncAPITool, ToolsData, ToolsListObject } from '@/types/scripts/tools';

import { convertToJson } from '../utils';
import { convertToJson } from '../../utils/utils';
import { logger } from '../utils/logger';
import { categoryList } from './categorylist';
import schema from './tools-schema.json';
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/readAndWriteJson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile, writeFile } from 'fs/promises';

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

/**
* Reads a file, converts its content to JSON, and writes the JSON content to another file.
Expand Down
4 changes: 2 additions & 2 deletions tests/readAndWriteJson.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs/promises');
const { convertToJson } = require('../scripts/utils.ts');
const { convertToJson } = require('../utils/utils.ts');
const { writeJSON } = require('../scripts/utils/readAndWriteJson.ts');
const { yamlString, jsonObject } = require('./fixtures/utilsData');

Expand All @@ -8,7 +8,7 @@ jest.mock('fs/promises', () => ({
writeFile: jest.fn()
}));

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

Expand Down
2 changes: 1 addition & 1 deletion tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { convertToJson } = require('../scripts/utils.ts');
const { convertToJson } = require('../utils/utils.ts');
const { jsonString, yamlString, jsonObject, invalidString } = require('./fixtures/utilsData');

describe('convertToJson', () => {
Expand Down
12 changes: 3 additions & 9 deletions scripts/utils.ts → utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 All @@ -18,13 +18,11 @@ function convertToJson(contentYAMLorJSON: unknown): any {
// Check if the content is valid JSON before attempting to parse as YAML
try {
const jsonContent = JSON.parse(contentYAMLorJSON);

return jsonContent;
} catch (jsonError) {
// If it's not valid JSON, try parsing it as YAML
try {
const yamlContent = yaml.parse(contentYAMLorJSON);

return yamlContent;
} catch (yamlError) {
// If parsing as YAML also fails, throw an error
Expand All @@ -38,10 +36,6 @@ function convertToJson(contentYAMLorJSON: unknown): any {
* @param {number} ms - The number of milliseconds to pause.
* @returns {Promise<void>}
*/
async function pause(ms: number): Promise<void> {
return new Promise((res) => {
setTimeout(res, ms);
});
export function pause(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

export { convertToJson, pause };
Loading