import output from '@/output' import { installProjectNpmDependencies } from '@/scripts/shared' import { getYamlConfigContent, getPackageInNodeModulesDir, updateYamlConfigContent, getPackageJsonContent, getPackageDependencies, } from '@ones-open/cli-utils' import { DEFAULT_CLI_PLUGIN_PACKAGE_NAME, DEFAULT_ABILITY_PACKAGE_NAME, DEFAULT_TEMPLATE_PACKAGE_NAME, PROJECT_CONFIG_DIR_NAME, PLUGIN_CONFIG_FILE_NAME, CI_DEPLOY_FILE_NAME, UPGRADE_CONFIG_FILE_NAME, } from '@ones-open/cli-utils' import { merge, clone } from '@senojs/lodash' import fse from 'fs-extra' import type { ListrTask } from 'listr' import { join } from 'path' import { cwd } from 'process' const { writeJson } = fse function createPluginUpgradeTasks(lastestVersion: string) { const upgradeTasks: ListrTask[] = [ { title: 'Update package.json', task: async () => { const updatePackages = [DEFAULT_ABILITY_PACKAGE_NAME, DEFAULT_TEMPLATE_PACKAGE_NAME] const pluginPackageJSONPath = join(cwd(), 'package.json') const currentPackageContent = await getPackageJsonContent(pluginPackageJSONPath) const newPackageContent = clone(currentPackageContent) const depencies = await getPackageDependencies( DEFAULT_CLI_PLUGIN_PACKAGE_NAME, lastestVersion, ) // @ones-open/cli-plugin 直接变更 if (newPackageContent?.devDependencies?.[DEFAULT_CLI_PLUGIN_PACKAGE_NAME]) { newPackageContent.devDependencies[DEFAULT_CLI_PLUGIN_PACKAGE_NAME] = lastestVersion } /** * 依赖变更 * 这里之所以没有处理[~^]这种限定符是因为依赖信息中已有了 */ updatePackages.forEach((pck) => { if (newPackageContent?.devDependencies?.[pck]) { newPackageContent.devDependencies[pck] = `${depencies[pck]}` } }) await writeJson(pluginPackageJSONPath, newPackageContent, { spaces: 2 }) }, }, { title: 'Install dependencies', task: () => { return installProjectNpmDependencies(cwd()) }, }, { title: 'Merge the config', task: async () => { // file name array which need to merge const mergeFileNames = [ PLUGIN_CONFIG_FILE_NAME, CI_DEPLOY_FILE_NAME, UPGRADE_CONFIG_FILE_NAME, ] const promiseList = [] const pluginConfigRootPath = join(cwd(), PROJECT_CONFIG_DIR_NAME) const templatePackagePath = getPackageInNodeModulesDir(cwd(), DEFAULT_TEMPLATE_PACKAGE_NAME) const templateConfigRootPath = join(templatePackagePath, 'template', 'config') for (let i = 0; i < mergeFileNames.length; i++) { const fileName = mergeFileNames[i] const pluginConfigPath = join(pluginConfigRootPath, fileName) const templateConfigPath = join(templateConfigRootPath, fileName) const pluginConfigContent = await getYamlConfigContent(pluginConfigPath) const templateConfigContent = await getYamlConfigContent(templateConfigPath) const mergedConfigContent = merge(templateConfigContent, pluginConfigContent) promiseList.push(() => updateYamlConfigContent(pluginConfigPath, mergedConfigContent)) } try { await Promise.all(promiseList.map((fn) => fn?.())) } catch (e) { output.error(e) } }, }, ] return upgradeTasks } export { createPluginUpgradeTasks }