diff --git a/packages/feflow-cli/src/core/native/install.ts b/packages/feflow-cli/src/core/native/install.ts index 50ca2383..43bf1bcb 100644 --- a/packages/feflow-cli/src/core/native/install.ts +++ b/packages/feflow-cli/src/core/native/install.ts @@ -684,13 +684,15 @@ async function uninstallUniversalPlugin(ctx: any, pluginName: string) { try { removeInvalidPkg(ctx); plugin?.postUninstall?.runLess(); - logger.info('uninstall success'); + logger.info(`uninstall success ${pluginName}`); } catch (e) { - logger.info(`uninstall succeeded, but failed to clean the data, ${e}`); + logger.info( + `uninstall ${pluginName} succeeded, but failed to clean the data, ${e}` + ); } } -async function uninstallNpmPlugin(ctx: any, dependencies: []) { +async function uninstallNpmPlugin(ctx: any, dependencies: [] | [any]) { const { logger, root, @@ -725,7 +727,8 @@ async function uninstallNpmPlugin(ctx: any, dependencies: []) { false, true ).then(() => { - ctx.logger.info('uninstall success'); + const deps = dependencies.join(' '); + ctx.logger.info(`uninstall success ${deps}`); }); } @@ -802,16 +805,18 @@ async function updatePlugin(ctx: any, pkg: string, version: string) { module.exports = (ctx: any) => { ctx.commander.register('install', 'Install a devkit or plugin', async () => { const dependencies = ctx.args['_']; - const installPluginStr = dependencies[0]; - if (!installPluginStr) { + if (!dependencies.length) { ctx.logger.error('parameter error'); return; } - try { - await installPlugin(ctx, installPluginStr, true); - } catch (e) { - ctx.logger.error(`install error: ${JSON.stringify(e)}`); - process.exit(2); + // eslint-disable-next-line + for (const installPluginStr of dependencies) { + try { + await installPlugin(ctx, installPluginStr, true); + } catch (e) { + ctx.logger.error(`install error: ${JSON.stringify(e)}`); + process.exit(2); + } } }); @@ -820,6 +825,12 @@ module.exports = (ctx: any) => { 'Uninstall a devkit or plugin', async () => { const dependencies = ctx.args['_']; + if (!dependencies.length) { + ctx.logger.error( + 'parameter error, you need to specify specify the plugins' + ); + return; + } ctx.logger.info( 'Uninstalling packages. This might take a couple of minutes.' ); @@ -828,16 +839,17 @@ module.exports = (ctx: any) => { return uninstallNpmPlugin(ctx, dependencies); } const { universalPkg } = ctx; - const installPluginStr = dependencies[0]; - const pkgInfo = await getPkgInfo(ctx, installPluginStr); - if (pkgInfo && universalPkg.isInstalled(pkgInfo.repoName)) { - return uninstallUniversalPlugin(ctx, pkgInfo.repoName); - } - - await uninstallNpmPlugin(ctx, dependencies); - const pickerConfig = new CommandPickConfig(ctx); - pickerConfig.removeCache(dependencies[0]); + // eslint-disable-next-line + for (const installPluginStr of dependencies) { + const pkgInfo = await getPkgInfo(ctx, installPluginStr); + if (pkgInfo && universalPkg.isInstalled(pkgInfo.repoName)) { + await uninstallUniversalPlugin(ctx, pkgInfo.repoName); + } else { + await uninstallNpmPlugin(ctx, [installPluginStr]); + } + pickerConfig.removeCache(installPluginStr); + } } ); }; diff --git a/packages/feflow-cli/src/core/native/upgrade.ts b/packages/feflow-cli/src/core/native/upgrade.ts index 3abb5b1c..5e7da49a 100644 --- a/packages/feflow-cli/src/core/native/upgrade.ts +++ b/packages/feflow-cli/src/core/native/upgrade.ts @@ -1,3 +1,5 @@ +import path from 'path'; +import fs from 'fs'; import spawn from 'cross-spawn'; import { getRegistryUrl } from '../../shared/npm'; import packageJson from '../../shared/packageJson'; @@ -6,6 +8,8 @@ import chalk from 'chalk'; import inquirer from 'inquirer'; import { safeDump } from '../../shared/yaml'; +const { installPlugin, getPkgInfo } = require('./install'); + async function updateCli(packageManager: string) { return new Promise((resolve, reject) => { const args = @@ -84,8 +88,50 @@ async function checkCliUpdate(ctx: any) { } module.exports = (ctx: any) => { - ctx.commander.register('upgrade', 'upgrade fef cli', () => { - checkCliUpdate(ctx); + ctx.commander.register('upgrade', 'upgrade fef cli', async () => { + const dependencies = ctx.args['_']; + const npmPluginInfoPath = path.join(ctx.root, 'package.json'); + const npmPluginInfoJson = fs.existsSync(npmPluginInfoPath) + ? require(npmPluginInfoPath) + : {}; + if (dependencies.length) { + try { + // eslint-disable-next-line + for (const installPluginStr of dependencies) { + // 判断是 universalPkg 且已安装 + const pkgInfo = await getPkgInfo(ctx, installPluginStr); + const { universalPkg } = ctx; + if (pkgInfo && universalPkg.isInstalled(pkgInfo.repoName)) { + await installPlugin(ctx, installPluginStr, true); + continue; + } + + // 判断是 npmPlugin 且已安装 + const splits = installPluginStr.split('@'); + let [pluginName] = splits; + if (splits.length === 3) { + splits.pop(); + pluginName = splits.join('@'); + } else if (splits.length === 2) { + pluginName = installPluginStr; + } + if (npmPluginInfoJson?.dependencies?.[pluginName]) { + await installPlugin(ctx, installPluginStr, true); + continue; + } + + ctx.logger.warn( + `${installPluginStr} is not installed, you need to install it before upgrade` + ); + } + } catch (e) { + ctx.logger.error(`install error: ${e}`); + process.exit(2); + } + } else { + // 没有参数则更新 cli + checkCliUpdate(ctx); + } }); };