Skip to content

Commit abf5031

Browse files
committed
Add flags for platform directories. #389
1 parent 2c965f1 commit abf5031

File tree

6 files changed

+56
-23
lines changed

6 files changed

+56
-23
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ npx capacitor-assets generate --iconBackgroundColor '#eeeeee' --iconBackgroundCo
3636

3737
Where the provided flags are:
3838

39+
- `--ios-project` - the path to the iOS project (default `ios/App`)
40+
- `--android-project` - the path to the Android project (default `android`)
3941
- `--iconBackgroundColor` - the background color (hex value) used when generating icon layers for light mode (default `#ffffff`)
4042
- `--iconBackgroundColorDark` - the background color (hex value) used when generating icon layers for dark mode (where supported) (default `#111111`)
4143
- `--splashBackgroundColor` - the background color (hex value) used when generating splash screens (default `#ffffff`)
4244
- `--splashBackgroundColorDark` - the background color (hex value) used when generating splash screens for dark mode (where supported) (default `#111111`)
4345
- `--logoSplashTargetWidth` - A specific width to set the logo to when generating splash screens from a single logo file (not used by default, logo is scaled as percentage of splash instead, see `--logoSplashScale`)
4446
- `--logoSplashScale` - the scale multiplier to apply to the logo when generating splash screens from a single logo file (default: `0.2`)
47+
- `--ios` - explicitly run iOS asset generation. Using a platform flag makes the platform list exclusive.
48+
- `--android` - explicitly run Android asset generation. Using a platform flag makes the platform list exclusive.
49+
- `--pwa` - explicitly run Android asset generation. Using a platform flag makes the platform list exclusive.
4550

4651
### Usage - Custom Mode
4752

src/ctx.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export async function loadContext(projectRootPath?: string): Promise<Context> {
2222

2323
let project: Project;
2424
try {
25-
project = await loadProject(projectRootPath, (argv.assetPath as string) ?? 'assets');
25+
project = await loadProject(argv, projectRootPath, (argv.assetPath as string) ?? 'assets');
2626
} catch (e) {
27-
throw new Error(`Unable to load Capacitor project: ${(e as any).message}`);
27+
throw new Error(`Unable to load project: ${(e as any).message}`);
2828
}
2929

3030
return {
@@ -42,21 +42,21 @@ export function setArguments(ctx: Context, args: any) {
4242
process.env.VERBOSE = '' + !!args.verbose;
4343
}
4444

45-
async function loadProject(projectRootPath?: string, projectAssetPath?: string): Promise<Project> {
46-
const config = await loadMobileProjectConfig();
45+
async function loadProject(args: any, projectRootPath?: string, projectAssetPath?: string): Promise<Project> {
46+
const config = await loadMobileProjectConfig(args);
4747
const project = new Project(projectRootPath, config, projectAssetPath);
4848
await project.load();
4949
return project;
5050
}
5151

5252
// TODO: Use the config loading stuff from @capacitor/configure
53-
function loadMobileProjectConfig(): MobileProjectConfig {
53+
function loadMobileProjectConfig(args: any): MobileProjectConfig {
5454
return {
5555
ios: {
56-
path: 'ios/App',
56+
path: args.iosProject ?? 'ios/App',
5757
},
5858
android: {
59-
path: 'android',
59+
path: args.androidProject ?? 'android',
6060
},
6161
};
6262
}

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export function runProgram(ctx: Context) {
7171
'--android-flavor <name>',
7272
'Android product flavor name where generated assets will be created. Defaults to "main".'
7373
)
74+
.option('--ios-project <dir>', 'Path to iOS project (defaults to "ios/App")')
75+
.option('--android-project <dir>', 'Path to Android project (defaults to "android")')
7476
/*
7577
.option(
7678
'--pwaTags',

src/platforms/android/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export class AndroidAssetGenerator extends AssetGenerator {
306306
const resPath = this.getResPath(project);
307307
const destRound = join(resPath, `mipmap-${template.density}`, 'ic_launcher_round.png');
308308

309-
// This pipeline is trick, but we need two separate pipelines
309+
// This pipeline is tricky, but we need two separate pipelines
310310
// per https://github.com/lovell/sharp/issues/2378#issuecomment-864132578
311311
const resized = await sharp(asset.path).resize(template.width, template.height).toBuffer();
312312
const composited = await sharp(resized)

src/project.ts

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export class Project extends MobileProject {
2121
this.detectAssetDir(projectRoot);
2222
}
2323

24+
async androidExists() {
25+
return this.config.android?.path && (await pathExists(this.config.android?.path));
26+
}
27+
28+
async iosExists() {
29+
return this.config.ios?.path && (await pathExists(this.config.ios?.path));
30+
}
31+
2432
async detectAssetDir(projectRoot: string) {
2533
if (this.assetPath === 'assets' && !(await pathExists(this.assetDir))) {
2634
this.assetDir = join(projectRoot, 'resources');

src/tasks/generate.ts

+33-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Context } from '../ctx';
22
import * as c from '../colors';
3-
import { error, log } from '../util/log';
3+
import { error, log, logger, warn } from '../util/log';
44
import { IosAssetGenerator } from '../platforms/ios';
55
import { PwaAssetGenerator } from '../platforms/pwa';
66
import { AndroidAssetGenerator } from '../platforms/android';
@@ -38,32 +38,50 @@ export async function run(ctx: Context): Promise<OutputAsset[]> {
3838
platforms.push('pwa');
3939
}
4040

41-
if (!ctx.args.silent) {
42-
log(`Generating assets for ${platforms.map((p) => c.strong(c.success(p))).join(', ')}`);
43-
}
41+
await verifyPlatformFolders(/* mut */ platforms, ctx.project);
4442

45-
const generators = getGenerators(ctx, platforms);
43+
if (platforms.length > 0) {
44+
if (!ctx.args.silent) {
45+
log(`Generating assets for ${platforms.map((p) => c.strong(c.success(p))).join(', ')}`);
46+
}
4647

47-
const generated = await generateAssets(assets, generators, ctx.project);
48+
const generators = getGenerators(ctx, platforms);
4849

49-
if (!ctx.args.silent) {
50-
logGenerated(generated);
51-
}
50+
const generated = await generateAssets(assets, generators, ctx.project);
5251

53-
/*
54-
if (!ctx.args.silent && platforms.indexOf('pwa') >= 0 && ctx.args.pwaTags) {
55-
PwaAssetGenerator.logInstructions(generated);
56-
}
57-
*/
52+
if (!ctx.args.silent) {
53+
logGenerated(generated);
54+
}
55+
56+
/*
57+
if (!ctx.args.silent && platforms.indexOf('pwa') >= 0 && ctx.args.pwaTags) {
58+
PwaAssetGenerator.logInstructions(generated);
59+
}
60+
*/
5861

59-
return generated;
62+
return generated;
63+
} else {
64+
logger.warn('No platforms found, exiting');
65+
return [];
66+
}
6067
} catch (e) {
6168
error('Unable to generate assets', (e as Error).message);
6269
error(e);
6370
}
6471
return [];
6572
}
6673

74+
async function verifyPlatformFolders(platforms: string[], project: Project) {
75+
if (platforms.indexOf('ios') >= 0 && !(await project.iosExists())) {
76+
platforms.splice(platforms.indexOf('ios'), 1);
77+
logger.warn(`iOS platform not found at ${project.config.ios?.path || ''}, skipping iOS generation`);
78+
}
79+
if (platforms.indexOf('android') >= 0 && !(await project.androidExists())) {
80+
platforms.splice(platforms.indexOf('android'), 1);
81+
logger.warn(`Android platform not found at ${project.config.android?.path || ''}, skipping android generation`);
82+
}
83+
}
84+
6785
async function generateAssets(assets: Assets, generators: AssetGenerator[], project: Project) {
6886
const generated: OutputAsset[] = [];
6987

0 commit comments

Comments
 (0)