1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-28 12:43:35 +03:00

refactor: setup skills config

This commit is contained in:
louistiti 2022-02-16 21:08:47 +08:00
parent 7d5cadbb82
commit dc80135cd0
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
4 changed files with 78 additions and 73 deletions

View File

@ -1,71 +0,0 @@
import { commandSync } from 'execa'
import fs from 'fs'
import path from 'path'
import log from '@/helpers/log'
import string from '@/helpers/string'
/**
* Setup packages configuration
*/
export default () => new Promise((resolve, reject) => {
log.info('Setting-up packages configuration...')
const packagesDir = 'packages'
// Get packages list
const packages = fs.readdirSync(packagesDir)
.filter((entity) => fs.statSync(path.join(packagesDir, entity)).isDirectory())
// Browse packages
for (let i = 0; i < packages.length; i += 1) {
const configDir = `${packagesDir}/${packages[i]}/config`
const configFile = `${configDir}/config.json`
const configSampleFile = `${configDir}/config.sample.json`
// Check if the config and config.sample file exist
if (fs.existsSync(configFile) && fs.existsSync(configSampleFile)) {
const config = JSON.parse(fs.readFileSync(configFile, 'utf8'))
const configSample = JSON.parse(fs.readFileSync(configSampleFile, 'utf8'))
const configModules = Object.keys(config)
const configSampleModules = Object.keys(configSample)
// Check if there is a new module in the config sample compare to the current config.json
if (JSON.stringify(configModules) !== JSON.stringify(configSampleModules)) {
// Browse modules of the current package
for (let j = 0; j < configSampleModules.length; j += 1) {
// Check if the current module does not exist
if (configModules.includes(configSampleModules[j]) === false) {
log.info(`Adding new module "${string.ucfirst(configSampleModules[j])}" configuration...`)
// Prepare to inject the new module config object
const module = { }
module[configSampleModules[j]] = configSample[configSampleModules[j]]
try {
// Add new module configuration in the config.json file
commandSync(`json -I -f ${configFile} -e 'this.${configSampleModules[j]}=${JSON.stringify(module[configSampleModules[j]])}'`, { shell: true })
log.success(`"${string.ucfirst(configSampleModules[j])}" module configuration added to ${configFile}`)
} catch (e) {
log.error(`Error while adding "${string.ucfirst(configSampleModules[j])}" module configuration to ${configFile}: ${e}`)
reject()
}
}
}
}
} else if (!fs.existsSync(configSampleFile)) {
// Stop the setup if the config.sample.json of the current package does not exist
log.error(`The "${string.ucfirst(packages[i])}" package configuration file does not exist. Try to pull the project (git pull)`)
reject()
} else {
// Duplicate config.sample.json of the current package to config.json
fs.createReadStream(configSampleFile)
.pipe(fs.createWriteStream(`${configDir}/config.json`))
log.success(`"${string.ucfirst(packages[i])}" package configuration file created`)
resolve()
}
}
log.success('Packages configured')
resolve()
})

View File

@ -0,0 +1,76 @@
import { commandSync } from 'execa'
import fs from 'fs'
import path from 'path'
import log from '@/helpers/log'
import domain from '@/helpers/domain'
/**
* Setup packages configuration
*/
export default () => new Promise(async (resolve, reject) => {
log.info('Setting up skills configuration...')
const [domainKeys, domains] = await Promise.all([domain.list(), domain.getDomainsObj()])
for (let i = 0; i < domainKeys.length; i += 1) {
const currentDomain = domains[domainKeys[i]]
const skillKeys = Object.keys(currentDomain.skills)
// Browse skills
for (let j = 0; j < skillKeys.length; j += 1) {
const { name: skillName } = currentDomain.skills[skillKeys[j]]
const currentSkill = currentDomain.skills[skillKeys[j]]
const configDir = path.join(currentSkill.path, 'src')
const configFile = path.join(configDir, 'config.json')
const configSampleFile = path.join(configDir, 'config.sample.json')
// Check if the config and config.sample file exist
if (fs.existsSync(configFile) && fs.existsSync(configSampleFile)) {
const config = JSON.parse(fs.readFileSync(configFile, 'utf8'))?.configurations
const configSample = JSON.parse(fs.readFileSync(configSampleFile, 'utf8'))?.configurations
const configKeys = Object.keys(config)
const configSampleKeys = Object.keys(configSample)
// Check if there is a new config key in the config sample compared to the config.json
if (JSON.stringify(configKeys) !== JSON.stringify(configSampleKeys)) {
// Browse config keys of the new skill config
for (let j = 0; j < configSampleKeys.length; j += 1) {
// Check if the current config key does not exist
if (configKeys.includes(configSampleKeys[j]) === false) {
log.info(`Adding new configuration key "${configSampleKeys[j]}" for the ${skillName} skill...`)
// Prepare to inject the new config key object
const configKey = {
[configSampleKeys[j]]: configSample[configSampleKeys[j]]
}
try {
// Add new module configuration in the config.json file
commandSync(`json -I -f ${configFile} -e 'this.configurations.${configSampleKeys[j]}=${JSON.stringify(configKey[configSampleKeys[j]])}'`, { shell: true })
log.success(`"${configSampleKeys[j]}" configuration key added to ${configFile}`)
} catch (e) {
log.error(`Error while adding "${configSampleKeys[j]}" configuration key to ${configFile}: ${e}`)
reject()
}
}
}
}
} else if (!fs.existsSync(configSampleFile)) {
// Stop the setup if the config.sample.json of the current skill does not exist
log.error(`The "${skillName}" skill configuration file does not exist. Try to pull the project (git pull)`)
reject()
} else {
// Duplicate config.sample.json of the current skill to config.json
fs.createReadStream(configSampleFile)
.pipe(fs.createWriteStream(`${configDir}/config.json`))
log.success(`"${skillName}" skill configuration file created`)
resolve()
}
}
}
log.success('Skills configured')
resolve()
})

View File

@ -5,7 +5,7 @@ import train from '../train'
import generateHttpApiKey from '../generate/generate-http-api-key'
import setupDotenv from './setup-dotenv'
import setupCore from './setup-core'
import setupPackagesConfig from './setup-packages-config'
import setupSkillsConfig from './setup-skills-config'
import setupPythonPackages from './setup-python-packages'
// Do not load ".env" file because it is not created yet
@ -23,7 +23,7 @@ import setupPythonPackages from './setup-python-packages'
loader.start()
await Promise.all([
setupCore(),
setupPackagesConfig()
setupSkillsConfig()
])
await setupPythonPackages()
loader.stop()