Skip to content

Commit

Permalink
fix: only set skill config when it is a logic type
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Mar 1, 2022
1 parent f4f9fff commit 9ce9a8b
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions scripts/setup/setup-skills-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,50 @@ export default () => new Promise(async (resolve, reject) => {
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)
if (currentSkill.type === 'logic') {
// 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 ${skillFriendlyName} skill...`)
// 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 ${skillFriendlyName} skill...`)

// Prepare to inject the new config key object
const configKey = {
[configSampleKeys[j]]: configSample[configSampleKeys[j]]
}
// 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 })
log.success(`"${configSampleKeys[j]}" configuration key added to ${configFile}`)
} catch (e) {
log.error(`Error while adding "${configSampleKeys[j]}" configuration key to ${configFile}: ${e}`)
reject()
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 })
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 "${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`))
} else if (!fs.existsSync(configSampleFile)) {
// Stop the setup if the config.sample.json of the current skill does not exist
log.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`))

log.success(`"${skillFriendlyName}" skill configuration file created`)
resolve()
log.success(`"${skillFriendlyName}" skill configuration file created`)
resolve()
}
}
}
}
Expand Down

0 comments on commit 9ce9a8b

Please sign in to comment.