mirror of
https://github.com/leon-ai/leon.git
synced 2024-08-17 06:00:33 +03:00
refactor(scripts): unify skills setup over one iteration only
This commit is contained in:
parent
0283878a90
commit
a3b8c40714
@ -1,94 +0,0 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
import { command } from 'execa'
|
||||
|
||||
import { isFileEmpty } from '@/utilities'
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
/**
|
||||
* Install Node.js skills packages on setup
|
||||
* 1. Browse skills
|
||||
* 2. If skill is Node.js, then verify if an installation is needed
|
||||
* 3. If install is needed, then install npm packages
|
||||
*/
|
||||
export default async function () {
|
||||
try {
|
||||
LogHelper.info('Installing Node.js skills npm packages...')
|
||||
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
for (const currentDomain of skillDomains.values()) {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
|
||||
// Browse skills
|
||||
for (let i = 0; i < skillKeys.length; i += 1) {
|
||||
const skillFriendlyName = skillKeys[i]
|
||||
const currentSkill = currentDomain.skills[skillFriendlyName]
|
||||
|
||||
if (currentSkill.bridge === 'nodejs') {
|
||||
const skillSRCPath = path.join(currentSkill.path, 'src')
|
||||
const skillPackageJSONPath = path.join(skillSRCPath, 'package.json')
|
||||
|
||||
if (fs.existsSync(skillPackageJSONPath)) {
|
||||
const isPackageJSONEmpty = await isFileEmpty(skillPackageJSONPath)
|
||||
|
||||
if (!isPackageJSONEmpty) {
|
||||
const packageJSONMtime = (
|
||||
await fs.promises.stat(skillPackageJSONPath)
|
||||
).mtime
|
||||
const lastSkillNPMInstallFilePath = path.join(
|
||||
skillSRCPath,
|
||||
'.last-skill-npm-install'
|
||||
)
|
||||
|
||||
if (fs.existsSync(lastSkillNPMInstallFilePath)) {
|
||||
const lastSkillNPMInstallTime = new Date(
|
||||
Number(
|
||||
await fs.promises.readFile(
|
||||
lastSkillNPMInstallFilePath,
|
||||
'utf8'
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
if (packageJSONMtime <= lastSkillNPMInstallTime) {
|
||||
LogHelper.success(
|
||||
`"${skillFriendlyName}" skill npm packages are up-to-date`
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
LogHelper.info(
|
||||
`Installing npm packages for the "${skillFriendlyName}" skill...`
|
||||
)
|
||||
|
||||
await command(
|
||||
`npm install --package-lock=false --prefix ${skillSRCPath}`,
|
||||
{
|
||||
shell: true
|
||||
}
|
||||
)
|
||||
await fs.promises.writeFile(
|
||||
lastSkillNPMInstallFilePath,
|
||||
`${Date.now()}`
|
||||
)
|
||||
|
||||
LogHelper.success(
|
||||
`"${skillFriendlyName}" skill npm packages installed`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LogHelper.success('npm packages for Node.js skills installed')
|
||||
} catch (e) {
|
||||
LogHelper.error(
|
||||
`Failed to install Node.js skills third-party dependencies: ${e}`
|
||||
)
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
import { commandSync } from 'execa'
|
||||
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
/**
|
||||
* Set up skills configuration
|
||||
*/
|
||||
export default () =>
|
||||
new Promise(async (resolve, reject) => {
|
||||
LogHelper.info('Setting up skills configuration...')
|
||||
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
for (const currentDomain of skillDomains.values()) {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
|
||||
// Browse skills
|
||||
for (let i = 0; i < skillKeys.length; i += 1) {
|
||||
const skillFriendlyName = skillKeys[i]
|
||||
const currentSkill = currentDomain.skills[skillFriendlyName]
|
||||
const configDir = path.join(currentSkill.path, 'src')
|
||||
const configFile = path.join(configDir, 'config.json')
|
||||
const configSampleFile = path.join(configDir, 'config.sample.json')
|
||||
|
||||
// If there is a bridge set from the skill config
|
||||
if (currentSkill.bridge) {
|
||||
// Check if the config and config.sample file exist
|
||||
if (fs.existsSync(configFile) && fs.existsSync(configSampleFile)) {
|
||||
const config = JSON.parse(
|
||||
await fs.promises.readFile(configFile, 'utf8')
|
||||
)?.configurations
|
||||
const configSample = JSON.parse(
|
||||
await fs.promises.readFile(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) {
|
||||
LogHelper.info(
|
||||
`Adding new configuration key "${configSampleKeys[j]}" for the ${skillFriendlyName} skill...`
|
||||
)
|
||||
|
||||
// Prepare to inject the new config key object
|
||||
const configKey = {
|
||||
[configSampleKeys[j]]: configSample[configSampleKeys[j]]
|
||||
}
|
||||
|
||||
try {
|
||||
// Add new skill configuration in the config.json file
|
||||
commandSync(
|
||||
`json -I -f ${configFile} -e 'this.configurations.${
|
||||
configSampleKeys[j]
|
||||
}=${JSON.stringify(configKey[configSampleKeys[j]])}'`,
|
||||
{ shell: true }
|
||||
)
|
||||
LogHelper.success(
|
||||
`"${configSampleKeys[j]}" configuration key added to ${configFile}`
|
||||
)
|
||||
} catch (e) {
|
||||
LogHelper.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
|
||||
LogHelper.error(
|
||||
`The "${skillFriendlyName}" 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`)
|
||||
)
|
||||
|
||||
LogHelper.success(
|
||||
`"${skillFriendlyName}" skill configuration file created`
|
||||
)
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LogHelper.success('Skills configured')
|
||||
resolve()
|
||||
})
|
65
scripts/setup/setup-skills/install-nodejs-skills-packages.js
Normal file
65
scripts/setup/setup-skills/install-nodejs-skills-packages.js
Normal file
@ -0,0 +1,65 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
import { command } from 'execa'
|
||||
|
||||
import { isFileEmpty } from '@/utilities'
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
|
||||
/**
|
||||
* Install Node.js skills packages on setup
|
||||
* 1. If skill is Node.js, then verify if an installation is needed
|
||||
* 2. If install is needed, then install npm packages
|
||||
*/
|
||||
export default async function (skillFriendlyName, currentSkill) {
|
||||
if (currentSkill.bridge === 'nodejs') {
|
||||
const skillSRCPath = path.join(currentSkill.path, 'src')
|
||||
const skillPackageJSONPath = path.join(skillSRCPath, 'package.json')
|
||||
|
||||
if (fs.existsSync(skillPackageJSONPath)) {
|
||||
const isPackageJSONEmpty = await isFileEmpty(skillPackageJSONPath)
|
||||
|
||||
if (!isPackageJSONEmpty) {
|
||||
const packageJSONMtime = (await fs.promises.stat(skillPackageJSONPath))
|
||||
.mtime
|
||||
const lastSkillNPMInstallFilePath = path.join(
|
||||
skillSRCPath,
|
||||
'.last-skill-npm-install'
|
||||
)
|
||||
|
||||
if (fs.existsSync(lastSkillNPMInstallFilePath)) {
|
||||
const lastSkillNPMInstallTime = new Date(
|
||||
Number(
|
||||
await fs.promises.readFile(lastSkillNPMInstallFilePath, 'utf8')
|
||||
)
|
||||
)
|
||||
|
||||
if (packageJSONMtime <= lastSkillNPMInstallTime) {
|
||||
LogHelper.success(
|
||||
`"${skillFriendlyName}" skill npm packages are up-to-date`
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
LogHelper.info(
|
||||
`Installing npm packages for the "${skillFriendlyName}" skill...`
|
||||
)
|
||||
|
||||
await command(
|
||||
`npm install --package-lock=false --prefix ${skillSRCPath}`,
|
||||
{
|
||||
shell: true
|
||||
}
|
||||
)
|
||||
await fs.promises.writeFile(
|
||||
lastSkillNPMInstallFilePath,
|
||||
`${Date.now()}`
|
||||
)
|
||||
|
||||
LogHelper.success(`"${skillFriendlyName}" skill npm packages installed`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
79
scripts/setup/setup-skills/setup-skills-config.js
Normal file
79
scripts/setup/setup-skills/setup-skills-config.js
Normal file
@ -0,0 +1,79 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
import { commandSync } from 'execa'
|
||||
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
|
||||
/**
|
||||
* Set up skills configuration
|
||||
*/
|
||||
export default async function (skillFriendlyName, currentSkill) {
|
||||
const configDir = path.join(currentSkill.path, 'src')
|
||||
const configFile = path.join(configDir, 'config.json')
|
||||
const configSampleFile = path.join(configDir, 'config.sample.json')
|
||||
|
||||
// If there is a bridge set from the skill config
|
||||
if (currentSkill.bridge) {
|
||||
// Check if the config and config.sample file exist
|
||||
if (fs.existsSync(configFile) && fs.existsSync(configSampleFile)) {
|
||||
const config = JSON.parse(
|
||||
await fs.promises.readFile(configFile, 'utf8')
|
||||
)?.configurations
|
||||
const configSample = JSON.parse(
|
||||
await fs.promises.readFile(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) {
|
||||
LogHelper.info(
|
||||
`Adding new configuration key "${configSampleKeys[j]}" for the ${skillFriendlyName} skill...`
|
||||
)
|
||||
|
||||
// Prepare to inject the new config key object
|
||||
const configKey = {
|
||||
[configSampleKeys[j]]: configSample[configSampleKeys[j]]
|
||||
}
|
||||
|
||||
try {
|
||||
// Add new skill configuration in the config.json file
|
||||
commandSync(
|
||||
`json -I -f ${configFile} -e 'this.configurations.${
|
||||
configSampleKeys[j]
|
||||
}=${JSON.stringify(configKey[configSampleKeys[j]])}'`,
|
||||
{ shell: true }
|
||||
)
|
||||
LogHelper.success(
|
||||
`"${configSampleKeys[j]}" configuration key added to ${configFile}`
|
||||
)
|
||||
} catch (e) {
|
||||
LogHelper.error(
|
||||
`Error while adding "${configSampleKeys[j]}" configuration key to ${configFile}: ${e}`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (!fs.existsSync(configSampleFile)) {
|
||||
// Stop the setup if the config.sample.json of the current skill does not exist
|
||||
LogHelper.error(
|
||||
`The "${skillFriendlyName}" skill configuration file does not exist. Try to pull the project (git pull)`
|
||||
)
|
||||
} else {
|
||||
// Duplicate config.sample.json of the current skill to config.json
|
||||
fs.createReadStream(configSampleFile).pipe(
|
||||
fs.createWriteStream(`${configDir}/config.json`)
|
||||
)
|
||||
|
||||
LogHelper.success(
|
||||
`"${skillFriendlyName}" skill configuration file created`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
37
scripts/setup/setup-skills/setup-skills.js
Normal file
37
scripts/setup/setup-skills/setup-skills.js
Normal file
@ -0,0 +1,37 @@
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
import setupSkillsConfig from './setup-skills-config'
|
||||
import installNodejsSkillsPackages from './install-nodejs-skills-packages'
|
||||
|
||||
/**
|
||||
* Browse skills and set them up
|
||||
*/
|
||||
export default async function () {
|
||||
LogHelper.info('Setting up skills...')
|
||||
|
||||
try {
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
for (const currentDomain of skillDomains.values()) {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
|
||||
// Browse skills
|
||||
for (let i = 0; i < skillKeys.length; i += 1) {
|
||||
const skillFriendlyName = skillKeys[i]
|
||||
const currentSkill = currentDomain.skills[skillFriendlyName]
|
||||
|
||||
LogHelper.info(`Setting up "${skillFriendlyName}" skill...`)
|
||||
|
||||
await setupSkillsConfig(skillFriendlyName, currentSkill)
|
||||
await installNodejsSkillsPackages(skillFriendlyName, currentSkill)
|
||||
|
||||
LogHelper.success(`"${skillFriendlyName}" skill set up`)
|
||||
}
|
||||
}
|
||||
|
||||
LogHelper.success('Skills are set up')
|
||||
} catch (e) {
|
||||
LogHelper.error(`Failed to set up skills: ${e}`)
|
||||
}
|
||||
}
|
@ -7,8 +7,7 @@ import generateJSONSchemas from '../generate/generate-json-schemas'
|
||||
|
||||
import setupDotenv from './setup-dotenv'
|
||||
import setupCore from './setup-core'
|
||||
import setupSkillsConfig from './setup-skills-config'
|
||||
import installNodeJSSkillsPackages from './install-nodejs-skills-packages'
|
||||
import setupSkills from './setup-skills/setup-skills'
|
||||
import setupBinaries from './setup-binaries'
|
||||
import createInstanceID from './create-instance-id'
|
||||
|
||||
@ -21,8 +20,8 @@ import createInstanceID from './create-instance-id'
|
||||
try {
|
||||
await setupDotenv()
|
||||
LoaderHelper.start()
|
||||
await Promise.all([setupCore(), setupSkillsConfig()])
|
||||
await installNodeJSSkillsPackages()
|
||||
await setupCore()
|
||||
await setupSkills()
|
||||
LoaderHelper.stop()
|
||||
await setupBinaries()
|
||||
await generateHTTPAPIKey()
|
||||
|
Loading…
Reference in New Issue
Block a user