2023-05-08 16:23:33 +03:00
|
|
|
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'
|
|
|
|
|
2023-05-08 16:35:14 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-05-08 16:23:33 +03:00
|
|
|
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(
|
2023-05-08 16:45:06 +03:00
|
|
|
Number(
|
|
|
|
await fs.promises.readFile(
|
|
|
|
lastSkillNPMInstallFilePath,
|
|
|
|
'utf8'
|
|
|
|
)
|
2023-05-08 16:23:33 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (packageJSONMtime <= lastSkillNPMInstallTime) {
|
|
|
|
LogHelper.success(
|
2023-05-08 16:45:06 +03:00
|
|
|
`"${skillFriendlyName}" skill npm packages are up-to-date`
|
2023-05-08 16:23:33 +03:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogHelper.info(
|
2023-05-08 16:45:06 +03:00
|
|
|
`Installing npm packages for the "${skillFriendlyName}" skill...`
|
2023-05-08 16:23:33 +03:00
|
|
|
)
|
|
|
|
|
2023-05-08 16:45:06 +03:00
|
|
|
await command(
|
|
|
|
`npm install --package-lock=false --prefix ${skillSRCPath}`,
|
|
|
|
{
|
2023-05-08 16:57:41 +03:00
|
|
|
shell: true
|
2023-05-08 16:45:06 +03:00
|
|
|
}
|
|
|
|
)
|
2023-05-08 16:23:33 +03:00
|
|
|
await fs.promises.writeFile(
|
|
|
|
lastSkillNPMInstallFilePath,
|
|
|
|
`${Date.now()}`
|
|
|
|
)
|
|
|
|
|
|
|
|
LogHelper.success(
|
2023-05-08 16:45:06 +03:00
|
|
|
`"${skillFriendlyName}" skill npm packages installed`
|
2023-05-08 16:23:33 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogHelper.success('npm packages for Node.js skills installed')
|
|
|
|
} catch (e) {
|
|
|
|
LogHelper.error(
|
|
|
|
`Failed to install Node.js skills third-party dependencies: ${e}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|