Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
57 changes: 53 additions & 4 deletions scripts/build-tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs-extra';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

import { logger } from './helpers/logger';
import { combineTools } from './tools/combine-tools';
import { getData } from './tools/extract-tools-github';
Expand All @@ -10,6 +9,28 @@ import { convertTools } from './tools/tools-object';
const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);

/**
* Combines automated and manual tools data.
*
* @param automatedTools - The automated tools data
* @param manualTools - The manual tools data
* @param toolsPath - The file path where the combined tools data will be written
* @param tagsPath - The file path where the tags data will be written
*/
async function combineAutomatedAndManualTools(
automatedTools: any,
manualTools: any,
toolsPath: string,
tagsPath: string
) {
try {
await combineTools(automatedTools, manualTools, toolsPath, tagsPath);
} catch (err) {
logger.error('Error while combining tools:', err);
throw new Error(`An error occurred while combining tools: ${(err as Error).message}`);
}
}

/**
* Combines automated and manual tools data and writes the results to the specified file paths.
*
Expand All @@ -32,13 +53,41 @@ async function buildTools(automatedToolsPath: string, manualToolsPath: string, t
await fs.writeFile(automatedToolsPath, JSON.stringify(automatedTools, null, ' '));

const manualTools = JSON.parse(await fs.readFile(manualToolsPath, 'utf-8'));

await combineTools(automatedTools, manualTools, toolsPath, tagsPath);
await combineAutomatedAndManualTools(automatedTools, manualTools, toolsPath, tagsPath);
} catch (err) {
throw new Error(`An error occurred while building tools: ${(err as Error).message}`);
}
}

/**
* Builds tools manually by combining existing automated tools with manual tools.
* This function is used to ensure that it reflects changes in tools-manual.json.
*/
async function buildToolsManual(
automatedToolsPath: string,
manualToolsPath: string,
toolsPath: string,
tagsPath: string
) {
try {
if (!await fs.pathExists(automatedToolsPath)) {
throw new Error(
`Automated tools file not found at ${automatedToolsPath}.`);
}

if (!await fs.pathExists(manualToolsPath)) {
throw new Error(`Manual tools file not found at ${manualToolsPath}.`);
}

const automatedTools = JSON.parse(await fs.readFile(automatedToolsPath, 'utf-8'));
const manualTools = JSON.parse(await fs.readFile(manualToolsPath, 'utf-8'));
await combineAutomatedAndManualTools(automatedTools, manualTools, toolsPath, tagsPath);
} catch (err) {
logger.error('Error in buildToolsManual:', err);
throw new Error(`An error occurred while building tools manually: ${(err as Error).message}`);
}
}

/* istanbul ignore next */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const automatedToolsPath = resolve(currentDirPath, '../config', 'tools-automated.json');
Expand All @@ -52,4 +101,4 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) {
});
}

export { buildTools };
export { buildTools, buildToolsManual };
13 changes: 12 additions & 1 deletion scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import { buildPostList } from './build-post-list';
import { rssFeed } from './build-rss';
import { buildCaseStudiesList } from './casestudies/index';
import { buildFinanceInfoList } from './finance/index';
import { buildToolsManual } from './build-tools';

const currentFilePath = fileURLToPath(import.meta.url);
const currentDirPath = dirname(currentFilePath);
const automatedToolsPath = resolve(currentDirPath, '../config', 'tools-automated.json');
const manualToolsPath = resolve(currentDirPath, '../config', 'tools-manual.json');
const toolsPath = resolve(currentDirPath, '../config', 'tools.json');
const tagsPath = resolve(currentDirPath, '../config', 'all-tags.json');


/**
* Initiates the build process for the project's content.
*
* This asynchronous function orchestrates the creation of various content lists by processing designated directories and files.
* It builds the posts list, generates the blog RSS feed, creates the case studies list, and compiles the adopters list.
* It builds the posts list, generates the blog RSS feed, creates the case studies list, compiles the adopters list,
* and combines tools data.
* For finance information, it reads the finance directory, filters and sorts numeric filenames representing years, and utilizes the latest year.
* The function throws an error if no valid finance data is found.
*
Expand All @@ -33,6 +40,10 @@ async function start() {
await buildPostList(postDirectories, basePath, writeFilePath);
await rssFeed('blog', 'AsyncAPI Initiative Blog RSS Feed', 'AsyncAPI Initiative Blog', 'rss.xml');
await buildCaseStudiesList('config/casestudies', resolve(currentDirPath, '../config', 'case-studies.json'));

// Build tools manually to reflect changes in tools-manual.json
await buildToolsManual(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);

await buildUsecasesList();
const financeDir = resolve('.', 'config', 'finance');

Expand Down
56 changes: 55 additions & 1 deletion tests/build-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra';
import os from 'os';
import path, { resolve } from 'path';

import { buildTools } from '../scripts/build-tools';
import { buildTools, buildToolsManual } from '../scripts/build-tools';
import { manualTools, mockConvertedData, mockExtractData, tagsData } from './fixtures/buildToolsData';

jest.mock('axios');
Expand Down Expand Up @@ -90,4 +90,58 @@ describe('buildTools', () => {

await expect(buildTools(invalidPath, manualToolsPath, toolsPath, tagsPath)).rejects.toThrow(/ENOENT|EACCES/);
});

it('should build tools manually from existing files', async () => {
fs.writeFileSync(automatedToolsPath, JSON.stringify(mockConvertedData));
fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools));

await buildToolsManual(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);

const combinedToolsContent = JSON.parse(fs.readFileSync(toolsPath, 'utf-8'));

expect(combinedToolsContent).toHaveProperty('Category1');
expect(combinedToolsContent).toHaveProperty('Category2');
});

it('should handle combineTools error', async () => {
mockedAxios.get.mockResolvedValue({ data: mockExtractData });

const combineSpy = jest
.spyOn(require('../scripts/tools/combine-tools'), 'combineTools')
.mockRejectedValueOnce(new Error('Combine error'));

await expect(
buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath)
).rejects.toThrow(
'An error occurred while building tools: An error occurred while combining tools: Combine error'
);

combineSpy.mockRestore();
});

it('should handle buildToolsManual error', async () => {
const invalidAutomatedToolsPath = resolve(testDir, 'non-existent-dir', 'tools-automated.json');

await expect(
buildToolsManual(invalidAutomatedToolsPath, manualToolsPath, toolsPath, tagsPath)
).rejects.toThrow('An error occurred while building tools manually:');
});

it('should handle missing automated tools file', async () => {
const invalidPath = resolve(testDir, 'missing-automated.json');

await expect(
buildToolsManual(invalidPath, manualToolsPath, toolsPath, tagsPath)
).rejects.toThrow('Automated tools file not found');
});

it('should handle missing manual tools file error', async () => {
const invalidManualPath = resolve(testDir, 'nonexistent-manual.json');

fs.writeFileSync(automatedToolsPath, JSON.stringify(mockConvertedData));

await expect(
buildToolsManual(automatedToolsPath, invalidManualPath, toolsPath, tagsPath)
).rejects.toThrow('Manual tools file not found');
});
});
Loading