Skip to content

Commit

Permalink
fix(cli): show error if npm install on migration failed (#5904)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Sep 13, 2022
1 parent c41d28f commit aa60a75
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions cli/src/tasks/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { writeFileSync, readFileSync, existsSync } from '@ionic/utils-fs';
import { join } from 'path';
import rimraf from 'rimraf';

import c from '../colors';
import { runTask } from '../common';
import type { Config } from '../definitions';
import { fatal } from '../errors';
import { logger, logPrompt, logSuccess } from '../log';
import { deleteFolderRecursive } from '../util/fs';
import { getCommandOutput } from '../util/subprocess';
import { runCommand, getCommandOutput } from '../util/subprocess';
import { extractTemplate } from '../util/template';
import { readXML } from '../util/xml';

Expand Down Expand Up @@ -105,16 +106,32 @@ export async function migrateCommand(config: Config): Promise<void> {
typeof npmInstallConfirm === 'string' &&
npmInstallConfirm.toLowerCase() === 'y';

await runTask(`Installing Latest NPM Modules.`, () => {
return installLatestNPMLibs(runNpmInstall, config);
});
try {
await runTask(`Installing Latest NPM Modules.`, () => {
return installLatestNPMLibs(runNpmInstall, config);
});
} catch (ex) {
logger.error(
`npm install failed. Try deleting node_modules folder and running ${c.input(
'npm install --force',
)} manually.`,
);
}

await runTask(
`Migrating @capacitor/storage to @capacitor/preferences.`,
() => {
return migrateStoragePluginToPreferences(runNpmInstall);
},
);
try {
await runTask(
`Migrating @capacitor/storage to @capacitor/preferences.`,
() => {
return migrateStoragePluginToPreferences(runNpmInstall);
},
);
} catch (ex) {
logger.error(
`@capacitor/preferences failed to install. Try deleting node_modules folder and running ${c.input(
'npm install @capacitor/preferences --force',
)} manually.`,
);
}

if (
allDependencies['@capacitor/ios'] &&
Expand Down Expand Up @@ -376,9 +393,8 @@ async function installLatestNPMLibs(runInstall: boolean, config: Config) {
});

if (runInstall) {
rimraf.sync(join(config.app.rootDir, 'package-lock.json'));
rimraf.sync(join(config.app.rootDir, 'node_modules/@capacitor/!(cli)'));
await getCommandOutput('npm', ['i']);
await runCommand('npm', ['i']);
} else {
logger.info(
`Please run an install command with your package manager of choice. (ex: yarn install)`,
Expand All @@ -393,10 +409,7 @@ async function migrateStoragePluginToPreferences(runInstall: boolean) {
);
if (runInstall) {
await getCommandOutput('npm', ['uninstall', '@capacitor/storage']);
await getCommandOutput('npm', [
'i',
`@capacitor/preferences@${pluginVersion}`,
]);
await runCommand('npm', ['i', `@capacitor/preferences@${pluginVersion}`]);
} else {
logger.info(
`Please manually uninstall @capacitor/storage and replace it with @capacitor/preferences@${pluginVersion}`,
Expand Down

0 comments on commit aa60a75

Please sign in to comment.