Skip to content

Commit 5115f07

Browse files
authored
Merge pull request #5088 from NativeScript/kddimitrov/fix-assets-generation
fix: resize image throws and error if no definition matched
2 parents 4db3e59 + 0328679 commit 5115f07

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/services/assets-generation/assets-generation-service.ts

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export class AssetsGenerationService implements IAssetsGenerationService {
7171
const outputPath = assetItem.path;
7272
const width = assetItem.width * scale;
7373
const height = assetItem.height * scale;
74+
75+
if (!width || !height) {
76+
this.$logger.warn(`Image ${assetItem.filename} is skipped as its width and height are invalid.`);
77+
continue;
78+
}
79+
7480
let image: Jimp;
7581
switch (operation) {
7682
case Operations.OverlayWith:

lib/services/project-data-service.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -236,33 +236,36 @@ export class ProjectDataService implements IProjectDataService {
236236
private async getIOSAssetSubGroup(dirPath: string): Promise<IAssetSubGroup> {
237237
const pathToContentJson = path.join(dirPath, AssetConstants.iOSResourcesFileName);
238238
const content = this.$fs.exists(pathToContentJson) && <IAssetSubGroup>this.$fs.readJson(pathToContentJson) || { images: [] };
239+
const finalContent: IAssetSubGroup = { images: [] };
239240

240241
const imageDefinitions = this.getImageDefinitions().ios;
241242

242243
_.each(content && content.images, image => {
244+
let foundMatchingDefinition = false;
243245
// In some cases the image may not be available, it will just be described.
244246
// When this happens, the filename will be empty.
245247
// So we'll keep the path empty as well.
246248
if (image.filename) {
247249
image.path = path.join(dirPath, image.filename);
248250
}
249251

252+
if (image.size) {
253+
// size is basically <width>x<height>
254+
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
255+
if (width && height) {
256+
image.width = +width;
257+
image.height = +height;
258+
}
259+
}
260+
250261
// Find the image size based on the hardcoded values in the image-definitions.json
251262
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
252263
const assetItem = _.find(assetSubGroup, assetElement =>
253264
assetElement.filename === image.filename && path.basename(assetElement.directory) === path.basename(dirPath)
254265
);
255266

256-
if (image.size) {
257-
// size is basically <width>x<height>
258-
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
259-
if (width && height) {
260-
image.width = +width;
261-
image.height = +height;
262-
}
263-
}
264-
265267
if (assetItem) {
268+
foundMatchingDefinition = true;
266269
if (!image.width || !image.height) {
267270
image.width = assetItem.width;
268271
image.height = assetItem.height;
@@ -273,13 +276,18 @@ export class ProjectDataService implements IProjectDataService {
273276
image.overlayImageScale = image.overlayImageScale || assetItem.overlayImageScale;
274277
image.scale = image.scale || assetItem.scale;
275278
image.rgba = assetItem.rgba;
279+
finalContent.images.push(image);
276280
// break each
277281
return false;
278282
}
279283
});
284+
285+
if (!foundMatchingDefinition && image.filename) {
286+
this.$logger.warn(`Didn't find a matching image definition for file ${path.join(path.basename(dirPath), image.filename)}. This file will be skipped from reources generation.`);
287+
}
280288
});
281289

282-
return content;
290+
return finalContent;
283291
}
284292

285293
private getAndroidAssetSubGroup(assetItems: IAssetItem[], realPaths: string[]): IAssetSubGroup {

0 commit comments

Comments
 (0)