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'
|
2022-09-13 18:06:57 +03:00
|
|
|
import { getSkillDomains } from '@/helpers/skill-domain'
|
2021-12-30 18:06:30 +03:00
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
/**
|
2022-02-16 17:28:05 +03:00
|
|
|
* Generate skills endpoints script
|
2022-08-20 17:35:34 +03:00
|
|
|
* Parse and convert skills config into a JSON file understandable by Fastify
|
2022-02-16 17:28:05 +03:00
|
|
|
* to dynamically generate endpoints so skills can be accessible over HTTP
|
2021-12-30 18:06:30 +03:00
|
|
|
*/
|
2022-09-03 14:12:41 +03:00
|
|
|
export default () =>
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
const supportedMethods = [
|
|
|
|
'DELETE',
|
|
|
|
'GET',
|
|
|
|
'HEAD',
|
|
|
|
'PATCH',
|
|
|
|
'POST',
|
|
|
|
'PUT',
|
|
|
|
'OPTIONS'
|
|
|
|
]
|
|
|
|
const outputFile = '/core/skills-endpoints.json'
|
|
|
|
const outputFilePath = path.join(__dirname, `../..${outputFile}`)
|
|
|
|
const lang = langs[process.env.LEON_HTTP_API_LANG].short
|
|
|
|
|
|
|
|
try {
|
2022-09-13 18:06:57 +03:00
|
|
|
const skillDomains = await getSkillDomains()
|
2022-09-03 14:12:41 +03:00
|
|
|
const finalObj = {
|
|
|
|
endpoints: []
|
|
|
|
}
|
|
|
|
let isFileNeedToBeGenerated = true
|
|
|
|
let loopIsBroken = false
|
|
|
|
|
|
|
|
// Check if a new routing generation is necessary
|
|
|
|
if (fs.existsSync(outputFilePath)) {
|
|
|
|
const mtimeEndpoints = fs.statSync(outputFilePath).mtime.getTime()
|
|
|
|
|
2022-09-13 18:06:57 +03:00
|
|
|
let i = 0
|
|
|
|
for (const currentDomain of skillDomains.values()) {
|
2022-09-03 14:12:41 +03:00
|
|
|
const skillKeys = Object.keys(currentDomain.skills)
|
|
|
|
|
|
|
|
// Browse skills
|
|
|
|
for (let j = 0; j < skillKeys.length; j += 1) {
|
|
|
|
const skillFriendlyName = skillKeys[j]
|
|
|
|
const currentSkill = currentDomain.skills[skillFriendlyName]
|
|
|
|
const fileInfo = fs.statSync(
|
|
|
|
path.join(currentSkill.path, 'config', `${lang}.json`)
|
|
|
|
)
|
|
|
|
const mtime = fileInfo.mtime.getTime()
|
|
|
|
|
|
|
|
if (mtime > mtimeEndpoints) {
|
|
|
|
loopIsBroken = true
|
|
|
|
break
|
|
|
|
}
|
2022-02-16 17:28:05 +03:00
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
if (loopIsBroken) {
|
|
|
|
break
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-09-13 18:06:57 +03:00
|
|
|
if (i + 1 === skillDomains.size) {
|
2022-09-03 14:12:41 +03:00
|
|
|
log.success(`${outputFile} is already up-to-date`)
|
|
|
|
isFileNeedToBeGenerated = false
|
|
|
|
}
|
2022-09-13 18:06:57 +03:00
|
|
|
|
|
|
|
i += 1
|
2022-01-08 14:55:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
// Force if a language is given
|
|
|
|
if (isFileNeedToBeGenerated) {
|
|
|
|
log.info('Parsing skills configuration...')
|
|
|
|
|
2022-09-13 18:06:57 +03:00
|
|
|
for (const currentDomain of skillDomains.values()) {
|
2022-09-03 14:12:41 +03:00
|
|
|
const skillKeys = Object.keys(currentDomain.skills)
|
|
|
|
|
|
|
|
// Browse skills
|
|
|
|
for (let j = 0; j < skillKeys.length; j += 1) {
|
|
|
|
const skillFriendlyName = skillKeys[j]
|
|
|
|
const currentSkill = currentDomain.skills[skillFriendlyName]
|
|
|
|
|
|
|
|
const configFilePath = path.join(
|
|
|
|
currentSkill.path,
|
|
|
|
'config',
|
|
|
|
`${lang}.json`
|
|
|
|
)
|
|
|
|
const { actions } = JSON.parse(
|
|
|
|
fs.readFileSync(configFilePath, 'utf8')
|
|
|
|
)
|
|
|
|
const actionsKeys = Object.keys(actions)
|
|
|
|
|
|
|
|
for (let k = 0; k < actionsKeys.length; k += 1) {
|
|
|
|
const action = actionsKeys[k]
|
|
|
|
const actionObj = actions[action]
|
2022-09-18 22:08:51 +03:00
|
|
|
const { entities, http_api } = actionObj
|
2022-09-03 14:12:41 +03:00
|
|
|
let finalMethod = entities || http_api?.entities ? 'POST' : 'GET'
|
|
|
|
|
|
|
|
// Only generate this route if it is not disabled from the skill config
|
|
|
|
if (
|
|
|
|
!http_api?.disabled ||
|
|
|
|
(http_api?.disabled && http_api?.disabled === false)
|
|
|
|
) {
|
|
|
|
if (http_api?.method) {
|
|
|
|
finalMethod = http_api.method.toUpperCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportedMethods.includes(finalMethod)) {
|
|
|
|
reject(
|
|
|
|
`The "${finalMethod}" HTTP method of the ${currentDomain.name}/${currentSkill.name}/${action} action is not supported`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = {
|
|
|
|
method: finalMethod.toUpperCase(),
|
|
|
|
route: `/api/action/${currentDomain.name}/${currentSkill.name}/${action}`,
|
|
|
|
params: []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (http_api?.timeout) {
|
|
|
|
endpoint.timeout = http_api.timeout
|
|
|
|
}
|
|
|
|
if (entities) {
|
|
|
|
// Handle explicit trim entities
|
|
|
|
endpoint.entitiesType = 'trim'
|
|
|
|
endpoint.params = entities.map((entity) => entity.name)
|
|
|
|
} else if (http_api?.entities) {
|
|
|
|
// Handle built-in entities
|
|
|
|
endpoint.entitiesType = 'builtIn'
|
|
|
|
endpoint.params = http_api.entities.map(
|
|
|
|
(entity) => entity.entity
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
finalObj.endpoints.push(endpoint)
|
2022-01-09 16:03:28 +03:00
|
|
|
}
|
2022-01-08 14:55:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-30 18:06:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-03 14:12:41 +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}`)
|
|
|
|
}
|
2022-01-08 14:55:26 +03:00
|
|
|
}
|
2022-09-03 14:12:41 +03:00
|
|
|
} catch (e) {
|
|
|
|
log.error(e.message)
|
|
|
|
reject(e)
|
2021-12-30 18:06:30 +03:00
|
|
|
}
|
2022-09-03 14:12:41 +03:00
|
|
|
})
|