Skip to content

Commit 82f9c5a

Browse files
authored
fix(cli): do not generate icons for platforms not in config.xml (#103)
1 parent 02422fc commit 82f9c5a

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/__tests__/cli.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as et from 'elementtree';
2+
13
import { Options } from '..';
2-
import { generateRunOptions, parseOptions } from '../cli';
4+
import { generateRunOptions, parseOptions, resolveOptions } from '../cli';
35
import { Platform } from '../platform';
46

57
function generatePlatformsConfig(resourcesDirectory: string) {
@@ -174,6 +176,54 @@ describe('cordova-res', () => {
174176

175177
});
176178

179+
describe('resolveOptions', () => {
180+
181+
it('should pull platforms from config.xml if none provided', async () => {
182+
183+
const configXml: et.Element = et.Element('widget');
184+
configXml.append(et.Element('platform', { name: 'android' }));
185+
configXml.append(et.Element('platform', { name: 'ios' }));
186+
187+
const options: Options = await resolveOptions([], 'resources', new et.ElementTree(configXml));
188+
189+
expect(options.platforms).toEqual({
190+
...generatePlatformsConfig('resources'),
191+
windows: undefined,
192+
});
193+
});
194+
195+
it('should use runtime platform if provided', async () => {
196+
197+
const configXml: et.Element = et.Element('widget');
198+
configXml.append(et.Element('platform', { name: 'android' }));
199+
configXml.append(et.Element('platform', { name: 'ios' }));
200+
201+
const options: Options = await resolveOptions(['android'], 'resources', new et.ElementTree(configXml));
202+
203+
expect(options.platforms).toEqual({
204+
...generatePlatformsConfig('resources'),
205+
windows: undefined,
206+
ios: undefined,
207+
});
208+
});
209+
210+
it('should generate for all platforms if no config.xml and no runtime platform', async () => {
211+
212+
const configXml: et.Element = et.Element('widget');
213+
configXml.append(et.Element('platform', { name: 'android' }));
214+
configXml.append(et.Element('platform', { name: 'ios' }));
215+
216+
const options: Options = await resolveOptions(['android'], 'resources', new et.ElementTree(configXml));
217+
218+
expect(options.platforms).toEqual({
219+
...generatePlatformsConfig('resources'),
220+
windows: undefined,
221+
ios: undefined,
222+
});
223+
});
224+
225+
});
226+
177227
});
178228

179229
});

src/cli.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ export async function resolveOptions(args: readonly string[], directory: string,
1717
const platformList = filterSupportedPlatforms(doc ? getPlatforms(doc) : []);
1818
const parsedOptions = parseOptions(args);
1919
const { resourcesDirectory = DEFAULT_RESOURCES_DIRECTORY } = parsedOptions;
20+
const platformOption = parsePlatformOption(args);
2021

2122
return {
22-
...{ directory, ...platformList.length > 0 ? { platforms: generatePlatformOptions(platformList, resourcesDirectory, args) } : {} },
23-
...parsedOptions,
23+
...{
24+
directory,
25+
...parsedOptions,
26+
platforms: platformOption ? parsedOptions.platforms : generatePlatformOptions(platformList, resourcesDirectory, args),
27+
},
2428
};
2529
}
2630

2731
export function parseOptions(args: readonly string[]): Required<Options> {
2832
const json = args.includes('--json');
2933
const resourcesDirectory = getOptionValue(args, '--resources', DEFAULT_RESOURCES_DIRECTORY);
30-
const platformArg = args[0] ? args[0] : undefined;
34+
const platformArg = parsePlatformOption(args);
3135
const platformList = validatePlatforms(platformArg && !platformArg.startsWith('-') ? [platformArg] : PLATFORMS);
3236

3337
return {
@@ -42,6 +46,10 @@ export function parseOptions(args: readonly string[]): Required<Options> {
4246
};
4347
}
4448

49+
export function parsePlatformOption(args: readonly string[]): string | undefined {
50+
return args[0] ? args[0] : undefined;
51+
}
52+
4553
export function generatePlatformOptions(platforms: readonly Platform[], resourcesDirectory: string, args: readonly string[]): PlatformOptions {
4654
return platforms.reduce((acc, platform) => {
4755
acc[platform] = generateRunOptions(platform, resourcesDirectory, args);

0 commit comments

Comments
 (0)