import { ProjectInterface , LearnEvent , Skill } from '@alfred/types' ; import mergeConfigs from '@alfred/merge-configs' ; import Config from '../config' ; import { requireSkill } from '../skill' ; export default async function learn ( project , skillsPkgNames ) { const { config } = project ; const beforeLearnEvent = { skillsPkgNames , skills : [ ] , } ; await project . emitAsync ( 'beforeLearn' , beforeLearnEvent ) ; const newConfig = new Config ( mergeConfigs ( { } , config , { skills : skillsPkgNames } ) ) ; project . setConfig ( newConfig ) ; await project . installDeps ( skillsPkgNames , 'dev' ) ; const skillsToLearn = skillsPkgNames . map ( requireSkill ) ; const { dependencies , devDependencies } = await project . findDepsToInstall ( skillsToLearn ) ; await project . installDeps ( dependencies , 'dep' ) ; await project . installDeps ( devDependencies , 'dev' ) ; await newConfig . write ( project . pkgPath , mergeConfigs ( { } , project . pkg . alfred || { } , { skills : skillsPkgNames , } ) ) ; const skillMap = await project . getSkillMap ( ) ; const learnedSkills = skillsToLearn . map ( ( skill ) => skillMap . get ( skill . name ) as Skill ) ; const event = { skillsPkgNames , skills : learnedSkills } ; await project . emitAsync ( 'afterLearn' , event ) ; }