mirror of
https://github.com/leon-ai/leon.git
synced 2024-12-25 09:44:22 +03:00
feat(scripts): install Node.js skills packages
This commit is contained in:
parent
2c2f49290a
commit
224158812f
84
scripts/setup/install-nodejs-skills-packages.js
Normal file
84
scripts/setup/install-nodejs-skills-packages.js
Normal file
@ -0,0 +1,84 @@
|
||||
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'
|
||||
|
||||
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(
|
||||
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 --prefix ${skillSRCPath}`, {
|
||||
shell: true,
|
||||
stdio: 'inherit'
|
||||
})
|
||||
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}`
|
||||
)
|
||||
}
|
||||
}
|
@ -19,8 +19,8 @@ export default () =>
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
|
||||
// Browse skills
|
||||
for (let j = 0; j < skillKeys.length; j += 1) {
|
||||
const skillFriendlyName = skillKeys[j]
|
||||
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')
|
||||
|
@ -8,6 +8,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 setupBinaries from './setup-binaries'
|
||||
import createInstanceID from './create-instance-id'
|
||||
|
||||
@ -21,6 +22,7 @@ import createInstanceID from './create-instance-id'
|
||||
await setupDotenv()
|
||||
LoaderHelper.start()
|
||||
await Promise.all([setupCore(), setupSkillsConfig()])
|
||||
await installNodeJSSkillsPackages()
|
||||
LoaderHelper.stop()
|
||||
await setupBinaries()
|
||||
await generateHTTPAPIKey()
|
||||
|
@ -1,8 +1,16 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
import type { ShortLanguageCode } from '@/types'
|
||||
import { GLOBAL_DATA_PATH } from '@/constants'
|
||||
|
||||
/**
|
||||
* Files
|
||||
*/
|
||||
export async function isFileEmpty(path: string): Promise<boolean> {
|
||||
return (await fs.promises.readFile(path)).length === 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Paths
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user