2021-12-30 18:06:30 +03:00
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
import log from '@/helpers/log'
|
|
|
|
|
|
|
|
import { langs } from '@@/core/langs.json'
|
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate packages endpoints script
|
|
|
|
* Parse and convert packages configuration into a JSON file understandable by Fastify
|
|
|
|
* to dynamically generate endpoints so packages can be accessible over HTTP
|
|
|
|
*/
|
|
|
|
export default () => new Promise(async (resolve, reject) => {
|
|
|
|
const supportedMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
|
|
|
|
const packagesDir = 'packages'
|
|
|
|
const outputFile = '/core/pkgs-endpoints.json'
|
2022-01-08 14:55:26 +03:00
|
|
|
const outputFilePath = path.join(__dirname, `..${outputFile}`)
|
2021-12-30 18:06:30 +03:00
|
|
|
const lang = langs[process.env.LEON_LANG].short.toLowerCase().substr(0, 2)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const packages = fs.readdirSync(packagesDir)
|
|
|
|
.filter((entity) => fs.statSync(path.join(packagesDir, entity)).isDirectory())
|
|
|
|
const finalObj = {
|
|
|
|
endpoints: []
|
|
|
|
}
|
2022-01-08 14:55:26 +03:00
|
|
|
let isFileNeedToBeGenerated = true
|
2021-12-30 18:06:30 +03:00
|
|
|
let pkgObj = { }
|
|
|
|
|
2022-01-08 21:32:02 +03:00
|
|
|
// Check if a new routing generation is necessary
|
2022-01-08 14:55:26 +03:00
|
|
|
if (fs.existsSync(outputFilePath)) {
|
|
|
|
const mtimeEndpoints = fs.statSync(outputFilePath).mtime.getTime()
|
2022-01-08 21:32:02 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
for (let i = 0; i < packages.length; i += 1) {
|
|
|
|
const pkg = packages[i]
|
|
|
|
const fileInfo = fs.statSync(`${packagesDir}/${pkg}/data/expressions/${lang}.json`)
|
|
|
|
const mtime = fileInfo.mtime.getTime()
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
if (mtime > mtimeEndpoints) {
|
|
|
|
break
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
if ((i + 1) === packages.length) {
|
|
|
|
isFileNeedToBeGenerated = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
if (isFileNeedToBeGenerated) {
|
|
|
|
log.info('Parsing packages configuration...')
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
for (let i = 0; i < packages.length; i += 1) {
|
|
|
|
const pkg = packages[i]
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
pkgObj = JSON.parse(fs.readFileSync(`${packagesDir}/${pkg}/data/expressions/${lang}.json`, 'utf8'))
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
const modules = Object.keys(pkgObj)
|
|
|
|
for (let j = 0; j < modules.length; j += 1) {
|
|
|
|
const module = modules[j]
|
|
|
|
const actions = Object.keys(pkgObj[module])
|
|
|
|
|
|
|
|
for (let k = 0; k < actions.length; k += 1) {
|
|
|
|
const action = actions[k]
|
|
|
|
const actionObj = pkgObj[module][action]
|
|
|
|
const { entities, http_api } = actionObj // eslint-disable-line camelcase
|
2022-01-08 21:32:02 +03:00
|
|
|
let finalMethod = entities || http_api?.entities ? 'POST' : 'GET'
|
2022-01-08 14:55:26 +03:00
|
|
|
|
|
|
|
if (http_api?.method) {
|
|
|
|
finalMethod = http_api.method.toUpperCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportedMethods.includes(finalMethod)) {
|
|
|
|
reject(`The "${finalMethod}" HTTP method of the ${pkg}/${module}/${action} action is not supported`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = {
|
|
|
|
method: finalMethod.toUpperCase(),
|
|
|
|
route: `/p/${pkg}/${module}/${action}`,
|
|
|
|
params: []
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
if (http_api?.timeout) {
|
|
|
|
endpoint.timeout = http_api.timeout
|
|
|
|
}
|
|
|
|
if (entities) {
|
|
|
|
endpoint.params = entities.map((entity) => entity.name)
|
2022-01-08 21:32:02 +03:00
|
|
|
} else if (http_api?.entities) {
|
|
|
|
endpoint.params = http_api.entities.map((entity) => entity.name)
|
2022-01-08 14:55:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
finalObj.endpoints.push(endpoint)
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 14:55:26 +03:00
|
|
|
log.info(`Writing ${outputFile} file...`)
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(outputFilePath, JSON.stringify(finalObj, null, 2))
|
|
|
|
log.success(`${outputFile} file generated`)
|
|
|
|
resolve()
|
|
|
|
} catch (e) {
|
|
|
|
reject(`Failed to generate ${outputFile} file: ${e.message}`)
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error(e.message)
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
})
|