import { semverGt, getPackageInNodeModulesDir, semverValid, getYamlConfigContent, } from '@ones-open/cli-utils' import { ABILITY_VERSION_JSON_NAME, DEFAULT_ABILITY_PACKAGE_NAME, PLUGIN_CONFIG_RELATIVE_PATH, } from '@ones-open/cli-utils' import type { ProjectPluginConfig, AbilityVersionJSON, ProjectPluginConfigAbilitiesField, } from '@ones-open/cli-utils' import fse from 'fs-extra' import { join, relative } from 'path' import { cwd } from 'process' const { readJson } = fse interface GetMinSystemVersion { pluginConfigAbilities: ProjectPluginConfigAbilitiesField[] currentWorkingDirectory?: string } async function getAbilityVersionJSONPath(currentWorkingDirectory: string) { const abilityPackagePath = getPackageInNodeModulesDir( currentWorkingDirectory, DEFAULT_ABILITY_PACKAGE_NAME, ) const path = join(abilityPackagePath, ABILITY_VERSION_JSON_NAME) return path } async function getMinSystemVersion({ pluginConfigAbilities, currentWorkingDirectory = cwd(), }: GetMinSystemVersion) { // gets the user-defined min_system_version const pluginConfigPath = join(currentWorkingDirectory, PLUGIN_CONFIG_RELATIVE_PATH) const pluginConfigContent = await getYamlConfigContent(pluginConfigPath) const { min_system_version: user_min_system_version } = pluginConfigContent.service const abilityVersionJSONPath = await getAbilityVersionJSONPath(currentWorkingDirectory) // gets the ability list data const abilityList: AbilityVersionJSON = await readJson(abilityVersionJSONPath) const map = new Map() for (const ability of abilityList) { const { ability_type, ability_version, constraints } = ability const [ones_release_version] = constraints.onesVersion.range map.set(`${ability_type}@${ability_version}`, ones_release_version) } let min_system_version = user_min_system_version ?? '0.0.1' const errorMap = new Map() for (const ability of pluginConfigAbilities) { const { version, abilityType } = ability const key = `${abilityType}@${version}` const ones_release_version = map.get(key) if (!semverValid(ones_release_version)) { errorMap.set(key, `ones_release_version('${ones_release_version}') is invalid from ${key}`) } if (errorMap.size) { continue } min_system_version = semverGt(ones_release_version, min_system_version) ? ones_release_version : min_system_version } if (errorMap.size) { let errorMessage = '' errorMap.forEach((value) => { errorMessage += '\n' + value }) throw new Error( errorMessage + `\nPlease check the file \`${relative( currentWorkingDirectory, abilityVersionJSONPath, )}\` on current directory`, ) } return min_system_version } export { getMinSystemVersion }