@@ -2,7 +2,7 @@ import { prettyPath } from '@ionic/cli-framework/utils/format';
22import { remove , unlink } from '@ionic/utils-fs' ;
33import * as path from 'path' ;
44
5- import { CommandInstanceInfo , CommandLineInputs , CommandLineOptions , CommandMetadata , IProject } from '../definitions' ;
5+ import { CommandInstanceInfo , CommandLineInputs , CommandLineOptions , CommandMetadata , IProject , ProjectIntegration } from '../definitions' ;
66import { input , strong } from '../lib/color' ;
77import { Command } from '../lib/command' ;
88import { FatalException } from '../lib/errors' ;
@@ -19,6 +19,13 @@ This command may be useful when obscure errors or issues are encountered. It rem
1919
2020For Cordova apps, it removes and recreates the generated native project and the native dependencies of your project.
2121` ,
22+ options : [
23+ {
24+ name : 'cordova' ,
25+ summary : 'Only perform the repair steps for Cordova platforms and plugins.' ,
26+ type : Boolean ,
27+ } ,
28+ ] ,
2229 } ;
2330 }
2431
@@ -30,17 +37,40 @@ For Cordova apps, it removes and recreates the generated native project and the
3037 const { pkgManagerArgs } = await import ( '../lib/utils/npm' ) ;
3138 const [ installer , ...installerArgs ] = await pkgManagerArgs ( this . env . config . get ( 'npmClient' ) , { command : 'install' } ) ;
3239
40+ const cordovaOnly = ! ! options [ 'cordova' ] ;
3341 const cordova = this . project . getIntegration ( 'cordova' ) ;
3442
43+ if ( cordovaOnly && ! cordova ) {
44+ throw new FatalException ( `${ input ( '--cordova' ) } was specified, but Cordova has not been added to this project.` ) ;
45+ }
46+
47+ if ( cordova && ! cordova . enabled ) {
48+ this . env . log . warn ( `Cordova integration found, but is disabled--not running repair for Cordova.` ) ;
49+ }
50+
3551 if ( this . env . flags . interactive ) {
36- this . env . log . info (
37- `${ input ( 'ionic repair' ) } will do the following:\n\n` +
38- `- Remove ${ strong ( 'node_modules/' ) } and ${ strong ( 'package-lock.json' ) } \n` +
39- `- Run ${ input ( [ installer , ...installerArgs ] . join ( ' ' ) ) } to restore dependencies\n` +
40- ( cordova && cordova . enabled ?
52+ const steps : string [ ] = [ ] ;
53+
54+ if ( ! cordovaOnly ) {
55+ steps . push (
56+ `- Remove ${ strong ( 'node_modules/' ) } and ${ strong ( 'package-lock.json' ) } \n` +
57+ `- Run ${ input ( [ installer , ...installerArgs ] . join ( ' ' ) ) } to restore dependencies\n`
58+ ) ;
59+ }
60+
61+ if ( cordova && cordova . enabled ) {
62+ steps . push (
4163 `- Remove ${ strong ( 'platforms/' ) } and ${ strong ( 'plugins/' ) } \n` +
42- `- Run ${ input ( 'cordova prepare' ) } to restore platforms and plugins\n` : '' )
43- ) ;
64+ `- Run ${ input ( 'cordova prepare' ) } to restore platforms and plugins\n`
65+ ) ;
66+ }
67+
68+ if ( steps . length === 0 ) {
69+ this . env . log . ok ( `${ input ( 'ionic repair' ) } has nothing to do.` ) ;
70+ throw new FatalException ( '' , 0 ) ;
71+ }
72+
73+ this . env . log . info ( `${ input ( 'ionic repair' ) } will do the following:\n\n` + steps . join ( '' ) ) ;
4474 }
4575
4676 const confirm = await this . env . prompt ( {
@@ -56,8 +86,13 @@ For Cordova apps, it removes and recreates the generated native project and the
5686
5787 this . env . log . nl ( ) ;
5888
59- await this . npmRepair ( this . project ) ;
60- await this . cordovaRepair ( this . project , runinfo ) ;
89+ if ( ! cordovaOnly ) {
90+ await this . npmRepair ( this . project ) ;
91+ }
92+
93+ if ( cordova && cordova . enabled ) {
94+ await this . cordovaRepair ( cordova , runinfo ) ;
95+ }
6196 }
6297
6398 async npmRepair ( project : IProject ) {
@@ -79,23 +114,20 @@ For Cordova apps, it removes and recreates the generated native project and the
79114 await this . env . shell . run ( installer , installerArgs , { cwd : project . directory , stdio : 'inherit' } ) ;
80115 }
81116
82- async cordovaRepair ( project : IProject , runinfo : CommandInstanceInfo ) {
117+ async cordovaRepair ( cordova : Required < ProjectIntegration > , runinfo : CommandInstanceInfo ) {
83118 const tasks = this . createTaskChain ( ) ;
84- const cordova = project . getIntegration ( 'cordova' ) ;
85119
86- if ( cordova && cordova . enabled ) {
87- const platformsDir = path . resolve ( cordova . root , 'platforms' ) ;
88- const pluginsDir = path . resolve ( cordova . root , 'plugins' ) ;
120+ const platformsDir = path . resolve ( cordova . root , 'platforms' ) ;
121+ const pluginsDir = path . resolve ( cordova . root , 'plugins' ) ;
89122
90- tasks . next ( `Removing ${ strong ( prettyPath ( platformsDir ) ) } ` ) ;
91- await remove ( platformsDir ) ;
123+ tasks . next ( `Removing ${ strong ( prettyPath ( platformsDir ) ) } ` ) ;
124+ await remove ( platformsDir ) ;
92125
93- tasks . next ( `Removing ${ strong ( prettyPath ( pluginsDir ) ) } ` ) ;
94- await remove ( pluginsDir ) ;
126+ tasks . next ( `Removing ${ strong ( prettyPath ( pluginsDir ) ) } ` ) ;
127+ await remove ( pluginsDir ) ;
95128
96- tasks . end ( ) ;
129+ tasks . end ( ) ;
97130
98- await runCommand ( runinfo , [ 'cordova' , 'prepare' , '--no-build' ] ) ;
99- }
131+ await runCommand ( runinfo , [ 'cordova' , 'prepare' , '--no-build' ] ) ;
100132 }
101133}
0 commit comments