1
+ import { ERROR_SHELL_COMMAND_NOT_FOUND , LOGGER_LEVELS , OptionGroup , ShellCommandError , createPrefixedFormatter } from '@ionic/cli-framework' ;
1
2
import { prettyPath } from '@ionic/cli-framework/utils/format' ;
2
3
import { cacheFileChecksum , copy , pathExists } from '@ionic/utils-fs' ;
3
4
import chalk from 'chalk' ;
4
5
import * as Debug from 'debug' ;
5
6
6
7
import { CommandInstanceInfo , CommandLineInputs , CommandLineOptions , CommandMetadata , CommandPreRun , KnownPlatform , ResourcesConfig , ResourcesImageConfig , SourceImage } from '../../definitions' ;
7
8
import { FatalException } from '../../lib/errors' ;
9
+ import { createDefaultLoggerHandlers } from '../../lib/utils/logger' ;
10
+ import { pkgManagerArgs } from '../../lib/utils/npm' ;
8
11
9
12
import { CordovaCommand } from './base' ;
10
13
@@ -63,6 +66,12 @@ This command uses Ionic servers, so we require you to be logged into your free I
63
66
type : Boolean ,
64
67
aliases : [ 's' ] ,
65
68
} ,
69
+ {
70
+ name : 'cordova-res' ,
71
+ summary : `Use ${ chalk . green ( 'cordova-res' ) } instead of Ionic resource server` ,
72
+ type : Boolean ,
73
+ groups : [ OptionGroup . Hidden ] ,
74
+ } ,
66
75
] ,
67
76
} ;
68
77
}
@@ -74,7 +83,7 @@ This command uses Ionic servers, so we require you to be logged into your free I
74
83
75
84
const isLoggedIn = this . env . session . isLoggedIn ( ) ;
76
85
77
- if ( ! isLoggedIn ) {
86
+ if ( ! options [ 'cordova-res' ] && ! isLoggedIn ) {
78
87
this . env . log . warn ( `You need to be logged into your Ionic account in order to run ${ chalk . green ( `ionic cordova resources` ) } .\n` ) ;
79
88
await promptToLogin ( this . env ) ;
80
89
}
@@ -96,16 +105,60 @@ This command uses Ionic servers, so we require you to be logged into your free I
96
105
}
97
106
98
107
async run ( inputs : CommandLineInputs , options : CommandLineOptions ) : Promise < void > {
99
- const { loadConfigXml } = await import ( '../../lib/integrations/cordova/config' ) ;
100
- const { addResourcesToConfigXml, createImgDestinationDirectories, findMostSpecificSourceImage, getImageResources, getSourceImages, transformResourceImage, uploadSourceImage } = await import ( '../../lib/integrations/cordova/resources' ) ;
108
+ const platform = inputs [ 0 ] ? String ( inputs [ 0 ] ) : undefined ;
109
+
110
+ if ( options [ 'cordova-res' ] ) {
111
+ await this . runCordovaRes ( platform , options ) ;
112
+ } else {
113
+ await this . runResourceServer ( platform , options ) ;
114
+ }
115
+ }
101
116
117
+ async runCordovaRes ( platform : string | undefined , options : CommandLineOptions ) : Promise < void > {
102
118
if ( ! this . project ) {
103
119
throw new FatalException ( `Cannot run ${ chalk . green ( 'ionic cordova resources' ) } outside a project directory.` ) ;
104
120
}
105
121
106
- const platform = inputs [ 0 ] ? String ( inputs [ 0 ] ) : undefined ;
107
- const { force } = options ;
122
+ const log = this . env . log . clone ( ) ;
123
+ log . handlers = createDefaultLoggerHandlers ( createPrefixedFormatter ( chalk . dim ( `[cordova-res]` ) ) ) ;
124
+ const ws = log . createWriteStream ( LOGGER_LEVELS . INFO ) ;
125
+
126
+ const args : string [ ] = [ ] ;
108
127
128
+ if ( platform ) {
129
+ args . push ( platform ) ;
130
+ }
131
+
132
+ if ( options [ 'icon' ] ) {
133
+ args . push ( '--type' , 'icon' ) ;
134
+ } else if ( options [ 'splash' ] ) {
135
+ args . push ( '--type' , 'splash' ) ;
136
+ }
137
+
138
+ if ( options [ 'verbose' ] ) {
139
+ args . push ( '--verbose' ) ;
140
+ }
141
+
142
+ try {
143
+ await this . env . shell . run ( 'cordova-res' , args , { showCommand : true , fatalOnNotFound : false , cwd : this . project . directory , stream : ws } ) ;
144
+ } catch ( e ) {
145
+ if ( e instanceof ShellCommandError && e . code === ERROR_SHELL_COMMAND_NOT_FOUND ) {
146
+ const installArgs = await pkgManagerArgs ( this . env . config . get ( 'npmClient' ) , { command : 'install' , pkg : 'cordova-res' , global : true } ) ;
147
+ throw new FatalException (
148
+ `${ chalk . green ( 'cordova-res' ) } was not found on your PATH. Please install it globally:\n` +
149
+ `${ chalk . green ( installArgs . join ( ' ' ) ) } \n`
150
+ ) ;
151
+ }
152
+
153
+ throw e ;
154
+ }
155
+ }
156
+
157
+ async runResourceServer ( platform : string | undefined , options : CommandLineOptions ) : Promise < void > {
158
+ const { loadConfigXml } = await import ( '../../lib/integrations/cordova/config' ) ;
159
+ const { addResourcesToConfigXml, createImgDestinationDirectories, findMostSpecificSourceImage, getImageResources, getSourceImages, transformResourceImage, uploadSourceImage } = await import ( '../../lib/integrations/cordova/resources' ) ;
160
+
161
+ const { force } = options ;
109
162
const tasks = this . createTaskChain ( ) ;
110
163
111
164
// if no resource filters are passed as arguments assume to use all.
0 commit comments