Skip to content

Commit 3c27e05

Browse files
committed
feat(resources): --cordova-res option for local resource generation
This hidden option offers an alternative to using Ionic's remote server to generate Cordova icons and splash screens to specific platform sizes. If specified, it will use `cordova-res` to generate resources locally using sharp: https://github.com/lovell/sharp
1 parent cbc9a8b commit 3c27e05

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

packages/ionic/src/commands/cordova/resources.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { ERROR_SHELL_COMMAND_NOT_FOUND, LOGGER_LEVELS, OptionGroup, ShellCommandError, createPrefixedFormatter } from '@ionic/cli-framework';
12
import { prettyPath } from '@ionic/cli-framework/utils/format';
23
import { cacheFileChecksum, copy, pathExists } from '@ionic/utils-fs';
34
import chalk from 'chalk';
45
import * as Debug from 'debug';
56

67
import { CommandInstanceInfo, CommandLineInputs, CommandLineOptions, CommandMetadata, CommandPreRun, KnownPlatform, ResourcesConfig, ResourcesImageConfig, SourceImage } from '../../definitions';
78
import { FatalException } from '../../lib/errors';
9+
import { createDefaultLoggerHandlers } from '../../lib/utils/logger';
10+
import { pkgManagerArgs } from '../../lib/utils/npm';
811

912
import { CordovaCommand } from './base';
1013

@@ -63,6 +66,12 @@ This command uses Ionic servers, so we require you to be logged into your free I
6366
type: Boolean,
6467
aliases: ['s'],
6568
},
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+
},
6675
],
6776
};
6877
}
@@ -74,7 +83,7 @@ This command uses Ionic servers, so we require you to be logged into your free I
7483

7584
const isLoggedIn = this.env.session.isLoggedIn();
7685

77-
if (!isLoggedIn) {
86+
if (!options['cordova-res'] && !isLoggedIn) {
7887
this.env.log.warn(`You need to be logged into your Ionic account in order to run ${chalk.green(`ionic cordova resources`)}.\n`);
7988
await promptToLogin(this.env);
8089
}
@@ -96,16 +105,60 @@ This command uses Ionic servers, so we require you to be logged into your free I
96105
}
97106

98107
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+
}
101116

117+
async runCordovaRes(platform: string | undefined, options: CommandLineOptions): Promise<void> {
102118
if (!this.project) {
103119
throw new FatalException(`Cannot run ${chalk.green('ionic cordova resources')} outside a project directory.`);
104120
}
105121

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[] = [];
108127

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;
109162
const tasks = this.createTaskChain();
110163

111164
// if no resource filters are passed as arguments assume to use all.

0 commit comments

Comments
 (0)