2
import * as fs from 'fs'
3
import { StatusCodes } from 'http-status-codes'
4
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
5
import { getErrorMessage } from '../../errors/utils'
8
const getAllTemplates = async () => {
10
let marketplaceDir = path.join(__dirname, '..', '..', '..', 'marketplaces', 'chatflows')
11
let jsonsInDir = fs.readdirSync(marketplaceDir).filter((file) => path.extname(file) === '.json')
12
let templates: any[] = []
13
jsonsInDir.forEach((file, index) => {
14
const filePath = path.join(__dirname, '..', '..', '..', 'marketplaces', 'chatflows', file)
15
const fileData = fs.readFileSync(filePath)
16
const fileDataObj = JSON.parse(fileData.toString())
19
templateName: file.split('.json')[0],
20
flowData: fileData.toString(),
21
badge: fileDataObj?.badge,
22
framework: fileDataObj?.framework,
23
categories: fileDataObj?.categories,
25
description: fileDataObj?.description || ''
27
templates.push(template)
30
marketplaceDir = path.join(__dirname, '..', '..', '..', 'marketplaces', 'tools')
31
jsonsInDir = fs.readdirSync(marketplaceDir).filter((file) => path.extname(file) === '.json')
32
jsonsInDir.forEach((file, index) => {
33
const filePath = path.join(__dirname, '..', '..', '..', 'marketplaces', 'tools', file)
34
const fileData = fs.readFileSync(filePath)
35
const fileDataObj = JSON.parse(fileData.toString())
40
framework: fileDataObj?.framework,
41
badge: fileDataObj?.badge,
43
templateName: file.split('.json')[0]
45
templates.push(template)
47
const sortedTemplates = templates.sort((a, b) => a.templateName.localeCompare(b.templateName))
48
const FlowiseDocsQnAIndex = sortedTemplates.findIndex((tmp) => tmp.templateName === 'Flowise Docs QnA')
49
if (FlowiseDocsQnAIndex > 0) {
50
sortedTemplates.unshift(sortedTemplates.splice(FlowiseDocsQnAIndex, 1)[0])
52
const dbResponse = sortedTemplates
55
throw new InternalFlowiseError(
56
StatusCodes.INTERNAL_SERVER_ERROR,
57
`Error: marketplacesService.getAllTemplates - ${getErrorMessage(error)}`