mirror of
https://github.com/leon-ai/leon.git
synced 2024-10-26 18:18:46 +03:00
feat: avoid unnecessary routes generation
This commit is contained in:
parent
5b41713a68
commit
cc0fb92351
@ -17,68 +17,89 @@ 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'
|
||||
const outputFilePath = path.join(__dirname, `..${outputFile}`)
|
||||
const lang = langs[process.env.LEON_LANG].short.toLowerCase().substr(0, 2)
|
||||
|
||||
try {
|
||||
// TODO: check packages folder mtime first
|
||||
log.info('Parsing packages configuration...')
|
||||
|
||||
const packages = fs.readdirSync(packagesDir)
|
||||
.filter((entity) => fs.statSync(path.join(packagesDir, entity)).isDirectory())
|
||||
const finalObj = {
|
||||
endpoints: []
|
||||
}
|
||||
let isFileNeedToBeGenerated = true
|
||||
let pkgObj = { }
|
||||
|
||||
for (let i = 0; i < packages.length; i += 1) {
|
||||
const pkg = packages[i]
|
||||
if (fs.existsSync(outputFilePath)) {
|
||||
const mtimeEndpoints = fs.statSync(outputFilePath).mtime.getTime()
|
||||
for (let i = 0; i < packages.length; i += 1) {
|
||||
const pkg = packages[i]
|
||||
|
||||
pkgObj = JSON.parse(fs.readFileSync(`${packagesDir}/${pkg}/data/expressions/${lang}.json`, 'utf8'))
|
||||
const fileInfo = fs.statSync(`${packagesDir}/${pkg}/data/expressions/${lang}.json`)
|
||||
const mtime = fileInfo.mtime.getTime()
|
||||
|
||||
const modules = Object.keys(pkgObj)
|
||||
for (let j = 0; j < modules.length; j += 1) {
|
||||
const module = modules[j]
|
||||
const actions = Object.keys(pkgObj[module])
|
||||
if (mtime > mtimeEndpoints) {
|
||||
break
|
||||
}
|
||||
|
||||
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
|
||||
let finalMethod = entities ? 'POST' : 'GET'
|
||||
|
||||
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: []
|
||||
}
|
||||
|
||||
if (http_api?.timeout) {
|
||||
endpoint.timeout = http_api.timeout
|
||||
}
|
||||
if (entities) {
|
||||
endpoint.params = entities.map((entity) => entity.name)
|
||||
}
|
||||
|
||||
finalObj.endpoints.push(endpoint)
|
||||
if ((i + 1) === packages.length) {
|
||||
isFileNeedToBeGenerated = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info(`Writing ${outputFile} file...`)
|
||||
try {
|
||||
fs.writeFileSync(path.join(__dirname, `..${outputFile}`), JSON.stringify(finalObj, null, 2))
|
||||
log.success(`${outputFile} file generated`)
|
||||
resolve()
|
||||
} catch (e) {
|
||||
reject(`Failed to generate ${outputFile} file: ${e.message}`)
|
||||
if (isFileNeedToBeGenerated) {
|
||||
log.info('Parsing packages configuration...')
|
||||
|
||||
for (let i = 0; i < packages.length; i += 1) {
|
||||
const pkg = packages[i]
|
||||
|
||||
pkgObj = JSON.parse(fs.readFileSync(`${packagesDir}/${pkg}/data/expressions/${lang}.json`, 'utf8'))
|
||||
|
||||
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
|
||||
let finalMethod = entities ? 'POST' : 'GET'
|
||||
|
||||
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: []
|
||||
}
|
||||
|
||||
if (http_api?.timeout) {
|
||||
endpoint.timeout = http_api.timeout
|
||||
}
|
||||
if (entities) {
|
||||
endpoint.params = entities.map((entity) => entity.name)
|
||||
}
|
||||
|
||||
finalObj.endpoints.push(endpoint)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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}`)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
log.error(e.message)
|
||||
|
Loading…
Reference in New Issue
Block a user